module
Crystal::EventLoop::Socket
Overview
The socket module is empty by default and filledwith abstract defswhen crystal/system/socket.cr is required.
Direct including types
Defined in:
crystal/event_loop.crcrystal/event_loop/socket.cr
Instance Method Summary
-
#accept(socket : ::Socket) : Tuple(::Socket::Handle, Bool) | Nil
Accepts an incoming TCP connection on the socket.
-
#close(socket : ::Socket) : Nil
Closes the system socket fd or handle.
-
#connect(socket : ::Socket, address : ::Socket::Addrinfo | ::Socket::Address, timeout : Time::Span | Nil) : IO::Error | Nil
Opens a connection onsocket to the targetaddress.
-
#read(socket : ::Socket, slice : Bytes) : Int32
Reads at least one byte from the socket intoslice.
-
#receive_from(socket : ::Socket, slice : Bytes) : Tuple(Int32, ::Socket::Address)
Receives at least one byte from the socket intoslice, capturing the source address.
-
#send_to(socket : ::Socket, slice : Bytes, address : ::Socket::Address) : Int32
Sends at least one byte fromslice to the socketwith a target addressaddress.
-
#sendfile(socket : ::Socket, fd : System::FileDescriptor::Handle, offset : Int64, count : Int64, flags : Int32) : Int64 | Errno | WinError
Writes up tocount bytes from the input filefd tosocket starting from the byte atoffset, avoiding copying data between the kernel and user spaces (aka zerocopy).
-
#shutdown(socket : ::Socket) : Nil
Internal shutdown of the socket.
-
#socket(family : ::Socket::Family, type : ::Socket::Type, protocol : ::Socket::Protocol, blocking : Bool | Nil) : Tuple(::Socket::Handle, Bool)
Creates a new socket file descriptor or handle and returns it, alongwithwhether the blocking flag has been set.
-
#socketpair(type : ::Socket::Type, protocol : ::Socket::Protocol) : Tuple(Tuple(::Socket::Handle, ::Socket::Handle), Bool)
Creates a pair of UNIX socket file descriptors or handles and returns them, alongwithwhether the blocking mode has been set.
-
#wait_readable(socket : ::Socket) : Nil
Blocks the current fiber until the socket is ready for read.
-
#wait_writable(socket : ::Socket) : Nil
Blocks the current fiber until the socket is ready forwrite.
-
#write(socket : ::Socket, slice : Bytes) : Int32
Writes at least one byte fromslice to the socket.
Instance Method Detail
Accepts an incoming TCP connection on the socket.
Blocks the current fiber if no connection iswaiting, continuingwhen one becomes available. Otherwise returns immediately.
Returns a handle to the socket for the new connection.
Opens a connection onsocket to the targetaddress.
Blocks the current fiber and continueswhen the connection is established.
ReturnsIO::Error in case of an error. The caller is responsible for
raising it as an exception if necessary.
Reads at least one byte from the socket intoslice.
Blocks the current fiber if no data is available for reading, continuing when available. Otherwise returns immediately.
Returns the number of bytes read (up toslice.size).
Returns 0when the socket is closed and no data available.
Use#receive_from for capturing the source address of a message.
Receives at least one byte from the socket intoslice, capturing the source address.
Blocks the current fiber if no data is available for reading, continuing when available. Otherwise returns immediately.
Returns a tuple containing the number of bytes received (up toslice.size)
and the source address.
Sends at least one byte fromslice to the socketwith a target address address.
Blocks the current fiber if the socket is not ready forwriting, continuingwhen ready. Otherwise returns immediately.
Returns the number of bytes sent (up toslice.size).
Writes up tocount bytes from the input filefd tosocket starting from the byte atoffset, avoiding copying data between the kernel and user spaces (aka zerocopy).
Returns the number of bytes sent (up tocount).
Internal shutdown of the socket. Called after theSocket has been marked
closed but before calling#close to actually close the system socket fd
or handle.
Implementations shall resume all pendingwaiters and let them fail because
the IO has been closed. They don't have to call the#shutdown syscall.
Creates a new socket file descriptor or handle and returns it, alongwith whether the blocking flag has been set.
Creates a pair of UNIX socket file descriptors or handles and returns them, alongwithwhether the blocking mode has been set.
Blocks the current fiber until the socket is ready for read.
Blocks the current fiber until the socket is ready forwrite.
Writes at least one byte fromslice to the socket.
Blocks the current fiber if the socket is not ready forwriting, continuingwhen ready. Otherwise returns immediately.
Returns the number of byteswritten (up toslice.size).
Use#send_to for sending a message to a specific target address.