package trail

  1. Overview
  2. Docs

Source file sock.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
48
49
50
51
52
53
54
55
56
57
58
59
open Riot

type upgrade_opts = { do_upgrade : bool }

type ('state, 'error) handle_result =
  [ `push of Frame.t list * 'state | `ok of 'state | `error of 'state * 'error ]

module type Intf = sig
  type state
  type args

  val init : args -> (state, [> `Unknown_opcode of int ]) handle_result

  val handle_frame :
    Frame.t ->
    Atacama.Connection.t ->
    state ->
    (state, [> `Unknown_opcode of int ]) handle_result

  val handle_message :
    Message.t -> state -> (state, [> `Unknown_opcode of int ]) handle_result
end

type t =
  | Sock : {
      handler : (module Intf with type args = 'args and type state = 'state);
      args : 'args;
      state : 'state option;
    }
      -> t

let make handler args = Sock { handler; args; state = None }

let init (Sock { handler = (module H) as handler; args; _ }) conn =
  match H.init args with
  | `ok state -> `continue (conn, Sock { handler; args; state = Some state })
  | `error (_state, err) -> `error (conn, err)
  | _ -> failwith "can't send frames on initialization"

let handle_frame (Sock ({ handler = (module H); state; _ } as sock)) frame conn
    =
  match H.handle_frame frame conn (Option.get state) with
  | `ok state -> `continue (conn, Sock { sock with state = Some state })
  | `error (_state, err) -> `error (conn, err)
  | `push (frames, state) ->
      `push (frames, Sock { sock with state = Some state })

let handle_message (Sock ({ handler = (module H); state; _ } as sock)) msg conn
    =
  match H.handle_message msg (Option.get state) with
  | `ok state -> `continue (conn, Sock { sock with state = Some state })
  | `error (_state, err) -> `error (conn, err)
  | `push (frames, state) ->
      `push (frames, Sock { sock with state = Some state })

module Default = struct
  let handle_frame _frame _conn _state = failwith "unimplemented"
  let handle_message _msg _state = failwith "unimplemented"
end
OCaml

Innovation. Community. Security.