Hi
I’ve created a simple echo server using eio
:
open Eio
let handler socket _addr =
traceln "Server accepted connection from client";
Eio.Flow.copy socket socket;
traceln "Connection closed"
let on_error e = traceln "Error handling connection: %a" Fmt.exn e
let run_server socket =
Switch.run @@ fun sw ->
let rec loop () =
Eio.Net.accept_fork ~sw ~on_error socket handler;
loop ()
in
loop ()
let main ~net ~addr =
Switch.run @@ fun sw ->
let socket = Eio.Net.listen net ~sw ~reuse_addr:true ~backlog:10 addr in
traceln "Server ready... Listening on %a" Net.Sockaddr.pp addr;
run_server socket
let () =
let addr = `Tcp (Net.Ipaddr.V4.any, 8080) in
Eio_main.run @@ fun env -> main ~net:(Eio.Stdenv.net env) ~addr
What I’m interested to understand is the inferred type of socket
in the handler
function.
According to ocaml-lsp
it is:
< copy : 'b. (#Flow.source as 'b) -> unit
; probe : 'a. 'a Generic.ty -> 'a option
; read_into : Cstruct.t -> int
; read_methods : Flow.read_method list
; write : Cstruct.t list -> unit
; .. >
I believe this is something to do with the object system but I haven’t used that before and would love some help unpacking what it means.
Also, why do I so often see Cstruct.t
in eio
, what is that used for?
Thanks
Ryan