Skip to content

Commit

Permalink
chore: fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
leostera committed Dec 23, 2023
1 parent 7d45513 commit 310103e
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 66 deletions.
10 changes: 3 additions & 7 deletions riot/lib/file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ type write_file = [ `w ] file
type rw_file = [ `w | `r ] file

let fd t = t

let base_permissions = 0o640

let do_open path flags =
let do_open path flags =
let raw_fd = Unix.openfile path flags base_permissions in
Fd.make raw_fd

let open_read path = do_open path Unix.[O_RDONLY]

let open_write path = do_open path Unix.[O_WRONLY;O_CREAT]

let open_read path = do_open path Unix.[ O_RDONLY ]
let open_write path = do_open path Unix.[ O_WRONLY; O_CREAT ]
let close t = Fd.close t

let remove path = Unix.unlink path
15 changes: 7 additions & 8 deletions riot/lib/io.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ let write = Low_level.write

let rec single_read fd ~buf =
match Low_level.readv fd [| buf |] with
| `Abort err -> Error (`Unix_error err)
| `Read read -> Ok read
| `Retry -> Runtime.syscall "single_read" `r fd @@ single_read ~buf
| `Abort err -> Error (`Unix_error err)
| `Read read -> Ok read
| `Retry -> Runtime.syscall "single_read" `r fd @@ single_read ~buf

let rec single_write fd ~data =
match Low_level.writev fd [| data |] with
| `Abort err -> Error (`Unix_error err)
| `Read read -> Ok read
| `Retry -> Runtime.syscall "single_write" `w fd @@ single_write ~data
| `Abort err -> Error (`Unix_error err)
| `Read read -> Ok read
| `Retry -> Runtime.syscall "single_write" `w fd @@ single_write ~data

let await_readable fd fn = Runtime.syscall "custom" `r fd fn
let await_writeable fd fn = Runtime.syscall "custom" `w fd fn
let await fd mode fn = Runtime.syscall "custom" mode fd fn

let copy _fd _cs = ()
let copy _fd _cs = ()
30 changes: 14 additions & 16 deletions riot/riot.mli
Original file line number Diff line number Diff line change
Expand Up @@ -411,21 +411,18 @@ module Fd : sig
end

module File : sig
type 'kind file
type read_file = [ `r ] file
type write_file = [ `w ] file
type rw_file = [ `w | `r ] file

val fd : _ file -> Fd.t

val open_read : string -> read_file
val open_write : string -> write_file

val close : _ file -> unit
val remove : string -> unit
type 'kind file
type read_file = [ `r ] file
type write_file = [ `w ] file
type rw_file = [ `w | `r ] file

val fd : _ file -> Fd.t
val open_read : string -> read_file
val open_write : string -> write_file
val close : _ file -> unit
val remove : string -> unit
end


module IO : sig
type read = [ `Abort of Unix.error | `Read of int | `Retry ]

Expand All @@ -434,14 +431,15 @@ module IO : sig
type write = [ `Abort of Unix.error | `Retry | `Wrote of int ]

val write : Fd.t -> bytes -> int -> int -> write

val await_readable : Fd.t -> (Fd.t -> 'a) -> 'a
val await_writeable : Fd.t -> (Fd.t -> 'a) -> 'a
val await : Fd.t -> Fd.Mode.t -> (Fd.t -> 'a) -> 'a

val single_read : Fd.t -> buf:Cstruct.t -> (int, [> | `Unix_error of Unix.error]) result
val single_read :
Fd.t -> buf:Cstruct.t -> (int, [> `Unix_error of Unix.error ]) result

val single_write : Fd.t -> data:Cstruct.t -> (int, [> | `Unix_error of Unix.error]) result
val single_write :
Fd.t -> data:Cstruct.t -> (int, [> `Unix_error of Unix.error ]) result
end

module Net : sig
Expand Down
9 changes: 5 additions & 4 deletions riot/runtime/net/dune
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
(library
(package riot)
(name net)
(foreign_stubs
(language c)
(names io_posix_stubs)
(flags (:standard -O2)))
(foreign_stubs
(language c)
(names io_posix_stubs)
(flags
(:standard -O2)))
(libraries core util log poll uri unix telemetry bigstringaf cstruct))
10 changes: 6 additions & 4 deletions riot/runtime/net/io.ml
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,10 @@ let write (fd : Fd.t) buf off len : write =
| exception Unix.(Unix_error ((EINTR | EAGAIN | EWOULDBLOCK), _, _)) -> `Retry
| exception Unix.(Unix_error (reason, _, _)) -> `Abort reason

external riot_readv : Unix.file_descr -> Cstruct.t array -> int = "caml_riot_posix_readv"
external riot_readv : Unix.file_descr -> Cstruct.t array -> int
= "caml_riot_posix_readv"

let readv (fd : Fd.t) (cs: Cstruct.t array) : read =
let readv (fd : Fd.t) (cs : Cstruct.t array) : read =
Fd.use ~op_name:"readv" fd @@ fun unix_fd ->
Log.debug (fun f -> f "Readv-ing from fd=%a" Fd.pp fd);
match riot_readv unix_fd cs with
Expand All @@ -194,9 +195,10 @@ let readv (fd : Fd.t) (cs: Cstruct.t array) : read =
| exception Unix.(Unix_error ((EINTR | EAGAIN | EWOULDBLOCK), _, _)) -> `Retry
| exception Unix.(Unix_error (reason, _, _)) -> `Abort reason

external riot_writev : Unix.file_descr -> Cstruct.t array -> int = "caml_riot_posix_writev"
external riot_writev : Unix.file_descr -> Cstruct.t array -> int
= "caml_riot_posix_writev"

let writev (fd : Fd.t) (cs: Cstruct.t array) : read =
let writev (fd : Fd.t) (cs : Cstruct.t array) : read =
Fd.use ~op_name:"readv" fd @@ fun unix_fd ->
Log.debug (fun f -> f "Readv-ing from fd=%a" Fd.pp fd);
match riot_writev unix_fd cs with
Expand Down
1 change: 0 additions & 1 deletion riot/runtime/net/io.mli
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,5 @@ val connect :
val accept : t -> Fd.t -> accept
val read : Fd.t -> bytes -> int -> int -> read
val write : Fd.t -> bytes -> int -> int -> write

val readv : Fd.t -> Cstruct.t array -> read
val writev : Fd.t -> Cstruct.t array -> read
98 changes: 80 additions & 18 deletions test/dune
Original file line number Diff line number Diff line change
@@ -1,19 +1,81 @@
(tests
(names
add_monitor
application_test
io_readv_test
io_writev_test
link_processes
net_addr_uri_test
net_test
process_registration_test
readme_example
selective_receive
send_after
send_interval
send_order
spawn_and_exit
supervisor_shutdown
telemetry_test)
(test
(name add_monitor)
(modules add_monitor)
(libraries riot))

(test
(name application_test)
(modules application_test)
(libraries riot))

(test
(name io_readv_test)
(deps fixtures/io_readv.txt)
(modules io_readv_test)
(libraries riot))

(test
(name io_writev_test)
(modules io_writev_test)
(deps generated/.gitkeep)
(libraries riot))

(test
(name link_processes)
(modules link_processes)
(libraries riot))

(test
(name net_addr_uri_test)
(modules net_addr_uri_test)
(libraries riot))

(test
(name net_test)
(modules net_test)
(libraries riot))

(test
(name process_registration_test)
(modules process_registration_test)
(libraries riot))

(test
(name readme_example)
(modules readme_example)
(libraries riot))

(test
(name selective_receive)
(modules selective_receive)
(libraries riot))

(test
(name send_after)
(modules send_after)
(libraries riot))

(test
(name send_interval)
(modules send_interval)
(libraries riot))

(test
(name send_order)
(modules send_order)
(libraries riot))

(test
(name spawn_and_exit)
(modules spawn_and_exit)
(libraries riot))

(test
(name supervisor_shutdown)
(modules supervisor_shutdown)
(libraries riot))

(test
(name telemetry_test)
(modules telemetry_test)
(libraries riot))
4 changes: 2 additions & 2 deletions test/io_readv_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ let () =
Riot.run @@ fun () ->
let _ = Logger.start () |> Result.get_ok in
Logger.set_log_level (Some Info);
let fd = File.open_read "./test/fixtures/io_readv.txt" in
let fd = File.open_read "./fixtures/io_readv.txt" in
let buf = Cstruct.create 8 in
let len = Riot.IO.single_read fd ~buf |> Result.get_ok in
let len = Riot.IO.single_read (File.fd fd) ~buf |> Result.get_ok in
let str = Cstruct.to_string ~off:0 ~len buf in
match str with
| "hello wo" ->
Expand Down
14 changes: 8 additions & 6 deletions test/io_writev_test.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
open Riot

let data =
let data =
let str = {| this is some data |} in
Cstruct.of_string ~off:0 ~len:(String.length str) str

Expand All @@ -9,16 +9,18 @@ let () =
let _ = Logger.start () |> Result.get_ok in
Logger.set_log_level (Some Info);
let now = Ptime_clock.now () in
let file = (Format.asprintf "./test/generated/%a.io_writev.txt" (Ptime.pp_rfc3339 ()) now) in
let file =
Format.asprintf "./generated/%a.io_writev.txt" (Ptime.pp_rfc3339 ()) now
in
let fd = File.open_write file in
let len = IO.single_write fd ~data |> Result.get_ok in
let len = IO.single_write (File.fd fd) ~data |> Result.get_ok in
File.close fd;
let buf = Cstruct.create len in
let fd = File.open_read file in
let len = IO.single_read fd ~buf |> Result.get_ok in
let len = IO.single_read (File.fd fd) ~buf |> Result.get_ok in
File.close fd;
match (Cstruct.to_string ~off:0 ~len buf) with
| " this is some data " ->
match Cstruct.to_string ~off:0 ~len buf with
| {| this is some data |} ->
File.remove file;
Logger.info (fun f -> f "io_readv_test: OK");
sleep 0.1;
Expand Down

0 comments on commit 310103e

Please sign in to comment.