mug
Types
pub type ConnectionOptions {
ConnectionOptions(host: String, port: Int, timeout: Int)
}
Constructors
-
ConnectionOptions(host: String, port: Int, timeout: Int)
Arguments
-
host
The hostname of the server to connect to.
-
port
The port of the server to connect to.
-
timeout
A timeout in milliseconds for the connection to be established.
Note that if the operating system returns a timeout then this package will also return a timeout, even if this timeout value has not been reached yet.
-
Errors that can occur when working with TCP sockets.
For more information on these errors see the Erlang documentation:
- https://www.erlang.org/doc/man/file#type-posix
- https://www.erlang.org/doc/man/inet#type-posix
pub type Error {
Closed
Timeout
Eaddrinuse
Eaddrnotavail
Eafnosupport
Ealready
Econnaborted
Econnrefused
Econnreset
Edestaddrreq
Ehostdown
Ehostunreach
Einprogress
Eisconn
Emsgsize
Enetdown
Enetunreach
Enopkg
Enoprotoopt
Enotconn
Enotty
Enotsock
Eproto
Eprotonosupport
Eprototype
Esocktnosupport
Etimedout
Ewouldblock
Exbadport
Exbadseq
Eacces
Eagain
Ebadf
Ebadmsg
Ebusy
Edeadlk
Edeadlock
Edquot
Eexist
Efault
Efbig
Eftype
Eintr
Einval
Eio
Eisdir
Eloop
Emfile
Emlink
Emultihop
Enametoolong
Enfile
Enobufs
Enodev
Enolck
Enolink
Enoent
Enomem
Enospc
Enosr
Enostr
Enosys
Enotblk
Enotdir
Enotsup
Enxio
Eopnotsupp
Eoverflow
Eperm
Epipe
Erange
Erofs
Espipe
Esrch
Estale
Etxtbsy
Exdev
}
Constructors
-
Closed
-
Timeout
-
Eaddrinuse
-
Eaddrnotavail
-
Eafnosupport
-
Ealready
-
Econnaborted
-
Econnrefused
-
Econnreset
-
Edestaddrreq
-
Ehostdown
-
Ehostunreach
-
Einprogress
-
Eisconn
-
Emsgsize
-
Enetdown
-
Enetunreach
-
Enopkg
-
Enoprotoopt
-
Enotconn
-
Enotty
-
Enotsock
-
Eproto
-
Eprotonosupport
-
Eprototype
-
Esocktnosupport
-
Etimedout
-
Ewouldblock
-
Exbadport
-
Exbadseq
-
Eacces
-
Eagain
-
Ebadf
-
Ebadmsg
-
Ebusy
-
Edeadlk
-
Edeadlock
-
Edquot
-
Eexist
-
Efault
-
Efbig
-
Eftype
-
Eintr
-
Einval
-
Eio
-
Eisdir
-
Eloop
-
Emfile
-
Emlink
-
Emultihop
-
Enametoolong
-
Enfile
-
Enobufs
-
Enodev
-
Enolck
-
Enolink
-
Enoent
-
Enomem
-
Enospc
-
Enosr
-
Enostr
-
Enosys
-
Enotblk
-
Enotdir
-
Enotsup
-
Enxio
-
Eopnotsupp
-
Eoverflow
-
Eperm
-
Epipe
-
Erange
-
Erofs
-
Espipe
-
Esrch
-
Estale
-
Etxtbsy
-
Exdev
Messages that can be sent by the socket to the process that controls it.
pub type TcpMessage {
Packet(Socket, BitArray)
SocketClosed(Socket)
TcpError(Socket, Error)
}
Constructors
-
Packet(Socket, BitArray)
A packet has been received from the client.
-
SocketClosed(Socket)
The socket has been closed by the client.
-
TcpError(Socket, Error)
An error has occurred on the socket.
Functions
pub fn connect(
options: ConnectionOptions,
) -> Result(Socket, Error)
Establish a TCP connection to the server specified in the connection options.
Returns an error if the connection could not be established.
The socket is created in passive mode, meaning the the receive
function is
to be called to receive packets from the client. The
receive_next_packet_as_message
function can be used to switch the socket
to active mode and receive the next packet as an Erlang message.
pub fn new(host: String, port port: Int) -> ConnectionOptions
Create a new set of connection options.
pub fn receive(
socket: Socket,
timeout_milliseconds timeout: Int,
) -> Result(BitArray, Error)
Receive a packet from the client.
Errors if the socket is closed, if the timeout is reached, or if there is some other problem receiving the packet.
pub fn receive_next_packet_as_message(socket: Socket) -> Nil
Switch the socket to active mode, meaning that the next packet received on the socket will be sent as an Erlang message to the socket owner’s inbox.
This is useful for when you wish to have an OTP actor handle incoming
messages as using the receive
function would result in the actor being
blocked and unable to handle other messages while waiting for the next
packet.
Messages will be send to the process that controls the socket, which is the
process that established the socket with the connect
function.
pub fn selecting_tcp_messages(
selector: Selector(a),
mapper: fn(TcpMessage) -> a,
) -> Selector(a)
Configure a selector to receive messages from TCP sockets.
Note this will receive messages from all TCP sockets that the process controls, rather than any specific one. If you wish to only handle messages from one socket then use one process per socket.
pub fn send(
socket: Socket,
packet: BitArray,
) -> Result(Nil, Error)
Send a packet to the client.
pub fn send_builder(
socket: Socket,
packet: BitBuilder,
) -> Result(Nil, Error)
Send a packet to the client, the data in BitBuilder
. Using this function
is more efficient turning an BitBuilder
or a StringBuilder
into a
BitArray
to use with the send
function.
pub fn shutdown(socket: Socket) -> Result(Nil, Error)
Close the socket, ensuring that any data buffered in the socket is flushed to the operating system kernel socket first.