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

Runs the games of its rooms for the players of its sockets, as r-type_server does. More...

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

Public Member Functions

 GameServer (aegis2d::net::NetworkContext &context, std::optional< std::uint64_t > seed, aegis2d::ecs::WaveSchedule waves, std::uint16_t tcp_port, std::uint16_t udp_port, std::uint32_t rate=tuning::tick_rate, std::uint32_t max_rooms=1)
 Opens both ports; the rooms open as their players join.
 
 GameServer (const GameServer &)=delete
 
GameServer & operator= (const GameServer &)=delete
 
 GameServer (GameServer &&)=delete
 
GameServer & operator= (GameServer &&)=delete
 
 ~GameServer ()=default
 Stops the simulation threads, then closes the sockets.
 
void set_waves (std::map< std::string, aegis2d::ecs::WaveSchedule > waves)
 Sets the waves files a room may be created with, besides the server's own.
 
void keep_scores (std::filesystem::path file)
 Keeps the best scores in a file: at the end of each game of a room, every player of it records its score there.
 
const Scoreboard * scoreboard () const noexcept
 The best scores kept.
 
ServerNetwork & network () noexcept
 The sockets and the sessions; use them on the context's thread.
 
Room & room (std::uint32_t id=0)
 An open room: its game, thread and event tracker. Use it on the context's thread: the room closes there when its last player leaves.
 
std::uint32_t open_rooms () const noexcept
 Number of rooms open, with a game running.
 
std::uint32_t max_rooms () const noexcept
 Number of rooms played at once.
 
std::uint32_t rate () const noexcept
 Ticks per second of each room.
 
void start_trace () noexcept
 Records the timings of the ticks of every room opened from now on.
 
std::size_t save_trace (const std::filesystem::path &file)
 Closes every room, then writes the timings recorded in the Chrome trace format, which Perfetto (ui.perfetto.dev) opens: a thread per room opened, named after it.
 
Simulation & simulation ()
 The thread running the game of the first room, room().simulation().
 

Detailed Description

Runs the games of its rooms for the players of its sockets, as r-type_server does.

The sockets work on the caller's context, each game in its Room, on the room's Simulation thread, and they only talk through posted tasks. Each session plays in the room of its Session::room, and everything below stays within that room:

  • a player whose UDP endpoint is bound joins the game: the others get its PlayerJoined, and it gets a PlayerJoined of every player already there, then a Spawn of every entity (RFC section 4.6);
  • a player whose session ends (Logout, Disconnect, TCP closed, UDP timeout, protocol error) leaves it: the others get its PlayerLeft with the reason, then, at the next tick, the Destroy of its ship;
  • the most recent Input of each player steers its ship;
  • after each tick, the events of the tick go to every bound player, and every third tick a snapshot;
  • with each snapshot, the round trip time of each player sets how far back its missiles meet the enemies (lag compensation, rewind_ticks()), when it changes.

The logins fill the first room with a free player number, then the next one, up to max_rooms() rooms (SessionTable::set_rooms()). A room opens when its first player joins: its game starts on a new thread, with the rules the room was created with (game_rules(), and the waves it names, see set_waves()). It closes when its last player leaves: its thread is stopped and joined, and its game is gone, so the next player of that room starts a new one. The caller runs the context and calls network().update() a few times per second. Destroying the server stops the simulation threads; the context must not run the tasks left in it afterwards.

Constructor & Destructor Documentation

◆ GameServer() [1/3]

rtype::GameServer::GameServer ( aegis2d::net::NetworkContext &  context,
std::optional< std::uint64_t >  seed,
aegis2d::ecs::WaveSchedule  waves,
std::uint16_t  tcp_port,
std::uint16_t  udp_port,
std::uint32_t  rate = tuning::tick_rate,
std::uint32_t  max_rooms = 1 
)

Opens both ports; the rooms open as their players join.

Parameters
contextContext of the sockets; it must outlive this object.
seedSeed of the random spawns of the rooms' games, or std::nullopt for none.
wavesWaves the rooms play in every game, at their start.
tcp_portTCP port of the logins, or 0 for any free port.
udp_portUDP port of the game, or 0 for any free port.
rateTicks per second of each room.
max_roomsRooms played at once, at least 1.
Exceptions
aegis2d::net::SocketErrorIf a port is already in use or cannot be bound.

◆ GameServer() [2/3]

rtype::GameServer::GameServer ( const GameServer &  )
delete

◆ GameServer() [3/3]

rtype::GameServer::GameServer ( GameServer &&  )
delete

◆ ~GameServer()

rtype::GameServer::~GameServer ( )
default

Stops the simulation threads, then closes the sockets.

Member Function Documentation

◆ keep_scores()

void rtype::GameServer::keep_scores ( std::filesystem::path  file)

Keeps the best scores in a file: at the end of each game of a room, every player of it records its score there.

Reads the board of the file now (Scoreboard), and uses it on the context's thread, where the network answers ListScores with it; without a call, no score is kept and the list is empty.

Parameters
fileWhere the scores are kept.

◆ max_rooms()

std::uint32_t rtype::GameServer::max_rooms ( ) const
noexcept

Number of rooms played at once.

Returns
The count given to the constructor, at least 1.

◆ network()

ServerNetwork & rtype::GameServer::network ( )
noexcept

The sockets and the sessions; use them on the context's thread.

Returns
The network side.

◆ open_rooms()

std::uint32_t rtype::GameServer::open_rooms ( ) const
noexcept

Number of rooms open, with a game running.

Returns
0 to max_rooms().

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ rate()

std::uint32_t rtype::GameServer::rate ( ) const
noexcept

Ticks per second of each room.

Returns
The rate given to the constructor.

◆ room()

Room & rtype::GameServer::room ( std::uint32_t  id = 0)

An open room: its game, thread and event tracker. Use it on the context's thread: the room closes there when its last player leaves.

Parameters
idNumber of the room, below max_rooms().
Returns
The room.
Exceptions
std::out_of_rangeIf the room is not open.

◆ save_trace()

std::size_t rtype::GameServer::save_trace ( const std::filesystem::path &  file)

Closes every room, then writes the timings recorded in the Chrome trace format, which Perfetto (ui.perfetto.dev) opens: a thread per room opened, named after it.

Parameters
fileFile to write, created or replaced.
Returns
Number of events written.
Exceptions
std::runtime_errorIf the file cannot be written.

◆ scoreboard()

const Scoreboard * rtype::GameServer::scoreboard ( ) const
noexcept

The best scores kept.

Returns
The board, or nullptr without keep_scores(); use it on the context's thread.

◆ set_waves()

void rtype::GameServer::set_waves ( std::map< std::string, aegis2d::ecs::WaveSchedule >  waves)

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

The network offers their names (ServerNetwork::set_waves()); a room naming one plays its waves, a room naming none the waves given to the constructor. Call it on the context's thread; the rooms open from now on use it.

Parameters
wavesThe schedules, by name (waves_names(), without .json).

◆ simulation()

Simulation & rtype::GameServer::simulation ( )

The thread running the game of the first room, room().simulation().

Returns
The simulation.
Exceptions
std::out_of_rangeIf the first room is not open.

◆ start_trace()

void rtype::GameServer::start_trace ( )
noexcept

Records the timings of the ticks of every room opened from now on.

Each room's thread keeps its own (Simulation::profiler()); a room that closes leaves its timings for save_trace().


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