pub struct Client<GameT, StepT>where
GameT: GameCallbacks<StepT> + Debug,
StepT: Clone + Deserialize + Serialize + Debug + Display,{
pub prediction_time_tick: TimeTick,
/* private fields */
}
Expand description
The main client structure handling datagram communication, participant management, and input (step) prediction.
The Client
does not handle game logic directly but relies on external game logic
provided through the GameCallbacks
trait.
Fields§
§prediction_time_tick: TimeTick
Implementations§
Source§impl<StepT, GameT> Client<GameT, StepT>where
StepT: Clone + Deserialize + Serialize + Debug + Display + Eq,
GameT: GameCallbacks<StepT> + Debug,
impl<StepT, GameT> Client<GameT, StepT>where
StepT: Clone + Deserialize + Serialize + Debug + Display + Eq,
GameT: GameCallbacks<StepT> + Debug,
Sourcepub fn new(now: Millis) -> Client<GameT, StepT>
pub fn new(now: Millis) -> Client<GameT, StepT>
Creates a new Client
instance with the given current time.
§Arguments
now
- The current time in milliseconds.
pub const fn with_tick_duration( self, tick_duration: MillisDuration, ) -> Client<GameT, StepT>
Sourcepub fn send(&mut self, now: Millis) -> Result<Vec<Vec<u8>>, ClientError>
pub fn send(&mut self, now: Millis) -> Result<Vec<Vec<u8>>, ClientError>
Creates outgoing messages and returns the serialized datagrams.
This method collects messages prepared by the client logic, serializes them into datagrams, updates network metrics, and returns the datagrams. They are usually sent over some datagram transport.
§Arguments
now
- The current time in milliseconds.
§Returns
A Result
containing a vector of serialized datagrams or a ClientError
.
§Errors
Returns ClientError
if serialization or sending fails.
Sourcepub fn receive(
&mut self,
now: Millis,
datagram: &[u8],
) -> Result<(), ClientError>
pub fn receive( &mut self, now: Millis, datagram: &[u8], ) -> Result<(), ClientError>
Receives and processes an incoming datagram.
This method handles incoming datagrams by updating metrics, deserializing the datagram, and passing the contained commands to the client logic for further processing.
§Arguments
millis
- The current time in milliseconds.datagram
- The received datagram bytes.
§Returns
A Result
indicating success or containing a ClientError
.
§Errors
Returns ClientError
if deserialization or processing fails.
pub const fn debug_rectify( &self, ) -> &Rectify<GameT, SeqMap<ParticipantId, Step<StepT>>>
Sourcepub fn update(&mut self, now: Millis) -> Result<(), ClientError>
pub fn update(&mut self, now: Millis) -> Result<(), ClientError>
Updates the client’s phase and handles synchronization tasks based on the current time.
This includes updating the network layer, metrics, tick durations, processing authoritative steps, and managing prediction phases.
§Arguments
now
- The current time in milliseconds.
§Returns
A Result
indicating success or containing a ClientError
.
§Errors
Returns ClientError
if any internal operations fail.
Sourcepub const fn game(&self) -> Option<&GameT>
pub const fn game(&self) -> Option<&GameT>
Retrieves a reference to the current game instance, if available.
Note: The Client
does not manage game logic directly. This method provides access to the
game state managed externally via callbacks.
§Returns
An Option
containing a reference to CallbacksT
or None
if no game is active.
Sourcepub fn required_prediction_count(&self) -> usize
pub fn required_prediction_count(&self) -> usize
Determines the number of predictions needed based on the current state.
§Returns
The number of predictions needed as a usize
.
Sourcepub const fn can_join_player(&self) -> bool
pub const fn can_join_player(&self) -> bool
Checks if a new player can join the game session.
§Returns
true
if a player can join, false
otherwise.
Sourcepub fn local_players(&self) -> Vec<LocalPlayer>
pub fn local_players(&self) -> Vec<LocalPlayer>
Retrieves a list of local players currently managed by the client.
§Returns
A vector of LocalPlayer
instances.
Sourcepub fn push_predicted_step(
&mut self,
tick_id: TickId,
step: &SeqMap<ParticipantId, StepT>,
) -> Result<(), ClientError>
pub fn push_predicted_step( &mut self, tick_id: TickId, step: &SeqMap<ParticipantId, StepT>, ) -> Result<(), ClientError>
Adds a predicted input (step) to the client’s logic and rectification system.
This method serializes predicted steps into datagrams (in the future) in upcoming send()
function calls.
§Arguments
tick_id
- The tick identifier for the predicted step.step
- The predicted step data.
§Returns
A Result
indicating success or containing a ClientError
.
§Errors
Returns ClientError
if the prediction queue is full or if processing fails.
Sourcepub fn latency(&self) -> Option<MinMaxAvg<u16>>
pub fn latency(&self) -> Option<MinMaxAvg<u16>>
Retrieves the current transmission round trip latency metrics.
§Returns
An Option
containing MinMaxAvg<u16>
representing latency metrics, or None
if unavailable.
Sourcepub fn metrics(&self) -> CombinedMetrics
pub fn metrics(&self) -> CombinedMetrics
Retrieves the combined network metrics.
§Returns
A CombinedMetrics
instance containing various network metrics.
Sourcepub fn server_buffer_delta_ticks(&self) -> Option<i16>
pub fn server_buffer_delta_ticks(&self) -> Option<i16>
Retrieves the delta ticks on the host for the incoming predicted steps A negative means that the incoming buffer is too low, a larger positive number means that the buffer is too big, and the prediction should slow down.
§Returns
An Option
containing the delta ticks as i16
, or None
if unavailable.
Sourcepub fn request_join_player(
&mut self,
local_players: Vec<u8>,
) -> Result<(), ClientError>
pub fn request_join_player( &mut self, local_players: Vec<u8>, ) -> Result<(), ClientError>
Requests to join a new player with the specified local indices.
This method sends a request to the host to add new participants to the game session.
§Arguments
local_players
- A vector ofLocalIndex
representing the local players to join.
§Returns
A Result
indicating success or containing a ClientError
.
§Errors
ClientError
// TODO: