(C++)  1.1.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
TCPServer.h
1 #ifndef GRAIL_TCPSERVER_H
2 #define GRAIL_TCPSERVER_H
3 
4 #include "../../../libs/poco/Net/include/Poco/Net/SocketAddress.h"
5 #include "../../../libs/poco/Net/include/Poco/Net/StreamSocket.h"
6 #include "../../../libs/poco/Net/include/Poco/Net/SocketStream.h"
7 #include "../../../libs/poco/Net/include/Poco/Net/TCPServer.h"
8 #include "Crc16.h"
9 
10 #include <thread>
11 #include <memory>
12 #include <mutex>
13 #include <functional>
14 
15 namespace grail
16 {
17  class TCPServer
18  {
19  public:
20  TCPServer(Poco::UInt16 port);
21  ~TCPServer();
22 
23  void Start();
24  void SendToAll(const std::string& data);
25 
26  void AddEventOnConnection(std::function<void()> event);
27 
28  bool IsAnyConnected() const;
29 
30  private:
31  void AcceptConnections();
32  void CheckDisconnects();
33 
34  constexpr static const Poco::Int32 MAGIC = 0x22669977;
35  const std::string STREAM_MAGIC;
36  const Crc16 crc;
37 
38  std::atomic<bool> isWorking{true};
39  Poco::UInt16 port;
40  std::unique_ptr<Poco::Net::ServerSocket> server;
41  std::unique_ptr<std::thread> connectionAcceptorThread = nullptr;
42  std::unique_ptr<std::thread> disconnectCheckerThread = nullptr;
43  std::vector<Poco::Net::StreamSocket> clients{};
44  std::vector<std::function<void()>> onClientConnected{};
45 
46  mutable std::mutex clientsMutex{};
47  std::mutex eventMutex{};
48 
49  };
50 }
51 
52 #endif // GRAIL_TCPSERVER_H
Definition: Crc16.h:11
Definition: TCPServer.h:18