wip: improve echo server/client

This commit is contained in:
Simon Cruanes 2025-05-02 00:27:21 -04:00
parent 299dd9dddb
commit caeae5794c
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 45 additions and 9 deletions

View file

@ -5,13 +5,24 @@ module IO = Nanoev_picos
[@@@ocaml.alert "-deprecated"] [@@@ocaml.alert "-deprecated"]
let ( let@ ) = ( @@ ) let ( let@ ) = ( @@ )
let spf = Printf.sprintf
let pf = Printf.printf let pf = Printf.printf
let verbose = ref false let verbose = ref false
let main ~port ~n ~n_conn () = let main ~port ~unix_sock ~n ~n_conn () =
pf "connect on localhost:%d n=%d n_conn=%d\n%!" port n n_conn; pf "connect on %s n=%d n_conn=%d\n%!"
(if unix_sock = "" then
spf "localhost:%d" port
else
spf "unix:%S" unix_sock)
n n_conn;
let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, port) in let addr =
if unix_sock = "" then
Unix.ADDR_INET (Unix.inet_addr_loopback, port)
else
Unix.ADDR_UNIX unix_sock
in
let remaining = Atomic.make n in let remaining = Atomic.make n in
let all_done = Atomic.make 0 in let all_done = Atomic.make 0 in
@ -67,6 +78,7 @@ let () =
Trace.set_thread_name "main"; Trace.set_thread_name "main";
let port = ref 1234 in let port = ref 1234 in
let unix_sock = ref "" in
let n = ref 1000 in let n = ref 1000 in
let n_conn = ref 20 in let n_conn = ref 20 in
let opts = let opts =
@ -74,10 +86,15 @@ let () =
"-p", Arg.Set_int port, " port"; "-p", Arg.Set_int port, " port";
"-v", Arg.Set verbose, " verbose"; "-v", Arg.Set verbose, " verbose";
"-n", Arg.Set_int n, " number of iterations"; "-n", Arg.Set_int n, " number of iterations";
"--unix", Arg.Set_string unix_sock, " unix socket";
"--n-conn", Arg.Set_int n_conn, " number of simultaneous connections"; "--n-conn", Arg.Set_int n_conn, " number of simultaneous connections";
] ]
|> Arg.align |> Arg.align
in in
Arg.parse opts ignore "echo_client"; Arg.parse opts ignore "echo_client";
F.main @@ fun _runner -> main ~port:!port ~n:!n ~n_conn:!n_conn () let@ () =
Nanoev_picos.Background_thread.with_setup (Nanoev_posix.create ())
in
F.main @@ fun _runner ->
main ~port:!port ~unix_sock:!unix_sock ~n:!n ~n_conn:!n_conn ()

View file

@ -14,10 +14,20 @@ let str_of_sockaddr = function
| Unix.ADDR_INET (addr, port) -> | Unix.ADDR_INET (addr, port) ->
spf "%s:%d" (Unix.string_of_inet_addr addr) port spf "%s:%d" (Unix.string_of_inet_addr addr) port
let main ~port ~runner () = let main ~port ~unix_sock ~runner () =
pf "serve on localhost:%d\n%!" port; pf "serve on %s\n%!"
(if unix_sock = "" then
spf "localhost:%d" port
else
spf "unix:%S" unix_sock);
let addr =
if unix_sock = "" then
Unix.ADDR_INET (Unix.inet_addr_loopback, port)
else
Unix.ADDR_UNIX unix_sock
in
let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, port) in
let server = let server =
IO.Net_server.establish addr IO.Net_server.establish addr
~spawn:(fun f -> Moonpool.spawn ~on:runner f) ~spawn:(fun f -> Moonpool.spawn ~on:runner f)
@ -45,16 +55,25 @@ let main ~port ~runner () =
if !verbose then if !verbose then
pf "done with client on %s\n%!" (str_of_sockaddr client_addr)) pf "done with client on %s\n%!" (str_of_sockaddr client_addr))
in in
IO.Net_server.join server;
IO.Net_server.shutdown server; IO.Net_server.shutdown server;
print_endline "exit" print_endline "exit"
let () = let () =
let@ () = Trace_tef.with_setup () in let@ () = Trace_tef.with_setup () in
let port = ref 1234 in let port = ref 1234 in
let unix_sock = ref "" in
let opts = let opts =
[ "-p", Arg.Set_int port, " port"; "-v", Arg.Set verbose, " verbose" ] [
"-p", Arg.Set_int port, " port";
"--unix", Arg.Set_string unix_sock, " unix socket";
"-v", Arg.Set verbose, " verbose";
]
|> Arg.align |> Arg.align
in in
Arg.parse opts ignore "echo_server"; Arg.parse opts ignore "echo_server";
F.main @@ fun runner -> main ~port:!port ~runner () let@ () =
Nanoev_picos.Background_thread.with_setup (Nanoev_posix.create ())
in
F.main @@ fun runner -> main ~port:!port ~unix_sock:!unix_sock ~runner ()