Grail (C++)  1.4.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
UDPServer.h
1 // Copyright QED Software 2023.
2 
3 #ifndef GRAIL_UDPSERVER_H
4 #define GRAIL_UDPSERVER_H
5 
6 #include "../libs/poco/Net/include/Poco/Net/DatagramSocket.h"
7 #include "../libs/poco/Net/include/Poco/Net/HostEntry.h"
8 #include "../libs/poco/Net/include/Poco/Net/SocketAddress.h"
9 
10 #include <cstddef>
11 #include <map>
12 #include <memory>
13 #include <string>
14 #include <thread>
15 #include <mutex>
16 
17 namespace grail
18 {
19 namespace live
20 {
21  class UDPServer
22  {
23  public:
24  UDPServer(const std::string& appName,
25  unsigned int serverPort,
26  unsigned long bufferSize = 1024,
27  bool sendFakeNames = false);
28  ~UDPServer();
29 
30  void Start();
31  void Stop();
32 
33  private:
34  void UDPChecker();
35  void RebuildInterfaces();
36  void CheckUDP();
37  void StartReceive(const Poco::Net::IPAddress& ipHost);
38 
39  constexpr static std::size_t ITERATIONS_BETWEEN_REBUILD{500};
40  constexpr static Poco::UInt16 UDP_LISTENER_PORT{6661};
41  const static std::string SEARCH_COMMAND;
42 
43  const std::string appName{""};
44  const unsigned int serverPort{0};
45  const bool sendFakeNames{false};
46  std::string hostName{""};
47 
48  std::unique_ptr<std::thread> udpCheckerThread{nullptr};
49 
50  std::vector<Poco::Net::IPAddress> interfaces{};
51  Poco::Net::HostEntry localHostEntry{};
52 
53  Poco::Net::SocketAddress anyAddress{};
54 
55  std::atomic<bool> isWorking{false};
56 
57  std::map<std::string, Poco::Net::DatagramSocket> sockets{};
58  char* buffer{nullptr};
59  unsigned long bufferSize;
60  std::mutex stateChangeMutex{};
61  };
62 }
63 }
64 
65 #endif // GRAIL_UDPSERVER_H
grail::live::UDPServer
Definition: UDPServer.h:21