Symphony Of Empires
server_network.hpp
Go to the documentation of this file.
1 // Symphony of Empires
2 // Copyright (C) 2021, Symphony of Empires contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // server/server_network.hpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <deque>
28 #include <mutex>
29 #include <atomic>
30 #include <vector>
31 #include <unordered_map>
32 
33 #include "eng3d/network.hpp"
34 #include "action.hpp"
35 
36 class ServerException : public std::exception {
37  std::string buffer;
38 public:
39  ServerException(const std::string& msg) {
40  buffer = msg;
41  }
42  virtual const char* what() const noexcept {
43  return buffer.c_str();
44  }
45 };
46 
47 class GameState;
48 class Nation;
50  GameState& gs;
51 public:
52  struct ClientData {
53  Nation* selected_nation = nullptr;
55  std::string username;
56 
58  : gs{ _gs }
59  {
60 
61  }
62  };
63  Server(GameState& gs, unsigned port = 1825, unsigned max_conn = 4);
64  ~Server() = default;
65  void netloop(int id);
66  void on_connect(int conn_fd, int id) override;
67  void on_disconnect() override;
68  void handler(const Eng3D::Networking::Packet& packet, Eng3D::Deser::Archive& ar, int id) override;
69 
70  std::vector<ClientData> clients_data;
71  std::vector<Nation*> clients_extra_data;
72  std::unordered_map<ActionType, std::function<void(ClientData& client_data, const Eng3D::Networking::Packet& packet, Eng3D::Deser::Archive& ar)>> action_handlers;
73 };
74 
75 extern Server* g_server;
ActionType
Definition: action.hpp:32
ServerException(const std::string &msg)
virtual const char * what() const noexcept
std::vector< ClientData > clients_data
void on_connect(int conn_fd, int id) override
void netloop(int id)
This is the handling thread-function for handling a connection to a single client Sending packets wil...
Server(GameState &gs, unsigned port=1825, unsigned max_conn=4)
std::unordered_map< ActionType, std::function< void(ClientData &client_data, const Eng3D::Networking::Packet &packet, Eng3D::Deser::Archive &ar)> > action_handlers
void on_disconnect() override
void handler(const Eng3D::Networking::Packet &packet, Eng3D::Deser::Archive &ar, int id) override
~Server()=default
std::vector< Nation * > clients_extra_data
Server * g_server
Base class that serves as archiver, stores (in memory) the data required for serialization/deserializ...
Definition: serializer.hpp:64
ClientData(GameState &_gs)