Skip to content

Commit

Permalink
Illustrate network example
Browse files Browse the repository at this point in the history
  • Loading branch information
talex5 committed Feb 21, 2024
1 parent 1cbcda0 commit 1a574e1
Show file tree
Hide file tree
Showing 4 changed files with 839 additions and 3 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,15 +392,16 @@ Here is a client that connects to address `addr` using network `net` and reads a

```ocaml
let run_client ~net ~addr =
Switch.run ~name:"client" @@ fun sw ->
traceln "Client: connecting to server";
Switch.run @@ fun sw ->
let flow = Eio.Net.connect ~sw net addr in
let b = Buffer.create 100 in
Eio.Flow.copy flow (Eio.Flow.buffer_sink b);
traceln "Client: received %S" (Buffer.contents b)
```

Note: the `flow` is attached to `sw` and will be closed automatically when it finishes.
We also named the switch here; this will appear in the trace output (see below).

This can also be tested on its own using a mock network:

Expand Down Expand Up @@ -439,7 +440,7 @@ We can now run the client and server together using the real network (in a singl

```ocaml
let main ~net ~addr =
Switch.run @@ fun sw ->
Switch.run ~name:"main" @@ fun sw ->
let server = Eio.Net.listen net ~sw ~reuse_addr:true ~backlog:5 addr in
Fiber.fork_daemon ~sw (fun () -> run_server server);
run_client ~net ~addr
Expand All @@ -460,6 +461,10 @@ the test would never finish.
- : unit = ()
```

<p align='center'>
<img src="./doc/traces/net-posix.svg"/>
</p>

See [examples/net](./examples/net/) for a more complete example.

## Design Note: Capabilities
Expand Down
2 changes: 1 addition & 1 deletion doc/traces/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
all: both-posix.svg cancel-posix.svg switch-mock.svg
all: both-posix.svg cancel-posix.svg switch-mock.svg net-posix.svg

%.svg: %.fxt
eio-trace render -f "$<" "$@"
Binary file added doc/traces/net-posix.fxt
Binary file not shown.
Loading

0 comments on commit 1a574e1

Please sign in to comment.