Hello! I just started reading up on the Eio library and I’m trying to use it for a simple toy application.
I connect to a port using TCP, receive some bytes and print them if they are successfully parsed as a valid message:
type message = { len : int; mt : int; msg : string }
let message =
let open Eio.Buf_read.Syntax in
let* len = Eio.Buf_read.uint8 in
let* mt = Eio.Buf_read.uint8 in
let+ msg = Eio.Buf_read.take (len-1) in
let _ = Eio.traceln "Reached -> %d %c %s" len (Char.chr mt) msg in
{ len; mt; msg }
let run_client conn =
Eio.traceln "Connection succeeded";
match Eio.Buf_read.parse message conn ~max_size:1024 with
| Ok { len; mt; msg } -> Eio.traceln "Not reached -> %d %c %s" len (Char.chr mt) msg
| Error (`Msg err) -> Eio.traceln "Parse failed: %s" err
let main net clock =
let rec reconnect () =
try Eio.Net.with_tcp_connect ~host:"localhost" ~service:"1234" net run_client with e -> Eio.traceln "%s" (Printexc.to_string e);
Eio.Time.sleep clock 30.0;
reconnect ()
in reconnect ()
let () =
Eio_main.run @@ fun env ->
let net = Eio.Stdenv.net env in
let clock = Eio.Stdenv.clock env in
main net clock
Now, I haven’t used parser combinators before and I don’t fully understand let* and let+ yet, so I am likely missing something obvious, but the type checker is happy. If I start a local server with nc -l 1234 and send AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA to the client, I can reach the traceln inside the message function, but not the traceln in the first case of the pattern match. It looks like the parser doesn’t return and the code is blocked waiting to read from the socket.
What should I do to avoid getting stuck and instead have valid messages reach the pattern match?