TCPConnection.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef TCP_CONNECTION_H
  17. #define TCP_CONNECTION_H
  18. /*
  19. Parent classes for interserver TCP Communication.
  20. -Quagmire
  21. */
  22. #ifdef WIN32
  23. #define snprintf _snprintf
  24. #define vsnprintf _vsnprintf
  25. #define strncasecmp _strnicmp
  26. #define strcasecmp _stricmp
  27. #include <process.h>
  28. #else
  29. #include <pthread.h>
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #include <arpa/inet.h>
  33. #include <netdb.h>
  34. #include <unistd.h>
  35. #include <errno.h>
  36. #include <fcntl.h>
  37. #define INVALID_SOCKET -1
  38. #define SOCKET_ERROR -1
  39. #include "unix.h"
  40. #endif
  41. #include "types.h"
  42. #include "Mutex.h"
  43. #include "linked_list.h"
  44. #include "queue.h"
  45. #include "servertalk.h"
  46. #include "timer.h"
  47. #include "MiscFunctions.h"
  48. class TCPServer;
  49. #define TCPConnection_ErrorBufferSize 1024
  50. #define MaxTCPReceiveBufferSize 524288
  51. #define TCPS_Ready 0
  52. #define TCPS_Connecting 1
  53. #define TCPS_Connected 100
  54. #define TCPS_Disconnecting 200
  55. #define TCPS_Disconnected 201
  56. #define TCPS_Closing 250
  57. #define TCPS_Error 255
  58. #ifndef DEF_eConnectionType
  59. #define DEF_eConnectionType
  60. enum eConnectionType {Incomming, Outgoing};
  61. #endif
  62. #ifdef WIN32
  63. void TCPServerLoop(void* tmp);
  64. void TCPConnectionLoop(void* tmp);
  65. #else
  66. void* TCPServerLoop(void* tmp);
  67. void* TCPConnectionLoop(void* tmp);
  68. #endif
  69. enum eTCPMode { modeConsole, modeTransition, modePacket };
  70. class TCPConnection {
  71. public:
  72. #pragma pack(1)
  73. struct TCPNetPacket_Struct {
  74. int32 size;
  75. struct {
  76. int8
  77. compressed : 1,
  78. destination : 1,
  79. flag3 : 1,
  80. flag4 : 1,
  81. flag5 : 1,
  82. flag6 : 1,
  83. flag7 : 1,
  84. flag8 : 1;
  85. } flags;
  86. int16 opcode;
  87. uchar buffer[0];
  88. };
  89. #pragma pack()
  90. static TCPNetPacket_Struct* MakePacket(ServerPacket* pack, int32 iDestination = 0);
  91. TCPConnection(TCPServer* iServer, SOCKET iSock, int32 irIP, int16 irPort, bool iOldFormat = false);
  92. TCPConnection(bool iOldFormat = false, TCPServer* iRelayServer = 0, eTCPMode iMode = modePacket); // for outgoing connections
  93. TCPConnection(TCPServer* iServer, TCPConnection* iRelayLink, int32 iRemoteID, int32 irIP, int16 irPort); // for relay connections
  94. virtual ~TCPConnection();
  95. // Functions for outgoing connections
  96. bool Connect(char* irAddress, int16 irPort, char* errbuf = 0);
  97. bool Connect(int32 irIP, int16 irPort, char* errbuf = 0);
  98. void AsyncConnect(char* irAddress, int16 irPort);
  99. void AsyncConnect(int32 irIP, int16 irPort);
  100. virtual void Disconnect(bool iSendRelayDisconnect = true);
  101. virtual bool SendPacket(ServerPacket* pack, int32 iDestination = 0);
  102. virtual bool SendPacket(TCPNetPacket_Struct* tnps);
  103. bool Send(const uchar* data, sint32 size);
  104. char* PopLine();
  105. ServerPacket* PopPacket(); // OutQueuePop()
  106. inline int32 GetrIP() { return rIP; }
  107. inline int16 GetrPort() { return rPort; }
  108. virtual int8 GetState();
  109. eTCPMode GetMode() { return TCPMode; }
  110. inline bool Connected() { return (GetState() == TCPS_Connected); }
  111. inline bool ConnectReady() { return (bool) (GetState() == TCPS_Ready && ConnectionType == Outgoing); }
  112. void Free(); // Inform TCPServer that this connection object is no longer referanced
  113. inline int32 GetID() { return id; }
  114. inline bool IsRelayServer() { return RelayServer; }
  115. inline int32 GetRemoteID() { return RemoteID; }
  116. inline TCPConnection* GetRelayLink() { return RelayLink; }
  117. bool GetEcho();
  118. void SetEcho(bool iValue);
  119. protected:
  120. friend class TCPServer;
  121. virtual bool Process();
  122. void SetState(int8 iState);
  123. inline bool IsFree() { return pFree; }
  124. bool CheckNetActive();
  125. #ifdef WIN32
  126. friend void TCPConnectionLoop(void* tmp);
  127. #else
  128. friend void* TCPConnectionLoop(void* tmp);
  129. #endif
  130. SOCKET sock;
  131. bool RunLoop();
  132. Mutex MLoopRunning;
  133. Mutex MAsyncConnect;
  134. bool GetAsyncConnect();
  135. bool SetAsyncConnect(bool iValue);
  136. char* charAsyncConnect;
  137. #ifdef WIN32
  138. friend class TCPConnection;
  139. #endif
  140. void OutQueuePush(ServerPacket* pack);
  141. void RemoveRelay(TCPConnection* relay, bool iSendRelayDisconnect);
  142. private:
  143. void ProcessNetworkLayerPacket(ServerPacket* pack);
  144. void SendNetErrorPacket(const char* reason = 0);
  145. TCPServer* Server;
  146. TCPConnection* RelayLink;
  147. int32 RemoteID;
  148. sint32 RelayCount;
  149. bool pOldFormat;
  150. bool SendData(char* errbuf = 0);
  151. bool RecvData(char* errbuf = 0);
  152. bool ProcessReceivedData(char* errbuf = 0);
  153. bool ProcessReceivedDataAsPackets(char* errbuf = 0);
  154. bool ProcessReceivedDataAsOldPackets(char* errbuf = 0);
  155. void ClearBuffers();
  156. bool pAsyncConnect;
  157. eConnectionType ConnectionType;
  158. eTCPMode TCPMode;
  159. bool RelayServer;
  160. Mutex MRunLoop;
  161. bool pRunLoop;
  162. SOCKET connection_socket;
  163. int32 id;
  164. int32 rIP;
  165. int16 rPort; // host byte order
  166. bool pFree;
  167. Mutex MState;
  168. int8 pState;
  169. void LineOutQueuePush(char* line);
  170. MyQueue<char> LineOutQueue;
  171. MyQueue<ServerPacket> OutQueue;
  172. Mutex MOutQueueLock;
  173. Timer* keepalive_timer;
  174. Timer* timeout_timer;
  175. uchar* recvbuf;
  176. sint32 recvbuf_size;
  177. sint32 recvbuf_used;
  178. sint32 recvbuf_echo;
  179. bool pEcho;
  180. Mutex MEcho;
  181. void InModeQueuePush(TCPNetPacket_Struct* tnps);
  182. MyQueue<TCPNetPacket_Struct> InModeQueue;
  183. Mutex MSendQueue;
  184. uchar* sendbuf;
  185. sint32 sendbuf_size;
  186. sint32 sendbuf_used;
  187. bool ServerSendQueuePop(uchar** data, sint32* size);
  188. void ServerSendQueuePushEnd(const uchar* data, sint32 size);
  189. void ServerSendQueuePushEnd(uchar** data, sint32 size);
  190. void ServerSendQueuePushFront(uchar* data, sint32 size);
  191. };
  192. class TCPServer {
  193. public:
  194. TCPServer(int16 iPort = 0, bool iOldFormat = false);
  195. virtual ~TCPServer();
  196. bool Open(int16 iPort = 0, char* errbuf = 0); // opens the port
  197. void Close(); // closes the port
  198. bool IsOpen();
  199. inline int16 GetPort() { return pPort; }
  200. TCPConnection* NewQueuePop();
  201. void SendPacket(ServerPacket* pack);
  202. void SendPacket(TCPConnection::TCPNetPacket_Struct** tnps);
  203. protected:
  204. #ifdef WIN32
  205. friend void TCPServerLoop(void* tmp);
  206. #else
  207. friend void* TCPServerLoop(void* tmp);
  208. #endif
  209. void Process();
  210. bool RunLoop();
  211. Mutex MLoopRunning;
  212. friend class TCPConnection;
  213. inline int32 GetNextID() { return NextID++; }
  214. void AddConnection(TCPConnection* con);
  215. TCPConnection* GetConnection(int32 iID);
  216. private:
  217. void ListenNewConnections();
  218. int32 NextID;
  219. bool pOldFormat;
  220. Mutex MRunLoop;
  221. bool pRunLoop;
  222. Mutex MSock;
  223. SOCKET sock;
  224. int16 pPort;
  225. Mutex MNewQueue;
  226. MyQueue<TCPConnection> NewQueue;
  227. void CheckInQueue();
  228. Mutex MInQueue;
  229. TCPConnection::TCPNetPacket_Struct* InQueuePop();
  230. MyQueue<TCPConnection::TCPNetPacket_Struct> InQueue;
  231. LinkedList<TCPConnection*>* list;
  232. };
  233. #endif