lightyear_connection 0.29.0

Connection handling for the lightyear networking library
Documentation
/*! # Lightyear Connection
Connection handling for the lightyear networking library.
This crate provides core types for managing long-term connections on top of a Link and Transport.

It also introduces helpers to setup a Client-Server architecture.

This crates provide concepts that are only useful for a client-server architecture (client/server).
*/
#![no_std]
// TODO: maybe lightyear_connection only stores primitives for a long-running Connection (ConnectionError, etc.)
//  on top of a Link
//  And client-server logic is only in lightyear_client, lightyear_server, lightyear_shared
//  OR:
//   for example the direction stuff should be lightyear_client + lightyear_server + lightyear_shared?
//  --
//   Fundamentally is it easier to find direction logic in lightyear_client/direction + lightyear_server/direction
//   or in lightyear_direction/client + lightyear_direction/server?
//   Maybe each crate can have #[client] and #[server] features for client-server specific stuff
//   And then lightyear_client just calls the relevant functions from all the other crates (inputs, etc.)

extern crate alloc;
extern crate core;

use bevy_app::{App, Plugin};
use bevy_ecs::schedule::SystemSet;

pub mod client;

pub mod server;

pub mod direction;
pub mod network_target;

pub mod client_of;
#[allow(unused)]
pub mod identity;
pub mod shared;

pub mod host;
pub mod network_topology;
pub mod p2p;

#[deprecated(note = "Use ConnectionSystems instead")]
pub type ConnectionSet = ConnectionSystems;

/// System sets for connection-related logic.
/// These are used to order systems that handle receiving and sending packets.
#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone, Copy)]
pub enum ConnectionSystems {
    // PRE UPDATE
    /// Receive bytes from the Link, process them as Packets and buffer them into the Transport
    Receive,

    // PostUpdate
    /// Flush the messages that were buffered into the Transport, process them as Packets and
    /// buffer them to the Link
    Send,
}

pub mod prelude {
    pub use crate::ConnectionSystems;
    pub use crate::direction::NetworkDirection;
    pub use crate::identity::{is_client, is_p2p};
    pub use crate::network_target::NetworkTarget;
    pub use crate::network_topology::{
        NetworkTopology, NetworkTopologyError, NetworkTopologySystems, NetworkingMetadata,
    };
    // we also export these types at the top level for easier access
    pub use crate::client::{
        Client, ClientState, Connect, Connected, Connecting, ConnectionError, Disconnect,
        Disconnected, DisconnectedReason, PeerMetadata,
    };
    pub use crate::p2p::P2P;

    #[cfg(feature = "client")]
    pub mod client {
        pub use crate::client::{
            Client, ClientState, Connect, Connected, Connecting, ConnectionError, Disconnect,
            Disconnected, DisconnectedReason,
        };
    }

    #[cfg(feature = "server")]
    pub mod server {
        pub use crate::client_of::ClientOf;
        pub use crate::identity::{is_headless_server, is_host_server, is_server};
        pub use crate::server::{ConnectionError, Start, Started, Starting, Stop, Stopped};
    }
}

/// Plugin to handle the connection logic.
/// Registers relevant types.
pub struct ConnectionPlugin;

impl Plugin for ConnectionPlugin {
    fn build(&self, app: &mut App) {
        app.add_plugins(network_topology::NetworkTopologyPlugin);
    }
}