pub struct P2PSession<T>where
T: Config,{ /* private fields */ }Expand description
A P2PSession provides all functionality to connect to remote clients in a peer-to-peer fashion, exchange inputs and handle the gamestate by saving, loading and advancing.
Implementations§
Source§impl<T: Config> P2PSession<T>
impl<T: Config> P2PSession<T>
Sourcepub fn add_local_input(
&mut self,
player_handle: PlayerHandle,
input: T::Input,
) -> Result<(), GgrsError>
pub fn add_local_input( &mut self, player_handle: PlayerHandle, input: T::Input, ) -> Result<(), GgrsError>
Registers local input for a player for the current frame. This should be successfully called for every local player before calling advance_frame().
If this is called multiple times for the same player before advancing the frame, older given inputs will be overwritten.
§Errors
- Returns
InvalidRequestwhen the given handle does not refer to a local player.
Sourcepub fn advance_frame(&mut self) -> Result<Vec<GgrsRequest<T>>, GgrsError>
pub fn advance_frame(&mut self) -> Result<Vec<GgrsRequest<T>>, GgrsError>
Advance the session by one frame. Returns an order-sensitive Vec<GgrsRequest> that
must be fulfilled in exact order.
Internally calls poll_remote_clients before doing anything else, so you do not need to
call it separately on the same tick.
In lockstep mode (max_prediction = 0), the returned vec may contain no
AdvanceFrame request if remote inputs have not yet arrived — the session stalls without
consuming a game frame. Call poll_remote_clients and retry on the next tick, or use
advance_frame_with_wait to poll for a bounded amount of time before returning. Unlike
rollback mode this is not an error; it is the normal wait behaviour.
§Errors
- Returns
InvalidRequestif a local input is missing for any registered local player. - Returns
NotSynchronizedif the session is not yet synchronized with remote peers. - Returns
PredictionThreshold(rollback mode only) if the remote peer is too far behind.
Sourcepub fn advance_frame_with_wait(
&mut self,
) -> Result<Vec<GgrsRequest<T>>, GgrsError>
pub fn advance_frame_with_wait( &mut self, ) -> Result<Vec<GgrsRequest<T>>, GgrsError>
Advance the session by one frame, waiting briefly for lockstep confirmation before returning an empty request list.
This is only different from advance_frame in lockstep mode (max_prediction = 0).
If the first advance attempt stalls because the current game frame is not confirmed yet,
GGRS keeps polling the non-blocking socket until either the frame becomes confirmed or one
frame duration (derived from the session’s configured FPS) has elapsed.
In rollback mode this behaves exactly like advance_frame.
Sourcepub fn advance_frame_with_wait_timeout(
&mut self,
timeout: Duration,
) -> Result<Vec<GgrsRequest<T>>, GgrsError>
pub fn advance_frame_with_wait_timeout( &mut self, timeout: Duration, ) -> Result<Vec<GgrsRequest<T>>, GgrsError>
Advance the session by one frame, waiting up to timeout for lockstep confirmation.
This helper makes the lockstep wait explicit. It may spin-poll for up to timeout, so use
it only when a short bounded wait is appropriate for your platform and game loop. Passing a
zero timeout is equivalent to advance_frame.
Sourcepub fn poll_remote_clients(&mut self)
pub fn poll_remote_clients(&mut self)
Should be called periodically by your application to give GGRS a chance to do internal work. GGRS will receive packets, distribute them to corresponding endpoints, handle all occurring events and send all outgoing packets.
Sourcepub fn disconnect_player(
&mut self,
player_handle: PlayerHandle,
) -> Result<(), GgrsError>
pub fn disconnect_player( &mut self, player_handle: PlayerHandle, ) -> Result<(), GgrsError>
Disconnects a remote player and all other remote players with the same address from the session.
§Errors
- Returns
InvalidRequestif you try to disconnect a local player or the provided handle is invalid.
Sourcepub fn set_input_delay(
&mut self,
player_handle: PlayerHandle,
delay: usize,
) -> Result<(), GgrsError>
pub fn set_input_delay( &mut self, player_handle: PlayerHandle, delay: usize, ) -> Result<(), GgrsError>
Changes the input delay for a local player. This can be called at any point during a session.
When decreasing delay, inputs that fall inside the now-removed frames are dropped. When increasing delay, the last known input is replicated to fill the created gap.
§Errors
- Returns
InvalidRequestif the handle does not refer to a local player.
Sourcepub fn network_stats(
&self,
player_handle: PlayerHandle,
) -> Result<NetworkStats, GgrsError>
pub fn network_stats( &self, player_handle: PlayerHandle, ) -> Result<NetworkStats, GgrsError>
Returns a NetworkStats struct that gives information about the quality of the network connection.
§Errors
- Returns
InvalidRequestif the handle is not referring to a remote player or spectator. - Returns
NotSynchronizedif the endpoint has not yet started connecting. - Returns
NotEnoughDataif less than one second has elapsed since the connection was established. The session may already beRunning; retry after a short delay.
Sourcepub fn confirmed_frame(&self) -> Frame
pub fn confirmed_frame(&self) -> Frame
Returns the highest confirmed frame. We have received all input for this frame and it is thus correct.
Sourcepub fn current_frame(&self) -> Frame
pub fn current_frame(&self) -> Frame
Returns the current frame of a session.
Sourcepub fn max_prediction(&self) -> usize
pub fn max_prediction(&self) -> usize
Returns the maximum prediction window of a session.
Sourcepub fn in_lockstep_mode(&self) -> bool
pub fn in_lockstep_mode(&self) -> bool
Returns true if the session is running in lockstep mode.
In lockstep mode, a session will only advance if the current frame has inputs confirmed from all other players.
Sourcepub fn current_state(&self) -> SessionState
pub fn current_state(&self) -> SessionState
Returns the current SessionState of a session.
Sourcepub fn events(&mut self) -> Drain<'_, GgrsEvent<T>>
pub fn events(&mut self) -> Drain<'_, GgrsEvent<T>>
Returns all events that happened since last queried for events. If the number of stored events exceeds MAX_EVENT_QUEUE_SIZE, the oldest events will be discarded.
Sourcepub fn num_players(&self) -> usize
pub fn num_players(&self) -> usize
Returns the number of players added to this session
Sourcepub fn num_spectators(&self) -> usize
pub fn num_spectators(&self) -> usize
Return the number of spectators currently registered
Sourcepub fn local_player_handles(&self) -> Vec<PlayerHandle> ⓘ
pub fn local_player_handles(&self) -> Vec<PlayerHandle> ⓘ
Returns the handles of local players that have been added
Sourcepub fn remote_player_handles(&self) -> Vec<PlayerHandle> ⓘ
pub fn remote_player_handles(&self) -> Vec<PlayerHandle> ⓘ
Returns the handles of remote players that have been added
Sourcepub fn spectator_handles(&self) -> Vec<PlayerHandle> ⓘ
pub fn spectator_handles(&self) -> Vec<PlayerHandle> ⓘ
Returns the handles of spectators that have been added
Sourcepub fn handles_by_address(&self, addr: T::Address) -> Vec<PlayerHandle> ⓘ
pub fn handles_by_address(&self, addr: T::Address) -> Vec<PlayerHandle> ⓘ
Returns all handles associated to a certain address
Sourcepub fn frames_ahead(&self) -> i32
pub fn frames_ahead(&self) -> i32
Returns the number of frames this session is estimated to be ahead of other sessions
Sourcepub fn desync_detection(&self) -> DesyncDetection
pub fn desync_detection(&self) -> DesyncDetection
Returns the DesyncDetection mode set for this session at creation time.