add Nanoev_picos.Net with send/recv functions

This commit is contained in:
Simon Cruanes 2025-05-06 11:01:54 -04:00
parent 00c845ec8d
commit 0f8e8797e8
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
4 changed files with 28 additions and 3 deletions

View file

@ -4,5 +4,6 @@ include Base
module Global_ev = Global_ev module Global_ev = Global_ev
module IO_in = IO_in module IO_in = IO_in
module IO_out = IO_out module IO_out = IO_out
module Net = Net
module Net_client = Net_client module Net_client = Net_client
module Net_server = Net_server module Net_server = Net_server

View file

@ -15,5 +15,6 @@ end
module IO_in = IO_in module IO_in = IO_in
module IO_out = IO_out module IO_out = IO_out
module Net = Net
module Net_client = Net_client module Net_client = Net_client
module Net_server = Net_server module Net_server = Net_server

11
src/picos/net.ml Normal file
View file

@ -0,0 +1,11 @@
let[@inline] send sock buf i len ~flags : int =
Base.Raw.retry_write sock (fun () -> Unix.send sock buf i len flags)
let[@inline] sendto sock ~addr ~flags buf i len : int =
Base.Raw.retry_write sock (fun () -> Unix.sendto sock buf i len flags addr)
let[@inline] recv sock buf i len ~flags : int =
Base.Raw.retry_read sock (fun () -> Unix.recv sock buf i len flags)
let[@inline] recvfrom sock buf i len ~flags : int * Unix.sockaddr =
Base.Raw.retry_read sock (fun () -> Unix.recvfrom sock buf i len flags)

View file

@ -159,7 +159,12 @@ let wakeup_from_outside (self : st) : unit =
let rec perform_cbs ~closed = function let rec perform_cbs ~closed = function
| Nil -> () | Nil -> ()
| Sub (x, y, f, tail) -> | Sub (x, y, f, tail) ->
f ~closed x y; (try f ~closed x y
with exn ->
let bt = Printexc.get_raw_backtrace () in
Printf.eprintf "nanoev-posix: uncaught error %s\n%s%!"
(Printexc.to_string exn)
(Printexc.raw_backtrace_to_string bt));
perform_cbs ~closed tail perform_cbs ~closed tail
(** Change the event loop right now. This must be called only from the owner (** Change the event loop right now. This must be called only from the owner
@ -320,8 +325,15 @@ let step (self : st) : unit =
let (Timer t) = Heap.peek_min_exn self.timer in let (Timer t) = Heap.peek_min_exn self.timer in
if t.deadline <= now then ( if t.deadline <= now then (
ignore (Heap.pop_min_exn self.timer : timer_ev); ignore (Heap.pop_min_exn self.timer : timer_ev);
try
t.f t.x t.y; t.f t.x t.y;
true true
with exn ->
let bt = Printexc.get_raw_backtrace () in
Printf.eprintf "nanoev-posix: uncaught error %s in timer\n%s%!"
(Printexc.to_string exn)
(Printexc.raw_backtrace_to_string bt);
false
) else ) else
false false
) )