|
R-Type
Networked shoot'em up built on Aegis2D
|
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). | |
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.
| using rtype::ClientSession::Clock = std::chrono::steady_clock |
Clock of the timers.
| rtype::ClientSession::ClientSession | ( | aegis2d::net::NetworkContext & | context, |
| ClientSettings | settings | ||
| ) |
Creates a session, not started.
| context | Context that runs the sockets; it must outlive this object. |
| settings | Server and player name. |
|
delete |
|
delete |
| rtype::ClientSession::~ClientSession | ( | ) |
Closes the sockets, without Logout: call leave() first to say goodbye.
|
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.
|
noexcept |
The last chat messages of the room, and why a message of this client was not sent, each for a few seconds.
|
noexcept |
Number of Connect datagrams sent.
| 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.
| request | Name and size of the room. |
|
noexcept |
Why the session failed or was lost.
|
noexcept |
Number of Input datagrams sent.
| 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.
| room | Number of the room. |
|
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.
Input. | 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.
| void rtype::ClientSession::leave_room | ( | ) |
Leaves the room for the lobby, where the rooms are listed again (LeaveRoom).
|
noexcept |
What the server gave at login.
|
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.
|
delete |
|
delete |
|
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.
|
noexcept |
Whether the player said it is ready, in its room.
|
noexcept |
Why the last room could not be created or joined.
|
noexcept |
The players of the room joined, from the server's last RoomPlayers.
|
noexcept |
The rooms of the server, from its last RoomList.
|
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.
Accept.
|
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.
ScoreList. | 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.
| text | Text typed; it must fit a string. |
false if the session is not logged in or has ended: nothing was sent.
|
noexcept |
The server's UDP endpoint, once logged in.
udp_port.
|
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.
| input | Held actions. |
| void rtype::ClientSession::set_ready | ( | bool | ready | ) |
Says whether the player is ready to start, in a room (SetReady, RFC 3.7).
| ready | Ready, or not any more. |
|
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.
| 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.
|
noexcept |
Where the session is.
|
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.
| 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.
| now | Current time. |
|
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.
|
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.
|
staticconstexpr |
Longest wait for Accept after LoginAccepted (RFC section 3.4).
|
staticconstexpr |
Time between two Connect datagrams (RFC section 3.4).
|
staticconstexpr |
Time between two Input datagrams: one per server tick (RFC section 4.2).
|
staticconstexpr |
Longest wait for the TCP connection and LoginAccepted.
|
staticconstexpr |
Time between two ListRooms in the lobby.
|
staticconstexpr |
Longest silence of the server in game before the session is lost (RFC 5.5).