diff options
Diffstat (limited to 'src/message.ml')
-rw-r--r-- | src/message.ml | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/message.ml b/src/message.ml new file mode 100644 index 0000000..24803fe --- /dev/null +++ b/src/message.ml @@ -0,0 +1,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 |