pub struct P2PSession<T>(/* private fields */)
where
T: Config;Expand description
The main peer-to-peer Backroll session.
This type internally wraps an Arc<RwLock<…>>, so it is safe to send and access across threads, and is cheap to clone.
Implementations§
Source§impl<T: Config> P2PSession<T>
impl<T: Config> P2PSession<T>
pub fn build() -> P2PSessionBuilder<T>
Sourcepub fn player_count(&self) -> usize
pub fn player_count(&self) -> usize
Gets the number of players in the current session. This includes users that are already disconnected.
Sourcepub fn in_rollback(&self) -> bool
pub fn in_rollback(&self) -> bool
Checks if the session currently in the middle of a rollback.
Sourcepub fn current_frame(&self) -> i32
pub fn current_frame(&self) -> i32
Gets the current frame of the game.
pub fn local_players(&self) -> SmallVec<[PlayerHandle; 8]>
pub fn remote_players(&self) -> SmallVec<[PlayerHandle; 8]>
Sourcepub fn is_synchronized(&self) -> bool
pub fn is_synchronized(&self) -> bool
Checks if all remote players are synchronized. If all players are local, this will always return true.
Sourcepub fn add_local_input(
&self,
player: PlayerHandle,
input: T::Input,
) -> BackrollResult<()>
pub fn add_local_input( &self, player: PlayerHandle, input: T::Input, ) -> BackrollResult<()>
Adds a local input for the current frame. This will register the input in the local input queues, as well as queue the input to be sent to all remote players. If called multiple times for the same player without advancing the session with advance_frame, the previously queued input for the frame will be overwritten.
For a corrrect simulation, this must be called on all local players every frame before calling advance_frame.
§Errors
Returns BackrollError::InRollback if the session is currently in the middle of a rollback.
Returns BackrollError::NotSynchronized if the all of the remote peers have not yet synchornized.
Returns BackrollError::InvalidPlayer if the provided player handle does not point a vali player.
§Panics
This function will panic if the player is not a local player.
Sourcepub fn advance_frame(&self) -> Commands<T>
pub fn advance_frame(&self) -> Commands<T>
Advances the game simulation by a single frame. This will issue a Command::AdvanceFrame then check if the simulation is consistent with the inputs sent by remote players. If not, a rollback will be triggered, and the game will be resimulated from the point of rollback.
For a corrrect simulation, add_local_input must be called on all local players every frame before calling this. If any call to add_local_input fails, this should not be called.
All of the provided commands must be executed in order, and must not be reordered or skipped.
Sourcepub fn poll(&self) -> Commands<T>
pub fn poll(&self) -> Commands<T>
Flushes lower level network events. This should always be called before adding local inputs every frame of the game regardless of if the game is advancing it’s state or not.
All of the provided commands must be executed in order, and must not be reordered or skipped.
Sourcepub fn disconnect_player(
&self,
player: PlayerHandle,
) -> BackrollResult<Commands<T>>
pub fn disconnect_player( &self, player: PlayerHandle, ) -> BackrollResult<Commands<T>>
Disconnects a player from the game.
If called on a local player, this will disconnect the client from all remote peers.
If called on a remote player, this will disconnect the connection with only that player.
§Errors
Returns BackrollError::InvalidPlayer if the provided player handle does not point a vali player.
Returns BackrollError::PlayerDisconnected if the provided player is already disconnected.
Sourcepub fn get_network_stats(
&self,
player: PlayerHandle,
) -> BackrollResult<NetworkStats>
pub fn get_network_stats( &self, player: PlayerHandle, ) -> BackrollResult<NetworkStats>
Gets network statistics with a remote player.
§Errors
Returns BackrollError::InvalidPlayer if the provided player handle does not point a vali player.
Sourcepub fn set_frame_delay(
&self,
player: PlayerHandle,
delay: i32,
) -> BackrollResult<()>
pub fn set_frame_delay( &self, player: PlayerHandle, delay: i32, ) -> BackrollResult<()>
Sets the frame delay for a given player.
§Errors
Returns BackrollError::InvalidPlayer if the provided player handle does not point a vali player.