glisten/handler

Types

pub type Handler(data) {
  Handler(
    socket: Socket,
    initial_data: data,
    loop: LoopFn(data),
    on_init: Option(fn(Subject(HandlerMessage)) -> Nil),
    on_close: Option(fn(Subject(HandlerMessage)) -> Nil),
    transport: Transport,
  )
}

Constructors

  • Handler(
      socket: Socket,
      initial_data: data,
      loop: LoopFn(data),
      on_init: Option(fn(Subject(HandlerMessage)) -> Nil),
      on_close: Option(fn(Subject(HandlerMessage)) -> Nil),
      transport: Transport,
    )
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)
  Ssl(socket: Port, data: BitString)
  SslClosed(Nil)
  Tcp(socket: Port, data: BitString)
  TcpClosed(Nil)
}

Constructors

  • Close
  • Ready
  • ReceiveMessage(BitString)
  • SendMessage(BitBuilder)
  • Ssl(socket: Port, data: BitString)
  • SslClosed(Nil)
  • Tcp(socket: Port, data: BitString)
  • TcpClosed(Nil)
pub type LoopFn(data) =
  fn(HandlerMessage, LoopState(data)) ->
    actor.Next(LoopState(data))
pub type LoopState(data) {
  LoopState(
    socket: Socket,
    sender: Subject(HandlerMessage),
    transport: Transport,
    data: data,
  )
}

Constructors

  • LoopState(
      socket: Socket,
      sender: Subject(HandlerMessage),
      transport: Transport,
      data: data,
    )

Functions

pub fn func(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 start(handler: Handler(a)) -> Result(
  Subject(HandlerMessage),
  StartError,
)

Starts an actor for the TCP connection

Search Document