ReplicaNet and RNLobby  1
XPSocket.h
1 /* START_LICENSE_HEADER
2 
3 Copyright (C) 2000 Martin Piper, original design and program code
4 Copyright (C) 2001 Replica Software
5 
6 This program file is copyright (C) Replica Software and can only be used under license.
7 For more information visit: http://www.replicanet.com/
8 Or email: info@replicanet.com
9 
10 END_LICENSE_HEADER */
11 #include "RNPlatform/Inc/MemoryTracking.h"
12 /* XPSocket.h */
13 #ifndef _XPSOCK_H_
14 #define _XPSOCK_H_
15 
16 #include <string>
17 #include "RNPlatform/Inc/MessageHelper.h"
18 
19 namespace RNReplicaNet
20 {
21 
22 #define _XPSOCK_VERSION 1700
23 
24 
25 /* Some error return defines */
29 #define XPSOCK_EOK (0)
30 #define XPSOCK_EERROR (-1)
31 #define XPSOCK_EWOULDBLOCK (-2)
32 #define XPSOCK_ECLOSED (-3)
33 #define XPSOCK_ENOEXIST (-4)
34 #define XPSOCK_ENOMEMORY (-5)
35 #define XPSOCK_ENOTASOCKET (-6)
36 #define XPSOCK_ECANTLISTEN (-7)
37 #define XPSOCK_ENOTALISTEN (-8)
38 #define XPSOCK_ENOTCONNECTED (-9)
39 #define XPSOCK_EBUFFEROVERFLOW (-10)
40 #define XPSOCK_EUDPSENDFAILED (-11)
41 #define XPSOCK_ECANTBIND (-12)
42 #define XPSOCK_ECANTRESOLVE (-13)
43 #define XPSOCK_EURGENTTOOBIG (-14)
44 #define XPSOCK_BUFFERTOOSMALL (-15)
45 #define XPSOCK_ILLEGALOPERATION (-16)
46 #define XPSOCK_ECONNINPROGRESS (-17)
47 #define XPSOCK_ESTACKSTARTFAILURE (-18)
48 #define XPSOCK_ESTACKNOTSTARTED (-19)
50 #define pXPSOCK_EERROR ((RNReplicaNet::t_XPSocket *) XPSOCK_EERROR)
52 /* Probable XPSocket types */
53 #define XPSOCK_TLOCALHOST (0)
54 #define XPSOCK_TINET (1)
55 
56 /* XPSocket status */
57 #define XPSOCK_SNULL (0)
58 #define XPSOCK_SLISTEN (1)
59 #define XPSOCK_SCONNECT (2)
60 #define XPSOCK_SCLOSE (3)
61 
62 /* Flags to pass to XPSock_Recv */
63 #define XPSOCK_FNULL (0)
64 #define XPSOCK_FPEEK (1)
67 #define XPSOCK_PORT_ANY (0)
68 #define XPSOCK_PORT_LOCAL (-1)
70 #define XPSOCK_MAXPACKETSIZE (8192)
72 #define XPSOCK_URGENTMAXPACKETSIZE (2048)
74 /* Some constants for the local loop back sockets */
75 #define XPSOCK_LOOPBACKBUFFER 20480
76 
77 /* Flags returned by XPSock_GetHardwareStatus */
78 #define XPSOCK_HARDWARE_AVAILABLE (1<<0)
79 #define XPSOCK_HARDWARE_CABLE_CONNECTED (1<<1)
80 #define XPSOCK_HARDWARE_10MBS (1<<2)
81 #define XPSOCK_HARDWARE_100MBS (1<<3)
82 #define XPSOCK_HARDWARE_FULL_DUPLEX (1<<4)
83 #define XPSOCK_HARDWARE_HALF_DUPLEX (1<<5)
84 
85 
86 /* Structs and their type defs */
87 
91 struct XPAddress
92 {
96  XPAddress(const int port,unsigned char addr1,unsigned char addr2,unsigned char addr3,unsigned char addr4);
97 
101  XPAddress(const int port);
102 
106  XPAddress(std::string addr);
107 
111  XPAddress();
112 
117  std::string Export(void) const;
118 
125  bool Import(const std::string address);
126 
130  void MakeAny(const int port = XPSOCK_PORT_ANY);
131 
135  static XPAddress Null(void);
136 
140  bool IsLocalhost(void) const;
141 
142  int port;
143  unsigned char addr[4];
145  int port6;
146  unsigned char addr6[16];
148  bool mIPv4;
149  bool mIPv6;
150 
151  // Useful operators to compare addresses
152  bool operator== (const XPAddress& b) const;
153  bool operator!= (const XPAddress& b) const;
154  // Can be used with STL less<> tests
155  bool operator< (const XPAddress& b) const;
156 
157 };
158 
159 typedef struct XPAddress t_XPAddress;
160 typedef struct XPAddress XPAddress;
161 
162 void operator<< (RNReplicaNet::DynamicMessageHelper &message,const XPAddress &a);
163 void operator>> (RNReplicaNet::DynamicMessageHelper &message,XPAddress &a);
164 
165 typedef struct s_XPSocket t_XPSocket;
166 
167 typedef struct s_XPSocket
168 {
169  int type;
170  int state;
175  int urgent;
177 #ifdef _WIN64
178  unsigned __int64 wins_socket;
179  unsigned __int64 wins_socket6;
180 #else
183 #endif
189  int port;
190  int port6;
191  int mCounter;
192  t_XPAddress mCachedAddress;
193  bool mCachedAddressGot;
194 } t_XPSocket;
196 typedef struct s_XPErrorTranslate
197 {
198  int error;
199  const char *text;
200  const char *longtext;
203 /* Macros to make life easier */
204 #define XPSOCK_PARANOID(x) if ((x) == NULL || (x) == pXPSOCK_EERROR)\
205  {\
206  asocket_lasterror = XPSOCK_ENOTASOCKET;\
207  return XPSOCK_EERROR;\
208  }
209 
210 
211 #define XPSOCK_S_PARANOID(x) if ((x) == NULL || (x) == pXPSOCK_EERROR)\
212  {\
213  asocket_lasterror = XPSOCK_ENOTASOCKET;\
214  return (t_XPSocket *) XPSOCK_EERROR;\
215  }
216 
217 } // namespace RNXPSockets
218 
219 // Return error values are either pXPSOCK_EERROR, XPSOCK_EERROR or pXPSOCK_EOK
220 
221 /* All the global functions */
222 extern int XPSock_Init(void);
223 extern int XPSock_Quit(void);
225 extern int XPSock_SetSendBuffer(RNReplicaNet::t_XPSocket * /*socket*/,int /*size*/);
226 extern int XPSock_SetRecvBuffer(RNReplicaNet::t_XPSocket * /*socket*/,int /*size*/);
227 extern int XPSock_Connect(RNReplicaNet::t_XPSocket * /*socket*/,const RNReplicaNet::t_XPAddress * /*addr*/);
228 extern int XPSock_Send(RNReplicaNet::t_XPSocket * /*socket*/,const char * /*data*/,const int /*len*/);
229 extern int XPSock_Recv(RNReplicaNet::t_XPSocket * /*socket*/,char * /*data*/,int /*maxlen*/,int /*flag*/);
230 extern int XPSock_Close(RNReplicaNet::t_XPSocket * /*socket*/);
231 extern int XPSock_Listen(RNReplicaNet::t_XPSocket * /*socket*/,int /*port*/);
232 extern int XPSock_Listen(RNReplicaNet::t_XPSocket * /*socket*/,const RNReplicaNet::t_XPAddress * /*addr*/);
234 
235 extern int XPSock_GetAddress(RNReplicaNet::t_XPSocket * /*socket*/,RNReplicaNet::t_XPAddress * /*addr*/);
237 
238 extern RNReplicaNet::t_XPSocket *XPSock_UrgentCreate(int /*port*/);
241 extern int XPSock_UrgentSend(RNReplicaNet::t_XPSocket * /*socket*/,const char * /*data*/,int /*len*/,const RNReplicaNet::t_XPAddress * /*addr*/);
242 extern int XPSock_UrgentRecv(RNReplicaNet::t_XPSocket * /*socket*/,char *const /*data*/,int /*maxlen*/,int /*flag*/,RNReplicaNet::t_XPAddress *const /*addr*/);
243 
244 extern RNReplicaNet::t_XPSocket *XPSock_ICMPCreate(void);
245 extern int XPSock_ICMPSendPing(RNReplicaNet::t_XPSocket * /*socket*/,const RNReplicaNet::t_XPAddress * /*addr*/,unsigned short *retID,unsigned short *retSeq,unsigned int *retTimeSent);
246 extern int XPSock_ICMPRecvPing(RNReplicaNet::t_XPSocket * /*socket*/,RNReplicaNet::t_XPAddress *const /*addr*/,unsigned short *retID,unsigned short *retSeq,unsigned int *retTimeElapsed);
247 extern int XPSock_ICMPWaitForPing(RNReplicaNet::t_XPSocket * socket,const float timeoutSeconds = 1.0f);
248 
249 extern int XPSock_GetSocketSelectMaxSize(void);
250 extern int XPSock_SocketSelect(RNReplicaNet::t_XPSocket **sockets,const int numSockets, const int milliseconds);
251 
252 extern const char *XPSock_TranslateError(int /*errno*/);
253 extern const char *XPSock_TranslateErrorLong(int /*errno*/);
254 
255 extern int XPSock_GetHostName(char * /*name*/,int /*name_len*/);
256 extern int XPSock_GetHostAddress(RNReplicaNet::XPAddress * /*addr*/);
257 extern int XPSock_Resolve(RNReplicaNet::t_XPAddress * /*resolve*/,const char * /*input*/);
258 
259 extern void XPSock_Poll(void);
260 
261 extern void XPSock_SentHistogram(unsigned int * /*classes*/,int /*numClasses*/);
262 extern void XPSock_RecvHistogram(unsigned int * /*classes*/,int /*numClasses*/);
263 
264 extern int XPSock_GetHardwareStatus(void);
265 
266 // The global variables
267 extern int asocket_lasterror;
269 /* Extern the counters */
270 extern int asocket_packets_got;
271 extern int asocket_packets_sent;
272 extern int asocket_packets_got_size;
273 extern int asocket_packets_sent_size;
275 extern void XPSock_UpdatePerSecondMetrics(void);
276 extern float asocket_packets_got_per_second;
277 extern float asocket_packets_sent_per_second;
281 // Only use these if you know exactly what you are doing
282 extern bool gXPSock_PacketLossEmulation;
283 extern float gXPSock_PacketLossEmulationPercent;
284 extern bool gXPSock_PacketLatencyEmulation;
285 extern float gXPSock_PacketLatencyEmulationBase;
286 extern float gXPSock_PacketLatencyEmulationJitter;
287 extern int gXPSock_UDPProtoParameter;
288 
289 #endif
290 
void XPSock_UpdatePerSecondMetrics(void)
Definition: XPSocket.cpp:359
float asocket_packets_got_per_second
Definition: XPSocket.cpp:241
int port6
Definition: XPSocket.h:190
int mCounter
Definition: XPSocket.h:191
std::string Export(void) const
Definition: XPSocket.cpp:2849
int asocket_packets_got
Definition: XPSocket.cpp:229
int wins_socket6
Definition: XPSocket.h:182
int state
Definition: XPSocket.h:170
int XPSock_GetHostName(char *name, int name_len)
Definition: XPSocket.cpp:2223
int asocket_packets_got_size
Definition: XPSocket.cpp:231
int total_connects
Definition: XPSocket.h:174
int port
Definition: XPSocket.h:142
int XPSock_GetHardwareStatus(void)
Definition: XPSocket.cpp:2969
t_XPAddress addr
Definition: XPSocket.h:176
Definition: XPSocket.h:91
const char * XPSock_TranslateError(int error)
Definition: XPSocket.cpp:2167
int loopback_pending
Definition: XPSocket.h:188
t_XPSocket * loopback_socket
Definition: XPSocket.h:184
int XPSock_Send(t_XPSocket *socket, const char *data, const int len)
Definition: XPSocket.cpp:841
int XPSock_GetHostAddress(XPAddress *addr)
Definition: XPSocket.cpp:2316
int error
Definition: XPSocket.h:198
float asocket_packets_got_size_per_second
Definition: XPSocket.cpp:239
int port6
Definition: XPSocket.h:145
float asocket_packets_sent_per_second
Definition: XPSocket.cpp:242
bool Import(const std::string address)
Definition: XPSocket.cpp:2675
int XPSock_GetAddress(t_XPSocket *socket, t_XPAddress *addr)
Definition: XPSocket.cpp:1368
bool IsLocalhost(void) const
Definition: XPSocket.cpp:2920
static XPAddress Null(void)
Definition: XPSocket.cpp:2913
Definition: XPSocket.h:196
const char * text
Definition: XPSocket.h:199
int loopback_recvpos
Definition: XPSocket.h:186
int XPSock_Close(t_XPSocket *socket)
Definition: XPSocket.cpp:1067
int XPSock_Connect(t_XPSocket *socket, const t_XPAddress *addr)
Definition: XPSocket.cpp:745
t_XPSocket * XPSock_Accept(t_XPSocket *socket)
Definition: XPSocket.cpp:1241
int XPSock_GetPeerAddress(t_XPSocket *socket, t_XPAddress *addr)
Definition: XPSocket.cpp:1490
int asocket_packets_sent_size
Definition: XPSocket.cpp:232
int XPSock_Init(void)
Definition: XPSocket.cpp:454
int urgent
Definition: XPSocket.h:175
int XPSock_SetRecvBuffer(t_XPSocket *socket, int size)
Definition: XPSocket.cpp:705
int asocket_packets_sent
Definition: XPSocket.cpp:230
int asocket_lasterror
Definition: XPSocket.cpp:193
int XPSock_UrgentSend(t_XPSocket *socket, const char *data, int len, const t_XPAddress *addr)
Definition: XPSocket.cpp:1903
int XPSock_Resolve(t_XPAddress *resolve, const char *input)
Definition: XPSocket.cpp:2344
int total_accepts
Definition: XPSocket.h:173
int loopback_readpos
Definition: XPSocket.h:187
Definition: XPSocket.h:167
XPAddress()
Definition: XPSocket.cpp:2558
int XPSock_UrgentRecv(t_XPSocket *socket, char *const data, int maxlen, int flag, t_XPAddress *const addr)
Definition: XPSocket.cpp:2006
unsigned char addr6[16]
Definition: XPSocket.h:146
int XPSock_UrgentMakeBroadcast(t_XPSocket *socket)
Definition: XPSocket.cpp:1754
int port
Definition: XPSocket.h:189
t_XPSocket * XPSock_Create(void)
Definition: XPSocket.cpp:604
int type
Definition: XPSocket.h:169
int total_received
Definition: XPSocket.h:172
int XPSock_Listen(t_XPSocket *socket, int port)
Definition: XPSocket.cpp:1137
int XPSock_Quit(void)
Definition: XPSocket.cpp:552
int wins_socket
Definition: XPSocket.h:181
unsigned char addr[4]
Definition: XPSocket.h:143
int XPSock_SetSendBuffer(t_XPSocket *socket, int size)
Definition: XPSocket.cpp:666
Definition: MessageHelper.h:211
t_XPSocket * XPSock_UrgentCreate(int port)
Definition: XPSocket.cpp:1553
int XPSock_Recv(t_XPSocket *socket, char *data, int maxlen, int flag)
Definition: XPSocket.cpp:957
int total_sent
Definition: XPSocket.h:171
void XPSock_SentHistogram(unsigned int *classes, int numClasses)
Definition: XPSocket.cpp:2943
const char * longtext
Definition: XPSocket.h:200
const char * XPSock_TranslateErrorLong(int error)
Definition: XPSocket.cpp:2192
void XPSock_RecvHistogram(unsigned int *classes, int numClasses)
Definition: XPSocket.cpp:2956
void MakeAny(const int port=XPSOCK_PORT_ANY)
Definition: XPSocket.cpp:2880
char * loopback_recvbuffer
Definition: XPSocket.h:185
float asocket_packets_sent_size_per_second
Definition: XPSocket.cpp:240