package atacama

  1. Overview
  2. Docs

Source file handler.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
open Riot

type ('state, 'error) handler_result =
  | Ok
  | Continue of 'state
  | Continue_with_timeout of 'state * Net.Socket.timeout
  | Close of 'state
  | Error of 'state * 'error

module type Intf = sig
  type state

  val handle_close : Socket.t -> state -> unit
  val handle_connection : Socket.t -> state -> (state, 'error) handler_result

  val handle_data :
    Bigstringaf.t -> Socket.t -> state -> (state, 'error) handler_result

  val handle_error :
    'error -> Socket.t -> state -> (state, 'error) handler_result

  val handle_shutdown : Socket.t -> state -> (state, 'error) handler_result
  val handle_timeout : Socket.t -> state -> (state, 'error) handler_result
end

module Default = struct
  let handle_close _sock _state = ()
  let handle_connection _sock state = Continue state
  let handle_data _data _sock state = Continue state
  let handle_error err _sock state = Error (state, err)
  let handle_shutdown _sock _state = Ok
  let handle_timeout _sock _state = Ok
end

type 's t = (module Intf with type state = 's)

let handle_close (type s) (module H : Intf with type state = s) sock (state : s)
    =
  H.handle_close sock state

let handle_connection (type s) (module H : Intf with type state = s) sock
    (state : s) =
  H.handle_connection sock state

let handle_data (type s) (module H : Intf with type state = s) data sock
    (state : s) =
  H.handle_data data sock state
OCaml

Innovation. Community. Security.