SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TCPInterface.h
Go to the documentation of this file.
1 /*
2 * Original work: Copyright (c) 2014, Oculus VR, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2016-2017, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
19
20
21 #include "NativeFeatureIncludes.h"
22 #if _RAKNET_SUPPORT_TCPInterface==1
23
24 #ifndef __SIMPLE_TCP_SERVER
25 #define __SIMPLE_TCP_SERVER
26
27 #include "memoryoverride.h"
28 #include "DS_List.h"
29 #include "types.h"
30 #include "Export.h"
31 #include "thread.h"
32 #include "DS_Queue.h"
33 #include "SimpleMutex.h"
34 #include "defines.h"
35 #include "SocketIncludes.h"
36 #include "DS_ByteQueue.h"
37 #include "DS_ThreadsafeAllocatingQueue.h"
38 #include "LocklessTypes.h"
39 #include "PluginInterface2.h"
40
41 #if OPEN_SSL_CLIENT_SUPPORT==1
42 #include <openssl/crypto.h>
43 #include <openssl/x509.h>
44 #include <openssl/pem.h>
45 #include <openssl/ssl.h>
46 #include <openssl/err.h>
47 #endif
48
49 namespace SLNet
50 {
52 struct RemoteClient;
53
56 class RAK_DLL_EXPORT TCPInterface
57 {
58 public:
59 // GetInstance() and DestroyInstance(instance*)
60 STATIC_FACTORY_DECLARATIONS(TCPInterface)
61
62 TCPInterface();
63 virtual ~TCPInterface();
64
65 // TODO - add socketdescriptor
72 bool Start(unsigned short port, unsigned short maxIncomingConnections, unsigned short maxConnections=0, int _threadPriority=-99999, unsigned short socketFamily=AF_INET, const char *bindAddress=0);
73
75 void Stop(void);
76
78 SystemAddress Connect(const char* host, unsigned short remotePort, bool block=true, unsigned short socketFamily=AF_INET, const char *bindAddress=0);
79
80 #if OPEN_SSL_CLIENT_SUPPORT==1
81
82 void StartSSLClient(SystemAddress systemAddress);
83
85 bool IsSSLActive(SystemAddress systemAddress);
86 #endif
87
89 virtual void Send( const char *data, unsigned int length, const SystemAddress &systemAddress, bool broadcast );
90
91 // Sends a concatenated list of byte streams
92 virtual bool SendList( const char **data, const unsigned int *lengths, const int numParameters, const SystemAddress &systemAddress, bool broadcast );
93
94 // Get how many bytes are waiting to be sent. If too many, you may want to skip sending
95 unsigned int GetOutgoingDataBufferSize(SystemAddress systemAddress) const;
96
99 virtual bool ReceiveHasPackets( void );
100
102 virtual Packet* Receive( void );
103
105 void CloseConnection( SystemAddress systemAddress );
106
108 void DeallocatePacket( Packet *packet );
109
113 void GetConnectionList( SystemAddress *remoteSystems, unsigned short *numberOfSystems ) const;
114
116 unsigned short GetConnectionCount(void) const;
117
120 SystemAddress HasCompletedConnectionAttempt(void);
121
124 SystemAddress HasFailedConnectionAttempt(void);
125
127 SystemAddress HasNewIncomingConnection(void);
128
130 SystemAddress HasLostConnection(void);
131
133 Packet* AllocatePacket(unsigned dataSize);
134
135 // Push a packet back to the queue
136 virtual void PushBackPacket( Packet *packet, bool pushAtHead );
137
139 bool WasStarted(void) const;
140
141 void AttachPlugin( PluginInterface2 *plugin );
142 void DetachPlugin( PluginInterface2 *plugin );
143 protected:
144
145 Packet* ReceiveInt( void );
146
147 #if defined(WINDOWS_STORE_RT)
148 bool CreateListenSocket_WinStore8(unsigned short port, unsigned short maxIncomingConnections, unsigned short socketFamily, const char *hostAddress);
149 #else
150 bool CreateListenSocket(unsigned short port, unsigned short maxIncomingConnections, unsigned short socketFamily, const char *hostAddress);
151 #endif
152
153 // Plugins
154 DataStructures::List<PluginInterface2*> messageHandlerList;
155
156 SLNet::LocklessUint32_t isStarted, threadRunning;
157 __TCPSOCKET__ listenSocket;
158
159 DataStructures::Queue<Packet*> headPush, tailPush;
160 RemoteClient* remoteClients;
161 unsigned short remoteClientsLength;
162
163 // Assuming remoteClients is only used by one thread!
164 // DataStructures::List<RemoteClient*> remoteClients;
165 // Use this thread-safe queue to add to remoteClients
166 // DataStructures::Queue<RemoteClient*> remoteClientsInsertionQueue;
167 // SimpleMutex remoteClientsInsertionQueueMutex;
168
169 /*
170 struct OutgoingMessage
171 {
172 unsigned char* data;
173 SystemAddress systemAddress;
174 bool broadcast;
175 unsigned int length;
176 };
177 */
178 // DataStructures::SingleProducerConsumer<OutgoingMessage> outgoingMessages;
179 // DataStructures::SingleProducerConsumer<Packet> incomingMessages;
180 // DataStructures::SingleProducerConsumer<SystemAddress> newIncomingConnections, lostConnections, requestedCloseConnections;
181 // DataStructures::SingleProducerConsumer<RemoteClient*> newRemoteClients;
182 // DataStructures::ThreadsafeAllocatingQueue<OutgoingMessage> outgoingMessages;
183 DataStructures::ThreadsafeAllocatingQueue<Packet> incomingMessages;
184 DataStructures::ThreadsafeAllocatingQueue<SystemAddress> newIncomingConnections, lostConnections, requestedCloseConnections;
185 DataStructures::ThreadsafeAllocatingQueue<RemoteClient*> newRemoteClients;
186 SimpleMutex completedConnectionAttemptMutex, failedConnectionAttemptMutex;
187 DataStructures::Queue<SystemAddress> completedConnectionAttempts, failedConnectionAttempts;
188
189 int threadPriority;
190
191 DataStructures::List<__TCPSOCKET__> blockingSocketList;
192 SimpleMutex blockingSocketListMutex;
193
194
195
196
197
198 friend RAK_THREAD_DECLARATION(UpdateTCPInterfaceLoop);
199 friend RAK_THREAD_DECLARATION(ConnectionAttemptLoop);
200
201 // void DeleteRemoteClient(RemoteClient *remoteClient, fd_set *exceptionFD);
202 // void InsertRemoteClient(RemoteClient* remoteClient);
203 __TCPSOCKET__ SocketConnect(const char* host, unsigned short remotePort, unsigned short socketFamily, const char *bindAddress);
204
205 struct ThisPtrPlusSysAddr
206 {
207 TCPInterface *tcpInterface;
208 SystemAddress systemAddress;
209 bool useSSL;
210 char bindAddress[64];
211 unsigned short socketFamily;
212 };
213
214 #if OPEN_SSL_CLIENT_SUPPORT==1
215 SSL_CTX* ctx;
216 SSL_METHOD *meth;
217 DataStructures::ThreadsafeAllocatingQueue<SystemAddress> startSSL;
218 DataStructures::List<SystemAddress> activeSSLConnections;
219 SimpleMutex sharedSslMutex;
220 #endif
221 };
222
224 struct RemoteClient
225 {
226 RemoteClient() {
227 #if OPEN_SSL_CLIENT_SUPPORT==1
228 ssl=0;
229 #endif
230 isActive=false;
231 #if !defined(WINDOWS_STORE_RT)
232 socket=0;
233 #endif
234 }
235 __TCPSOCKET__ socket;
236 SystemAddress systemAddress;
237 DataStructures::ByteQueue outgoingData;
238 bool isActive;
239 SimpleMutex outgoingDataMutex;
240 SimpleMutex isActiveMutex;
241
242 #if OPEN_SSL_CLIENT_SUPPORT==1
243 SSL* ssl;
244 bool InitSSL(SSL_CTX* ctx, SSL_METHOD *meth);
245 void DisconnectSSL(void);
246 void FreeSSL(void);
247 int Send(const char *data, unsigned int length);
248 int Recv(char *data, const int dataSize);
249 #else
250 int Send(const char *data, unsigned int length);
251 int Recv(char *data, const int dataSize);
252 #endif
253 void Reset(void)
254 {
255 outgoingDataMutex.Lock();
256 outgoingData.Clear(_FILE_AND_LINE_);
257 outgoingDataMutex.Unlock();
258 }
259 void SetActive(bool a);
260 void SendOrBuffer(const char **data, const unsigned int *lengths, const int numParameters);
261 };
262
263 } // namespace SLNet
264
265 #endif
266
267 #endif // _RAKNET_SUPPORT_*
268