Skip to content

Commit

Permalink
introduce module Buffered_flow
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro committed Aug 5, 2022
1 parent c38f488 commit 3435604
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion mirage/dune
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(library
(name gluten_mirage)
(public_name gluten-mirage)
(libraries faraday-lwt gluten-lwt lwt mirage-flow conduit-mirage cstruct)
(libraries faraday-lwt gluten-lwt lwt mirage-flow cstruct)
(flags
(:standard -safe-string)))
27 changes: 15 additions & 12 deletions mirage/gluten_mirage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,27 @@

open Lwt.Infix

type 'a socket = {
flow : 'a;
mutable buf : Cstruct.t;
}
module Buffered_flow = struct
type 'a t = {
flow : 'a;
mutable buf : Cstruct.t;
}

let create_socket flow = { flow; buf = Cstruct.empty }
let create flow = { flow; buf = Cstruct.empty }
end

module Make_IO (Flow : Mirage_flow.S) :
Gluten_lwt.IO with type socket = Flow.flow socket and type addr = unit =
struct
type nonrec socket = Flow.flow socket
Gluten_lwt.IO
with type socket = Flow.flow Buffered_flow.t
and type addr = unit = struct
type socket = Flow.flow Buffered_flow.t
type addr = unit

let shutdown sock = Flow.close sock.flow
let shutdown (sock : _ Buffered_flow.t) = Flow.close sock.flow
let shutdown_receive sock = Lwt.async (fun () -> shutdown sock)
let close = shutdown

let buffered_read sock len =
let buffered_read (sock : _ Buffered_flow.t) len =
let trunc buf =
match Cstruct.length buf > len with
| false ->
Expand All @@ -76,7 +79,7 @@ struct
assert (Cstruct.is_empty sock.buf);
match data with Ok (`Data buf) -> Ok (`Data (trunc buf)) | x -> x)

let read sock bigstring ~off ~len =
let read (sock : _ Buffered_flow.t) bigstring ~off ~len =
Lwt.catch
(fun () ->
buffered_read sock len >|= function
Expand All @@ -90,7 +93,7 @@ struct
failwith (Format.asprintf "%a" Flow.pp_error error))
(fun exn -> shutdown sock >>= fun () -> Lwt.fail exn)

let writev sock iovecs =
let writev (sock : _ Buffered_flow.t) iovecs =
let cstruct_iovecs =
List.map
(fun { Faraday.buffer; off; len } ->
Expand Down
15 changes: 11 additions & 4 deletions mirage/gluten_mirage.mli
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,19 @@
* POSSIBILITY OF SUCH DAMAGE.
*---------------------------------------------------------------------------*)

type 'a socket
module Buffered_flow : sig
type 'a t = {
flow : 'a;
mutable buf : Cstruct.t;
}

val create_socket : 'a -> 'a socket
val create : 'a -> 'a t
end

module Server (Flow : Mirage_flow.S) :
Gluten_lwt.Server with type socket = Flow.flow socket and type addr = unit
Gluten_lwt.Server
with type socket = Flow.flow Buffered_flow.t
and type addr = unit

module Client (Flow : Mirage_flow.S) :
Gluten_lwt.Client with type socket = Flow.flow socket
Gluten_lwt.Client with type socket = Flow.flow Buffered_flow.t

0 comments on commit 3435604

Please sign in to comment.