glisten/tcp
Types
pub type AcceptorError {
AcceptError
HandlerError
ControlError
}
Constructors
-
AcceptError
-
HandlerError
-
ControlError
pub type AcceptorMessage {
AcceptConnection(ListenSocket)
}
Constructors
-
AcceptConnection(ListenSocket)
pub type AcceptorPool(data) {
AcceptorPool(
listener_socket: ListenSocket,
handler: LoopFn(data),
initial_data: data,
pool_count: Int,
on_init: Option(fn(Sender(HandlerMessage)) -> Nil),
on_close: Option(fn(Sender(HandlerMessage)) -> Nil),
)
}
Constructors
-
AcceptorPool( listener_socket: ListenSocket, handler: LoopFn(data), initial_data: data, pool_count: Int, on_init: Option(fn(Sender(HandlerMessage)) -> Nil), on_close: Option(fn(Sender(HandlerMessage)) -> Nil), )
pub type AcceptorState {
AcceptorState(
sender: Sender(AcceptorMessage),
socket: Option(Socket),
)
}
Constructors
-
AcceptorState( sender: Sender(AcceptorMessage), socket: Option(Socket), )
Mapping to the {active, _} option
pub type ActiveState {
Once
Passive
Count(Int)
Active
}
Constructors
-
Once
-
Passive
-
Count(Int)
-
Active
pub type Handler(data) {
Handler(
socket: Socket,
initial_data: data,
loop: LoopFn(data),
on_init: Option(fn(Sender(HandlerMessage)) -> Nil),
on_close: Option(fn(Sender(HandlerMessage)) -> Nil),
)
}
Constructors
-
Handler( socket: Socket, initial_data: data, loop: LoopFn(data), on_init: Option(fn(Sender(HandlerMessage)) -> Nil), on_close: Option(fn(Sender(HandlerMessage)) -> Nil), )
pub type HandlerFunc(data) =
fn(BitString, LoopState(data)) -> actor.Next(LoopState(data))
All message types that the handler will receive, or that you can send to the handler process
pub type HandlerMessage {
Close
Ready
ReceiveMessage(BitString)
SendMessage(BitBuilder)
Tcp(socket: Port, data: BitString)
TcpClosed(Nil)
}
Constructors
-
Close
-
Ready
-
ReceiveMessage(BitString)
-
SendMessage(BitBuilder)
-
Tcp(socket: Port, data: BitString)
-
TcpClosed(Nil)
pub opaque type ListenSocket
pub type LoopFn(data) =
fn(HandlerMessage, LoopState(data)) ->
actor.Next(LoopState(data))
pub type LoopState(data) {
LoopState(
socket: Socket,
sender: Sender(HandlerMessage),
data: data,
)
}
Constructors
-
LoopState( socket: Socket, sender: Sender(HandlerMessage), data: data, )
Mode for the socket. Currently list
is not supported
pub type SocketMode {
Binary
}
Constructors
-
Binary
pub type SocketReason {
Closed
Timeout
}
Constructors
-
Closed
-
Timeout
Options for the TCP socket
pub type TcpOption {
Backlog(Int)
Nodelay(Bool)
Linger(#(Bool, Int))
SendTimeout(Int)
SendTimeoutClose(Bool)
Reuseaddr(Bool)
ActiveMode(ActiveState)
Mode(SocketMode)
}
Constructors
-
Backlog(Int)
-
Nodelay(Bool)
-
Linger(#(Bool, Int))
-
SendTimeout(Int)
-
SendTimeoutClose(Bool)
-
Reuseaddr(Bool)
-
ActiveMode(ActiveState)
-
Mode(SocketMode)
Functions
pub external fn accept_timeout(
socket: ListenSocket,
timeout: Int,
) -> Result(Socket, SocketReason)
pub fn acceptor_pool(handler: fn(HandlerMessage, LoopState(Nil)) ->
Next(LoopState(Nil))) -> fn(ListenSocket) ->
AcceptorPool(Nil)
Initialize acceptor pool where each handler has no state
pub fn acceptor_pool_with_data(handler: fn(
HandlerMessage,
LoopState(a),
) -> Next(LoopState(a)), initial_data: a) -> fn(ListenSocket) ->
AcceptorPool(a)
Initialize an acceptor pool where each handler holds some state
pub external fn do_shutdown(socket: Socket, write: Atom) -> Nil
pub fn handler(handler func: fn(BitString, LoopState(a)) ->
Next(LoopState(a))) -> fn(HandlerMessage, LoopState(a)) ->
Next(LoopState(a))
This helper will generate a TCP handler that will call your handler function with the BitString data in the packet as well as the LoopState, with any associated state data you are maintaining
pub fn listen(port: Int, options: List(TcpOption)) -> Result(
ListenSocket,
SocketReason,
)
Start listening over TCP on a port with the given options
pub external fn receive(
socket: Socket,
length: Int,
) -> Result(BitString, SocketReason)
pub external fn receive_timeout(
socket: Socket,
length: Int,
timeout: Int,
) -> Result(BitString, SocketReason)
pub fn receiver_to_iterator(receiver: Receiver(a)) -> Iterator(a)
pub fn set_opts(socket: Socket, opts: List(TcpOption)) -> Result(
Nil,
Nil,
)
Update the optons for a socket (mutates the socket)
pub external fn socket_info(socket: Socket) -> Map(a, b)
pub fn start_acceptor(pool: AcceptorPool(a)) -> Result(
Sender(AcceptorMessage),
StartError,
)
Worker process that handles accept
ing connections and starts a new process
which receives the messages from the socket
pub fn start_acceptor_pool(pool: AcceptorPool(a)) -> Result(
Sender(Message),
StartError,
)
Starts a pool of acceptors of size pool_count
.
Runs loop_fn
on ever message received
pub fn start_handler(handler: Handler(a)) -> Result(
Sender(HandlerMessage),
StartError,
)
Starts an actor for the TCP connection
pub fn with_close(make_pool: fn(ListenSocket) -> AcceptorPool(a), func: fn(
Sender(HandlerMessage),
) -> Nil) -> fn(ListenSocket) -> AcceptorPool(a)
Add an on_close
handler to the acceptor pool
pub fn with_init(make_pool: fn(ListenSocket) -> AcceptorPool(a), func: fn(
Sender(HandlerMessage),
) -> Nil) -> fn(ListenSocket) -> AcceptorPool(a)
Add an on_init
handler to the acceptor pool
pub fn with_pool_size(make_pool: fn(ListenSocket) ->
AcceptorPool(a), pool_count: Int) -> fn(ListenSocket) ->
AcceptorPool(a)
Adjust the number of TCP acceptors in the pool