Networking
The chapters start with sockets. Then the chapters teach the part that decides if a server works. A stream has no message boundaries, so you must restore the boundaries yourself. The topics are three types of framing, a parser for a message split across two reads, text and binary protocols in both directions, and byte order. The last topic is one handler for each connection through the Io interface. After UDP and HTTP, four chapters build WebSockets: the handshake, the frames, a client and a server. The protocol chapters run in your browser, because no protocol chapter uses a socket.
- TCP is a byte stream, not a message stream. If a read returns 7 bytes, that tells you nothing about where a message ends. Code that assumes a different result works until it runs on a real network.
- Parse from a
Reader, never from a socket. Then the same parser works with a connection, with a test fixture and in a browser, and you write the parser once. - State the byte order at every call. Never send a struct. You do not define the padding, and the layout is what your compiler chose today.
takeDelimiterExclusivetreats end of stream as a delimiter. If a client stops in the middle of a message, you get a fragment that looks like a complete message.io.asyncandGroupreplaced the removedasync/awaitkeywords. The same source can serve connections on a thread pool or inline, and the caller chooses.
17 chapters.
- What a Socket IsAn address, a mode, and a handle that 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 that returns 7 bytes does not tell you where a message ends.
- Three Ways to FrameLength prefix, delimiter, self-describing, and the cost of each one.
- A Text Protocol, Both DirectionsParse a command from bytes, and write a reply back into bytes.
- Binary Protocols and Byte OrderA struct is not a wire format. Write the fields, and state which end is first.
- Serve Many ClientsOne handler for each connection through Io, thus the same source runs with threads and without threads.
- Failure Is OrdinaryThe end of a stream is not an error, and a truncated message can look like a good message.
- UDP DatagramsBound sockets and discrete messages, without a connection.
- Lost DatagramsLoss, duplication and reordering, made on demand so that you can see them.
- Sequence Numbers and AcksOne counter makes an invisible loss into a detected gap, and one reply gives the problem to the sender.
- Retransmission and the WindowA timeout recovers the messages that the network lost, and a window keeps traffic in motion during the recovery.
- An HTTP Round TripA std.http.Server on a thread, and a std.http.Client that fetches from it, with no dependencies.
- The WebSocket HandshakeAn HTTP request asks to upgrade, std.http.Server answers 101, and the checks that std leaves to your code.
- WebSocket FramesThe two header bytes as packed structs, the three length forms, the client mask, and the frames that std.http.Server refuses.
- A WebSocket ClientA client that std does not have. It checks the handshake, masks each frame, puts fragments together, answers a ping, and closes correctly.
- A WebSocket Server on a Socketstd.http.Server upgrades a real TCP connection and echoes messages. The server answers pings, closes correctly, and refuses a message that is too big.