this repo has no description
1open Swim.Types
2
3external env_cast : 'a -> 'b = "%identity"
4
5let hex_of_cstruct cs =
6 let len = Cstruct.length cs in
7 let buf = Buffer.create (len * 2) in
8 for i = 0 to len - 1 do
9 Buffer.add_string buf (Printf.sprintf "%02x" (Cstruct.get_uint8 cs i))
10 done;
11 Buffer.contents buf
12
13let () =
14 Eio_main.run @@ fun env ->
15 let env = env_cast env in
16 Eio.Switch.run @@ fun sw ->
17 let net = env#net in
18 let sock =
19 Swim.Transport.create_udp_socket net ~sw ~addr:"\127\000\000\001" ~port:7947
20 in
21
22 Printf.printf "Listening on 127.0.0.1:7947 for UDP packets...\n%!";
23
24 let buf = Cstruct.create 1500 in
25 for i = 1 to 10 do
26 Printf.printf "Waiting for packet %d...\n%!" i;
27 let src, n = Eio.Net.recv sock buf in
28 let received = Cstruct.sub buf 0 n in
29 Printf.printf "Received %d bytes from %s\n%!" n
30 (match src with
31 | `Udp (ip, port) ->
32 Printf.sprintf "%s:%d" (Fmt.to_to_string Eio.Net.Ipaddr.pp ip) port
33 | _ -> "unknown");
34 Printf.printf "Hex: %s\n%!" (hex_of_cstruct received);
35 Printf.printf "First byte (msg type): %d\n%!" (Cstruct.get_uint8 received 0);
36
37 (* Try to decode *)
38 match Swim.Codec.decode_packet received with
39 | Ok packet -> Printf.printf "Decoded! Cluster: %s\n%!" packet.cluster
40 | Error e -> Printf.printf "Decode error: %s\n%!" (decode_error_to_string e)
41 done