Skip to main content

P2PSession

Struct P2PSession 

Source
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>

Source

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 InvalidRequest when the given handle does not refer to a local player.
Source

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 InvalidRequest if a local input is missing for any registered local player.
  • Returns NotSynchronized if the session is not yet synchronized with remote peers.
  • Returns PredictionThreshold (rollback mode only) if the remote peer is too far behind.
Source

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.

Source

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.

Source

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.

Source

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 InvalidRequest if you try to disconnect a local player or the provided handle is invalid.
Source

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 InvalidRequest if the handle does not refer to a local player.
Source

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 InvalidRequest if the handle is not referring to a remote player or spectator.
  • Returns NotSynchronized if the endpoint has not yet started connecting.
  • Returns NotEnoughData if less than one second has elapsed since the connection was established. The session may already be Running; retry after a short delay.
Source

pub fn confirmed_frame(&self) -> Frame

Returns the highest confirmed frame. We have received all input for this frame and it is thus correct.

Source

pub fn current_frame(&self) -> Frame

Returns the current frame of a session.

Source

pub fn max_prediction(&self) -> usize

Returns the maximum prediction window of a session.

Source

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.

Source

pub fn current_state(&self) -> SessionState

Returns the current SessionState of a session.

Source

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.

Source

pub fn num_players(&self) -> usize

Returns the number of players added to this session

Source

pub fn num_spectators(&self) -> usize

Return the number of spectators currently registered

Source

pub fn local_player_handles(&self) -> Vec<PlayerHandle>

Returns the handles of local players that have been added

Source

pub fn remote_player_handles(&self) -> Vec<PlayerHandle>

Returns the handles of remote players that have been added

Source

pub fn spectator_handles(&self) -> Vec<PlayerHandle>

Returns the handles of spectators that have been added

Source

pub fn handles_by_address(&self, addr: T::Address) -> Vec<PlayerHandle>

Returns all handles associated to a certain address

Source

pub fn frames_ahead(&self) -> i32

Returns the number of frames this session is estimated to be ahead of other sessions

Source

pub fn desync_detection(&self) -> DesyncDetection

Returns the DesyncDetection mode set for this session at creation time.

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for P2PSession<T>

§

impl<T> !Send for P2PSession<T>

§

impl<T> !Sync for P2PSession<T>

§

impl<T> !UnwindSafe for P2PSession<T>

§

impl<T> Freeze for P2PSession<T>

§

impl<T> Unpin for P2PSession<T>
where <T as Config>::Address: Unpin, <T as Config>::Input: Unpin,

§

impl<T> UnsafeUnpin for P2PSession<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more