Eile Mit Weile
Loading...
Searching...
No Matches
ServerNetworkManager Class Reference

Handles all incoming messages and offers functionality to broadcast messages to all connected players of a game. More...

#include <ServerNetworkManager.hpp>

Public Member Functions

 ServerNetworkManager ()
 

Static Public Member Functions

static void BroadcastMessage (ServerResponse &msg, const std::vector< Player * > &players, const Player *exclude)
 
static void OnPlayerLeft (std::string player_id)
 

Private Member Functions

void Connect (const std::string &url, const uint16_t port)
 

Static Private Member Functions

static void ListenerLoop ()
 
static void ReadMessage (sockpp::tcp_socket socket, const std::function< void(const std::string &, const sockpp::tcp_socket::addr_t &)> &message_handler)
 
static void HandleIncomingMessage (const std::string &msg, const sockpp::tcp_socket::addr_t &peer_address)
 
static ssize_t SendMessage (const std::string &msg, const std::string &address)
 

Static Private Attributes

static ServerNetworkManager_instance
 
static std::shared_mutex rw_lock
 
static sockpp::tcp_acceptor acc
 
static std::unordered_map< std::string, std::string > player_id_to_address
 
static std::unordered_map< std::string, sockpp::tcp_socketaddress_to_socket
 

Detailed Description

Handles all incoming messages and offers functionality to broadcast messages to all connected players of a game.

Note: This class design is taken from https://gitlab.ethz.ch/hraymond/cse-lama-example-project. They made the choice to have almost all members be static, which is why this class does not need to be instantiated for most functionality. Notably, GameInstance uses members of this class without having any access to an instantiated version. It also references several other classes with static members without having to instantiate them, most notably the RequestHandler and derivatives of ClientRequest. We consider this bad practice but changing would require a lot of refactoring so we decided to keep it as is.

Constructor & Destructor Documentation

◆ ServerNetworkManager()

ServerNetworkManager::ServerNetworkManager ( )

Creates a new tcp socket and connects to it using default values:

  • server_host = 127.0.0.1
  • port = 50505

Member Function Documentation

◆ BroadcastMessage()

void ServerNetworkManager::BroadcastMessage ( ServerResponse msg,
const std::vector< Player * > &  players,
const Player exclude 
)
static

Broadcasts a ServerResponse to all players except exclude.

◆ Connect()

void ServerNetworkManager::Connect ( const std::string &  url,
const uint16_t  port 
)
private

Connects to the server at the given url and port.

◆ HandleIncomingMessage()

void ServerNetworkManager::HandleIncomingMessage ( const std::string &  msg,
const sockpp::tcp_socket::addr_t &  peer_address 
)
staticprivate

Handles an incoming message. Determines type of request and calls RequestHandler::handle_request

◆ ListenerLoop()

void ServerNetworkManager::ListenerLoop ( )
staticprivate

Endless loop to listen for incoming connections.

◆ OnPlayerLeft()

void ServerNetworkManager::OnPlayerLeft ( std::string  player_id)
static

Called when a player leaves the game. Removes the player from the player_id_to_address and address_to_socket maps.

◆ ReadMessage()

void ServerNetworkManager::ReadMessage ( sockpp::tcp_socket  socket,
const std::function< void(const std::string &, const sockpp::tcp_socket::addr_t &)> &  message_handler 
)
staticprivate

Reads a message from the given socket and passes it to the message_handler.

◆ SendMessage()

ssize_t ServerNetworkManager::SendMessage ( const std::string &  msg,
const std::string &  address 
)
staticprivate

Sends a message to the given address.

Member Data Documentation

◆ _instance

ServerNetworkManager* ServerNetworkManager::_instance
inlinestaticprivate

Singleton instance of ServerNetworkManager.

◆ acc

sockpp::tcp_acceptor ServerNetworkManager::acc
inlinestaticprivate

TCP acceptor to accept incoming connections.

◆ address_to_socket

std::unordered_map<std::string, sockpp::tcp_socket> ServerNetworkManager::address_to_socket
inlinestaticprivate

Maps a sockpp address to a tcp_socket

◆ player_id_to_address

std::unordered_map<std::string, std::string> ServerNetworkManager::player_id_to_address
inlinestaticprivate

Maps the Player::id (in UniqueSerializable) to a sockpp address

◆ rw_lock

std::shared_mutex ServerNetworkManager::rw_lock
inlinestaticprivate

Mutex to lock the unordered_map of players for reading and writing.


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