glisten/tcp

Types

pub type Acceptor {
  AcceptConnection(ListenSocket)
}

Constructors

  • AcceptConnection(ListenSocket)
pub type AcceptorError {
  AcceptError
  HandlerError
  ControlError
}

Constructors

  • AcceptError
  • HandlerError
  • ControlError
pub type AcceptorState {
  AcceptorState(sender: Sender(Acceptor), socket: Option(Socket))
}

Constructors

  • AcceptorState(sender: Sender(Acceptor), socket: Option(Socket))

Mapping to the {active, _} option

pub type ActiveState {
  Once
  Passive
  Count(Int)
  Active
}

Constructors

  • Once
  • Passive
  • Count(Int)
  • Active
pub type Channel =
  #(Socket, Receiver(Acceptor))
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)
  Tcp(socket: Port, data: BitString)
  TcpClosed(Nil)
}

Constructors

  • Close
  • Ready
  • ReceiveMessage(BitString)
  • 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,
    )
pub opaque type Socket

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(
  socket: ListenSocket,
) -> Result(Socket, SocketReason)
pub external fn accept_timeout(
  socket: ListenSocket,
  timeout: Int,
) -> Result(Socket, SocketReason)
pub external fn close(socket: Socket) -> Atom
pub external fn do_shutdown(socket: Socket, write: Atom) -> Nil
pub fn echo_loop(msg: HandlerMessage, state: AcceptorState) -> Next(
  AcceptorState,
)
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 external fn send(
  socket: Socket,
  packet: BitBuilder,
) -> Result(Nil, SocketReason)
pub fn set_opts(socket: Socket, opts: List(TcpOption)) -> Result(
  Nil,
  Nil,
)

Update the optons for a socket (mutates the socket)

pub fn shutdown(socket: Socket) -> Nil
pub external fn socket_info(socket: Socket) -> Map(a, b)
pub fn start_acceptor(socket: ListenSocket, initial_data: a, loop_fn: fn(
    HandlerMessage,
    LoopState(a),
  ) -> Next(LoopState(a))) -> Result(Sender(Acceptor), StartError)

Worker process that handles accepting connections and starts a new process which receives the messages from the socket

pub fn start_acceptor_pool(listener_socket: ListenSocket, handler: fn(
    HandlerMessage,
    LoopState(a),
  ) -> Next(LoopState(a)), initial_data: a, pool_count: Int) -> Result(
  Sender(Message),
  StartError,
)

Starts a pool of acceptors of size pool_count.

Runs loop_fn on ever message received

pub fn start_handler(socket: Socket, initial_data: a, loop: fn(
    HandlerMessage,
    LoopState(a),
  ) -> Next(LoopState(a))) -> Result(
  Sender(HandlerMessage),
  StartError,
)

Starts an actor for the TCP connection