Wizard
Software Engineering Project - Wizard
Loading...
Searching...
No Matches
server_network_manager.h
1//
2// Created by Manuel on 12.02.2021.
3//
4// The server_network_manager handles all incoming messages and offers functionality to broadcast messages
5// to all connected players of a game.
6
7#ifndef WIZARD_SERVER_NETWORK_MANAGER_H
8#define WIZARD_SERVER_NETWORK_MANAGER_H
9
10#include <thread>
11#include <functional>
12#include <unordered_map>
13#include <shared_mutex>
14
15#include "sockpp/tcp_socket.h"
16#include "sockpp/tcp_connector.h"
17#include "sockpp/tcp_acceptor.h"
18
19#include "../common/network/requests/client_request.h"
20#include "../common/network/responses/server_response.h"
21#include "../common/game_state/player/player.h"
22#include "../common/game_state/game_state.h"
23
25private:
26
27 inline static server_network_manager* _instance;
28 inline static std::shared_mutex _rw_lock;
29 inline static sockpp::tcp_acceptor _acc;
30
31 inline static std::unordered_map<std::string, std::string> _player_id_to_address;
32 inline static std::unordered_map<std::string, sockpp::tcp_socket> _address_to_socket;
33
34 void connect(const std::string& url, const uint16_t port);
35
36 static void listener_loop();
37 static void read_message(sockpp::tcp_socket socket,
38 const std::function<void(const std::string&, const sockpp::tcp_socket::addr_t&)>& message_handler);
39 static void handle_incoming_message(const std::string& msg, const sockpp::tcp_socket::addr_t& peer_address);
40 static ssize_t send_message(const std::string& msg, const std::string& address);
41public:
44
45 // Used to broadcast a server_response (e.g. a full_state_response) to all 'players' except 'exclude'
46 static void broadcast_message(server_response& msg, const std::vector<player*>& players, const player* exclude);
47
48 static void on_player_left(std::string player_id);
49};
50
51
52
53#endif //WIZARD_SERVER_NETWORK_MANAGER_H
Represents a player in the game.
Definition player.h:18
Definition server_network_manager.h:24
Definition server_response.h:24