CMSDK  2.0.1
NetworkManager.h
1 #if !defined(_NETWORKMANAGER_H_)
2 #define _NETWORKMANAGER_H_
3 
4 #pragma once
5 
6 #include "NetworkConnections.h"
7 #include "NetworkProtocols.h"
8 
9 
10 namespace cmlabs{
11 
12 struct NetworkEvent {
13  uint64 cid;
14  uint64 conid;
15  uint64 time;
16  uint8 type;
17  uint8 protocol;
18 };
19 
21 // Receiver Interface
23 
24 class NetworkChannel;
26 public:
27  NetworkReceiver() {}
28  virtual bool receiveNetworkEvent(NetworkEvent* evt, NetworkChannel* channel, uint64 conid) {return false;}
29  virtual bool receiveHTTPRequest(HTTPRequest* req, NetworkChannel* channel, uint64 conid) {return false;}
30  virtual bool receiveMessage(DataMessage* msg, NetworkChannel* channel, uint64 conid) {return false;}
31  virtual bool receiveTelnetLine(TelnetLine* line, NetworkChannel* channel, uint64 conid) {return false;}
32  virtual bool receiveHTTPReply(HTTPReply* reply, HTTPRequest* req, NetworkChannel* channel, uint64 conid) {return false;}
33 };
34 
36 public:
38  ~HTTPTestServer();
39  virtual bool receiveHTTPRequest(HTTPRequest* req, NetworkChannel* channel, uint64 conid);
40 };
41 
43 // Network Manager
45 
46 class NetworkChannel;
47 class NetworkManager : public Runnable {
48  friend class NetworkChannel;
49 public:
50  static bool UnitTest();
51  static bool UnitTestDelayedConnect();
52  static bool UnitTestHTTP();
53  static bool TestHTTP(const char* host, uint32 port, std::vector<std::string> &urls);
54 
55  THREAD_RET THREAD_FUNCTION_CALL NetworkManagerRun(THREAD_ARG arg);
56 
58  virtual ~NetworkManager();
59 
60  bool setSSLCertificate(const char* sslCertPath, const char* sslKeyPath);
61 
62  NetworkChannel* createListener(uint16 port, uint8 encryption, uint8 protocol, bool isAsync, uint32 protocolTimeout, bool isDefaultProtocol, uint32 channelID, NetworkReceiver* recv);
63  NetworkChannel* createTCPConnection(const char* addr, uint16 port, uint8 encryption, uint8 protocol, bool isAsync, bool autoreconnect, uint32 channelID, NetworkReceiver* recv, uint64& conid, uint64& location, uint32 timeoutMS = 5000);
64  NetworkChannel* createTCPConnection(uint64 location, uint8 encryption, uint8 protocol, bool isAsync, bool autoreconnect, uint32 channelID, NetworkReceiver* recv, uint64& conid, uint32 timeoutMS = 5000);
65  NetworkChannel* createTCPConnection(const uint32* addresses, uint16 addressCount, uint16 port, uint8 encryption, uint8 protocol, bool isAsync, bool autoreconnect, uint32 channelID, NetworkReceiver* recv, uint64& conid, uint64& location, uint32 timeoutMS = 5000);
66  NetworkChannel* createUDPConnection(uint16 port, uint8 protocol, bool isAsync, bool autoreconnect, uint32 channelID, NetworkReceiver* recv, uint64& conid);
67  NetworkChannel* addTCPConnection(const char* addr, uint16 port, uint8 encryption, uint8 protocol, bool isAsync, uint32 channelID, NetworkReceiver* recv, uint64& conid, uint64& location, uint32 timeoutMS = 5000, const char* greetingData = NULL, uint32 greetingSize = 0);
68  NetworkChannel* addTCPConnection(uint64 location, uint8 encryption, uint8 protocol, bool isAsync, uint32 channelID, NetworkReceiver* recv, uint64& conid, uint32 timeoutMS = 5000, const char* greetingData = NULL, uint32 greetingSize = 0);
69 
70  bool stopListener(uint16 port, uint8 protocol);
71  bool endConnection(uint64 conid);
72  bool endUDPConnection(uint16 port);
73 
74  uint8 getConnectionType(uint64 conid);
75  uint64 getRemoteAddress(uint64 conid);
76  NetworkChannel* getConnection(uint64 conid);
77  NetworkChannel* getTCPConnectionByPort(uint16 port);
78  NetworkChannel* getUDPConnectionByPort(uint16 port);
79  uint64 addConnection(NetworkChannel* channel);
80 
81  bool sendUDPMessage(DataMessage* msg, uint64 destination);
82 
83  HTTPReply* makeHTTPRequest(const char* url, uint32 timeout, const char* content = NULL, uint32 contentSize = 0);
84  HTTPReply* makeHTTPRequest(HTTPRequest* req, const char* addr, uint16 port, uint8 encryption, uint32 timeout);
85 
86 protected:
87  std::map<uint32, NetworkChannel*> channels;
88  std::map<uint64, NetworkChannel*> channelsByConnection;
89  std::map<uint16, NetworkChannel*> listeners;
90  std::map<uint16, NetworkChannel*> udpListeners;
91 
92  std::string sslCertPath;
93  std::string sslKeyPath;
94 
95  UDPConnection* udpOutputCon;
96  utils::Mutex udpOutputConMutex;
97 
98  uint32 lastChannelID;
99  uint64 lastConnectionID;
100  bool run();
101 };
102 
104 // Connections
106 
107 #define NETWORKEVENT_CONNECT 1
108 #define NETWORKEVENT_RECONNECT 2
109 #define NETWORKEVENT_DISCONNECT 3
110 #define NETWORKEVENT_DISCONNECT_RETRYING 4
111 #define NETWORKEVENT_BUFFER_LOW 5
112 #define NETWORKEVENT_BUFFER_FULL 6
113 #define NETWORKEVENT_UNPROCESSED_DATA 7
114 
116 public:
117  NetworkThread(NetworkChannel* parent, uint64 id);
118  ~NetworkThread();
119 
120  uint64 id;
121  uint32 threadID;
122  uint16 port;
123  TCPListener* listener;
124  NetworkConnection* con;
125  uint32 defaultProtocol;
126  uint32 autoProtocols;
127  uint32 autoProtocolTimeout;
128  bool autoreconnect;
129  bool shouldContinue;
130  bool isRunning;
131  bool isAsync;
132  HTTPRequest* lastRequest;
133  NetworkChannel* parent;
134 };
135 
137  friend class NetworkManager;
138 public:
139  NetworkChannel(NetworkManager* manager);
140  virtual ~NetworkChannel();
141  uint32 cid;
142 
143  bool isConnected(uint64 conid);
144  // getStats();
145 
146  bool shutdown();
147 
148  bool startListener(uint64 cid, uint16 port, uint8 encryption, uint8 protocol, bool isAsync, uint32 protocolTimeout = 3000, bool isDefaultProtocol = false);
149  bool stopListener(uint16 port, uint8 protocol);
150 
151  uint64 createTCPConnection(const char* addr, uint16 port, uint8 encryption, uint8 protocol, bool isAsync, bool autoreconnect, uint64& location, uint32 timeoutMS = 5000);
152  uint64 createTCPConnection(uint64 location, uint8 encryption, uint8 protocol, bool isAsync, bool autoreconnect, uint32 timeoutMS = 5000);
153  uint64 createTCPConnection(const uint32* addresses, uint16 addressCount, uint16 port, uint8 encryption, uint8 protocol, bool isAsync, bool autoreconnect, uint64& location, uint32 timeoutMS = 5000);
154  uint64 createUDPConnection(uint16 port, uint8 protocol, bool isAsync, bool autoreconnect);
155  uint64 addTCPConnection(const char* addr, uint16 port, uint8 encryption, uint8 protocol, bool isAsync, uint64& location, uint32 timeoutMS = 5000, const char* greetingData = NULL, uint32 greetingSize = 0);
156  uint64 addTCPConnection(uint64 location, uint8 encryption, uint8 protocol, bool isAsync, uint32 timeoutMS = 5000, const char* greetingData = NULL, uint32 greetingSize = 0);
157  bool endConnection(uint64 conid);
158  bool endUDPConnection(uint16 port);
159  uint8 getConnectionType(uint64 conid);
160  uint64 getRemoteAddress(uint64 conid);
161 
162  uint64 autoDetectConnection(NetworkConnection* con, uint16 port, uint32 autoProtocols, uint32 autoProtocolTimeout, uint32 defaultProtocol, bool isAsync, bool autoreconnect);
163 
164  uint64 startConnection(NetworkConnection* con, uint8 protocol, bool isAsync, bool autoreconnect, uint32 timeoutMS = 5000);
165 
166  bool setNewReceiver(NetworkReceiver* recv);
167 
168  NetworkEvent* waitForNetworkEvent(uint32 ms);
169 
170  HTTPRequest* waitForHTTPRequest(uint64& conid, uint32 ms);
171  TelnetLine* waitForTelnetLine(uint64& conid, uint32 ms);
172  DataMessage* waitForMessage(uint64& conid, uint32 ms);
173  HTTPReply* waitForHTTPReply(uint64& conid, uint32 ms);
174 
175  bool sendHTTPReply(HTTPReply* reply, uint64 conid);
176  bool sendTelnetLine(TelnetLine* line, uint64 conid);
177  bool sendMessage(DataMessage* msg, uint64 conid);
178  bool sendHTTPRequest(HTTPRequest* req, uint64 conid);
179  HTTPReply* sendReceiveHTTPRequest(HTTPRequest* req, uint64 conid, uint32 timeout);
180  TelnetLine* sendReceiveTelnetLine(TelnetLine* line, uint64 conid, uint32 timeout, uint32 size = 0);
181 
182  uint32 getOutputSpeed(uint64 conid);
183  uint32 getInputSpeed(uint64 conid);
184 
185 protected:
186  NetworkManager* manager;
187  NetworkReceiver* receiver;
188  std::map<uint64, NetworkThread*> connectionThreads;
189  std::map<uint16, NetworkThread*> listeners;
190  std::map<uint16, NetworkThread*> udpListeners;
191 
192  std::queue<NetworkEvent*> eventQueue;
193  utils::Semaphore eventQueueSemaphore;
194  utils::Mutex eventQueueMutex;
195 
196  bool enterNetworkEvent(uint8 type, uint8 protocol, uint64 conid);
197 
198  bool enterHTTPRequest(HTTPRequest* req, uint64 conid);
199  bool enterHTTPReply(HTTPReply* reply, HTTPRequest* req, uint64 conid);
200  bool enterMessage(DataMessage* msg, uint64 conid);
201  bool enterTelnetLine(TelnetLine* line, uint64 conid);
202 
203  std::queue<HTTPRequest*> queueHTTPRequests;
204  utils::Semaphore queueHTTPRequestsSemaphore;
205  utils::Mutex queueHTTPRequestsMutex;
206  std::queue<HTTPReply*> queueHTTPReplies;
207  utils::Semaphore queueHTTPRepliesSemaphore;
208  utils::Mutex queueHTTPRepliesMutex;
209  std::queue<DataMessage*> queueMessages;
210  utils::Semaphore queueMessagesSemaphore;
211  utils::Mutex queueMessagesMutex;
212  std::queue<TelnetLine*> queueTelnetLines;
213  utils::Semaphore queueTelnetLinesSemaphore;
214  utils::Mutex queueTelnetLinesMutex;
215 
216  static THREAD_RET THREAD_FUNCTION_CALL NetworkListenerRun(THREAD_ARG arg);
217  static THREAD_RET THREAD_FUNCTION_CALL ConnectionAutodetectRun(THREAD_ARG arg);
218 
219  static THREAD_RET THREAD_FUNCTION_CALL HTTPClientRun(THREAD_ARG arg);
220  static THREAD_RET THREAD_FUNCTION_CALL HTTPServerRun(THREAD_ARG arg);
221  static THREAD_RET THREAD_FUNCTION_CALL MessageConnectionRun(THREAD_ARG arg);
222  static THREAD_RET THREAD_FUNCTION_CALL TelnetServerRun(THREAD_ARG arg);
223 };
224 
225 
226 }
227 
228 #endif // _NETWORKMANAGER_H_
Definition: NetworkProtocols.h:194
Definition: NetworkManager.h:47
Definition: Bitmap.h:7
Definition: NetworkConnections.h:66
Definition: NetworkManager.h:136
Definition: ThreadManager.h:33
Definition: NetworkProtocols.h:135
Definition: NetworkManager.h:115
Definition: NetworkConnections.h:153
Definition: Utils.h:299
Definition: NetworkManager.h:25
Definition: NetworkConnections.h:95
Definition: NetworkProtocols.h:81
Definition: NetworkManager.h:12
Definition: Utils.h:276
Definition: NetworkManager.h:35
Definition: DataMessage.h:95