unbanished_net/lib.rs
1//! Un-Banished multiplayer networking: the protocol between a game and the relay, and a WebSocket
2//! connection to the relay that works natively and in the browser.
3//!
4//! The relay (`apps/server`) keeps rooms. A game says [`Hello`] (the protocol version, its engine,
5//! fingerprints of its game data and mods), then hosts a room and gets its code, or joins one
6//! with the code. The relay numbers the players in the order they come (the host is 0), refuses
7//! a game that differs from the host with the reason, and passes their data on, to one player or
8//! to all the others. It never reads the data: the game's own messages (its lockstep turns,
9//! digests and saves) travel inside [`ToRelay::Send`] and [`FromRelay::Data`]. Every WebSocket
10//! message is one value, encoded with postcard. The rules are in [`rooms`]; [`memory`] runs a
11//! relay in one process for tests.
12//!
13//! Not in the original game, which has no multiplayer; see docs/MULTIPLAYER-PLAN.md, step 5.
14#![doc(
15 html_logo_url = "https://tanepiper.github.io/unbanished/assets/un-banished-icon.svg",
16 html_favicon_url = "https://tanepiper.github.io/unbanished/favicon.svg"
17)]
18
19pub mod checks;
20pub mod memory;
21mod protocol;
22pub mod rooms;
23pub mod store;
24
25pub use protocol::{decode, encode, FromRelay, Hello, RoomInfo, ToRelay, PROTOCOL};
26
27/// The state of a connection.
28#[derive(Debug, Clone, PartialEq)]
29pub enum State {
30 /// The connection is being made.
31 Connecting,
32 /// The relay has answered.
33 Open,
34 /// The connection is closed, for this reason.
35 Closed(String),
36}
37
38#[cfg(feature = "native")]
39mod native;
40#[cfg(feature = "native")]
41pub use native::Connection;
42
43#[cfg(target_arch = "wasm32")]
44mod web;
45#[cfg(target_arch = "wasm32")]
46pub use web::Connection;
47
48#[cfg(test)]
49mod tests;