Networking
Sockets from the beginning, then the part that actually decides whether a server works: a stream has no message boundaries, so you put them back yourself. Framing three ways, a parser that survives a message split across two reads, text and binary protocols in both directions, byte order, and one handler per connection through the Io interface. The protocol chapters run in your browser, because none of them know what a socket is.
- TCP is a byte stream, not a message stream. A read returning 7 bytes says nothing about where a message ends, and code that assumes otherwise works until it meets a real network.
- Parse from a
Reader, never from a socket. The same parser then works over a connection, over a test fixture and in a browser, and you write it once. - State the byte order at every call, and never send a struct. Padding is not yours to define and the layout is whatever your compiler chose today.
takeDelimiterExclusivetreats end of stream as a delimiter, so a client that dies mid-message hands you a fragment that looks like a complete one.io.asyncandGroupreplaced the removedasync/awaitkeywords. The same source serves connections on a thread pool or inline, and the caller picks.
10 chapters.
- What a Socket IsAn address, a mode, and a handle you read and write like a file.
- A TCP Round TripListen, connect, and echo through std.Io.net, all in one process.
- Bytes Have No EdgesA read returning 7 bytes tells you nothing about where a message ends.
- Three Ways to FrameLength prefix, delimiter, self-describing, and what each one costs.
- A Text Protocol, Both DirectionsParse a command out of bytes, render a reply back into bytes.
- Binary Protocols and Byte OrderA struct is not a wire format. Write the fields, and say which end first.
- Serving Many ClientsOne handler per connection through Io, so the same source runs either way.
- Failure Is OrdinaryEnd of stream is not an error, and a truncated message can look like a good one.
- UDP DatagramsBound sockets and discrete messages, no connection anywhere.
- An HTTP Round TripA std.http.Server on a thread, a std.http.Client fetching from it, no dependencies.