mug
Types
pub type ConnectionOptions {
ConnectionOptions(
host: String,
port: Int,
timeout: Int,
ip_version_preference: IpVersionPreference,
)
}
Constructors
-
ConnectionOptions( host: String, port: Int, timeout: Int, ip_version_preference: IpVersionPreference, )
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.
If both IPv4 and IPv6 are being tried (the default) then this timeout will be used twice, so connecting in total could take twice this amount of time.
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.
The default is 1000ms.
- ip_version_preference
-
What approach to take selecting between IPv4 and IPv6.
The default is
Ipv6Preferred
.
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
Nxdomain
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
-
Nxdomain
-
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
What approach to take with selection between IPv4 and IPv6
IPv6 is superior when available, but unfortunately not all networks support it.
pub type IpVersionPreference {
Ipv4Only
Ipv4Preferred
Ipv6Only
Ipv6Preferred
}
Constructors
-
Ipv4Only
Only connect over IPv4.
-
Ipv4Preferred
Attempt to connect over IPv4 first, attempting IPv6 if connection with IPv4 did not succeed.
-
Ipv6Only
Only connect over IPv6.
-
Ipv6Preferred
Attempt to connect over IPv6 first, attempting IPv4 if connection with IPv6 did not succeed.
Values
pub fn connect(
options: ConnectionOptions,
) -> Result(Socket, ConnectError)
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 ip_version_preference(
options: ConnectionOptions,
preference: IpVersionPreference,
) -> ConnectionOptions
What approach to take selecting between IPv4 and IPv6.
See the IpVersionPreference
type for documentation.
The default is Ipv6Preferred
.
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 message 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_exact(
socket: Socket,
byte_size size: Int,
timeout_milliseconds timeout: Int,
) -> Result(BitArray, Error)
Receive the specified number of bytes from the client, unless the socket
was closed, from the other side. In that case, the last read may return
less bytes.
If the specified number of bytes is not available to read from the socket
then the function will block until the bytes are available, or until the
timeout is reached.
This directly calls the underlying Erlang function gen_tcp:recv/3
.
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 select_tcp_messages(
selector: process.Selector(t),
mapper: fn(TcpMessage) -> t,
) -> process.Selector(t)
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,
message: BitArray,
) -> Result(Nil, Error)
Send a message to the client.
pub fn send_builder(
socket: Socket,
packet: bytes_tree.BytesTree,
) -> Result(Nil, Error)
Send a message to the client, the data in BytesBuilder
. Using this function
is more efficient turning an BytesBuilder
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.
pub fn timeout(
options: ConnectionOptions,
milliseconds timeout: Int,
) -> ConnectionOptions
Specify a timeout for the connection to be established, in milliseconds.
The default is 1000ms.