OpenAstra
 
Loading...
Searching...
No Matches
connectionsmanager.h
1#ifndef CONNECTIONSMANAGER_H
2#define CONNECTIONSMANAGER_H
3
4#include <stdint.h>
5
6#include <thread>
7#include <map>
8#include <memory>
9#include <set>
10#include <vector>
11
12#include "gameobject.h"
13#include "../utils/json.hpp"
14
15class TcpServer;
16class SocketDevice;
17class GameManager;
18
19class ConnectionsManager
20// : public Thread
21{
22public:
23 using WatermarkType = int64_t;
24 static ConnectionsManager* connections_manager;
25
26 static constexpr WatermarkType watermark_ref = 0x08FEEF0100112233ULL;
27
28 // CLIENT -> SERVER requests.
29 //
30 // A message is always a JSON object serialized with MessagePack format, only one JSON for each command.
31 //
32 // *****To avoid messing up the TCP channel, you must **never** mix send and receive for the
33 // same command *****
34 //.
35 // The CLIENT initiate an object_subscribe command.
36 // The CLIENT can send addiitonal object_unsubscribe to stop updates.
37 // the CLIENT can send additional command specific messages.
38 // The server will NOT reply at this time, but reply with one of the SERVER -> CLIENT commands.
39 static constexpr char cmd_from_client_subscribe_starsystem[] = "subscribe_starsystem";
40 static constexpr char cmd_from_client_unsubscribe_starsystem[] = "unsubscribe_starsystem";
41 static constexpr char cmd_from_client_get_player_data[] = "player_data";
42 static constexpr char cmd_from_client_get_starsystem_data[] = "starsystem_data";
43 static constexpr char cmd_from_client_get_rules[] = "rules";
44
45 // SERVER -> CLIENT messages.
46 // The SERVER will send back to the CLIENT one of the following messages:
47 static constexpr char cmd_to_client_player_data[] = "player_data";
48 static constexpr char cmd_to_client_starsystem_data[] = "starsystem_data";
49 static constexpr char cmd_to_client_rules_data[] = "rules";
50
51 // System messages (client -> server)
52 static constexpr char cmd_quit_request[] = "quit";
53
54public:
55 ConnectionsManager( int listen_port, const std::string& listen_on, std::atomic<bool>& keep_running );
56 ~ConnectionsManager();
57
58 bool isReady();
59
60 static bool sendMessage( const std::string& command_name, const nlohmann::json& command_data, std::unique_ptr<SocketDevice>& connection );
61 static bool sendPackedMessage( const std::vector<uint8_t>& command_packed_data, std::unique_ptr<SocketDevice>& connection );
62 static bool receiveMessage( nlohmann::json& command_data, std::unique_ptr<SocketDevice>& connection );
63
64private:
65 static void static_run( ConnectionsManager* myself );
66 void run();
67
68 // accept incoming new connections
69 bool _acceptConnection( std::unique_ptr<SocketDevice>& connection );
70
71 // Process connection -> server requests
72 bool _checkCommand(std::unique_ptr<SocketDevice>& connection, ObjectId player_id );
73 bool _subscribeStarsystem( const nlohmann::json& command_data, ObjectId player_id);
74 bool _unsubscribeStarsystem(const nlohmann::json &command_data, ObjectId player_id );
75 bool _getPlayerData(const nlohmann::json &command_data, std::unique_ptr<SocketDevice> &client);
76 bool _getStarsystemData(const nlohmann::json &command_data, std::unique_ptr<SocketDevice> &client);
77 bool _getRules( std::unique_ptr<SocketDevice>& client );
78
79 // Process server -> client replies
80 bool _sendPlayerData( std::unique_ptr<SocketDevice>& client, ObjectId player_id );
81 bool _sendStarsystemData( std::unique_ptr<SocketDevice>& client, ObjectId starsystem_id );
82 bool _sendRules( std::unique_ptr<SocketDevice>& client );
83
84 std::thread _t;
85 TcpServer* _server;
86 std::atomic<bool>& _keep_running;
87
88 std::map<ObjectId, std::unique_ptr<SocketDevice>> _connected_players;
89 std::map<ObjectId, std::set<ObjectId>> _subscribed_starsystems;
90};
91
92#endif // CONNECTIONSMANAGER_H
Definition gamemanager.h:13
Definition gameid.hpp:9
Definition socketdevice.h:11
Definition tcpserver.hpp:10