Crystal::EventLoop::Socket - Crystal 1.14.0

# module Crystal::EventLoop::Socket

##
Overview

The socket module is empty by default and filled with abstract defs when crystal/system/socket.cr is required.

##
Direct including types

- Crystal::EventLoop

##
Defined in:

crystal/system/event_loop.cr
crystal/system/event_loop/socket.cr

##
Instance Method Summary

- #accept(socket : ::Socket) : ::Socket::Handle | Nil

  Accepts an incoming TCP connection on the socket.

- #close(socket : ::Socket) : Nil

  Closes the socket.

- #connect(socket : ::Socket, address : ::Socket::Addrinfo | ::Socket::Address, timeout : Time::Span | Nil) : IO::Error | Nil

  Opens a connection on socket to the target address.

- #read(socket : ::Socket, slice : Bytes) : Int32

  Reads at least one byte from the socket into slice.

- #receive_from(socket : ::Socket, slice : Bytes) : Tuple(Int32, ::Socket::Address)

  Receives at least one byte from the socket into slice, capturing the source address.

- #remove(socket : ::Socket) : Nil

  Removes the socket from the event loop.

- #send_to(socket : ::Socket, slice : Bytes, address : ::Socket::Address) : Int32

  Sends at least one byte from slice to the socket with a target address address.

- #write(socket : ::Socket, slice : Bytes) : Int32

  Writes at least one byte from slice to the socket.

##
Instance Method Detail

abstract def accept(socket : ::Socket) : ::Socket::Handle | Nil #

Accepts an incoming TCP connection on the socket.

Blocks the current fiber if no connection is waiting, continuing when one becomes available. Otherwise returns immediately.

Returns a handle to the socket for the new connection.

abstract def close(socket : ::Socket) : Nil #

Closes the socket.

abstract def connect(socket : ::Socket, address : ::Socket::Addrinfo | ::Socket::Address, timeout : Time::Span | Nil) : IO::Error | Nil #

Opens a connection on socket to the target address.

Blocks the current fiber and continues when the connection is established.

Returns `IO::Error` in case of an error. The caller is responsible for raising it as an exception if necessary.

abstract def read(socket : ::Socket, slice : Bytes) : Int32 #

Reads at least one byte from the socket into slice.

Blocks the current fiber if no data is available for reading, continuing when available. Otherwise returns immediately.

Returns the number of bytes read (up to `slice.size`). Returns 0 when the socket is closed and no data available.

Use `#send_to` for sending a message to a specific target address.

abstract def receive_from(socket : ::Socket, slice : Bytes) : Tuple(Int32, ::Socket::Address) #

Receives at least one byte from the socket into slice, 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 to `slice.size`) and the source address.

abstract def remove(socket : ::Socket) : Nil #

Removes the socket from the event loop. Can be used to free up memory resources associated with the socket, as well as removing the socket from kernel data structures.

Called by `::Socket#finalize` before closing the socket. Errors shall be silently ignored.

abstract def send_to(socket : ::Socket, slice : Bytes, address : ::Socket::Address) : Int32 #

Sends at least one byte from slice to the socket with a target address address.

Blocks the current fiber if the socket is not ready for writing, continuing when ready. Otherwise returns immediately.

Returns the number of bytes sent (up to `slice.size`).

abstract def write(socket : ::Socket, slice : Bytes) : Int32 #

Writes at least one byte from slice to the socket.

Blocks the current fiber if the socket is not ready for writing, continuing when ready. Otherwise returns immediately.

Returns the number of bytes written (up to `slice.size`).

Use `#receive_from` for capturing the source address of a message.