ReplicaNet and RNLobby
1
|
#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"
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_XPSocket * | XPSock_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_XPSocket * | XPSock_Accept (t_XPSocket *socket) |
int | XPSock_GetAddress (t_XPSocket *socket, t_XPAddress *addr) |
int | XPSock_GetPeerAddress (t_XPSocket *socket, t_XPAddress *addr) |
t_XPSocket * | XPSock_UrgentCreate (int port) |
t_XPSocket * | XPSock_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_XPSocket * | XPSock_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 |
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.
#define NO_NAGLE |
Use this to turn off the Nagle packet compression algorithm
t_XPSocket* XPSock_Accept | ( | t_XPSocket * | socket | ) |
Tries to accept any incoming connections and creates a connected socket if successful.
socket | the socket pointer |
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
socket | the socket pointer |
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
socket | the socket pointer |
addr | the address to connect to. If addr is NULL then a local connect is tried |
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
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.
socket | the socket pointer |
addr | the address structure to fill in |
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.
int XPSock_GetHostAddress | ( | XPAddress * | addr | ) |
Gets the host machine address
addr | the XPAddress to fill in |
References XPSock_GetHostName(), and XPSock_Resolve().
Referenced by XPSock_GetAddress().
int XPSock_GetHostName | ( | char * | name, |
int | name_len | ||
) |
Gets the host machine name
name | pointer to a buffer to receive the machines textual name |
name_len | the length of the buffer |
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.
socket | the socket pointer |
addr | the address structure to fill in |
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.
socket | the socket pointer |
port | the port number to listen from |
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.
socket | the socket pointer |
addr | the address to use. |
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
socket | the socket pointer |
data | a char pointer to the data buffer |
maxlen | the maximum length of the data buffer to receive to |
flag | the flag to use for this operation. Removes the read message from the queue unless XPSOCKF_PEEK is used |
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.
classes | The pointer to the array of classes. |
numClasses | The number of classes in the array. |
int XPSock_Resolve | ( | t_XPAddress * | resolve, |
const char * | input | ||
) |
Handy routine to resolve anything into an address
resolve | the address structure to fill in with the result. Specifically the port and port6 members are left unchanged. |
input | the textual name of a machine name to resolve |
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
socket | the socket pointer |
data | a char pointer to the data buffer |
len | the length of the data to send |
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.
classes | The pointer to the array of classes. |
numClasses | The number of classes in the array. |
int XPSock_SetRecvBuffer | ( | t_XPSocket * | socket, |
int | size | ||
) |
Sets the size of the recv buffer for a socket
socket | the socket pointer |
size | The size to set the recv buffer to or -1 to only return the value |
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
socket | the socket pointer |
size | The size to set the send buffer to or -1 to only return the value |
References s_XPSocket::wins_socket.
Referenced by XPSocket::SetSendBuffer().
const char* XPSock_TranslateError | ( | int | error | ) |
This translates an XPSocket error to a text string
error | the XPSOCK error number |
References s_XPErrorTranslate::text.
const char* XPSock_TranslateErrorLong | ( | int | error | ) |
This translates an XPSocket error to a long descriptive text string
error | the XPSOCK error number |
References s_XPErrorTranslate::longtext.
void XPSock_UpdatePerSecondMetrics | ( | void | ) |
This is automatically called by other ReplicaNet modules using the XPURL Transport system
References asocket_packets_got, asocket_packets_got_per_second, asocket_packets_got_size, asocket_packets_got_size_per_second, asocket_packets_sent, asocket_packets_sent_per_second, asocket_packets_sent_size, asocket_packets_sent_size_per_second, SysTime::FloatTime(), and SysTime::Reset().
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
port | the port number to use |
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
addr | the address to use. |
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
socket | the socket pointer |
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
socket | the socket pointer |
data | pointer to a data buffer |
maximum | length of the data buffer |
flag | the flag to use for this operation. Removes the read message from the queue unless XPSOCK_FPEEK is used |
addr | the address of the sending socket for any data received |
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
socket | the socket pointer |
data | the pointer to the data to send |
len | the length of the data to send |
the | address and port number to send to |
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().
int asocket_lasterror = XPSOCK_EOK |
asocket_lasterror This contains the last error value returned from any of the socket functions
Referenced by XPSocket::GetLastError(), XPSock_Accept(), XPSock_Close(), XPSock_Connect(), XPSock_Create(), XPSock_GetAddress(), XPSock_GetHostName(), XPSock_GetPeerAddress(), XPSock_Init(), XPSock_Listen(), XPSock_Quit(), XPSock_Recv(), XPSock_Resolve(), XPSock_Send(), XPSock_UrgentCreate(), XPSock_UrgentMakeBroadcast(), XPSock_UrgentRecv(), and XPSock_UrgentSend().
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().