Skip to content

Commit

Permalink
Use Buf_write.BE and remove flushes
Browse files Browse the repository at this point in the history
  • Loading branch information
patricoferris committed Sep 11, 2022
1 parent 7d43000 commit 6181364
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
39 changes: 11 additions & 28 deletions eio/io.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ let int_value shift len v = (v lsr shift) land ((1 lsl len) - 1)

(* Eio only uses Cstructs so we lose 0copy :(! *)
let read_exactly src remaining =
let open Cohttp_eio.Server in
try
Buf_read.ensure src remaining ;
let t = Some (Cstruct.to_string (Buf_read.peek src) ~off:0 ~len:remaining) in
Buf_read.consume src remaining ;
t
Some (Buf_read.take remaining src)
with End_of_file -> None

let read_uint16 ic =
Expand All @@ -38,7 +34,6 @@ let read_int64 ic =
| Some s -> Some (Int64.to_int @@ EndianString.BigEndian.get_int64 s 0)

let write_frame_to_buf ~mode buf fr =
let scratch = Bytes.create 8 in
let open Frame in
let content = Bytes.unsafe_of_string fr.content in
let len = Bytes.length content in
Expand All @@ -54,16 +49,14 @@ let write_frame_to_buf ~mode buf fr =
let hdr = set_bit hdr 7 (is_client mode) in
let hdr = hdr lor payload_len in
(* Payload len is guaranteed to fit in 7 bits *)
EndianBytes.BigEndian.set_int16 scratch 0 hdr ;
Buf_write.bytes ~off:0 ~len:2 buf scratch ;
Buf_write.BE.uint16 buf hdr;
( match len with
| n when n < 126 -> ()
| n when n < 1 lsl 16 ->
EndianBytes.BigEndian.set_int16 scratch 0 n ;
Buf_write.bytes ~off:0 ~len:2 buf scratch
Buf_write.BE.uint16 buf n
| n ->
EndianBytes.BigEndian.set_int64 scratch 0 Int64.(of_int n) ;
Buf_write.bytes ~off:0 ~len:8 buf scratch ) ;
Buf_write.BE.uint64 buf Int64.(of_int n);
);
( match mode with
| Server -> ()
| Client random_string ->
Expand All @@ -73,9 +66,7 @@ let write_frame_to_buf ~mode buf fr =
Buf_write.bytes buf content

let close_with_code mode dst code =
Buf_write.flush dst ;
write_frame_to_buf ~mode dst @@ Frame.close code ;
Buf_write.flush dst
write_frame_to_buf ~mode dst @@ Frame.close code

let read_frame ic oc mode hdr =
let hdr_part1 = EndianString.BigEndian.get_int8 hdr 0 in
Expand All @@ -86,7 +77,6 @@ let read_frame ic oc mode hdr =
let frame_masked = is_bit_set 7 hdr_part2 in
let length = int_value 0 7 hdr_part2 in
let opcode = Frame.Opcode.of_enum opcode in
Buf_write.flush oc ;
let payload_len =
match length with
| i when i < 126 -> i
Expand All @@ -103,14 +93,12 @@ let read_frame ic oc mode hdr =
else
let mask =
if frame_masked then (
Buf_write.flush oc ;
match read_exactly ic 4 with
| None -> proto_error "could not read mask"
| Some mask -> mask )
else String.empty in
if payload_len = 0 then Frame.create ~opcode ~extension ~final ()
else (
Buf_write.flush oc ;
match read_exactly ic payload_len with
| None -> proto_error "could not read payload (len=%d)" payload_len
| Some payload ->
Expand All @@ -120,7 +108,6 @@ let read_frame ic oc mode hdr =
frame )

let make_read_frame ~mode ic oc () =
Buf_write.flush oc ;
match read_exactly ic 2 with
| None -> raise End_of_file
| Some hdr -> read_frame ic oc mode hdr
Expand All @@ -136,24 +123,20 @@ module Connected_client = struct

let source {endp; _} = endp

let create ?read_buf http_request endp ic oc =
let create http_request endp ic oc =
let read_frame = make_read_frame ~mode:Server ic oc in
{ buffer= oc;
{ buffer = oc;
endp;
ic;
http_request;
standard_frame_replies= false;
standard_frame_replies = false;
read_frame }

let send {buffer; _} frame =
Buf_write.flush buffer ;
write_frame_to_buf ~mode:Server buffer frame ;
Buf_write.flush buffer
write_frame_to_buf ~mode:Server buffer frame

let send_multiple {buffer; _} frames =
Buf_write.flush buffer ;
List.iter (write_frame_to_buf ~mode:Server buffer) frames ;
Buf_write.flush buffer
List.iter (write_frame_to_buf ~mode:Server buffer) frames

let standard_recv t =
let fr = t.read_frame () in
Expand Down
4 changes: 4 additions & 0 deletions websocket-eio.opam
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ build: [
["dune" "install" "-p" name "--create-install-files" name]
]
dev-repo: "git+https://github.com/vbmithr/ocaml-websocket.git"
pin-depends: [
[ "http.dev" "git+https://github.com/mirage/ocaml-cohttp#ce5f271b69fe42471ede858c5b8ce8203e3c14ad" ]
[ "cohttp-eio.dev" "git+https://github.com/mirage/ocaml-cohttp#ce5f271b69fe42471ede858c5b8ce8203e3c14ad" ]
]

0 comments on commit 6181364

Please sign in to comment.