R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
rtype::ClientSession Class Reference

The client side of a session (RFC section 3). More...

#include </__w/project-rtype/project-rtype/client/network/include/rtype/client_session.hpp>

Public Types

using Clock = std::chrono::steady_clock
 Clock of the timers.
 

Public Member Functions

 ClientSession (aegis2d::net::NetworkContext &context, ClientSettings settings)
 Creates a session, not started.
 
 ClientSession (const ClientSession &)=delete
 
ClientSession & operator= (const ClientSession &)=delete
 
 ClientSession (ClientSession &&)=delete
 
ClientSession & operator= (ClientSession &&)=delete
 
 ~ClientSession ()
 Closes the sockets, without Logout: call leave() first to say goodbye.
 
void start ()
 Connects to the server and logs in.
 
void update (Clock::time_point now)
 Runs the timers: Connect resent, the login and binding timeouts, and, in game, one Input every input_interval.
 
void set_input (aegis2d::ecs::PlayerInput input) noexcept
 Sets the actions the player holds, sent in every Input until the next call.
 
void create_room (const CreateRoomRequest &request)
 Asks the server for a new room, in the lobby (CreateRoom, RFC section 3.6).
 
void join_room (std::uint8_t room)
 Asks to join a room, in the lobby (JoinRoom, RFC section 3.7).
 
void set_ready (bool ready)
 Says whether the player is ready to start, in a room (SetReady, RFC 3.7).
 
void leave_room ()
 Leaves the room for the lobby, where the rooms are listed again (LeaveRoom).
 
bool send_chat (std::string_view text)
 Sends a chat message to the players of the room, or of the lobby (ChatSend, RFC section 3.8).
 
ChatLog & chat () noexcept
 The last chat messages of the room, and why a message of this client was not sent, each for a few seconds.
 
const std::vector< ScoreRecord > & scores () const noexcept
 The best scores of the server, as the lobby shows them (RFC section 3.9).
 
bool ready () const noexcept
 Whether the player said it is ready, in its room.
 
const RoomPlayers & room_players () const noexcept
 The players of the room joined, from the server's last RoomPlayers.
 
const std::vector< RoomRecord > & rooms () const noexcept
 The rooms of the server, from its last RoomList.
 
const std::string & room_error () const noexcept
 Why the last room could not be created or joined.
 
void leave ()
 Leaves the server: sends Logout and closes once it has left.
 
ClientState state () const noexcept
 Where the session is.
 
const std::string & error () const noexcept
 Why the session failed or was lost.
 
const std::optional< LoginAccepted > & login () const noexcept
 What the server gave at login.
 
const std::optional< aegis2d::net::Endpoint > & server_udp () const noexcept
 The server's UDP endpoint, once logged in.
 
std::uint64_t connects_sent () const noexcept
 Number of Connect datagrams sent.
 
ClientWorld & world () noexcept
 The world as the server describes it, once in game.
 
const ClientWorld & world () const noexcept
 The world as the server describes it, once in game.
 
const SnapshotDecoder & snapshots () const noexcept
 The snapshots rebuilt from the Snapshot datagrams, as baselines of the next.
 
const ShipPrediction & prediction () const noexcept
 The client's own ship, moved at once by each Input sent.
 
std::uint64_t inputs_sent () const noexcept
 Number of Input datagrams sent.
 
std::optional< std::uint16_t > last_input () const noexcept
 Sequence of the last Input sent (RFC section 4.2).
 
std::optional< std::uint16_t > acknowledged_input () const noexcept
 Sequence of the last Input of the player the server applied, from the player record of its own player in the snapshots (RFC section 4.3).
 
const aegis2d::net::TrafficCounters & traffic () const noexcept
 Datagrams exchanged with the server, by direction and packet type.
 
const aegis2d::net::RttEstimator & rtt () const noexcept
 Round trip time to the server, with its jitter.
 
const NetworkStats & network_stats () const noexcept
 Loss, snapshot interval and jitter of the server's datagrams.
 

Static Public Attributes

static constexpr std::chrono::milliseconds connect_interval {250}
 Time between two Connect datagrams (RFC section 3.4).
 
static constexpr std::chrono::seconds bind_timeout {5}
 Longest wait for Accept after LoginAccepted (RFC section 3.4).
 
static constexpr std::chrono::seconds login_timeout {5}
 Longest wait for the TCP connection and LoginAccepted.
 
static constexpr std::chrono::seconds server_timeout {5}
 Longest silence of the server in game before the session is lost (RFC 5.5).
 
static constexpr std::chrono::seconds room_refresh {1}
 Time between two ListRooms in the lobby.
 
static constexpr aegis2d::core::Duration input_interval
 Time between two Input datagrams: one per server tick (RFC section 4.2).
 

Detailed Description

The client side of a session (RFC section 3).

start() opens the TCP connection and sends LoginRequest. On LoginAccepted, the client opens a UDP socket and sends Connect to the server's udp_port every connect_interval until Accept arrives: the session is then InGame. With ClientSettings::choose_room, it stays in the Lobby state instead, where rooms() is refreshed every room_refresh, and create_room() and join_room() send their requests. A RoomJoined puts it InRoom, with the player's number in that room: room_players() follows the room, set_ready() and leave_room() send their requests, and GameStart, once every player of the room is ready, starts the binding. It fails, with a reason in error(), when the server cannot be reached, refuses the login, sends anything unexpected, closes the connection, or does not answer: no LoginAccepted within login_timeout, or no Accept within bind_timeout of LoginAccepted (then Logout is sent, as the UDP port is likely blocked).

From the server's UDP traffic, only datagrams from its udp_port with the session's token are read (section 3.4); before the first Accept, only Accept.

Handlers run on the thread running the context; call update() a few times per second for the timers. Create, use and destroy it on that thread, or while the context is not running.

Member Typedef Documentation

◆ Clock

using rtype::ClientSession::Clock = std::chrono::steady_clock

Clock of the timers.

Constructor & Destructor Documentation

◆ ClientSession() [1/3]

rtype::ClientSession::ClientSession ( aegis2d::net::NetworkContext &  context,
ClientSettings  settings 
)

Creates a session, not started.

Parameters
contextContext that runs the sockets; it must outlive this object.
settingsServer and player name.

◆ ClientSession() [2/3]

rtype::ClientSession::ClientSession ( const ClientSession &  )
delete

◆ ClientSession() [3/3]

rtype::ClientSession::ClientSession ( ClientSession &&  )
delete

◆ ~ClientSession()

rtype::ClientSession::~ClientSession ( )

Closes the sockets, without Logout: call leave() first to say goodbye.

Member Function Documentation

◆ acknowledged_input()

std::optional< std::uint16_t > rtype::ClientSession::acknowledged_input ( ) const
noexcept

Sequence of the last Input of the player the server applied, from the player record of its own player in the snapshots (RFC section 4.3).

The inputs after it, up to last_input(), are those the server has not applied yet. It only moves forward: a late snapshot does not move it back.

Returns
The sequence, or nothing until a snapshot says the server applied one.

◆ chat()

ChatLog & rtype::ClientSession::chat ( )
noexcept

The last chat messages of the room, and why a message of this client was not sent, each for a few seconds.

Returns
The log; ChatLog::prune() it before drawing.

◆ connects_sent()

std::uint64_t rtype::ClientSession::connects_sent ( ) const
noexcept

Number of Connect datagrams sent.

Returns
The count since start().

◆ create_room()

void rtype::ClientSession::create_room ( const CreateRoomRequest &  request)

Asks the server for a new room, in the lobby (CreateRoom, RFC section 3.6).

rooms() lists it once created; room_error() says why it was not.

Parameters
requestName and size of the room.

◆ error()

const std::string & rtype::ClientSession::error ( ) const
noexcept

Why the session failed or was lost.

Returns
A sentence for the player, empty unless the state is Failed or Lost.

◆ inputs_sent()

std::uint64_t rtype::ClientSession::inputs_sent ( ) const
noexcept

Number of Input datagrams sent.

Returns
The count since the game state was reached.

◆ join_room()

void rtype::ClientSession::join_room ( std::uint8_t  room)

Asks to join a room, in the lobby (JoinRoom, RFC section 3.7).

On RoomJoined, login() takes the player's number in the room and the session is InRoom; room_error() says why the room was not joined.

Parameters
roomNumber of the room.

◆ last_input()

std::optional< std::uint16_t > rtype::ClientSession::last_input ( ) const
noexcept

Sequence of the last Input sent (RFC section 4.2).

The inputs are numbered from 0, one more for each, from 65535 back to 0: the low 16 bits of inputs_sent() before it is sent.

Returns
The sequence, or nothing before the first Input.

◆ leave()

void rtype::ClientSession::leave ( )

Leaves the server: sends Logout and closes once it has left.

The state becomes Closed at once; keep running the context a little for the Logout to leave.

◆ leave_room()

void rtype::ClientSession::leave_room ( )

Leaves the room for the lobby, where the rooms are listed again (LeaveRoom).

◆ login()

const std::optional< LoginAccepted > & rtype::ClientSession::login ( ) const
noexcept

What the server gave at login.

Returns
Token, player number and UDP port, once logged in.

◆ network_stats()

const NetworkStats & rtype::ClientSession::network_stats ( ) const
noexcept

Loss, snapshot interval and jitter of the server's datagrams.

Records every datagram received with the token of the session, since the login; NetworkStats::summary() gives the figures of the last NetworkStats::window.

Returns
The statistics.

◆ operator=() [1/2]

ClientSession & rtype::ClientSession::operator= ( ClientSession &&  )
delete

◆ operator=() [2/2]

ClientSession & rtype::ClientSession::operator= ( const ClientSession &  )
delete

◆ prediction()

const ShipPrediction & rtype::ClientSession::prediction ( ) const
noexcept

The client's own ship, moved at once by each Input sent.

Started from the position of the ship in the first snapshot that has it, and again when the player loses a life (the server moved it back to its spawn point) or gets another ship; stopped once the player is out. Each newer snapshot reconciles it: the ship goes back to the server's position once it applied the input the player record acknowledges, and the inputs after it are replayed (ShipPrediction::reconcile()). Each update() smooths the corrections (ShipPrediction::smooth()): the screen draws the ship at its drawn() position, so it answers the keys within a frame whatever the round trip time, and never jumps by more than a few pixels for a small correction.

Returns
The prediction.

◆ ready()

bool rtype::ClientSession::ready ( ) const
noexcept

Whether the player said it is ready, in its room.

Returns
The last value given to set_ready() since the room was joined.

◆ room_error()

const std::string & rtype::ClientSession::room_error ( ) const
noexcept

Why the last room could not be created or joined.

Returns
A sentence for the player, empty after a success.

◆ room_players()

const RoomPlayers & rtype::ClientSession::room_players ( ) const
noexcept

The players of the room joined, from the server's last RoomPlayers.

Returns
The room and its players, by number; no player before the first one.

◆ rooms()

const std::vector< RoomRecord > & rtype::ClientSession::rooms ( ) const
noexcept

The rooms of the server, from its last RoomList.

Returns
Them, by number; none before the first answer.

◆ rtt()

const aegis2d::net::RttEstimator & rtype::ClientSession::rtt ( ) const
noexcept

Round trip time to the server, with its jitter.

A sample each time a datagram of the server acknowledges, for the first time, one sent to it (Connect, Input). It includes the time the server waits before its next datagram: in game, for the Input it acknowledges, at most the input interval, as a snapshot acknowledges the last one received. Next to traffic(), the source of the network statistics of the client.

Returns
The estimate, without samples before the server's Accept.

◆ scores()

const std::vector< ScoreRecord > & rtype::ClientSession::scores ( ) const
noexcept

The best scores of the server, as the lobby shows them (RFC section 3.9).

Asked with the rooms, at the login and at each refresh of the lobby; a score whose name breaks the rules of LoginRequest is left out.

Returns
Best first, empty until the first ScoreList.

◆ send_chat()

bool rtype::ClientSession::send_chat ( std::string_view  text)

Sends a chat message to the players of the room, or of the lobby (ChatSend, RFC section 3.8).

Only once logged in, until the session ends; the server's ChatMessage puts it in chat() for every player of the room, this one too, or its ChatRejected the reason it was not sent.

Parameters
textText typed; it must fit a string.
Returns
false if the session is not logged in or has ended: nothing was sent.

◆ server_udp()

const std::optional< aegis2d::net::Endpoint > & rtype::ClientSession::server_udp ( ) const
noexcept

The server's UDP endpoint, once logged in.

Returns
Its address and udp_port.

◆ set_input()

void rtype::ClientSession::set_input ( aegis2d::ecs::PlayerInput  input)
noexcept

Sets the actions the player holds, sent in every Input until the next call.

The client sends what is held, not what changed, so a lost Input is replaced by the next one (RFC section 4.2). Bits 5 to 7 are cleared.

Parameters
inputHeld actions.

◆ set_ready()

void rtype::ClientSession::set_ready ( bool  ready)

Says whether the player is ready to start, in a room (SetReady, RFC 3.7).

Parameters
readyReady, or not any more.

◆ snapshots()

const SnapshotDecoder & rtype::ClientSession::snapshots ( ) const
noexcept

The snapshots rebuilt from the Snapshot datagrams, as baselines of the next.

A datagram whose baseline the client no longer has is dropped and not acknowledged, so the server encodes the next snapshots against another one.

Returns
The decoder.

◆ start()

void rtype::ClientSession::start ( )

Connects to the server and logs in.

Fails at once if the host is not a numeric address or "localhost", or the name breaks the rules of LoginRequest.

◆ state()

ClientState rtype::ClientSession::state ( ) const
noexcept

Where the session is.

Returns
The state.

◆ traffic()

const aegis2d::net::TrafficCounters & rtype::ClientSession::traffic ( ) const
noexcept

Datagrams exchanged with the server, by direction and packet type.

Counts every datagram sent to the server's UDP port, and every one received with the token of the session, since the login: the source of the network statistics of the client.

Returns
The counters.

◆ update()

void rtype::ClientSession::update ( Clock::time_point  now)

Runs the timers: Connect resent, the login and binding timeouts, and, in game, one Input every input_interval.

Inputs keep their rate whatever the calls: one is sent when its time has come, and after a stall the next one is due an interval later, without a burst.

Parameters
nowCurrent time.

◆ world() [1/2]

const ClientWorld & rtype::ClientSession::world ( ) const
noexcept

The world as the server describes it, once in game.

Updated with each Snapshot and Event datagram received (RFC sections 4.3 to 4.5), each snapshot rebuilt from its baseline first: see ClientWorld.

Returns
The world.

◆ world() [2/2]

ClientWorld & rtype::ClientSession::world ( )
noexcept

The world as the server describes it, once in game.

Updated with each Snapshot and Event datagram received (RFC sections 4.3 to 4.5), each snapshot rebuilt from its baseline first: see ClientWorld.

Returns
The world.

Member Data Documentation

◆ bind_timeout

constexpr std::chrono::seconds rtype::ClientSession::bind_timeout {5}
staticconstexpr

Longest wait for Accept after LoginAccepted (RFC section 3.4).

◆ connect_interval

constexpr std::chrono::milliseconds rtype::ClientSession::connect_interval {250}
staticconstexpr

Time between two Connect datagrams (RFC section 3.4).

◆ input_interval

constexpr aegis2d::core::Duration rtype::ClientSession::input_interval
staticconstexpr
Initial value:
=
aegis2d::core::step_from_rate(tuning::tick_rate)
constexpr std::uint32_t tick_rate
Simulation rate of the server, in ticks per second.
Definition tuning.hpp:15

Time between two Input datagrams: one per server tick (RFC section 4.2).

◆ login_timeout

constexpr std::chrono::seconds rtype::ClientSession::login_timeout {5}
staticconstexpr

Longest wait for the TCP connection and LoginAccepted.

◆ room_refresh

constexpr std::chrono::seconds rtype::ClientSession::room_refresh {1}
staticconstexpr

Time between two ListRooms in the lobby.

◆ server_timeout

constexpr std::chrono::seconds rtype::ClientSession::server_timeout {5}
staticconstexpr

Longest silence of the server in game before the session is lost (RFC 5.5).


The documentation for this class was generated from the following file: