Eile Mit Weile
Loading...
Searching...
No Matches
ServerNetworkManager.hpp
1#ifndef EILE_MIT_WEILE_SERVER_NETWORK_MANAGER_HPP
2#define EILE_MIT_WEILE_SERVER_NETWORK_MANAGER_HPP
3
4#include <thread>
5#include <functional>
6#include <unordered_map>
7#include <shared_mutex>
8#include <vector>
9
10#include "sockpp/tcp_socket.h"
11#include "sockpp/tcp_connector.h"
12#include "sockpp/tcp_acceptor.h"
13
17#include "common/gameState/GameState.hpp"
18#include "common/network/requests/move_figure_request.hpp"
19#include "common/network/requests/throw_dice_request.hpp"
20#include "common/network/requests/skip_turn_request.hpp"
21#include "common/network/requests/join_game_request.hpp"
22#include "common/network/requests/start_game_request.hpp"
23
36private:
37
39 inline static std::shared_mutex rw_lock;
40 inline static sockpp::tcp_acceptor acc;
42 inline static std::unordered_map<std::string, std::string> player_id_to_address;
43 inline static std::unordered_map<std::string, sockpp::tcp_socket> address_to_socket;
45 void Connect(const std::string& url, const uint16_t port);
47 static void ListenerLoop();
49 const std::function<void(const std::string&, const sockpp::tcp_socket::addr_t&)>& message_handler);
50 static void HandleIncomingMessage(const std::string& msg, const sockpp::tcp_socket::addr_t& peer_address);
51 static ssize_t SendMessage(const std::string& msg, const std::string& address);
52public:
57
58 static void BroadcastMessage(ServerResponse& msg, const std::vector<Player*>& players, const Player* exclude);
60 static void OnPlayerLeft(std::string player_id);
62};
63
64
65#endif //EILE_MIT_WEILE_SERVER_NETWORK_MANAGER_HPP
Holds all relevant information of a player.
Definition Player.hpp:27
A serializable value, used for writing values into json.
Definition serializable_value.hpp:28
SerializableValue(T val)
Definition serializable_value.hpp:30
Handles all incoming messages and offers functionality to broadcast messages to all connected players...
Definition ServerNetworkManager.hpp:35
static void OnPlayerLeft(std::string player_id)
Definition ServerNetworkManager.cpp:190
static void ReadMessage(sockpp::tcp_socket socket, const std::function< void(const std::string &, const sockpp::tcp_socket::addr_t &)> &message_handler)
Definition ServerNetworkManager.cpp:64
void Connect(const std::string &url, const uint16_t port)
Definition ServerNetworkManager.cpp:22
static void HandleIncomingMessage(const std::string &msg, const sockpp::tcp_socket::addr_t &peer_address)
Definition ServerNetworkManager.cpp:117
ServerNetworkManager()
Definition ServerNetworkManager.cpp:12
static std::unordered_map< std::string, sockpp::tcp_socket > address_to_socket
Definition ServerNetworkManager.hpp:43
static sockpp::tcp_acceptor acc
Definition ServerNetworkManager.hpp:40
static void BroadcastMessage(ServerResponse &msg, const std::vector< Player * > &players, const Player *exclude)
Definition ServerNetworkManager.cpp:204
static void ListenerLoop()
Definition ServerNetworkManager.cpp:34
static std::unordered_map< std::string, std::string > player_id_to_address
Definition ServerNetworkManager.hpp:42
static ssize_t SendMessage(const std::string &msg, const std::string &address)
Definition ServerNetworkManager.cpp:198
static std::shared_mutex rw_lock
Definition ServerNetworkManager.hpp:39
static ServerNetworkManager * _instance
Definition ServerNetworkManager.hpp:38
Base class for all messages sent from the server to the client.
Definition ServerResponse.hpp:33