ReplicaNet and RNLobby  1
Classes | Macros | Functions | Variables
XPSocket.cpp File Reference
#include "RNPlatform/Inc/MemoryTracking.h"
#include <stdlib.h>
#include <assert.h>
#include "RNPlatform/Inc/PlatformHeap.h"
#include "RNPlatform/Inc/Rand.h"
#include <stdio.h>
#include <string.h>
#include "RNXPSockets/debug.h"
#include "RNXPSockets/Inc/XPSocket.h"
#include "RNPlatform/Inc/ThreadClass.h"
#include "RNPlatform/Inc/SysTime.h"
#include <list>
#include "RNXPSockets/Ping.h"
Include dependency graph for XPSocket.cpp:

Classes

class  XPSocketsHeapBlock
 
struct  ImpairedPacket
 

Macros

#define RNmax(a, b)   (((a) > (b)) ? (a) : (b))
 
#define RNmin(a, b)   (((a) < (b)) ? (a) : (b))
 
#define closesocket   close
 
#define ioctlsocket   ioctl
 
#define SOCKET   int
 
#define INVALID_SOCKET   -1
 
#define SOCKET_ERROR   -1
 
#define WSAEWOULDBLOCK   EWOULDBLOCK
 
#define WSAECONNRESET   ECONNRESET
 
#define WSAENOTCONN   ENOTCONN
 
#define NO_NAGLE
 
#define SOMAXCONN   0x7fffffff
 

Functions

int WSAGetLastError (void)
 
void XPSock_Poll (void)
 
void XPSock_UpdatePerSecondMetrics (void)
 
int XPSock_Init (void)
 
int XPSock_Quit (void)
 
t_XPSocketXPSock_Create (void)
 
int XPSock_SetSendBuffer (t_XPSocket *socket, int size)
 
int XPSock_SetRecvBuffer (t_XPSocket *socket, int size)
 
int XPSock_Connect (t_XPSocket *socket, const t_XPAddress *addr)
 
int XPSock_Send (t_XPSocket *socket, const char *data, const int len)
 
int XPSock_Recv (t_XPSocket *socket, char *data, int maxlen, int flag)
 
int XPSock_Close (t_XPSocket *socket)
 
int XPSock_Listen (t_XPSocket *socket, int port)
 
int XPSock_Listen (t_XPSocket *socket, const RNReplicaNet::t_XPAddress *addr)
 
t_XPSocketXPSock_Accept (t_XPSocket *socket)
 
int XPSock_GetAddress (t_XPSocket *socket, t_XPAddress *addr)
 
int XPSock_GetPeerAddress (t_XPSocket *socket, t_XPAddress *addr)
 
t_XPSocketXPSock_UrgentCreate (int port)
 
t_XPSocketXPSock_UrgentCreate (const RNReplicaNet::t_XPAddress *addr)
 
int XPSock_UrgentMakeBroadcast (t_XPSocket *socket)
 
int XPSock_UrgentSend (t_XPSocket *socket, const char *data, int len, const t_XPAddress *addr)
 
int XPSock_UrgentRecv (t_XPSocket *socket, char *const data, int maxlen, int flag, t_XPAddress *const addr)
 
const char * XPSock_TranslateError (int error)
 
const char * XPSock_TranslateErrorLong (int error)
 
int XPSock_GetHostName (char *name, int name_len)
 
int XPSock_GetHostAddress (XPAddress *addr)
 
int XPSock_Resolve (t_XPAddress *resolve, const char *input)
 
void XPSock_SentHistogram (unsigned int *classes, int numClasses)
 
void XPSock_RecvHistogram (unsigned int *classes, int numClasses)
 
int XPSock_GetHardwareStatus (void)
 
t_XPSocketXPSock_ICMPCreate (void)
 
int XPSock_ICMPSendPing (RNReplicaNet::t_XPSocket *socket, const RNReplicaNet::t_XPAddress *addr, unsigned short *retID, unsigned short *retSeq, unsigned int *retTimeSent)
 
int XPSock_ICMPRecvPing (RNReplicaNet::t_XPSocket *socket, RNReplicaNet::t_XPAddress *const addr, unsigned short *retID, unsigned short *retSeq, unsigned int *retTimeElapsed)
 
int XPSock_ICMPWaitForPing (RNReplicaNet::t_XPSocket *socket, const float timeoutSeconds)
 
int XPSock_GetSocketSelectMaxSize (void)
 
int XPSock_SocketSelect (RNReplicaNet::t_XPSocket **sockets, const int numSockets, const int milliseconds)
 

Variables

int asocket_lasterror = XPSOCK_EOK
 
int asocket_packets_got = 0
 
int asocket_packets_sent = 0
 
int asocket_packets_got_size = 0
 
int asocket_packets_sent_size = 0
 
float asocket_packets_got_size_per_second = 0.0f
 
float asocket_packets_sent_size_per_second = 0.0f
 
float asocket_packets_got_per_second = 0.0f
 
float asocket_packets_sent_per_second = 0.0f
 
MutexClass g_xpsockets_lockclass
 
bool gXPSock_PacketLossEmulation = false
 
float gXPSock_PacketLossEmulationPercent = 0.0f
 
bool gXPSock_PacketLatencyEmulation = false
 
float gXPSock_PacketLatencyEmulationBase = 0.0f
 
float gXPSock_PacketLatencyEmulationJitter = 0.0f
 
int gXPSock_UDPProtoParameter = IPPROTO_UDP
 

Detailed Description

Simple local host packet stuff A lot like standard sockets, but made simpler with all the error handling done internally

Handles all these protocols transparently:

Localhost sockets. TCPIP using streamed and UDP connections.

Sockets with a permanent connection and also connectionless sockets are implemented. Every urgent socket call can be thought of as mapping to equivalent UDP packet protocols.

Macro Definition Documentation

#define NO_NAGLE

Use this to turn off the Nagle packet compression algorithm

Function Documentation

t_XPSocket* XPSock_Accept ( t_XPSocket socket)

Tries to accept any incoming connections and creates a connected socket if successful.

Parameters
socketthe socket pointer
Returns
returns the new socket connection that got accepted or an XPSOCK error code or null for no socket

References XPAddress::addr, s_XPSocket::addr, asocket_lasterror, s_XPSocket::loopback_recvbuffer, XPAddress::port, s_XPSocket::state, s_XPSocket::total_accepts, s_XPSocket::type, s_XPSocket::wins_socket, XPSock_Close(), and XPSock_Create().

Referenced by XPSocket::Accept().

int XPSock_Close ( t_XPSocket socket)

Closes a socket

Parameters
socketthe socket pointer
Returns
returns an XPSOCK error code, XPSOCK_EOK is no error.

References asocket_lasterror, s_XPSocket::loopback_recvbuffer, s_XPSocket::state, s_XPSocket::type, s_XPSocket::wins_socket, and s_XPSocket::wins_socket6.

Referenced by XPSocket::Close(), XPSocket::MutateSocket(), XPSock_Accept(), and XPSock_Connect().

int XPSock_Connect ( t_XPSocket socket,
const t_XPAddress addr 
)

Connect to a machine address

Parameters
socketthe socket pointer
addrthe address to connect to. If addr is NULL then a local connect is tried
Returns
returns an XPSOCK error code, XPSOCK_EOK is no error.

References XPAddress::addr, s_XPSocket::addr, asocket_lasterror, s_XPSocket::loopback_recvbuffer, XPAddress::port, s_XPSocket::state, s_XPSocket::type, s_XPSocket::wins_socket, and XPSock_Close().

Referenced by XPSocket::Connect().

t_XPSocket* XPSock_Create ( void  )

Creates a socket

Returns
returns the socket pointer or XPSOCK_EERROR if an error occurred

References asocket_lasterror, s_XPSocket::wins_socket, and s_XPSocket::wins_socket6.

Referenced by XPSocket::Create(), and XPSock_Accept().

int XPSock_GetAddress ( t_XPSocket socket,
t_XPAddress addr 
)

Fills in the internet address and port of a socket to the address structure.

Parameters
socketthe socket pointer
addrthe address structure to fill in
Returns
returns XPSOCK_EOK or an XPSOCK error code.

References XPAddress::addr, XPAddress::addr6, asocket_lasterror, XPAddress::port, XPAddress::port6, s_XPSocket::wins_socket, s_XPSocket::wins_socket6, and XPSock_GetHostAddress().

Referenced by XPSocket::GetAddress().

int XPSock_GetHardwareStatus ( void  )

Gets the status of the network hardware used by this socket interface.

Returns
int XPSock_GetHostAddress ( XPAddress addr)

Gets the host machine address

Parameters
addrthe XPAddress to fill in
Returns
returns an XPSOCK error code.

References XPSock_GetHostName(), and XPSock_Resolve().

Referenced by XPSock_GetAddress().

int XPSock_GetHostName ( char *  name,
int  name_len 
)

Gets the host machine name

Parameters
namepointer to a buffer to receive the machines textual name
name_lenthe length of the buffer
Returns
returns an XPSOCK error code.

References asocket_lasterror.

Referenced by XPSock_GetHostAddress().

int XPSock_GetPeerAddress ( t_XPSocket socket,
t_XPAddress addr 
)

Fills in the internet address of the peer of a socket to the address structure.

Parameters
socketthe socket pointer
addrthe address structure to fill in
Returns
returns XPSOCK_EOK or an XPSOCK error code.

References XPAddress::addr, asocket_lasterror, XPAddress::port, and s_XPSocket::wins_socket.

Referenced by XPSocket::GetPeerAddress().

int XPSock_Init ( void  )

This inits the socket interface

References asocket_lasterror, and SysTime::FloatTime().

Referenced by MyService::Main(), and XPSocket::XPSocket().

int XPSock_Listen ( t_XPSocket socket,
int  port 
)

Sets a socket to be a listen socket for a certain port number. This should only really be used for a server not the client as it is better network programming practice.

Parameters
socketthe socket pointer
portthe port number to listen from
Returns
returns an XPSOCK error code, XPSOCK_EOK is no error.

References XPAddress::MakeAny().

Referenced by XPSocket::Listen().

int XPSock_Listen ( t_XPSocket socket,
const RNReplicaNet::t_XPAddress addr 
)

Sets a socket to be a listen socket that is bound to the address supplied. This should only really be used for a server not the client as it is better network programming practice.

Parameters
socketthe socket pointer
addrthe address to use.
Returns
returns an XPSOCK error code, XPSOCK_EOK is no error.

References XPAddress::addr, asocket_lasterror, XPAddress::port, s_XPSocket::port, s_XPSocket::state, s_XPSocket::type, and s_XPSocket::wins_socket.

int XPSock_Quit ( void  )

Quits all sockets and exits Calling PlatformHeap::ForceFree() will call XPSock_Quit() if XPSock_Init() was previously called.

References asocket_lasterror.

int XPSock_Recv ( t_XPSocket socket,
char *  data,
int  maxlen,
int  flag 
)

Tries to receive data from a socket

Parameters
socketthe socket pointer
dataa char pointer to the data buffer
maxlenthe maximum length of the data buffer to receive to
flagthe flag to use for this operation. Removes the read message from the queue unless XPSOCKF_PEEK is used
Returns
returns the number of bytes read or 0 if no data is pending or an XPSOCK_EERROR error code.

References asocket_lasterror, asocket_packets_got, asocket_packets_got_size, s_XPSocket::loopback_pending, s_XPSocket::loopback_readpos, s_XPSocket::loopback_recvbuffer, s_XPSocket::state, s_XPSocket::type, and s_XPSocket::wins_socket.

Referenced by XPSocket::Recv().

void XPSock_RecvHistogram ( unsigned int *  classes,
int  numClasses 
)

Supplies a histogram table and the size of the table for sent packets. Each class entry corresponds to a size of packet.

Parameters
classesThe pointer to the array of classes.
numClassesThe number of classes in the array.
int XPSock_Resolve ( t_XPAddress resolve,
const char *  input 
)

Handy routine to resolve anything into an address

Parameters
resolvethe address structure to fill in with the result. Specifically the port and port6 members are left unchanged.
inputthe textual name of a machine name to resolve
Returns
returns an XPSOCK error code.

References XPAddress::addr, XPAddress::addr6, and asocket_lasterror.

Referenced by XPSock_GetHostAddress().

int XPSock_Send ( t_XPSocket socket,
const char *  data,
const int  len 
)

Tries to send data via the socket

Parameters
socketthe socket pointer
dataa char pointer to the data buffer
lenthe length of the data to send
Returns
returns The number of bytes sent or an SOCKET_ERROR error code, XPSOCK_EOK is no error.

References asocket_lasterror, asocket_packets_sent, asocket_packets_sent_size, s_XPSocket::loopback_pending, s_XPSocket::loopback_recvbuffer, s_XPSocket::loopback_recvpos, s_XPSocket::state, s_XPSocket::type, and s_XPSocket::wins_socket.

Referenced by XPSocket::Send().

void XPSock_SentHistogram ( unsigned int *  classes,
int  numClasses 
)

Supplies a histogram table and the size of the table for sent packets. Each class entry corresponds to a size of packet.

Parameters
classesThe pointer to the array of classes.
numClassesThe number of classes in the array.
int XPSock_SetRecvBuffer ( t_XPSocket socket,
int  size 
)

Sets the size of the recv buffer for a socket

Parameters
socketthe socket pointer
sizeThe size to set the recv buffer to or -1 to only return the value
Returns
the new size of the recv buffer

References s_XPSocket::wins_socket.

Referenced by XPSocket::SetRecvBuffer().

int XPSock_SetSendBuffer ( t_XPSocket socket,
int  size 
)

Sets the size of the send buffer for a socket

Parameters
socketthe socket pointer
sizeThe size to set the send buffer to or -1 to only return the value
Returns
the new size of the send buffer

References s_XPSocket::wins_socket.

Referenced by XPSocket::SetSendBuffer().

const char* XPSock_TranslateError ( int  error)

This translates an XPSocket error to a text string

Parameters
errorthe XPSOCK error number
Returns
returns a pointer to an XPSOCK error string.

References s_XPErrorTranslate::text.

const char* XPSock_TranslateErrorLong ( int  error)

This translates an XPSocket error to a long descriptive text string

Parameters
errorthe XPSOCK error number
Returns
returns a pointer to an XPSOCK long error string.

References s_XPErrorTranslate::longtext.

void XPSock_UpdatePerSecondMetrics ( void  )
t_XPSocket* XPSock_UrgentCreate ( int  port)

Creates an urgent socket Returns XPSOCK_EERROR if an error occurred If port is zero (0) then the network layer will pick a port number to be bound to Using a value of zero for the port when operating a client is advised since the any chosen port might already be in use

Parameters
portthe port number to use
Returns
returns a pointer to new socket or an XPSOCK error code.

References XPAddress::MakeAny().

Referenced by XPSocketUrgent::Create(), and MyService::Main().

t_XPSocket* XPSock_UrgentCreate ( const RNReplicaNet::t_XPAddress addr)

Creates an urgent socket that is bound to the address supplied. Returns XPSOCK_EERROR if an error occurred If port is zero (0) then the network layer will pick a port number to be bound to Using a value of zero for the port when operating a client is advised since the any chosen port might already be in use

Parameters
addrthe address to use.
Returns
returns a pointer to new socket or an XPSOCK error code.

References XPAddress::addr, XPAddress::addr6, asocket_lasterror, XPAddress::port, s_XPSocket::port, XPAddress::port6, s_XPSocket::port6, s_XPSocket::wins_socket, and s_XPSocket::wins_socket6.

int XPSock_UrgentMakeBroadcast ( t_XPSocket socket)

Makes an urgent socket broadcast capable

Parameters
socketthe socket pointer
Returns
returns an XPSOCK error code.

References asocket_lasterror, s_XPSocket::wins_socket, and s_XPSocket::wins_socket6.

Referenced by XPSocketUrgent::MakeBroadcast().

int XPSock_UrgentRecv ( t_XPSocket socket,
char *const  data,
int  maxlen,
int  flag,
t_XPAddress *const  addr 
)

Receives data from an urgent socket

Parameters
socketthe socket pointer
datapointer to a data buffer
maximumlength of the data buffer
flagthe flag to use for this operation. Removes the read message from the queue unless XPSOCK_FPEEK is used
addrthe address of the sending socket for any data received
Returns
returns the length of the data received or 0 if no data is pending or an XPSOCK_EERROR error code

References XPAddress::addr, XPAddress::addr6, asocket_lasterror, asocket_packets_got, asocket_packets_got_size, s_XPSocket::mCounter, XPAddress::port, XPAddress::port6, s_XPSocket::wins_socket, and s_XPSocket::wins_socket6.

Referenced by XPSocketUrgent::Recv().

int XPSock_UrgentSend ( t_XPSocket socket,
const char *  data,
int  len,
const t_XPAddress addr 
)

Send an urgent packet from the socket to the specified address. This will return XPSOCK_EUDPSENDFAILED if this method doesn't exist. If so use a non-urgent socket

Parameters
socketthe socket pointer
datathe pointer to the data to send
lenthe length of the data to send
theaddress and port number to send to
Returns
returns an XPSOCK error code.

References XPAddress::addr, asocket_lasterror, asocket_packets_sent, asocket_packets_sent_size, SysTime::FloatTime(), s_XPSocket::wins_socket, and s_XPSocket::wins_socket6.

Referenced by XPSocketUrgent::Send().

Variable Documentation

int asocket_lasterror = XPSOCK_EOK
int asocket_packets_got = 0

Total number of packets received

Referenced by XPSock_Recv(), XPSock_UpdatePerSecondMetrics(), and XPSock_UrgentRecv().

float asocket_packets_got_per_second = 0.0f

Total number of packets received per second

Referenced by XPSock_UpdatePerSecondMetrics().

int asocket_packets_got_size = 0

Total byte size of packets received

Referenced by XPSock_Recv(), XPSock_UpdatePerSecondMetrics(), and XPSock_UrgentRecv().

float asocket_packets_got_size_per_second = 0.0f

Total number of packets size received per second

Referenced by XPSock_UpdatePerSecondMetrics().

int asocket_packets_sent = 0

Total number of packets sent

Referenced by XPSock_Send(), XPSock_UpdatePerSecondMetrics(), and XPSock_UrgentSend().

float asocket_packets_sent_per_second = 0.0f

Total number of packets sent per second

Referenced by XPSock_UpdatePerSecondMetrics().

int asocket_packets_sent_size = 0

Total byte size of packets sent

Referenced by XPSock_Send(), XPSock_UpdatePerSecondMetrics(), and XPSock_UrgentSend().

float asocket_packets_sent_size_per_second = 0.0f

Total number of packets size sent per second

Referenced by XPSock_UpdatePerSecondMetrics().