Skip to content
dream-net

Library active

dream-net

DreamWeave's networking substrate: secure UDP between authenticated peers, with reliable-ordered and unreliable event channels and a schema handshake.

cargo add dream-net
Stable
2.1.0 2026-11-19
Package
Rust crate
License
(MIT OR Apache-2.0) AND BSD-3-Clause AND ISC

Overview

A multiplayer engine needs two kinds of traffic. A chat line, a spell cast or an inventory change must arrive exactly once and in order, however many packets the network loses. An actor’s position is stale the moment the next one exists, and resending it is worse than useless. Both kinds have to share one encrypted connection, reach only peers that proved who they are, and never let a hostile peer grow the server’s memory or crash it.

dream-net is that transport for the DreamWeave engine. It moves opaque event payloads between authenticated peers and stops there: it knows event ids, channels, peers and bytes, and never what an event means. Who may send what, and where it goes next, is decided above it.

One event, from send to poll

The host hands dream-net an event id and bytes. They cross in a packet that many events share, inside an encrypted netcode datagram, and come out of the other host's poll with the sender attached.

  1. source

    send

    Checks the event and its size against the schema, and copies the bytes into its channel's queue before returning.

  2. tool

    flush

    Packs events from every channel into packets sized to one datagram. reliable numbers and acknowledges them; netcode encrypts them and sends them over UDP.

  3. result

    poll

    On the other host, after its update: lifecycle records and events in arrival order, with the authenticated sender.

input

Schema

Channels and events, frozen before networking starts. Both ends compare its fingerprint before any event is accepted.

input

TransportConfig

Packet sizes, timers, and the byte budgets that bound every queue.

How the pieces relate
  • send flush
  • flush poll
  • Schema ids and sizes send
  • TransportConfig bounds flush

It is built on Más Bandwidth’s C libraries, vendored at pinned releases and compiled into the crate: netcode for connect tokens, encryption, replay protection and client slots, and reliable for packet acknowledgement, fragmentation and link statistics. dream-net adds the part they leave out: channels of events, packed many to a datagram, in its own bit-packed wire format.

  • Events are the protocol unit. A schema names every channel and event before networking starts. Event ids come from the sorted names, never the order they were declared in, and both ends compare a fingerprint of the whole schema before a single event is accepted. A mismatch is reported on both ends, with both fingerprints.
  • Polled, never called back. A host runs one network phase per frame: update, poll, send, flush. Nothing calls into the host from inside the transport.
  • The sender is transport metadata. A PeerId comes from the authenticated netcode slot and carries a generation, so a handle to a peer that left never reaches the next one in its slot.
  • Bounded memory. Every queue has a limit, and what a peer can make a connection hold on receive has a fixed ceiling. Malformed input is refused, counted and disconnected, never a panic.
use dream_net::{ChannelConfig, Schema, Server, ServerConfig, ServerEvent, TransportConfig};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut schema = Schema::builder(1);
    let reliable = schema.channel(ChannelConfig::reliable_ordered("reliable"))?;
    let state = schema.channel(ChannelConfig::unreliable_unordered("state"))?;
    schema.event("Chat", reliable, 256)?;
    schema.event("ActorPosition", state, 28)?;
    let schema = schema.build()?;
    let chat = schema.event_id("Chat").expect("declared above");

    let config = ServerConfig {
        public_address: "127.0.0.1:0".parse()?,
        protocol_id: 0x4452_4541_4d00_0001,
        max_clients: 16,
        transport: TransportConfig::default(),
    };
    let mut server = Server::new(config, &dream_net::generate_key(), schema, 0.0)?;
    let mut buffer = [0u8; 256];

    // once per frame, in one network phase
    server.update(1.0 / 60.0);
    while let Some(event) = server.poll_into(&mut buffer)? {
        match event {
            ServerEvent::Connected { peer, .. } => server.send(peer, chat, b"welcome")?,
            ServerEvent::Message { peer, event, payload, .. } => {
                println!("{peer} sent {event}: {:?}", &buffer[..payload]);
            }
            ServerEvent::Disconnected { .. } | ServerEvent::Rejected { .. } => {}
        }
    }
    server.flush();
    Ok(())
}

Luau scripts reach it through l3i, which binds dream-net as the @dream/net module. The host still creates servers in Rust: the private key never reaches a script.

Documentation#

  • Start here: a server and a client in one program, exchanging their first events.
  • Guide: how it fits together, schemas, channels, hosting, memory limits, captures and the simulator.
  • Rust API: every public type and function.
  • Compatibility and performance: the Rust version, the license, what is tested, and what a frame costs.

Install

Rust crate · 2.1.0

Add it with Cargofrom crates.io, 2.1.0 today

From your project's directory:

cargo add dream-net

Or by hand, in Cargo.toml:

[dependencies]
dream-net = "2.1.0"

Cargo reads a bare version as "this or any compatible newer release" and records the exact one in Cargo.lock.

From the repositorythe default branch, ahead of the latest release
[dependencies]
dream-net = { git = "https://github.com/DreamWeave-MP/dream-net" }

Cargo pins the commit it fetched in Cargo.lock; cargo update -p dream-net moves it forward.

  1. 2.1.0 stable 2026-11-19

    Fixes AEAD nonce reuse in the netcode handshake (GHSA-8mrq-q9c6-h23v) by moving to netcode 1.5.0 and the NETCODE 1.03 protocol. 2.1.0 peers do not connect to 2.0.0 or 1.x peers: upgrade servers and clients together.

    Breaking changes

    • The netcode protocol is NETCODE 1.03, where 2.0.0 and every 1.x release speak NETCODE 1.02. Peers of the two never connect: a server ignores the other protocol’s connection requests, so the client’s attempt ends with ConnectFailed { reason: TimedOut }. dream-net’s own wire format is unchanged.

    Changed

    • The vendored C netcode is v1.5.0 (from v1.4.10), with the same three local patches. Every connection attempt draws a fresh client salt, every server mapping a fresh nonce, and the packet keys are derived from them and the token’s keys, so no two attempts or servers ever share a key.
    • A client no longer refuses a token it already passed to connect in the same process. That was 2.0.0’s stopgap for this advisory; netcode now makes a token used twice safe, and the server enforces one attempt per token, holding an unfinished token only for the address and salt that first presented it, for the token’s timeout. Still mint a new token for every attempt.

    Fixed

    • GHSA-8mrq-q9c6-h23v: with netcode 1.4.10 and earlier, a retried unfinished handshake with the same connect token, or one token listing several servers, could encrypt different packets under the same key and nonce, which exposes plaintext and allows forging those packets. 2.0.0 blocked the retried handshake within one process only; 1.x did not block it at all.
    dream-net 2.1.0 491.80 kB · crates.io
    View on crates.io
    Verify · sha256 9889d5757fb3…fe331458
    File
    dream-net-2.1.0.crate
    Size
    503602 bytes
    SHA-256
    9889d5757fb3d7fe2000dd0c0a7e01d94b0ad8cacdab4247da0aaba4fe331458

    The same digest as the crates.io index's checksum, which Cargo checks on every download.

  2. 2.0.0 stable 2026-11-19

    The transport now runs on Más Bandwidth’s C netcode and reliable, vendored and compiled in, with single-use connect tokens and restart protection. The wire format is unchanged, so 2.0.0 and 1.x peers still connect to each other.

    Breaking changes

    • Building needs Clang 18 or newer (Apple Clang 16 on macOS): build.rs compiles the vendored C and refuses GCC, MSVC’s cl and fast-math flags rather than fall back. The crate declares links = "dream_net_native".
    • The minimum Rust version is 1.95.
    • Error::Netcode and generate_connect_token now carry NetcodeError, dream-net’s own type with the same variants and texts as netcode-official’s Error plus InvalidConnectTokenLifetime. Code that named or matched the old type must use NetcodeError.
    • TransportConfig::reliable_config returns ReliableConfig and Connection::reliable_counters returns &ReliableCounters, dream-net’s own types, in place of the reliable crate’s Config and Counters.
    • TransportConfig::validate refuses sent_packets_buffer_size and received_packets_buffer_size above 16384, the C reliable’s limit; 1.1.1 allowed 32768.
    • Connect tokens are single use: a client refuses a token already passed to connect in the same process, and a server refuses one that already established a session there, even after that session ended.
    • A server refuses tokens that expire within its maximum token lifetime (30 seconds by default) of its start, so a token minted before a restart cannot open a second session; a 30-second token minted in the server’s start second is refused too.

    Added

    • NetcodeError, ReliableConfig and ReliableCounters.
    • Server::with_max_token_lifetime and DEFAULT_MAX_CONNECT_TOKEN_LIFETIME, to set the token lifetime the start floor is built on.

    Changed

    • netcode-official, reliable and serialize-official are gone from the dependency graph. The vendored C (netcode v1.4.10 with three local patches, reliable v1.4.6, and netcode’s libsodium subset) is pinned file by file and checked against upstream. The wire format is written by dream-net’s own safe Rust and is byte-for-byte unchanged.
    • The license expression is (MIT OR Apache-2.0) AND BSD-3-Clause AND ISC: dream-net’s own code is unchanged, and the vendored C carries its own terms. NOTICE.md reproduces every notice.
    • MemoryUsage::reliable is now an upper bound on reliable’s endpoint, about 581 KB with the default TransportConfig, almost all of it fragment reassembly an idle connection does not hold.
    • A server holds its port exclusively on every platform: it fails to start when any other socket holds the port, and nothing can bind the port on one of its addresses afterwards. Windows allowed both before.
    • A client that finds a schema or protocol mismatch reports ConnectFailed at once, but keeps its session for mismatch_linger seconds, sending only its hello, so the server can report the mismatch too.

    Fixed

    • A server could report nothing for a client with a mismatched schema, when the client’s hello and its disconnect arrived in the same update. 1.1.1 has the same gap.
    • On Windows a server could start on a port another program held on one address, and lose the traffic for that address to it without an error.
    • On Windows the cause of a socket failure, such as a port in use, was lost before it reached Error::Netcode.
    dream-net 2.0.0 488.06 kB · crates.io
    View on crates.io
    Verify · sha256 cf275903fc5b…2d5833a5
    File
    dream-net-2.0.0.crate
    Size
    499772 bytes
    SHA-256
    cf275903fc5b767d2e89c6d3e3a0a19215f3514908649315f6405bc22d5833a5

    The same digest as the crates.io index's checksum, which Cargo checks on every download.

  3. 1.1.1 stable 2026-11-05

    Requires a ctutils with RUSTSEC-2026-0385 fixed, for the constant-time comparison behind netcode’s packet authentication.

    Fixed

    • ctutils before 0.4.4 could generate branches on secret values in its constant-time masks and comparisons (RUSTSEC-2026-0385). netcode’s Poly1305 tag verification goes through it, so dream-net now requires ctutils 0.4.4 or newer, which also moves projects whose lockfiles still held an older version.
    dream-net 1.1.1 101.13 kB · crates.io
    View on crates.io
    Verify · sha256 4224182c0284…763bd291
    File
    dream-net-1.1.1.crate
    Size
    103559 bytes
    SHA-256
    4224182c0284ac157dcb61f99c791d8759341f600f2f56863a62397b763bd291

    The same digest as the crates.io index's checksum, which Cargo checks on every download.

Credits