DCSModulesAPI  1.0.0
DCS sub project containing all the control modules as libraries.
DCS_ModuleNetwork.h
Go to the documentation of this file.
1 #ifndef _DCS_NETWORK_H
2 #define _DCS_NETWORK_H
3 
4 #pragma once
5 #include "../../config/exports.h"
6 #include "../../DCS_Utils/include/DCS_ModuleUtils.h"
7 #include "../../config/registry.h"
8 
9 #include <unordered_map>
10 
22 namespace DCS
23 {
29  namespace Network
30  {
35 
39  DCS_API void Init();
40 
44  DCS_API bool GetStatus();
45 
49  DCS_API void Destroy();
50 
54  namespace Server
55  {
59  enum class StopMode
60  {
61  IMMEDIATE,
62  WAIT
63  };
64 
69  DCS_API Socket Create(i32 port);
70 
75  DCS_API void WaitForConnections(Socket server);
76 
81  DCS_API void StopListening(Socket server);
82 
88  DCS_API bool StartThread(Socket client);
89 
95  DCS_API void StopThread(Socket client, StopMode mode = StopMode::IMMEDIATE);
96 
100  DCS_API Socket GetConnectedClient();
101 
105  DCS_API Socket GetListenSocket();
106 
110  DCS_API bool IsRunning();
111 
116  }
117 
121  namespace Client
122  {
128  DCS_API Socket Connect(DCS::Utils::String host, i32 port);
129 
137  DCS_API void Authenticate(Socket socket, DCS::Utils::String username, DCS::Utils::String password);
138 
144  DCS_API bool StartThread(Socket connection);
145 
150  DCS_API void StopThread(Socket connection);
151 
159 
164  }
165 
181  namespace Message
182  {
186  enum class Operation
187  {
188  NO_OP = 0,
189  REQUEST = 1,
190  // 2 and 3 reserved
191  RESPONSE = 4,
192  // 5 and 6 reserved
193  EVT_SUB = 7,
194  // 8 reserved
195  EVT_UNSUB = 9,
196  OP_ERROR,
197  CON_VALID,
198  DATA
199  };
200 
213 
226 
233  DCS_API void FibSeqEvt();
234  }
235  }
236 
240  namespace CLI
241  {
245  class Command
246  {
247  public:
248  using MapType = std::map<std::string, Command>;
249  using RunCB = std::function<void(bool*)>;
250 
255  Command(const char *DCL, const char *help = "", RunCB cb = nullptr)
256  {
257  cmd_name = DCL;
258  help_string = help;
259  to_run = cb;
260 
261  if (!IsCommand(cmd_name))
262  cmd_reg.emplace(cmd_name, *this);
263  else
264  LOG_WARNING("Attempted to create command %s. But it was already registered. Ignoring...", DCL);
265  }
266 
271  static Command *Get(std::string name)
272  {
273  MapType::iterator it = cmd_reg.find(name);
274 
275  if (it != cmd_reg.end())
276  return &it->second;
277  return nullptr;
278  }
279 
284  static bool IsCommand(std::string name)
285  {
286  MapType::iterator it = cmd_reg.find(name);
287  return it != cmd_reg.end();
288  }
289 
295  static Command *Closest(std::string name);
296 
301  static std::vector<std::string> ListCommands()
302  {
303  std::vector<std::string> cmds;
304  for (auto &p : cmd_reg)
305  {
306  cmds.push_back(p.second.cmd_name + " - " + p.second.help_string);
307  }
308  return cmds;
309  }
310 
315  inline const std::string getName() const
316  {
317  return cmd_name;
318  }
319 
324  inline void Run(bool* brk)
325  {
326  if (to_run != nullptr)
327  {
328  to_run(brk);
329  }
330 
331  // Silently ignore
332  }
333 
334  private:
335  inline static MapType cmd_reg;
336 
337  private:
338  std::string cmd_name;
339  std::string help_string;
340 
341  RunCB to_run;
342  };
343 
347  DCS_API void Spin();
348  }
349 }
350 
351 #endif _DCS_NETWORK_H
Stop server thread without waiting for the client to disconnect (force shutdown server-side).
A class that holds possible future data.
Definition: DCS_ModuleUtils.h:246
Send back a response to the client.
#define DCS_API
Defines the export interface acessible via the dll-interface. Only important for SHARED LIB Compile M...
Definition: exports.h:116
DCS_API bool GetStatus()
Check if the Network module is initilized.
Definition: socket.cpp:14
Holds messages return types.
Definition: registry.h:383
Operation
Defines the message operation modes in the tcp/ip connection.
Definition: DCS_ModuleNetwork.h:186
DCS_API DCS::Utils::AsyncItem< DCS::Registry::SVReturn > SendAsync(Operation op, u8 *data, i32 size)
Sends an asynchronous message to the server.
Definition: message.cpp:132
DCS_API Socket GetConnectedClient()
Returns the currently server connected client.
Definition: server.cpp:457
DCS::GenericHandle Socket
A generic socket.
Definition: DCS_ModuleNetwork.h:34
Send or receive data only.
DCS_API bool StartThread(Socket client)
Starts the server thread on a established connection.
Definition: server.cpp:205
DCS_API void StopListening(Socket server)
Closes the server listening socket, no longer accepting new connections.
Definition: server.cpp:101
CON_VALID
Server connection validity message.
Definition: internal.h:178
DCS_API bool IsRunning()
Checks if the server is running.
Definition: server.cpp:467
Class responsible for keeping the state of a declared command to use in the server console...
Definition: DCS_ModuleNetwork.h:245
DCS_API void Spin()
Make the current thread wait for, and process, console commands.
Definition: cli.cpp:208
NO_OP
Ping the server only.
Definition: internal.h:171
DCS_API Registry::SVReturn SendSync(Operation op, u8 *data, i32 size)
Sends a synchronous message to the server.
Definition: message.cpp:155
EVT_SUB
Subscribe to a server-side event.
Definition: internal.h:176
void * GenericHandle
A generic opaque handle that is only meaningful for the API.
Definition: DCS_ModuleUtils.h:66
signed long i32
Equivalent to int_32t.
Definition: DCS_ModuleUtils.h:53
EVT_UNSUB
Unsubscribe from a previously subscribed event.
Definition: internal.h:178
A wrapper class used to export a simple string to the client side.
Definition: DCS_ModuleUtils.h:73
DCS_API Socket Connect(DCS::Utils::String host, i32 port)
Connect to a running server.
Definition: client.cpp:32
StopMode
Dictates how the server thread should stop.
Definition: DCS_ModuleNetwork.h:59
DCS_API void Init()
Initializes the Network module.
Definition: socket.cpp:4
DCS_API void Destroy()
Destroys the Network module.
Definition: socket.cpp:19
DCS_API void Authenticate(Socket socket, DCS::Utils::String username, DCS::Utils::String password)
Authenticates a valid connection. Call after Connect.
Definition: client.cpp:47
Definition: registry.h:70
#define LOG_WARNING(msg,...)
Alias to LOG_LVL(Warning, msg, VA_ARGS)
Definition: DCS_ModuleUtils.h:36
DCS_API void WaitForConnections(Socket server)
Wait for any client to connect to the server connection Socket.
Definition: server.cpp:79
DCS_API i16 GetMillisLatency()
Gets the current connection latency in milliseconds.
Definition: client.cpp:360
unsigned char u8
Equivalent to uint_8t.
Definition: DCS_ModuleUtils.h:58
DCS_API Socket Create(i32 port)
Create server listening port.
Definition: server.cpp:62
Request a function call to the server.
#define DCS_REGISTER_EVENT
Append this definition before function declarations (or anywhere in the code) to register a tcp conne...
Definition: exports.h:95
OP_ERROR
Send an error to the client/server.
Definition: internal.h:178
DCS_REGISTER_EVENT DCS_API void FibSeqEvt()
A Sample Event possible to implement in the server/API. When called, returns the next number in the F...
Definition: server.cpp:27
short i16
Equivalent to int_16t.
Definition: DCS_ModuleUtils.h:55
DCS_API Socket GetListenSocket()
Returns the server listen socket.
Definition: server.cpp:462
Stop server after waiting for the client to disconnect (blocking the calling thread).
DCS_API void StopThread(Socket client, StopMode mode=StopMode::IMMEDIATE)
Stops the server thread on a established connection (Forces disconnect).
Definition: server.cpp:406