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

The sessions of the server and the rooms they are in. More...

#include </__w/project-rtype/project-rtype/server/include/rtype/sessions.hpp>

Public Member Functions

 SessionTable ()
 Creates an empty table drawing tokens from the system's random source.
 
 SessionTable (std::uint64_t seed) noexcept
 Creates an empty table drawing tokens from a seed, for tests.
 
std::variant< Session, LoginRejection > login (const LoginRequest &request)
 Decides a login and opens its session if it is accepted.
 
BindResult bind (std::uint32_t token, const aegis2d::net::Endpoint &endpoint)
 Binds the source endpoint of a Connect to the session of its token.
 
bool end (std::uint32_t token) noexcept
 Ends a session and frees its place, name and endpoint; the last one of its room ends the room.
 
std::variant< std::uint8_t, JoinRejection > join (std::uint32_t token, std::uint32_t room)
 Moves a session out of game to another room in use (JoinRoom, RFC 3.7).
 
std::optional< std::uint32_t > leave (std::uint32_t token)
 Moves a session out of game from its room to the lobby (LeaveRoom).
 
bool set_ready (std::uint32_t token, bool ready) noexcept
 Sets whether a session out of game, in a room, is ready (SetReady).
 
std::vector< Session > members (std::uint32_t room) const
 The sessions of a room.
 
const Session * find (std::uint32_t token) const noexcept
 Finds a session.
 
const Session * find (const aegis2d::net::Endpoint &endpoint) const noexcept
 Finds the session bound to an endpoint.
 
std::size_t size () const noexcept
 Number of open sessions, those of the lobby included.
 
void set_rooms (std::uint32_t rooms)
 Sets how many rooms the sessions are spread over, tuning::max_players in each at most.
 
std::uint32_t rooms () const noexcept
 Number of rooms the sessions are spread over.
 
std::variant< std::uint8_t, RoomRejection > create_room (const CreateRoomRequest &request)
 Creates a room, empty, for the next logins (CreateRoom, RFC section 3.6).
 
void set_waves (std::vector< std::string > names)
 Sets the waves files a room may be created with, besides the server's own.
 
std::optional< RoomRules > rules (std::uint32_t room) const
 The rules of a room in use.
 
std::vector< RoomRecord > list_rooms () const
 The rooms in use, by number, as a RoomList describes them.
 

Detailed Description

The sessions of the server and the rooms they are in.

Decides each login (version, name, free place), puts it in a room, draws the tokens, moves the sessions between the rooms and the lobby, binds each session to the UDP endpoint of its Connect, and forgets it when it ends. It also keeps the rooms in use, with their name and size (RFC section 3.6): a room is in use from its creation, by create_room() or for a login that found no free place, until its last session leaves or ends. It does not touch sockets, so it is tested alone. Not thread-safe: use it from the network thread only.

Constructor & Destructor Documentation

◆ SessionTable() [1/2]

rtype::SessionTable::SessionTable ( )

Creates an empty table drawing tokens from the system's random source.

◆ SessionTable() [2/2]

rtype::SessionTable::SessionTable ( std::uint64_t  seed)
explicitnoexcept

Creates an empty table drawing tokens from a seed, for tests.

Parameters
seedSeed of the tokens: the same seed gives the same tokens.

Member Function Documentation

◆ bind()

BindResult rtype::SessionTable::bind ( std::uint32_t  token,
const aegis2d::net::Endpoint &  endpoint 
)

Binds the source endpoint of a Connect to the session of its token.

Parameters
tokenThe session field of the datagram.
endpointSource address and port of the datagram.
Returns
What was done, and so whether to answer with Accept.

◆ create_room()

std::variant< std::uint8_t, RoomRejection > rtype::SessionTable::create_room ( const CreateRoomRequest &  request)

Creates a room, empty, for the next logins (CreateRoom, RFC section 3.6).

Checked in this order: the name, by the rules of player names (valid_player_name()), the size, the lives, the difficulty, the waves (empty, or one of set_waves()), the friendly fire, a room not in use. The room takes the lowest number of those.

Parameters
requestName and most players of the room.
Returns
Its number, or why it is not created.

◆ end()

bool rtype::SessionTable::end ( std::uint32_t  token)
noexcept

Ends a session and frees its place, name and endpoint; the last one of its room ends the room.

Parameters
tokenToken of the session.
Returns
true if the session existed.

◆ find() [1/2]

const Session * rtype::SessionTable::find ( const aegis2d::net::Endpoint &  endpoint) const
noexcept

Finds the session bound to an endpoint.

Parameters
endpointSource address and port of a datagram.
Returns
The session, or nullptr if the endpoint is not bound.

◆ find() [2/2]

const Session * rtype::SessionTable::find ( std::uint32_t  token) const
noexcept

Finds a session.

Parameters
tokenToken received.
Returns
The session, or nullptr if no session has this token.

◆ join()

std::variant< std::uint8_t, JoinRejection > rtype::SessionTable::join ( std::uint32_t  token,
std::uint32_t  room 
)

Moves a session out of game to another room in use (JoinRoom, RFC 3.7).

Checked in this order: the session not in game, the room in use, a free place in it. The session takes the lowest free number there and is not ready; its old room ends if it was its last session. Joining its own room changes nothing.

Parameters
tokenToken of the session.
roomNumber of the room.
Returns
Its number in the room, or why it did not join.

◆ leave()

std::optional< std::uint32_t > rtype::SessionTable::leave ( std::uint32_t  token)

Moves a session out of game from its room to the lobby (LeaveRoom).

Parameters
tokenToken of the session.
Returns
The room it left, or std::nullopt if it is in game, in the lobby already, or unknown.

◆ list_rooms()

std::vector< RoomRecord > rtype::SessionTable::list_rooms ( ) const

The rooms in use, by number, as a RoomList describes them.

A room is RoomState::Playing once one of its sessions is bound.

Returns
One record per room in use.

◆ login()

std::variant< Session, LoginRejection > rtype::SessionTable::login ( const LoginRequest &  request)

Decides a login and opens its session if it is accepted.

Checked in this order: the version, the name, a free place, the name not used by another player of any room. The session goes to the first room in use with a free place, so a room fills before the next gets players, or else to a new room for tuning::max_players named Room <number + 1>; the server takes rooms() times tuning::max_players sessions, those of the lobby included. It takes the lowest free player number of its room, and a new random token, never 0 and never the token of another session.

Parameters
requestVersion and name received.
Returns
The new session, or why the login is refused.

◆ members()

std::vector< Session > rtype::SessionTable::members ( std::uint32_t  room) const

The sessions of a room.

Parameters
roomNumber of the room.
Returns
Copies of them, by player number.

◆ rooms()

std::uint32_t rtype::SessionTable::rooms ( ) const
noexcept

Number of rooms the sessions are spread over.

Returns
1 unless set_rooms() said otherwise.

◆ rules()

std::optional< RoomRules > rtype::SessionTable::rules ( std::uint32_t  room) const

The rules of a room in use.

A room opened for a login has the default RoomRules.

Parameters
roomNumber of the room.
Returns
Its rules, or std::nullopt if it is not in use.

◆ set_ready()

bool rtype::SessionTable::set_ready ( std::uint32_t  token,
bool  ready 
)
noexcept

Sets whether a session out of game, in a room, is ready (SetReady).

Parameters
tokenToken of the session.
readyIts new state.
Returns
true if it changed.

◆ set_rooms()

void rtype::SessionTable::set_rooms ( std::uint32_t  rooms)

Sets how many rooms the sessions are spread over, tuning::max_players in each at most.

Call it before the first login: the server is full once every room is.

Parameters
roomsNumber of rooms, 1 to max_listed_rooms; 1 by default.

◆ set_waves()

void rtype::SessionTable::set_waves ( std::vector< std::string >  names)

Sets the waves files a room may be created with, besides the server's own.

Parameters
namesTheir names, without .json (waves_names()).

◆ size()

std::size_t rtype::SessionTable::size ( ) const
noexcept

Number of open sessions, those of the lobby included.

Returns
0 to rooms() times tuning::max_players.

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