glisten

Types

pub type ClientIp =
  InternalClientIp

This type holds useful bits of data for the active connection.

pub type Connection(user_message) {
  Connection(
    client_ip: ClientIp,
    socket: Socket,
    transport: Transport,
    subject: Subject(handler.Message(user_message)),
  )
}

Constructors

  • Connection(
      client_ip: ClientIp,
      socket: Socket,
      transport: Transport,
      subject: Subject(handler.Message(user_message)),
    )

    Arguments

    • client_ip

      This will be optionally a tuple for the IPv4 of the other end of the socket

    • transport

      This provides a uniform interface for both TCP and SSL methods.

pub opaque type Handler(user_message, data)

This is the shape of the function you need to provide for the handler argument to serve(_ssl).

pub type Loop(user_message, data) =
  fn(Message(user_message), data, Connection(user_message)) ->
    actor.Next(Message(user_message), data)

Your provided loop function with receive these message types as the first argument.

pub type Message(user_message) {
  Packet(BitArray)
  User(user_message)
}

Constructors

  • Packet(BitArray)

    These are messages received from the socket

  • User(user_message)

    These are any messages received from the selector returned from on_init

pub type Socket =
  InternalSocket
pub type SocketReason =
  InternalSocketReason

Reasons that serve might fail

pub type StartError {
  ListenerClosed
  ListenerTimeout
  AcceptorTimeout
  AcceptorFailed(process.ExitReason)
  AcceptorCrashed(Dynamic)
  SystemError(SocketReason)
}

Constructors

  • ListenerClosed
  • ListenerTimeout
  • AcceptorTimeout
  • AcceptorFailed(process.ExitReason)
  • AcceptorCrashed(Dynamic)
  • SystemError(SocketReason)

Functions

pub fn handler(
  on_init: fn(Connection(a)) -> #(b, Option(Selector(a))),
  loop: fn(Message(a), b, Connection(a)) -> Next(Message(a), b),
) -> Handler(a, b)

Create a new handler for each connection. The required arguments mirror the actor.start API from gleam_otp. The default pool is 10 accceptor processes.

pub fn send(
  conn: Connection(a),
  msg: BytesBuilder,
) -> Result(Nil, SocketReason)

Sends a BytesBuilder message over the socket using the active transport

pub fn serve(
  handler: Handler(a, b),
  port: Int,
) -> Result(Subject(Message), StartError)

Start the TCP server with the given handler on the provided port

pub fn serve_ssl(
  handler: Handler(a, b),
  port port: Int,
  certfile certfile: String,
  keyfile keyfile: String,
) -> Result(Subject(Message), StartError)

Start the SSL server with the given handler on the provided port. The key and cert files must be provided, valid, and readable by the current user.

pub fn with_close(
  handler: Handler(a, b),
  on_close: fn(b) -> Nil,
) -> Handler(a, b)

Adds a function to the handler to be called when the connection is closed.

pub fn with_pool_size(
  handler: Handler(a, b),
  size: Int,
) -> Handler(a, b)

Modify the size of the acceptor pool

Search Document