Skip to main content

Module net

Module net 

Source
Available on crate feature net only.
Expand description

Ergonomic std socket helpers — MessageStream + MessageDatagram (the net feature). Ergonomic std socket helpers — the net feature.

Two wrappers so you exchange #[bin] messages instead of bytes + buffers, one per transport shape:

  • MessageStream — whole-message read/write over any Read + Write (a byte stream, e.g. a TcpStream). It owns the stream and buffers reads, so one value does both directions (no try_clone); messages must be self-delimiting (their #[bin] structure, a magic, or a length prefix bounds them).
  • MessageDatagram — whole-message send/recv over any DatagramSocket (a message-oriented socket where one recv is one whole message, e.g. a UdpSocket or a UnixDatagram). It owns the socket and reuses one receive buffer.

DatagramSocket is the datagram counterpart to Read + Write that std doesn’t ship — so MessageDatagram is generic across UDP and Unix datagram sockets (and, under the mock feature, MockDatagramSocket; the trait is sealed, so those are the only impls). Both wrappers bridge std::io::Error into BitError (the std feature), so a single ? covers I/O and codec errors.

Structs§

MessageDatagram
A whole-message sender/receiver over a DatagramSocket (a UdpSocket, a UnixDatagram, or — under the mock feature — a MockDatagramSocket). It owns the socket and reuses one receive buffer, so each datagram is exchanged as a #[bin] value — the datagram counterpart to MessageStream. Unlike a stream, a datagram socket talks to many peers, so every call carries the peer address.
MessageStream
A whole-message reader/writer over a byte stream (anything Read + Write, e.g. a TcpStream). It owns the stream and keeps a read buffer, so read_message and write_message exchange #[bin] values — and one MessageStream serves both directions on a single connection, no try_clone needed.
MockDatagramSocketmock
A test-only DatagramSocket backed by in-memory queues — exchange datagrams with a MessageDatagram in unit tests, no real socket bound. Enabled by the mock feature (put it in your [dev-dependencies]). Queue inbound datagrams with push_inbound (each is one recv_from) and inspect what was sent with sent.
MockStreammock
A test-only Read + Write byte stream backed by in-memory buffers — exercise MessageStream code in unit tests with no real socket. Enabled by the mock feature (put it in your [dev-dependencies]). Queue inbound bytes with push_inbound and inspect what was written with written.

Traits§

DatagramSocket
A message-oriented (datagram) socket: each recv_from yields exactly one whole message with its sender, and each send_to writes one message to a peer. This is the datagram counterpart to Read + Write (which std does ship but has no datagram analog of) — it makes a transport usable with MessageDatagram.