|
Eile Mit Weile
|
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_socket > | address_to_socket |
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.
| ServerNetworkManager::ServerNetworkManager | ( | ) |
Creates a new tcp socket and connects to it using default values:
|
static |
Broadcasts a ServerResponse to all players except exclude.
Connects to the server at the given url and port.
|
staticprivate |
Handles an incoming message. Determines type of request and calls RequestHandler::handle_request
|
staticprivate |
Endless loop to listen for incoming connections.
|
static |
Called when a player leaves the game. Removes the player from the player_id_to_address and address_to_socket maps.
|
staticprivate |
Reads a message from the given socket and passes it to the message_handler.
|
staticprivate |
Sends a message to the given address.
|
inlinestaticprivate |
Singleton instance of ServerNetworkManager.
|
inlinestaticprivate |
TCP acceptor to accept incoming connections.
|
inlinestaticprivate |
Maps a sockpp address to a tcp_socket
|
inlinestaticprivate |
Maps the Player::id (in UniqueSerializable) to a sockpp address
|
inlinestaticprivate |
Mutex to lock the unordered_map of players for reading and writing.