-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
115 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
open Std | ||
|
||
type shutdown_command = [ `Receive | `Send | `All ] | ||
|
||
type 't read_method = .. | ||
type 't read_method += Read_source_buffer of ('t -> (Cstruct.t list -> int) -> unit) | ||
|
||
type source_ty = [`R | `Flow] | ||
type 'a source = ([> source_ty] as 'a) r | ||
|
||
type sink_ty = [`W | `Flow] | ||
type 'a sink = ([> sink_ty] as 'a) r | ||
|
||
type shutdown_ty = [`Shutdown] | ||
type 'a shutdown = ([> shutdown_ty] as 'a) r | ||
|
||
module Pi = struct | ||
module type SOURCE = sig | ||
type t | ||
val read_methods : t read_method list | ||
val single_read : t -> Cstruct.t -> int | ||
end | ||
|
||
module type SINK = sig | ||
type t | ||
val single_write : t -> Cstruct.t list -> int | ||
val copy : t -> src:_ source -> unit | ||
end | ||
|
||
module type SHUTDOWN = sig | ||
type t | ||
val shutdown : t -> shutdown_command -> unit | ||
end | ||
|
||
type (_, _, _) Resource.pi += | ||
| Source : ('t, (module SOURCE with type t = 't), [> source_ty]) Resource.pi | ||
| Sink : ('t, (module SINK with type t = 't), [> sink_ty]) Resource.pi | ||
| Shutdown : ('t, (module SHUTDOWN with type t = 't), [> shutdown_ty]) Resource.pi | ||
|
||
let source (type t) (module X : SOURCE with type t = t) = | ||
Resource.handler [H (Source, (module X))] | ||
|
||
let sink (type t) (module X : SINK with type t = t) = | ||
Resource.handler [H (Sink, (module X))] | ||
|
||
let shutdown (type t) (module X : SHUTDOWN with type t = t) = | ||
Resource.handler [ H (Shutdown, (module X))] | ||
|
||
module type TWO_WAY = sig | ||
include SHUTDOWN | ||
include SOURCE with type t := t | ||
include SINK with type t := t | ||
end | ||
|
||
let two_way (type t) (module X : TWO_WAY with type t = t) = | ||
Resource.handler [ | ||
H (Shutdown, (module X)); | ||
H (Source, (module X)); | ||
H (Sink, (module X)); | ||
] | ||
|
||
let simple_copy ~single_write t ~src:(Resource.T (src, src_ops)) = | ||
let rec write_all buf = | ||
if not (Cstruct.is_empty buf) then ( | ||
let sent = single_write t [buf] in | ||
write_all (Cstruct.shift buf sent) | ||
) | ||
in | ||
let module Src = (val (Resource.get src_ops Source)) in | ||
try | ||
let buf = Cstruct.create 4096 in | ||
while true do | ||
let got = Src.single_read src buf in | ||
write_all (Cstruct.sub buf 0 got) | ||
done | ||
with End_of_file -> () | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters