Jetpack documentation 1.0.0
Loading...
Searching...
No Matches
Server Class Reference

A class that represents the server. More...

#include <Server.hpp>

Data Structures

class  Client
 A class that represents a client connected to the server. More...
 

Public Member Functions

 Server (int ac, char **av)
 Constructor for Server class.
 
 ~Server ()
 Destructor for Server class.
 
 Server (const Server &)=delete
 Deleted copy constructor.
 
Serveroperator= (const Server &)=delete
 Deleted assignment operator.
 
void run ()
 Runs the server.
 

Static Public Member Functions

static void sendResponse (int client_fd, CmdID cmd_id, int cmd_code, const std::string &message)
 Sends a response to the client.
 

Protected Member Functions

void executeId (int client_fd, std::vector< std::string > &command)
 Executes the ID command.
 
void executeMap (int client_fd, std::vector< std::string > &command)
 Executes the MAP command.
 
void executePseudo (int client_fd, std::vector< std::string > &command)
 Executes the PSEUDO command.
 
void executeMove (int client_fd, std::vector< std::string > &command)
 Executes the MOVE command.
 
void executeClientsNb (int client_fd, std::vector< std::string > &command)
 Executes the CLIENTS_NB command.
 
void executeClientsStats (int client_fd, std::vector< std::string > &command)
 Executes the CLIENTS_STATS command.
 
void executeClientsPositions (int client_fd, std::vector< std::string > &command)
 Executes the CLIENTS_POSITIONS command.
 

Protected Attributes

std::unordered_map< std::string, std::function< void(int client_fd, std::vector< std::string > &command)> > _commands
 

Private Member Functions

void _checkArgs (int ac, char **av)
 Checks the command line arguments.
 
void _initMap ()
 Initializes the map of the game.
 
void _initCommands ()
 Initializes the commands of the server.
 
void _initServer ()
 Initializes the server.
 
bool _isPlayerOn (const double &x, const double &y, const char c) const
 Checks if the player is on the map.
 
void _analyseClientOnCoin (Client &client)
 Analyse if the client is on a coin and add it to the client coin list.
 
void _updatePlayer (Client &client, double &delta_time, bool &reset_coins)
 Updates a client (player)
 
void _updateGame ()
 Updates the game.
 
void _addNewClient ()
 Adds a new client to the server.
 
void _disconnectClient (std::size_t poll_index)
 Disconnects a client from the server.
 
void _executeCommand (std::size_t poll_index, const std::string &command)
 Executes a command.
 
void _writeClientAction (std::size_t poll_index)
 Writes a response to the client.
 
void _readClientAction (std::size_t poll_index)
 Reads a command from the client.
 
void _analysePoll (std::size_t poll_index)
 Analyzes the poll events.
 

Private Attributes

std::string _port
 
std::string _map_path
 
bool _infinite_mode = false
 
std::vector< std::string > _map
 
struct sockaddr_in _server
 
std::unique_ptr< Socket_server_socket
 
std::vector< pollfd > _poll_list
 
std::map< std::size_t, std::unique_ptr< Client > > _clients
 
std::chrono::time_point< std::chrono::high_resolution_clock > _last_update
 
double _map_x_pos = 0
 

Detailed Description

A class that represents the server.

Constructor & Destructor Documentation

◆ Server() [1/2]

Server ( int ac,
char ** av )

Constructor for Server class.

Parameters
acNumber of command line arguments
avArray of command line arguments
Exceptions
JetpackErrorIf the arguments are invalid

◆ Server() [2/2]

Server ( const Server & )
delete

Deleted copy constructor.

Parameters
otherThe other Server object to copy from

Member Function Documentation

◆ _addNewClient()

void _addNewClient ( )
private

Adds a new client to the server.

Exceptions
JetpackErrorIf the accept fails

◆ _analyseClientOnCoin()

void _analyseClientOnCoin ( Client & client)
private

Analyse if the client is on a coin and add it to the client coin list.

Parameters
clientThe client to analyse

◆ _analysePoll()

void _analysePoll ( std::size_t poll_index)
private

Analyzes the poll events.

Parameters
poll_indexThe index of the client in the poll list

◆ _checkArgs()

void _checkArgs ( int ac,
char ** av )
private

Checks the command line arguments.

Check the arguments passed to the server.

Parameters
acNumber of command line arguments
avArray of command line arguments
Exceptions
JetpackErrorIf the arguments are invalid
JetpackUsageIf the help flag is passed
Parameters
acThe number of arguments
avThe arguments
Exceptions
JetpackErrorIf the arguments are invalid
JetpackUsageIf the help flag is passed

◆ _disconnectClient()

void _disconnectClient ( std::size_t poll_index)
private

Disconnects a client from the server.

Parameters
poll_indexThe index of the client in the poll list

◆ _executeCommand()

void _executeCommand ( std::size_t poll_index,
const std::string & command )
private

Executes a command.

Parameters
poll_indexThe index of the client in the poll list
commandThe command to execute

◆ _initCommands()

void _initCommands ( )
private

Initializes the commands of the server.

Initialize the commands of the server.

◆ _initMap()

void _initMap ( )
private

Initializes the map of the game.

Initialize the map from the file.

Exceptions
JetpackErrorIf the map file is empty or cannot be opened

◆ _initServer()

void _initServer ( )
private

Initializes the server.

Initialize the server.

Exceptions
JetpackErrorIf the server cannot be initialized

◆ _isPlayerOn()

bool _isPlayerOn ( const double & x,
const double & y,
const char c ) const
private

Checks if the player is on the map.

Parameters
xThe x position of the player
yThe y position of the player
cThe character to check
Returns
True if the player is on the map, false otherwise

◆ _readClientAction()

void _readClientAction ( std::size_t poll_index)
private

Reads a command from the client.

Parameters
poll_indexThe index of the client in the poll list
Exceptions
JetpackErrorIf the read fails

◆ _updateGame()

void _updateGame ( )
private

Updates the game.

Note
This function is called every frame/poll

◆ _updatePlayer()

void _updatePlayer ( Client & client,
double & delta_time,
bool & reset_coins )
private

Updates a client (player)

Parameters
clientThe client to update
delta_timeThe time since the last frame
reset_coinsTrue if the coins should be reset and false if not

◆ _writeClientAction()

void _writeClientAction ( std::size_t poll_index)
private

Writes a response to the client.

Parameters
poll_indexThe index of the client in the poll list

◆ executeClientsNb()

void executeClientsNb ( int client_fd,
std::vector< std::string > & command )
protected

Executes the CLIENTS_NB command.

Execute the clients nb command.

Parameters
client_fdThe file descriptor of the client
commandThe command to execute

◆ executeClientsPositions()

void executeClientsPositions ( int client_fd,
std::vector< std::string > & command )
protected

Executes the CLIENTS_POSITIONS command.

Execute the clients positions command.

Parameters
client_fdThe file descriptor of the client
commandThe command to execute

◆ executeClientsStats()

void executeClientsStats ( int client_fd,
std::vector< std::string > & command )
protected

Executes the CLIENTS_STATS command.

Execute the clients stats command.

Parameters
client_fdThe file descriptor of the client
commandThe command to execute

◆ executeId()

void executeId ( int client_fd,
std::vector< std::string > & command )
protected

Executes the ID command.

Execute the id command.

Parameters
client_fdThe file descriptor of the client
commandThe command to execute

◆ executeMap()

void executeMap ( int client_fd,
std::vector< std::string > & command )
protected

Executes the MAP command.

Execute the map command.

Parameters
client_fdThe file descriptor of the client
commandThe command to execute

◆ executeMove()

void executeMove ( int client_fd,
std::vector< std::string > & command )
protected

Executes the MOVE command.

Execute the move command.

Parameters
client_fdThe file descriptor of the client
commandThe command to execute

◆ executePseudo()

void executePseudo ( int client_fd,
std::vector< std::string > & command )
protected

Executes the PSEUDO command.

Execute the pseudo command.

Parameters
client_fdThe file descriptor of the client
commandThe command to execute

◆ operator=()

Server & operator= ( const Server & )
delete

Deleted assignment operator.

Parameters
otherThe other Server object to assign from
Returns
Reference to the current object

◆ run()

void run ( )

Runs the server.

Note
This function is the main loop of the server
Exceptions
JetpackErrorIf the poll fails

◆ sendResponse()

void sendResponse ( int client_fd,
CmdID cmd_id,
int cmd_code,
const std::string & message )
static

Sends a response to the client.

Parameters
client_fdThe file descriptor of the client
cmd_idThe command ID
cmd_codeThe command code
messageThe message to send

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