Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include "NativeFeatureIncludes.h"
00009 #if _RAKNET_SUPPORT_TCPInterface==1
00010
00011 #ifndef __SIMPLE_TCP_SERVER
00012 #define __SIMPLE_TCP_SERVER
00013
00014 #include "RakMemoryOverride.h"
00015 #include "DS_List.h"
00016 #include "RakNetTypes.h"
00017 #include "Export.h"
00018 #include "RakThread.h"
00019 #include "DS_Queue.h"
00020 #include "SimpleMutex.h"
00021 #include "RakNetDefines.h"
00022 #include "SocketIncludes.h"
00023 #include "DS_ByteQueue.h"
00024 #include "DS_ThreadsafeAllocatingQueue.h"
00025
00026 struct RemoteClient;
00027
00028 #if defined(OPEN_SSL_CLIENT_SUPPORT)
00029 #include <openssl/crypto.h>
00030 #include <openssl/x509.h>
00031 #include <openssl/pem.h>
00032 #include <openssl/ssl.h>
00033 #include <openssl/err.h>
00034 #endif
00035
00038 class RAK_DLL_EXPORT TCPInterface
00039 {
00040 public:
00041 TCPInterface();
00042 virtual ~TCPInterface();
00043
00046 bool Start(unsigned short port, unsigned short maxIncomingConnections, unsigned short maxConnections=0, int _threadPriority=-99999);
00047
00049 void Stop(void);
00050
00052 SystemAddress Connect(const char* host, unsigned short remotePort, bool block=true);
00053
00054 #if defined(OPEN_SSL_CLIENT_SUPPORT)
00055
00056 void StartSSLClient(SystemAddress systemAddress);
00057
00059 bool IsSSLActive(SystemAddress systemAddress);
00060 #endif
00061
00063 void Send( const char *data, unsigned int length, SystemAddress systemAddress, bool broadcast );
00064
00065
00066 bool SendList( const char **data, const unsigned int *lengths, const int numParameters, SystemAddress systemAddress, bool broadcast );
00067
00068
00069 unsigned int GetOutgoingDataBufferSize(SystemAddress systemAddress) const;
00070
00072 bool ReceiveHasPackets( void );
00073
00075 Packet* Receive( void );
00076
00078 void CloseConnection( SystemAddress systemAddress );
00079
00081 void DeallocatePacket( Packet *packet );
00082
00086 void GetConnectionList( SystemAddress *remoteSystems, unsigned short *numberOfSystems ) const;
00087
00089 unsigned short GetConnectionCount(void) const;
00090
00093 SystemAddress HasCompletedConnectionAttempt(void);
00094
00097 SystemAddress HasFailedConnectionAttempt(void);
00098
00100 SystemAddress HasNewIncomingConnection(void);
00101
00103 SystemAddress HasLostConnection(void);
00104
00106 Packet* AllocatePacket(unsigned dataSize);
00107
00108
00109 virtual void PushBackPacket( Packet *packet, bool pushAtHead );
00110 protected:
00111
00112 bool isStarted, threadRunning;
00113 SOCKET listenSocket;
00114
00115 DataStructures::Queue<Packet*> headPush, tailPush;
00116 RemoteClient* remoteClients;
00117 int remoteClientsLength;
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 DataStructures::ThreadsafeAllocatingQueue<Packet> incomingMessages;
00140 DataStructures::ThreadsafeAllocatingQueue<SystemAddress> newIncomingConnections, lostConnections, requestedCloseConnections;
00141 DataStructures::ThreadsafeAllocatingQueue<RemoteClient*> newRemoteClients;
00142 SimpleMutex completedConnectionAttemptMutex, failedConnectionAttemptMutex;
00143 DataStructures::Queue<SystemAddress> completedConnectionAttempts, failedConnectionAttempts;
00144
00145 int threadPriority;
00146
00147 DataStructures::List<SOCKET> blockingSocketList;
00148 SimpleMutex blockingSocketListMutex;
00149
00150 friend RAK_THREAD_DECLARATION(UpdateTCPInterfaceLoop);
00151 friend RAK_THREAD_DECLARATION(ConnectionAttemptLoop);
00152
00153
00154
00155 SOCKET SocketConnect(const char* host, unsigned short remotePort);
00156
00157 struct ThisPtrPlusSysAddr
00158 {
00159 TCPInterface *tcpInterface;
00160 SystemAddress systemAddress;
00161 bool useSSL;
00162 };
00163
00164 #if defined(OPEN_SSL_CLIENT_SUPPORT)
00165 SSL_CTX* ctx;
00166 SSL_METHOD *meth;
00167 DataStructures::ThreadsafeAllocatingQueue<SystemAddress> startSSL;
00168 DataStructures::List<SystemAddress> activeSSLConnections;
00169 #endif
00170 };
00171
00173 struct RemoteClient
00174 {
00175 RemoteClient() {
00176 #if defined(OPEN_SSL_CLIENT_SUPPORT)
00177 ssl=0;
00178 #endif
00179 isActive=false;
00180 socket=INVALID_SOCKET;
00181 }
00182 SOCKET socket;
00183 SystemAddress systemAddress;
00184 DataStructures::ByteQueue outgoingData;
00185 bool isActive;
00186 SimpleMutex outgoingDataMutex;
00187 SimpleMutex isActiveMutex;
00188
00189 #if defined(OPEN_SSL_CLIENT_SUPPORT)
00190 SSL* ssl;
00191 void InitSSL(SSL_CTX* ctx, SSL_METHOD *meth);
00192 void DisconnectSSL(void);
00193 void FreeSSL(void);
00194 int Send(const char *data, unsigned int length);
00195 int Recv(char *data, const int dataSize);
00196 #else
00197 int Send(const char *data, unsigned int length);
00198 int Recv(char *data, const int dataSize);
00199 #endif
00200 void Reset(void)
00201 {
00202 outgoingDataMutex.Lock();
00203 outgoingData.Clear(__FILE__,__LINE__);
00204 outgoingDataMutex.Unlock();
00205 }
00206 void SetActive(bool a);
00207 void SendOrBuffer(const char **data, const unsigned int *lengths, const int numParameters);
00208 };
00209
00210 #endif
00211
00212 #endif // _RAKNET_SUPPORT_*
00213