Skip to main content

Module link

Module link 

Source
Expand description

Link-play transport seam — game-agnostic and zero-I/O.

Any JRPG on this engine can do link play (battle, trade, … between two players) by plugging a NetworkTransport implementation into its link state machines. The engine defines the transport interface; each game defines its own wire protocol — the message type M (e.g. a serde enum serialized as JSON lines over TCP).

This module is deliberately zero-I/O: it defines the trait, the shared TransportError, the LinkRole connection-side identity, and an in-memory ChannelTransport pair for local/testing use. Real transports (TCP sockets, Web BroadcastChannel, …) live in the game or platform layer, never here.

§What lives here vs. in the game

  • Engine (this module): the NetworkTransport<M> trait, TransportError, ChannelTransport<M>, and LinkRole (which side clocks/hosts the connection — needed by any asymmetric handshake).
  • Game: the wire message type M, the link state machines (battles, trades, …), and the concrete transports that implement this trait.

§Why NetworkTransport<M> (a type parameter, not an associated type)

The message type is a plain generic parameter so implementations stay one line (impl<M> NetworkTransport<M> for ChannelTransport<M>), trait objects read as dyn NetworkTransport<MyMessage>, and the trait is usable with several protocols if a transport ever needs that. An associated type (type Message) would be equally valid — each transport would then own exactly one message type — but the parameter was chosen for the least ceremony across implementors.

§Usage

// Game side: the wire protocol (a serde enum, a byte protocol, …).
enum MyMessage { Hello, Bye }

// In-memory pair for local play / tests.
let (mut t_a, mut t_b) = ChannelTransport::<MyMessage>::new_pair();
t_a.send(MyMessage::Hello).unwrap();
assert_eq!(t_b.recv().unwrap(), MyMessage::Hello);

Structs§

ChannelTransport
An in-memory NetworkTransport pair over std::sync::mpsc channels.

Enums§

LinkRole
Which side of a link connection the local player is.
TransportError
Errors a NetworkTransport can report.

Traits§

NetworkTransport
A bidirectional message transport for link play.