-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from c-cube/wip-cleanup-2023-06-20
cleanup and IO backend
- Loading branch information
Showing
18 changed files
with
597 additions
and
175 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
#!/bin/sh | ||
exec dune exec --profile=release "examples/echo.exe" -- $@ | ||
exec dune exec --display=quiet --profile=release "examples/echo.exe" -- $@ |
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
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,103 @@ | ||
(** IO abstraction. | ||
We abstract IO so we can support classic unix blocking IOs | ||
with threads, and modern async IO with Eio. | ||
{b NOTE}: experimental. | ||
@since NEXT_RELEASE | ||
*) | ||
|
||
module Buf = Tiny_httpd_buf | ||
|
||
module In_channel = struct | ||
type t = { | ||
input: bytes -> int -> int -> int; | ||
(** Read into the slice. Returns [0] only if the | ||
channel is closed. *) | ||
close: unit -> unit; | ||
} | ||
|
||
let of_in_channel ?(close_noerr = false) (ic : in_channel) : t = | ||
{ | ||
input = (fun buf i len -> input ic buf i len); | ||
close = | ||
(fun () -> | ||
if close_noerr then | ||
close_in_noerr ic | ||
else | ||
close_in ic); | ||
} | ||
|
||
let of_unix_fd ?(close_noerr = false) (fd : Unix.file_descr) : t = | ||
{ | ||
input = (fun buf i len -> Unix.read fd buf i len); | ||
close = | ||
(fun () -> | ||
if close_noerr then ( | ||
try Unix.close fd with _ -> () | ||
) else | ||
Unix.close fd); | ||
} | ||
|
||
let[@inline] input (self : t) buf i len = self.input buf i len | ||
let[@inline] close self : unit = self.close () | ||
end | ||
|
||
module Out_channel = struct | ||
type t = { | ||
output: bytes -> int -> int -> unit; (** Output slice *) | ||
flush: unit -> unit; (** Flush underlying buffer *) | ||
close: unit -> unit; | ||
} | ||
|
||
let of_out_channel ?(close_noerr = false) (oc : out_channel) : t = | ||
{ | ||
output = (fun buf i len -> output oc buf i len); | ||
flush = (fun () -> flush oc); | ||
close = | ||
(fun () -> | ||
if close_noerr then | ||
close_out_noerr oc | ||
else | ||
close_out oc); | ||
} | ||
|
||
let[@inline] output (self : t) buf i len : unit = self.output buf i len | ||
|
||
let[@inline] output_string (self : t) (str : string) : unit = | ||
self.output (Bytes.unsafe_of_string str) 0 (String.length str) | ||
|
||
let[@inline] close self : unit = self.close () | ||
let[@inline] flush self : unit = self.flush () | ||
|
||
let output_buf (self : t) (buf : Buf.t) : unit = | ||
let b = Buf.bytes_slice buf in | ||
output self b 0 (Buf.size buf) | ||
end | ||
|
||
(** A TCP server abstraction *) | ||
module TCP_server = struct | ||
type conn_handler = { | ||
handle: In_channel.t -> Out_channel.t -> unit; | ||
(** Handle client connection *) | ||
} | ||
|
||
type t = { | ||
endpoint: unit -> string * int; | ||
(** Endpoint we listen on. This can only be called from within [serve]. *) | ||
active_connections: unit -> int; | ||
(** Number of connections currently active *) | ||
running: unit -> bool; (** Is the server currently running? *) | ||
stop: unit -> unit; | ||
(** Ask the server to stop. This might not take effect immediately. *) | ||
} | ||
(** Running server. *) | ||
|
||
type builder = { | ||
serve: after_init:(t -> unit) -> handle:conn_handler -> unit -> unit; | ||
(** Blocking call to listen for incoming connections and handle them. | ||
Uses the connection handler to handle individual client connections. *) | ||
} | ||
(** A TCP server implementation. *) | ||
end |
Oops, something went wrong.