blob: 24803fe9935bcebc8e511b57ba57e38c6fc290be (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
(** The wire protocol of VyConf.
Messages are preceded by a length header, four bytes in network order.
*)
let read ic =
let header = Bytes.create 4 in
let%lwt () = Lwt_io.read_into_exactly ic header 0 4 in
let length = EndianBytes.BigEndian.get_int32 header 0 |> Int32.to_int in
if length < 0 then failwith (Printf.sprintf "Bad message length: %d" length) else
let buffer = Bytes.create length in
let%lwt () = Lwt_io.read_into_exactly ic buffer 0 length in
Lwt.return buffer
let write oc msg =
let length = Bytes.length msg in
let length' = Int32.of_int length in
if length' < 0l then failwith (Printf.sprintf "Bad message length: %d" length) else
let header = Bytes.create 4 in
let () = EndianBytes.BigEndian.set_int32 header 0 length' in
let%lwt () = Lwt_io.write_from_exactly oc header 0 4 in
Lwt_io.write_from_exactly oc msg 0 length
|