From c55243bd1988fe08cb094bb1190a2b0c8bcc4868 Mon Sep 17 00:00:00 2001 From: Anonymous Date: Tue, 14 Jan 2014 06:47:08 -0700 Subject: [PATCH] Added the udplibrary library --- external/3rd/CMakeLists.txt | 2 + external/3rd/library/CMakeLists.txt | 2 + .../3rd/library/udplibrary/CMakeLists.txt | 21 + .../3rd/library/udplibrary/PointerDeque.hpp | 126 + external/3rd/library/udplibrary/UdpHandler.h | 6 + .../3rd/library/udplibrary/UdpHandler.hpp | 76 + .../3rd/library/udplibrary/UdpLibrary.cpp | 4364 +++++++++++++++++ .../3rd/library/udplibrary/UdpLibrary.doc | Bin 0 -> 230400 bytes external/3rd/library/udplibrary/UdpLibrary.h | 6 + .../3rd/library/udplibrary/UdpLibrary.hpp | 2095 ++++++++ .../library/udplibrary/UdpLibraryRelease.txt | 1298 +++++ external/3rd/library/udplibrary/hashtable.hpp | 619 +++ external/3rd/library/udplibrary/priority.hpp | 225 + external/3rd/library/udplibrary/udpclient.cpp | 245 + .../3rd/library/udplibrary/udplibrary.dsp | 152 + external/3rd/library/udplibrary/udpserver.cpp | 290 ++ external/CMakeLists.txt | 1 + 17 files changed, 9528 insertions(+) create mode 100644 external/3rd/CMakeLists.txt create mode 100644 external/3rd/library/CMakeLists.txt create mode 100644 external/3rd/library/udplibrary/CMakeLists.txt create mode 100644 external/3rd/library/udplibrary/PointerDeque.hpp create mode 100644 external/3rd/library/udplibrary/UdpHandler.h create mode 100644 external/3rd/library/udplibrary/UdpHandler.hpp create mode 100644 external/3rd/library/udplibrary/UdpLibrary.cpp create mode 100644 external/3rd/library/udplibrary/UdpLibrary.doc create mode 100644 external/3rd/library/udplibrary/UdpLibrary.h create mode 100644 external/3rd/library/udplibrary/UdpLibrary.hpp create mode 100644 external/3rd/library/udplibrary/UdpLibraryRelease.txt create mode 100644 external/3rd/library/udplibrary/hashtable.hpp create mode 100644 external/3rd/library/udplibrary/priority.hpp create mode 100644 external/3rd/library/udplibrary/udpclient.cpp create mode 100644 external/3rd/library/udplibrary/udplibrary.dsp create mode 100644 external/3rd/library/udplibrary/udpserver.cpp diff --git a/external/3rd/CMakeLists.txt b/external/3rd/CMakeLists.txt new file mode 100644 index 00000000..64660762 --- /dev/null +++ b/external/3rd/CMakeLists.txt @@ -0,0 +1,2 @@ + +add_subdirectory(library) diff --git a/external/3rd/library/CMakeLists.txt b/external/3rd/library/CMakeLists.txt new file mode 100644 index 00000000..c45299c8 --- /dev/null +++ b/external/3rd/library/CMakeLists.txt @@ -0,0 +1,2 @@ + +add_subdirectory(udplibrary) diff --git a/external/3rd/library/udplibrary/CMakeLists.txt b/external/3rd/library/udplibrary/CMakeLists.txt new file mode 100644 index 00000000..507105a0 --- /dev/null +++ b/external/3rd/library/udplibrary/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 2.8) + +project(udplibrary) + +if(WIN32) + add_definitions(/D_CRT_SECURE_NO_WARNINGS) +endif() + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) + +add_library(udplibrary + hashtable.hpp + PointerDeque.hpp + priority.hpp + UdpHandler.hpp + UdpHandler.h + UdpLibrary.cpp + UdpLibrary.hpp + UdpLibrary.h +) + diff --git a/external/3rd/library/udplibrary/PointerDeque.hpp b/external/3rd/library/udplibrary/PointerDeque.hpp new file mode 100644 index 00000000..6124afd8 --- /dev/null +++ b/external/3rd/library/udplibrary/PointerDeque.hpp @@ -0,0 +1,126 @@ +#ifndef POINTERDEQUE_HPP +#define POINTERDEQUE_HPP + + // This is a simple double ended queue template. Any pointer-type can be stored in this deque + // I pop/peek return NULL if the queue is empty. +template class PointerDeque +{ + public: + PointerDeque(int entriesPerPage); + ~PointerDeque(); + + void PushLeft(T* obj); + T* PopLeft(); + void PushRight(T* obj); + T* PopRight(); + T* PeekLeft(); + T* PeekRight(); + T* Peek(int index); + int Count(); + + protected: + void Expand(); + + int mEntriesPerPage; + T** mEntries; + int mEntriesCount; + int mEntriesMax; + int mOffsetLeft; +}; + + +template PointerDeque::PointerDeque(int entriesPerPage) +{ + mEntriesPerPage = entriesPerPage; + mEntries = NULL; + mOffsetLeft = 0; + mEntriesMax = 0; + mEntriesCount = 0; +} + +template PointerDeque::~PointerDeque() +{ + delete[] mEntries; +} + +template void PointerDeque::Expand() +{ + int countToEnd = (mEntriesMax - mOffsetLeft); + mEntriesMax += mEntriesPerPage; + T** newHold = new T*[mEntriesMax]; + + if (countToEnd < mEntriesCount) + { + memcpy(newHold, &mEntries[mOffsetLeft], countToEnd * sizeof(T*)); + memcpy(newHold + countToEnd, mEntries, (mEntriesCount - countToEnd) * sizeof(T*)); + } + else + memcpy(newHold, &mEntries[mOffsetLeft], mEntriesCount * sizeof(T*)); + + delete[] mEntries; + mEntries = newHold; + mOffsetLeft = 0; +} + +template void PointerDeque::PushLeft(T* obj) +{ + if (mEntriesCount >= mEntriesMax) + Expand(); + mEntries[(mOffsetLeft - 1 + mEntriesMax) % mEntriesMax] = obj; + mEntriesCount++; +} + +template T* PointerDeque::PopLeft() +{ + if (mEntriesCount == 0) + return(NULL); + T* hold = mEntries[mOffsetLeft]; + mOffsetLeft = (mOffsetLeft + 1) % mEntriesMax; + mEntriesCount--; + return(hold); +} + +template void PointerDeque::PushRight(T* obj) +{ + if (mEntriesCount >= mEntriesMax) + Expand(); + mEntries[(mOffsetLeft + mEntriesCount) % mEntriesMax] = obj; + mEntriesCount++; +} + +template T* PointerDeque::PopRight() +{ + if (mEntriesCount == 0) + return(NULL); + mEntriesCount--; + return(mEntries[(mOffsetLeft + mEntriesCount) % mEntriesMax]); +} + +template T* PointerDeque::PeekLeft() +{ + if (mEntriesCount == 0) + return(NULL); + return(mEntries[mOffsetLeft]); +} + +template T* PointerDeque::PeekRight() +{ + if (mEntriesCount == 0) + return(NULL); + return(mEntries[(mOffsetLeft + mEntriesCount - 1) % mEntriesMax]); +} + +template T* PointerDeque::Peek(int index) +{ + if (index >= mEntriesCount) + return(NULL); + return(mEntries[(mOffsetLeft + index) % mEntriesMax]); +} + +template int PointerDeque::Count() +{ + return(mEntriesCount); +} + +#endif + diff --git a/external/3rd/library/udplibrary/UdpHandler.h b/external/3rd/library/udplibrary/UdpHandler.h new file mode 100644 index 00000000..ea2a2ca6 --- /dev/null +++ b/external/3rd/library/udplibrary/UdpHandler.h @@ -0,0 +1,6 @@ +#ifndef _Network_UdpLibrary_H +#define _Network_UdpLibrary_H + +#include "UdpHandler.hpp" + +#endif//_Network_UdpLibrary_H diff --git a/external/3rd/library/udplibrary/UdpHandler.hpp b/external/3rd/library/udplibrary/UdpHandler.hpp new file mode 100644 index 00000000..d7b3869a --- /dev/null +++ b/external/3rd/library/udplibrary/UdpHandler.hpp @@ -0,0 +1,76 @@ +#ifndef UDPHANDLER_HPP +#define UDPHANDLER_HPP + +class UdpManager; +class UdpConnection; +class LogicalPacket; + +typedef unsigned char uchar; +typedef unsigned short ushort; +typedef unsigned int uint; +typedef unsigned long ulong; + +enum UdpCorruptionReason { cUdpCorruptionNone + , cUdpCorruptionMultiPacket + , cUdpCorruptionReliablePacketTooShort + , cUdpCorruptionInternalPacketTooShort + , cUdpCorruptionDecryptFailed + , cCorruptionReasonZeroLengthPacket + , cCorruptionReasonPacketShorterThanCrcBytes + }; + + + +class UdpManagerHandler +{ + public: + // This function is called when a listening UdpManager receives a new connection request. If the application wishes to + // accept this connection, it is the responsibility of the application to AddRef the UdpConnection object such that it + // doesn't get destroyed upon returning from the callback. Additionally, the application should set the handler object + // for the connection. See sample code in the UdpLibrary.doc file for an example. + virtual void OnConnectRequest(UdpConnection *con); + + // The following functions are called when user-supplied encryption is specified. In particular, the functions may wish + // to query the connection object for the encryption-code it is using. The encryption code is simply a randomly generated + // 32-bit value that the client and server negotiated during the connection-establishment. It can be used as a key + // of sorts for encrypting data uniquely on a per-connection basis. (see UdpConnection::GetEncryptCode()) + virtual int OnUserSuppliedEncrypt(UdpConnection *con, uchar *destData, const uchar *sourceData, int sourceLen); + virtual int OnUserSuppliedDecrypt(UdpConnection *con, uchar *destData, const uchar *sourceData, int sourceLen); + virtual int OnUserSuppliedEncrypt2(UdpConnection *con, uchar *destData, const uchar *sourceData, int sourceLen); + virtual int OnUserSuppliedDecrypt2(UdpConnection *con, uchar *destData, const uchar *sourceData, int sourceLen); +}; + +class UdpConnectionHandler +{ + public: + // This function is called whenever an application-packet is ready to be delivered to the application. + // Packets will only ever be delivered to the application during UdpManager::GiveTime calls, so the application + // has complete control of when it willing/able to receive packets. + virtual void OnRoutePacket(UdpConnection *con, const uchar *data, int dataLen) = 0; + + // This function is called whenever a UdpManager::EstablishConnection call succeeds in establishing the connection. + // Generally on client-side establishment of a connection, I prefer to sit in a tight loop polling the connection + // to see if it has connected yet or not, rather than using this callback (see sample code) + // note: If it is unable to establish the connection, the OnTerminated callback will be called. + // note: this is a change from previous behavior, see release notes for details. + virtual void OnConnectComplete(UdpConnection *con); + + // This function is called anytime the UdpConnection object changes to a cStatusDisconnected state. Every UdpConnection + // object is guaranteed to eventually call this once and only once, even connections that never successfully established. + // note: this is a change from previous behavior, see release notes for details + virtual void OnTerminated(UdpConnection *con); + + // This function is called anytime an incoming packet fails the CRC check. Normally these packets are just ignored, as + // corruption does occur from time to time; however, some applications may wish to log these events as often times, they + // are an indication of somebody attempting to hack the data stream. + virtual void OnCrcReject(UdpConnection *con, const uchar *data, int dataLen); + + // This function is called anytime a corrupt packet is sensed for some reason. + // The reason we give a callback for it is because you may wish to log it as it could be an early indicator of + // somebody trying to hack the protocol. + + virtual void OnPacketCorrupt(UdpConnection *con, const uchar *data, int dataLen, UdpCorruptionReason reason); +}; + +#endif + diff --git a/external/3rd/library/udplibrary/UdpLibrary.cpp b/external/3rd/library/udplibrary/UdpLibrary.cpp new file mode 100644 index 00000000..2ad0dfd4 --- /dev/null +++ b/external/3rd/library/udplibrary/UdpLibrary.cpp @@ -0,0 +1,4364 @@ +#include +#include +#include +#include +#include + +#include "UdpLibrary.hpp" + +#if defined(WIN32) + #pragma warning(disable : 4710) + #if defined(UDPLIBRARY_WINSOCK2) + #include + #include + #else + #include + #endif + + typedef int socklen_t; +#elif defined(sparc) + #include + #include + #include + #include + #include + #include + #include + #include + #include + + const int INADDR_NONE = -1; + const int INVALID_SOCKET = 0xFFFFFFFF; + const int SOCKET_ERROR = 0xFFFFFFFF; +#else // for non-windows platforms (linux) + #include + #include + #include + #include + #include + #include + #include + #include // needed by gcc 3.1 for linux + const int INVALID_SOCKET = 0xFFFFFFFF; + const int SOCKET_ERROR = 0xFFFFFFFF; +#endif + +template +const ValueType & udpMax(const ValueType & a, const ValueType & b) +{ + if(a < b) + return b; + return a; +} + +template +const ValueType & udpMin(const ValueType & a, const ValueType & b) +{ + if(b < a) + return b; + return a; +} + + ///////////////////////////////////////////////////////////////////////////////////////////// + // operating system dependent initialization routines (internally called when needed) + ///////////////////////////////////////////////////////////////////////////////////////////// +void InitializeOperatingSystem() +{ +#if defined(WIN32) + WSADATA wsaData; + #if defined(UDPLIBRARY_WINSOCK2) + WSAStartup(MAKEWORD(2,2), &wsaData); + #else + WSAStartup(MAKEWORD(1,1), &wsaData); + #endif +#endif +} + +void TerminateOperatingSystem() +{ +#if defined(WIN32) + WSACleanup(); +#endif +} + + + ///////////////////////////////////////////////////////////////////////////////////////////////////// + // UdpConnectionHandler default implementation + ///////////////////////////////////////////////////////////////////////////////////////////////////// +void UdpConnectionHandler::OnConnectComplete(UdpConnection * /*con*/) +{ +} + +void UdpConnectionHandler::OnTerminated(UdpConnection * /*con*/) +{ +} + +void UdpConnectionHandler::OnCrcReject(UdpConnection * /*con*/, const uchar * /*data*/, int /*dataLen*/) +{ +} + +void UdpConnectionHandler::OnPacketCorrupt(UdpConnection * /*con*/, const uchar * /*data*/, int /*dataLen*/, UdpCorruptionReason /*reason*/) +{ +} + + + ///////////////////////////////////////////////////////////////////////////////////////////////////// + // UdpManagerHandler default implementation + ///////////////////////////////////////////////////////////////////////////////////////////////////// +void UdpManagerHandler::OnConnectRequest(UdpConnection * /*con*/) +{ +} + +int UdpManagerHandler::OnUserSuppliedEncrypt(UdpConnection * /*con*/, uchar *destData, const uchar *sourceData, int sourceLen) +{ + memcpy(destData, sourceData, sourceLen); + return(sourceLen); +} + +int UdpManagerHandler::OnUserSuppliedEncrypt2(UdpConnection * /*con*/, uchar *destData, const uchar *sourceData, int sourceLen) +{ + memcpy(destData, sourceData, sourceLen); + return(sourceLen); +} + +int UdpManagerHandler::OnUserSuppliedDecrypt(UdpConnection * /*con*/, uchar *destData, const uchar *sourceData, int sourceLen) +{ + memcpy(destData, sourceData, sourceLen); + return(sourceLen); +} + +int UdpManagerHandler::OnUserSuppliedDecrypt2(UdpConnection * /*con*/, uchar *destData, const uchar *sourceData, int sourceLen) +{ + memcpy(destData, sourceData, sourceLen); + return(sourceLen); +} + + + ///////////////////////////////////////////////////////////////////////////////////////////////////// + // UdpIpAddress implementation + ///////////////////////////////////////////////////////////////////////////////////////////////////// +UdpIpAddress::UdpIpAddress(unsigned int ip) +{ + mIp = ip; +} + +char *UdpIpAddress::GetAddress(char *buffer) const +{ + assert(buffer != NULL); + + struct sockaddr_in addr_serverUDP; + addr_serverUDP.sin_addr.s_addr = mIp; + strcpy(buffer, inet_ntoa(addr_serverUDP.sin_addr)); + return(buffer); +} + + ///////////////////////////////////////////////////////////////////////////////////////////////////// + // UdpManager::Params initializations constructor (ie. default values) + ///////////////////////////////////////////////////////////////////////////////////////////////////// +UdpManager::Params::Params() +{ + handler = NULL; + outgoingBufferSize = 64 * 1024; + incomingBufferSize = 64 * 1024; + packetHistoryMax = 100; + maxDataHoldTime = 50; + maxDataHoldSize = -1; + maxRawPacketSize = 512; + hashTableSize = 100; + avoidPriorityQueue = false; + clockSyncDelay = 0; + crcBytes = 0; + encryptMethod[0] = UdpManager::cEncryptMethodNone; + encryptMethod[1] = UdpManager::cEncryptMethodNone; + keepAliveDelay = 0; + portAliveDelay = 0; + noDataTimeout = 0; + maxConnections = 10; + port = 0; + portRange = 0; + pooledPacketMax = 1000; + pooledPacketSize = -1; + pooledPacketInitial = 0; + replyUnreachableConnection = true; + allowPortRemapping = true; + allowAddressRemapping = false; + icmpErrorRetryPeriod = 5000; + oldestUnacknowledgedTimeout = 90000; + processIcmpErrors = true; + processIcmpErrorsDuringNegotiating = false; + connectAttemptDelay = 1000; + reliableOverflowBytes = 0; + memset(bindIpAddress, 0, sizeof(bindIpAddress)); + + userSuppliedEncryptExpansionBytes = 0; + userSuppliedEncryptExpansionBytes2 = 0; + simulateIncomingByteRate = 0; + simulateIncomingLossPercent = 0; + simulateOutgoingByteRate = 0; + simulateOutgoingLossPercent = 0; + simulateDestinationOverloadLevel = 0; + simulateOutgoingOverloadLevel = 0; + + reliable[0].maxInstandingPackets = 400; + reliable[0].maxOutstandingBytes = 200 * 1024; + reliable[0].maxOutstandingPackets = 400; + reliable[0].outOfOrder = false; + reliable[0].processOnSend = false; + reliable[0].coalesce = true; + reliable[0].ackDeduping = true; + reliable[0].fragmentSize = 0; + reliable[0].resendDelayAdjust = 300; + reliable[0].resendDelayPercent = 125; + reliable[0].resendDelayCap = 5000; + reliable[0].congestionWindowMinimum = 0; + reliable[0].trickleRate = 0; + reliable[0].trickleSize = 0; + for (int j = 1; j < cReliableChannelCount; j++) + reliable[j] = reliable[0]; +} + + + ///////////////////////////////////////////////////////////////////////////////////////////////////// + // UdpManager implementation + ///////////////////////////////////////////////////////////////////////////////////////////////////// +UdpManager::UdpManager(const UdpManager::Params *params) +{ + assert(params->clockSyncDelay >= 0); // negative clockSyncDelay is not allowed (makes no sense) + assert(params->crcBytes >= 0 && params->crcBytes <= 4); // crc bytes must be between 0 and 4 + assert(params->encryptMethod[0] >= 0 && params->encryptMethod[0] < cEncryptMethodCount); // illegal encryption method specified + assert(params->encryptMethod[1] >= 0 && params->encryptMethod[1] < cEncryptMethodCount); // illegal encryption method specified + assert(params->hashTableSize > 0); // a hash table size greater than zero is required + assert(params->maxRawPacketSize >= 64); // raw packet size must be at least 64 bytes + assert(params->incomingBufferSize >= params->maxRawPacketSize); // incoming socket buffer size must be at least as big as one raw packet + assert(params->keepAliveDelay >= 0); // keep alive delay can't be negative + assert(params->portAliveDelay >= 0); // port alive delay can't be negative + assert(params->maxConnections > 0); // must have at least 1 connection allowed + assert(params->outgoingBufferSize >= params->maxRawPacketSize); // outgoing socket buffer must larger than a raw packet size + assert(params->packetHistoryMax > 0); // packet history must be at least 1 + assert(params->port >= 0); // port cannot be negative + assert(params->userSuppliedEncryptExpansionBytes + params->userSuppliedEncryptExpansionBytes2 < params->maxRawPacketSize); // if encryption expansion is larger than raw packet size, we are screwed + assert(params->reliable[0].maxOutstandingBytes >= params->maxRawPacketSize); + assert(params->reliable[1].maxOutstandingBytes >= params->maxRawPacketSize); + assert(params->reliable[2].maxOutstandingBytes >= params->maxRawPacketSize); + assert(params->reliable[3].maxOutstandingBytes >= params->maxRawPacketSize); + assert(params->port != 0 || params->portRange == 0); + + mRefCount = 1; + mParams = *params; + mParams.maxRawPacketSize = udpMin(mParams.maxRawPacketSize, (int)cHardMaxRawPacketSize); + if (mParams.maxDataHoldSize == -1) + { + mParams.maxDataHoldSize = mParams.maxRawPacketSize; + } + if (mParams.pooledPacketSize == -1) + { + mParams.pooledPacketSize = mParams.maxRawPacketSize; + } + + mParams.maxDataHoldSize = udpMin(mParams.maxDataHoldSize, mParams.maxRawPacketSize); + mParams.packetHistoryMax = udpMax(1, mParams.packetHistoryMax); + mPacketHistoryPosition = 0; + mPassThroughData = NULL; + + typedef PacketHistoryEntry *PacketHistoryEntryPtr; + mPacketHistory = new PacketHistoryEntryPtr[mParams.packetHistoryMax]; + + int i; + for (i = 0; i < mParams.packetHistoryMax; i++) + { + mPacketHistory[i] = new PacketHistoryEntry(mParams.maxRawPacketSize); + } + + ResetStats(); + mLastReceiveTime = 0; + mLastSendTime = 0; + mLastEmptySocketBufferStamp = 0; + mProcessingInducedLag = 0; + mStartTtl = 32; + mMinimumScheduledStamp = 0; + mUdpSocket = INVALID_SOCKET; + + mConnectionListCount = 0; + mConnectionList = NULL; + + mPoolCreated = 0; + mPoolAvailable = 0; + mPoolAvailableRoot = NULL; + mPoolCreatedRoot = NULL; + + mWrappedCreated = 0; + mWrappedAvailable = 0; + mWrappedAvailableRoot = NULL; + mWrappedCreatedRoot = NULL; + + for (i = 0; i < mParams.pooledPacketInitial && i < mParams.pooledPacketMax; i++) + { + mPoolCreated++; + PooledLogicalPacket *lp = new PooledLogicalPacket(this, mParams.pooledPacketSize); + PoolReturn(lp); + lp->Release(); + } + + mSimulateQueueStart = NULL; + mSimulateQueueEnd = NULL; + mSimulateNextOutgoingTime = 0; + mSimulateNextIncomingTime = 0; + mSimulateQueueBytes = 0; + + mDisconnectPendingList = NULL; + + if (mParams.avoidPriorityQueue) + mPriorityQueue = NULL; + else + mPriorityQueue = new PriorityQueue(mParams.maxConnections); + + mAddressHashTable = new ObjectHashTable(mParams.hashTableSize); + mConnectCodeHashTable = new ObjectHashTable(udpMax(mParams.hashTableSize / 5, 10)); // rarely used, so make it a fraction of the main tables size + + InitializeOperatingSystem(); + + if (mParams.portRange == 0) + { + CreateAndBindSocket(mParams.port); + } + else + { + int r = rand() % mParams.portRange; + for (int i = 0; i < mParams.portRange; i++) + { + CreateAndBindSocket(mParams.port + ((r + i) % mParams.portRange)); + if (mErrorCondition != cErrorConditionCouldNotBindSocket) + break; + } + } +} + +UdpManager::~UdpManager() +{ + { + // first we need to tell all the pooled packets we have created that they can no longer check themselves back into use + // when they are released + PooledLogicalPacket *walk = mPoolCreatedRoot; + while (walk != NULL) + { + walk->mUdpManager = NULL; + walk = walk->mCreatedNext; + } + // next release the ones we have in our available pool + walk = mPoolAvailableRoot; + while (walk != NULL) + { + PooledLogicalPacket *hold = walk; + walk = walk->mAvailableNext; + hold->Release(); + } + } + + + { + // next we need to tell all the warpped packets we have created that they can no longer check themselves back into use + // when they are released + WrappedLogicalPacket *walk = mWrappedCreatedRoot; + while (walk != NULL) + { + walk->mUdpManager = NULL; + walk = walk->mCreatedNext; + } + // next release the ones we have in our available pool + walk = mWrappedAvailableRoot; + while (walk != NULL) + { + WrappedLogicalPacket *hold = walk; + walk = walk->mAvailableNext; + hold->Release(); + } + } + + // next thing we must do is tell all the connections to disconnect (which severs their link to this dying manager) + // this has to be done first since they will call back into us and have themselves removed from our connection-list/priority-queue/etc + while (mConnectionList != NULL) + { + mConnectionList->InternalDisconnect(0, UdpConnection::cDisconnectReasonManagerDeleted); + // the above call ended up calling us back and removing them from our connection list, so now mConnectionList is pointing to the next entry + // hopefully the compiler will be smart enough not to over optimize this. + } + + // release any objects that were pending disconnection + while (mDisconnectPendingList != NULL) + { + UdpConnection *next = mDisconnectPendingList->mDisconnectPendingNextConnection; + mDisconnectPendingList->Release(); + mDisconnectPendingList = next; + } + + CloseSocket(); + + TerminateOperatingSystem(); + + delete mAddressHashTable; + delete mConnectCodeHashTable; + delete mPriorityQueue; + for (int i = 0; i < mParams.packetHistoryMax; i++) + { + delete mPacketHistory[i]; + } + delete[] mPacketHistory; + + while (mSimulateQueueStart != NULL) + { + SimulateQueueEntry *entry = mSimulateQueueStart; + mSimulateQueueStart = entry->mNext; + delete entry; + } +} + +void UdpManager::CreateAndBindSocket(int usePort) +{ + CloseSocket(); + mErrorCondition = cErrorConditionNone; + mUdpSocket = socket(PF_INET, SOCK_DGRAM, 0); + if (mUdpSocket != INVALID_SOCKET) + { +#if defined(WIN32) + ulong lb = 1; + int err = ioctlsocket(mUdpSocket, FIONBIO, &lb); + int nb = mParams.outgoingBufferSize; + err = setsockopt(mUdpSocket, SOL_SOCKET, SO_SNDBUF, (char *)&nb, sizeof(nb)); + nb = mParams.incomingBufferSize; + err = setsockopt(mUdpSocket, SOL_SOCKET, SO_RCVBUF, (char *)&nb, sizeof(nb)); + int optLen = sizeof(mStartTtl); + getsockopt(mUdpSocket, IPPROTO_IP, IP_TTL, (char *)&mStartTtl, &optLen); +#elif defined(sparc) + ulong nb = 1; + int err = ioctl(mUdpSocket, FIONBIO, &nb); + assert(err != -1); + nb = udpMin(256 * 1024, mParams.outgoingBufferSize); + err = setsockopt(mUdpSocket, SOL_SOCKET, SO_SNDBUF, &nb, sizeof(nb)); + assert(err == 0); + nb = udpMin(256 * 1024, mParams.incomingBufferSize); + err = setsockopt(mUdpSocket, SOL_SOCKET, SO_RCVBUF, &nb, sizeof(nb)); + assert(err == 0); + + int optLen = sizeof(mStartTtl); + getsockopt(mUdpSocket, IPPROTO_IP, IP_TTL, &mStartTtl, (socklen_t *)&optLen); + + nb = 1; + err = setsockopt(mUdpSocket, SOL_SOCKET, SO_DGRAM_ERRIND, &nb, sizeof(nb)); + assert(err == 0); +#else // linux is to remain the default compile mode + unsigned long nb = 1; + int err = ioctl(mUdpSocket, FIONBIO, &nb); + assert(err != -1); + nb = mParams.outgoingBufferSize; + err = setsockopt(mUdpSocket, SOL_SOCKET, SO_SNDBUF, &nb, sizeof(nb)); + assert(err == 0); + nb = mParams.incomingBufferSize; + err = setsockopt(mUdpSocket, SOL_SOCKET, SO_RCVBUF, &nb, sizeof(nb)); + assert(err == 0); + nb = 0; + err = setsockopt(mUdpSocket, SOL_SOCKET, SO_BSDCOMPAT, &nb, sizeof(nb)); + assert(err == 0); + nb = 1; + err = setsockopt(mUdpSocket, SOL_IP, IP_RECVERR, &nb, sizeof(nb)); + assert(err == 0); + + int optLen = sizeof(mStartTtl); + getsockopt(mUdpSocket, IPPROTO_IP, IP_TTL, &mStartTtl, (socklen_t *)&optLen); +#endif + + // bind it to any address + struct sockaddr_in addr_loc; + addr_loc.sin_family = PF_INET; + addr_loc.sin_port = htons((ushort)usePort); + + addr_loc.sin_addr.s_addr = htonl(INADDR_ANY); + if (mParams.bindIpAddress[0] != 0) + { + unsigned long address = inet_addr(mParams.bindIpAddress); + assert(address != INADDR_NONE); // if this asserts, it means you are trying to explicitly bind to an illegally-formatted IP address. + + if (address != INADDR_NONE) + addr_loc.sin_addr.s_addr = address; // this is already in network order from the call above + } + + if (bind(mUdpSocket, (struct sockaddr *)&addr_loc, sizeof(addr_loc)) != 0) + { + mErrorCondition = cErrorConditionCouldNotBindSocket; + CloseSocket(); + } + } + else + { + mErrorCondition = cErrorConditionCouldNotAllocateSocket; + } +} + +void UdpManager::CloseSocket() +{ + if (mUdpSocket != INVALID_SOCKET) + { +#if defined(WIN32) + closesocket(mUdpSocket); +#else + close(mUdpSocket); +#endif + mUdpSocket = INVALID_SOCKET; + } +} + + +UdpManager::ErrorCondition UdpManager::GetErrorCondition() const +{ + return(mErrorCondition); +} + + +void UdpManager::ProcessDisconnectPending() +{ + UdpConnection *entry = mDisconnectPendingList; + UdpConnection **prev = &mDisconnectPendingList; + while (entry != NULL) + { + if (entry->GetStatus() == UdpConnection::cStatusDisconnected) + { + *prev = entry->mDisconnectPendingNextConnection; + entry->mDisconnectPendingNextConnection = NULL; + entry->Release(); + entry = *prev; + } + else + { + prev = &entry->mDisconnectPendingNextConnection; + entry = entry->mDisconnectPendingNextConnection; + } + } +} + +void UdpManager::RemoveConnection(UdpConnection *con) +{ + assert(con != NULL); // attemped to remove a NULL connection object + + // note: it's a bug to Remove a connection object that is already removed...should never be able to happen. + mConnectionListCount--; + if (con->mPrevConnection != NULL) + con->mPrevConnection->mNextConnection = con->mNextConnection; + if (con->mNextConnection != NULL) + con->mNextConnection->mPrevConnection = con->mPrevConnection; + if (mConnectionList == con) + mConnectionList = con->mNextConnection; + con->mNextConnection = NULL; + con->mPrevConnection = NULL; + if (mPriorityQueue != NULL) + mPriorityQueue->Remove(con); + + mAddressHashTable->Remove(con, AddressHashValue(con->mIp, con->mPort)); + mConnectCodeHashTable->Remove(con, con->mConnectCode); +} + +void UdpManager::AddConnection(UdpConnection *con) +{ + assert(con != NULL); // attemped to add a NULL connection object + + con->mNextConnection = mConnectionList; + con->mPrevConnection = NULL; + if (mConnectionList != NULL) + mConnectionList->mPrevConnection = con; + mConnectionList = con; + mConnectionListCount++; + + mAddressHashTable->Insert(con, AddressHashValue(con->mIp, con->mPort)); + mConnectCodeHashTable->Insert(con, con->mConnectCode); +} + +void UdpManager::FlushAllMultiBuffer() +{ + AddRef(); + UdpConnection *cur = mConnectionList; + while (cur != NULL) + { + cur->FlushMultiBuffer(); + cur = cur->mNextConnection; + } + Release(); +} + +bool UdpManager::GiveTime(int maxPollingTime, bool giveConnectionsTime) +{ + // process incoming raw packets from the port + AddRef(); // keep a reference to ourself in case we callback to the application and the application releases us. + + mManagerStats.iterations++; + + bool found = false; + if (maxPollingTime != 0) + { + UdpMisc::ClockStamp start = UdpMisc::Clock(); + do + { + PacketHistoryEntry *e = ActualReceive(); + + if (e == NULL) + { + mLastEmptySocketBufferStamp = UdpMisc::Clock(); + break; + } + + // if the application takes too long to process packets, or doesn't give the UdpManager frequent enough time via GiveTime + // then it's possible that we will have a clock-sync packet that is sitting in the socket buffer waiting to be processed + // we don't want the applications inability to give us frequent processing time to totally whack up the clock sync stuff + // so we have the clock-sync code ignore clock-sync packets that get stalled in the socket buffer for too long because our + // application is busy processing other packets that were queued before it, or because the application paused for a long + // time before calling GiveTime. + // note: this is intended to prevent cpu induced stalls from causing a sync packet to appear to take longer. For example + // if the player is on a modem, it's possible for the socket-buffer to fill up while the application is stalled and cause the + // the sync-packet to actually get stalled at the terminal buffer on the other end up of the modem. When the application starts + // processing again, it will empty the socket-buffer, but then the get an empty-socket-buffer briefly until the terminal server + // can send the rest of the buffered packets on over. A large client side receive socket buffer may help in this regard. + mProcessingInducedLag = UdpMisc::ClockElapsed(mLastEmptySocketBufferStamp); + found = true; + if (e->mLen > 0) + ProcessRawPacket(e); + } while (UdpMisc::ClockElapsed(start) < maxPollingTime); + + ProcessIcmpErrors(); + } + + if (giveConnectionsTime) + { + if (mPriorityQueue != NULL) + { + // give time to everybody in the priority-queue that needs it + UdpMisc::ClockStamp curPriority = UdpMisc::Clock(); + + // at the time we start processing the priority queue, we should effectively be taking a snap-shot + // of everybody who needs time, before we give anybody time. Otherwise, it is possible that in the + // process of giving one connection time, another connection could get bumped up the queue to the point + // where it needs time now as well (for example, one connection sending another connection data during the + // give time phase). Although very rare, in theory this could result in an infinite loop situation. + // To solve this, we simply set the earliest time period that somebody can schedule for to 1 ms after + // the current time stamp that we are processing, effectively making it impossible for any connection + // to be given time twice in the same interation of the loop below + mMinimumScheduledStamp = curPriority + 1; + + for (;;) + { + UdpConnection *top = mPriorityQueue->TopRemove(curPriority); + if (top == NULL) + break; + top->AddRef(); + top->GiveTime(); + top->Release(); + mManagerStats.priorityQueueProcessed++; + } + mManagerStats.priorityQueuePossible += mConnectionListCount; + } + else + { + // give time to everybody + UdpConnection *cur = mConnectionList; + while (cur != NULL) + { + cur->GiveTime(); + cur = cur->mNextConnection; + } + } + + ProcessDisconnectPending(); + } + + if (mSimulateQueueStart != NULL && UdpMisc::Clock() >= mSimulateNextOutgoingTime) + { + SimulateQueueEntry *entry = mSimulateQueueStart; + mSimulateQueueStart = mSimulateQueueStart->mNext; + mSimulateNextOutgoingTime = UdpMisc::Clock() + (entry->mDataLen * 1000 / mParams.simulateOutgoingByteRate); + ActualSendHelper(entry->mData, entry->mDataLen, entry->mIp, entry->mPort); + + UdpConnection *con = AddressGetConnection(entry->mIp, entry->mPort); + if (con != NULL) + con->mSimulateQueueBytes -= entry->mDataLen; + mSimulateQueueBytes -= entry->mDataLen; + delete entry; + } + + Release(); + return(found); +} + +UdpConnection *UdpManager::EstablishConnection(const char *serverAddress, int serverPort, int timeout) +{ + assert(serverPort != 0); // can't connect to no port + assert(serverAddress != NULL); + assert(serverAddress[0] != 0); + + if (mConnectionListCount >= mParams.maxConnections) + return(NULL); + + // get server address + unsigned long address = inet_addr(serverAddress); + if (address == INADDR_NONE) + { + struct hostent * lphp; + lphp = gethostbyname(serverAddress); + if (lphp == NULL) + return(NULL); + address = ((struct in_addr *)(lphp->h_addr))->s_addr; + } + UdpIpAddress destIp(address); + + // first, see if we already have a connection object managing this ip/port, if we do, then fail + UdpConnection *con = AddressGetConnection(destIp, serverPort); + if (con != NULL) + return(NULL); + return(new UdpConnection(this, destIp, serverPort, timeout)); +} + +void UdpManager::KeepUntilDisconnected(UdpConnection *con) +{ + con->AddRef(); + con->mDisconnectPendingNextConnection = mDisconnectPendingList; + mDisconnectPendingList = con; +} + +void UdpManager::GetStats(UdpManagerStatistics *stats) const +{ + assert(stats != NULL); + *stats = mManagerStats; + stats->poolAvailable = mPoolAvailable; + stats->poolCreated = mPoolCreated; + stats->elapsedTime = UdpMisc::ClockElapsed(mManagerStatsResetTime); +} + +void UdpManager::ResetStats() +{ + mManagerStatsResetTime = UdpMisc::Clock(); + memset(&mManagerStats, 0, sizeof(mManagerStats)); +} + +void UdpManager::DumpPacketHistory(const char *filename) const +{ + assert(filename != NULL); + assert(filename[0] != 0); + FILE *file = fopen(filename, "wt"); + if (file != NULL) + { + // dump history of packets... + for (int i = 0; i < mParams.packetHistoryMax; i++) + { + int pos = (mPacketHistoryPosition + i) % mParams.packetHistoryMax; + + if (mPacketHistory[pos]->mLen > 0) + { + char hold[64]; + uchar *ptr = mPacketHistory[pos]->mBuffer; + fprintf(file, "%16s,%5d %3d: ", mPacketHistory[pos]->mIp.GetAddress(hold), mPacketHistory[pos]->mPort, mPacketHistory[pos]->mLen); + int len = mPacketHistory[pos]->mLen; + while (len-- > 0) + { + fprintf(file, "%02x ", *ptr); + ptr++; + } + fprintf(file, "\n"); + } + } + fclose(file); + } +} + +UdpIpAddress UdpManager::GetLocalIp() const +{ + struct sockaddr_in addr_self; + memset(&addr_self, 0, sizeof(addr_self)); + socklen_t len = sizeof(addr_self); + getsockname(mUdpSocket, (struct sockaddr *)&addr_self, &len); + return(UdpIpAddress(addr_self.sin_addr.s_addr)); +} + +int UdpManager::GetLocalPort() const +{ + struct sockaddr_in addr_self; + memset(&addr_self, 0, sizeof(addr_self)); + socklen_t len = sizeof(addr_self); + getsockname(mUdpSocket, (struct sockaddr *)&addr_self, &len); + return(ntohs(addr_self.sin_port)); +} + +UdpManager::PacketHistoryEntry *UdpManager::ActualReceive() +{ + if (mParams.simulateIncomingByteRate > 0 && UdpMisc::Clock() < mSimulateNextIncomingTime) + return(NULL); + + struct sockaddr_in addr_from; + socklen_t sf = sizeof(addr_from); + int pos = mPacketHistoryPosition; + int res = recvfrom(mUdpSocket, (char *)mPacketHistory[pos]->mBuffer, mParams.maxRawPacketSize, 0, (struct sockaddr *)&addr_from, &sf); + + if (res != SOCKET_ERROR) + { + if (mParams.simulateIncomingLossPercent > 0 && ((rand() % 100) < mParams.simulateIncomingLossPercent)) + return(NULL); // packet, what packet? + + if (mParams.simulateIncomingByteRate > 0) + mSimulateNextIncomingTime = UdpMisc::Clock() + (res * 1000 / mParams.simulateIncomingByteRate); + + mLastReceiveTime = UdpMisc::Clock(); + mPacketHistory[pos]->mLen = res; + mPacketHistory[pos]->mIp = UdpIpAddress(addr_from.sin_addr.s_addr); + mPacketHistory[pos]->mPort = (int)ntohs(addr_from.sin_port); + + mPacketHistoryPosition = (mPacketHistoryPosition + 1) % mParams.packetHistoryMax; + mManagerStats.bytesReceived += res; + mManagerStats.packetsReceived++; + return(mPacketHistory[pos]); + } + else + { +#if defined(WIN32) + // windows is kind enough to put ICMP error packets inline within the stream as errors, so we + // can easily see the errors indicating that the destination address is unreachable for some reason + if (WSAGetLastError() == WSAECONNRESET) + { + UdpIpAddress ip = UdpIpAddress(addr_from.sin_addr.s_addr); + int port = (int)ntohs(addr_from.sin_port); + UdpConnection *con = AddressGetConnection(ip, port); + if (con != NULL) + { + con->AddRef(); + con->PortUnreachable(); + con->Release(); + } + + // in order to get our parent to give us time again to poll another packet, we must return it a packet + // to process. We will do this by sending it an empty packet, which it will simply ignore and call us + // asking for yet another packet. We will enter an empty packet into the packet-history so we can effectively + // see these ICMP packets in the history. + mLastReceiveTime = UdpMisc::Clock(); + mPacketHistory[pos]->mLen = 0; + mPacketHistory[pos]->mIp = UdpIpAddress(addr_from.sin_addr.s_addr); + mPacketHistory[pos]->mPort = (int)ntohs(addr_from.sin_port); + mPacketHistoryPosition = (mPacketHistoryPosition + 1) % mParams.packetHistoryMax; + return(mPacketHistory[pos]); + } +#endif + } + return(NULL); +} + +void UdpManager::ProcessIcmpErrors() +{ +#if defined(WIN32) + // nothing needed for WIN32, it handles these errors inline with standard packets +#elif defined(sparc) + // Just to be bassackwards, solaris handles these errors on a subsequent sendto call +#else + // we can use some ICMP errors to our advantage to more quickly realize that the connection on the other end has disappeared + unsigned char msg_control[1024]; + struct msghdr msgh = {0}; + struct sockaddr_in msg_name; + socklen_t sf = sizeof(msg_name); + msgh.msg_name = &msg_name; + msgh.msg_namelen = sf; + msgh.msg_iov = 0; + msgh.msg_iovlen = 0; + msgh.msg_control = msg_control; + msgh.msg_controllen = sizeof(msg_control); + + int err = recvmsg(mUdpSocket, &msgh, MSG_ERRQUEUE); + if(err != -1) + { + struct cmsghdr * cmsg; + if(CMSG_FIRSTHDR(&msgh)) + { + for(cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL; cmsg = CMSG_NXTHDR(&msgh, cmsg)) + { + if(cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR) + { + // HACK! HACK! Can't find any portable definition of sock_extended_err! + unsigned char * errData = (unsigned char *)CMSG_DATA(cmsg); + if(errData[4] == 2) // ICMP origin + { + uchar code = errData[6]; + if(code == ICMP_PORT_UNREACH) + { + UdpIpAddress ip = UdpIpAddress(msg_name.sin_addr.s_addr); + int port = (int)htons(msg_name.sin_port); + UdpConnection *con = AddressGetConnection(ip, port); + if (con != NULL) + { + con->AddRef(); + con->PortUnreachable(); + con->Release(); + } + } + } + } + } + } + else + { + // ancillary data missing + } + } +#endif +} + +void UdpManager::ActualSend(const uchar *data, int dataLen, UdpIpAddress ip, int port) +{ + mLastSendTime = UdpMisc::Clock(); + mManagerStats.bytesSent += dataLen; + mManagerStats.packetsSent++; + + if (mParams.simulateOutgoingByteRate != 0) + { + // simulating outgoing byte-rate, so queue it up for sending later + UdpConnection *con = AddressGetConnection(ip, port); + if (con != NULL) + { + if (mParams.simulateDestinationOverloadLevel > 0 && con->mSimulateQueueBytes + dataLen > mParams.simulateDestinationOverloadLevel) + return; // no room, packet gets lost + } + if (mParams.simulateOutgoingOverloadLevel > 0 && mSimulateQueueBytes + dataLen > mParams.simulateOutgoingOverloadLevel) + return; // no room, packet gets lost + + if (con != NULL) + con->mSimulateQueueBytes += dataLen; + mSimulateQueueBytes += dataLen; + SimulateQueueEntry *entry = new SimulateQueueEntry(data, dataLen, ip, port); + + if (mSimulateQueueStart != NULL) + mSimulateQueueEnd->mNext = entry; + else + mSimulateQueueStart = entry; + mSimulateQueueEnd = entry; + mSimulateQueueEnd->mNext = NULL; + return; + } + ActualSendHelper(data, dataLen, ip, port); +} + +void UdpManager::ActualSendHelper(const uchar *data, int dataLen, UdpIpAddress ip, int port) +{ + if (mParams.simulateOutgoingLossPercent > 0 && ((rand() % 100) < mParams.simulateOutgoingLossPercent)) + return; + struct sockaddr_in addr_dest; + addr_dest.sin_family = PF_INET; + addr_dest.sin_addr.s_addr = ip.GetAddress(); + addr_dest.sin_port = htons((ushort)port); + if (SOCKET_ERROR == sendto(mUdpSocket, (const char *)data, dataLen, 0, (struct sockaddr *)&addr_dest, sizeof(addr_dest))) + { + // error writing to socket, what is the error? +#if defined(sparc) + if(errno == ECONNREFUSED || errno == EHOSTUNREACH) + { + // flag connection to terminate itself for port-unreachable error on next give time + // we need to flag it instead of actually terminating it to prevent callbacks from occuring during application sends + UdpConnection *con = AddressGetConnection(ip, port); + if (con != NULL) + con->FlagPortUnreachable(); + return; + } +#endif + + // error types are OS specific, so unless a particular OS grabs the error and treats it differently just above (as sparc does) + // then we are going to just treat all errors as socket overflows (which we only track for statistical purposes) + mManagerStats.socketOverflowErrors++; + } +} + +void UdpManager::SendPortAlive(UdpIpAddress ip, int port) +{ + uchar buf[2]; + buf[0] = 0; + buf[1] = UdpConnection::cUdpPacketPortAlive; + +#if defined(WIN32) + int val = 5; + setsockopt(mUdpSocket, IPPROTO_IP, IP_TTL, (char *)&val, sizeof(val)); + ActualSendHelper(buf, 2, ip, port); + setsockopt(mUdpSocket, IPPROTO_IP, IP_TTL, (char *)&mStartTtl, sizeof(mStartTtl)); +#else + unsigned long val = 5; + setsockopt(mUdpSocket, IPPROTO_IP, IP_TTL, &val, sizeof(val)); + ActualSendHelper(buf, 2, ip, port); + setsockopt(mUdpSocket, IPPROTO_IP, IP_TTL, &mStartTtl, sizeof(mStartTtl)); +#endif +} + +void UdpManager::ProcessRawPacket(const PacketHistoryEntry *e) +{ + if (e->mBuffer[0] == 0 && e->mBuffer[1] == UdpConnection::cUdpPacketPortAlive) + return; // port-alive packets are not supposed to reach the destination machine, but on the odd chance they do, pretend like they never existed + + UdpConnection *con = AddressGetConnection(e->mIp, e->mPort); + + if (con == NULL) + { + // packet coming from an unknown ip/port + // if it is a connection request packet, then establish a new connection object to reply to it + // connection establish packet must always be at least 6 bytes long as we must have a version number, no matter how it changes + if (e->mBuffer[0] == 0 && e->mBuffer[1] == UdpConnection::cUdpPacketConnect && e->mLen == UdpConnection::cUdpPacketConnectSize) + { + if (mConnectionListCount >= mParams.maxConnections) + return; // can't handle any more connections, so ignore this request entirely + + int protocolVersion = UdpMisc::GetValue32(e->mBuffer + 2); + if (protocolVersion == cProtocolVersion) + { + if (mParams.handler != NULL) + { + UdpConnection *newcon = new UdpConnection(this, e); + mParams.handler->OnConnectRequest(newcon); + if (newcon->GetRefCount() == 1) + { + // we are going to end up destroying this connection when we release it on this next line + // so disconnect it first giving it a reason + newcon->InternalDisconnect(0, UdpConnection::cDisconnectReasonConnectionRefused); + } + newcon->Release(); + } + } + } + else + { + if (mParams.allowPortRemapping) + { + if (e->mBuffer[0] == 0 && e->mBuffer[1] == UdpConnection::cUdpPacketRequestRemap) + { + // ok, we got a packet from somebody, that we don't know who they are, but, it appears they are asking + // for their address/port to be remapped. If we allow port (and/or address) remapping, then go ahead + // an honor their request if possible + uchar *ptr = e->mBuffer + 2; + int connectCode = UdpMisc::GetValue32(ptr); + ptr += 4; + int encryptCode = UdpMisc::GetValue32(ptr); + + UdpConnection *con = ConnectCodeGetConnection(connectCode); + if (con != NULL) + { + if (mParams.allowAddressRemapping || con->mIp == e->mIp) + { + // one final security check to ensure these are really the same connection, compare encryption codes + if (con->mConnectionConfig.encryptCode == encryptCode) + { + // remapping is allowed, remap ourselves to the address of the incoming request + mAddressHashTable->Remove(con, AddressHashValue(con->mIp, con->mPort)); + con->mIp = e->mIp; + con->mPort = e->mPort; + mAddressHashTable->Insert(con, AddressHashValue(con->mIp, con->mPort)); + return; + } + } + } + } + } + + + // got a packet from somebody and we don't know who they are and the packet we got was not a connection request + // just in case they are a previous client who thinks they are still connected, we will send them an internal + // packet telling them that we don't know who they are + if (mParams.replyUnreachableConnection) + { + // do not reply back with unreachable if the packet coming in is a terminate or unreachable packet itself + if (e->mBuffer[0] != 0 || (e->mBuffer[0] == 0 && e->mBuffer[1] != UdpConnection::cUdpPacketUnreachableConnection && e->mBuffer[1] != UdpConnection::cUdpPacketTerminate)) + { + // since we do not have a connection-object associated with this incoming packet, there is no way we could + // encrypt it or add CRC bytes to it, since we have no idea what the other end of the connection is expecting + // in this regard. As such, the UnreachableConnection packet (like the connect and confirm packets) is one + // of those internal packet types that is designated as not being encrypted or CRC'ed. + unsigned char buf[8]; + buf[0] = 0; + buf[1] = UdpConnection::cUdpPacketUnreachableConnection; + ActualSend(buf, 2, e->mIp, e->mPort); + } + } + } + return; + } + + con->AddRef(); + con->ProcessRawPacket(e); + con->Release(); +} + +UdpConnection *UdpManager::AddressGetConnection(UdpIpAddress ip, int port) const +{ + UdpConnection *found = static_cast(mAddressHashTable->FindFirst(AddressHashValue(ip, port))); + while (found != NULL) + { + if (found->mIp == ip && found->mPort == port) + return(found); + found = static_cast(mAddressHashTable->FindNext(found)); + } + return(NULL); +} + +UdpConnection *UdpManager::ConnectCodeGetConnection(int connectCode) const +{ + UdpConnection *found = static_cast(mConnectCodeHashTable->FindFirst(connectCode)); + while (found != NULL) + { + if (found->mConnectCode == connectCode) + return(found); + found = static_cast(mConnectCodeHashTable->FindNext(found)); + } + return(NULL); +} + +WrappedLogicalPacket *UdpManager::WrappedBorrow(const LogicalPacket *lp) +{ + if (mWrappedAvailable > 0) + { + WrappedLogicalPacket *wp = mWrappedAvailableRoot; + mWrappedAvailableRoot = mWrappedAvailableRoot->mAvailableNext; + mWrappedAvailable--; + wp->SetLogicalPacket(lp); + return(wp); + } + else + { + WrappedLogicalPacket *wp = new WrappedLogicalPacket(this); + wp->SetLogicalPacket(lp); + return(wp); + } +} + +void UdpManager::WrappedCreated(WrappedLogicalPacket *wp) +{ + wp->mCreatedNext = mWrappedCreatedRoot; + if (mWrappedCreatedRoot != NULL) + mWrappedCreatedRoot->mCreatedPrev = wp; + mWrappedCreatedRoot = wp; + mWrappedCreated++; +} + +void UdpManager::WrappedDestroyed(WrappedLogicalPacket *wp) +{ + if (wp->mCreatedNext != NULL) + { + wp->mCreatedNext->mCreatedPrev = wp->mCreatedPrev; + } + if (wp->mCreatedPrev != NULL) + { + wp->mCreatedPrev->mCreatedNext = wp->mCreatedNext; + } + else + { + // we are first entry, set root + mWrappedCreatedRoot = wp->mCreatedNext; + } + + wp->mCreatedPrev = NULL; + wp->mCreatedNext = NULL; + wp->mUdpManager = NULL; + mWrappedCreated--; +} + +LogicalPacket *UdpManager::CreatePacket(const void *data, int dataLen, const void *data2, int dataLen2) +{ + if (mParams.pooledPacketMax > 0) + { + int totalLen = dataLen + dataLen2; + if (totalLen <= mParams.pooledPacketSize) + { + if (mPoolAvailable > 0) + { + PooledLogicalPacket *lp = mPoolAvailableRoot; + mPoolAvailableRoot = mPoolAvailableRoot->mAvailableNext; + mPoolAvailable--; + lp->SetData(data, dataLen, data2, dataLen2); + return(lp); + } + else + { + // create a new pooled packet to fulfil request + PooledLogicalPacket *lp = new PooledLogicalPacket(this, mParams.pooledPacketSize); + lp->SetData(data, dataLen, data2, dataLen2); + return(lp); + } + } + } + + return(UdpMisc::CreateQuickLogicalPacket(data, dataLen, data2, dataLen2)); +} + +void UdpManager::PoolCreated(PooledLogicalPacket *packet) +{ + packet->mCreatedNext = mPoolCreatedRoot; + if (mPoolCreatedRoot != NULL) + mPoolCreatedRoot->mCreatedPrev = packet; + mPoolCreatedRoot = packet; + mPoolCreated++; +} + +void UdpManager::PoolDestroyed(PooledLogicalPacket *packet) +{ + if (packet->mCreatedNext != NULL) + { + packet->mCreatedNext->mCreatedPrev = packet->mCreatedPrev; + } + if (packet->mCreatedPrev != NULL) + { + packet->mCreatedPrev->mCreatedNext = packet->mCreatedNext; + } + else + { + // we are first entry, set root + mPoolCreatedRoot = packet->mCreatedNext; + } + + packet->mCreatedPrev = NULL; + packet->mCreatedNext = NULL; + packet->mUdpManager = NULL; + mPoolCreated--; +} + + + + ///////////////////////////////////////////////////////////////////////////////////////////////////// + // PacketHistory implementation + ///////////////////////////////////////////////////////////////////////////////////////////////////// +UdpManager::PacketHistoryEntry::PacketHistoryEntry(int maxRawPacketSize) +{ + mBuffer = new uchar[maxRawPacketSize]; + mPort = 0; + mLen = 0; +} + +UdpManager::PacketHistoryEntry::~PacketHistoryEntry() +{ + delete[] mBuffer; +} + + + ///////////////////////////////////////////////////////////////////////////////////////////////////// + // UdpConnection implementation + ///////////////////////////////////////////////////////////////////////////////////////////////////// + +UdpConnection::UdpConnection(UdpManager *udpManager, UdpIpAddress destIp, int destPort, int timeout) +{ + // client side initializations + Init(udpManager, destIp, destPort); + + mConnectAttemptTimeout = timeout; + mStatus = cStatusNegotiating; + mConnectCode = (rand() << 16) | rand(); + mUdpManager->AddConnection(this); + + GiveTime(); +} + +UdpConnection::UdpConnection(UdpManager *udpManager, const UdpManager::PacketHistoryEntry *e) +{ + // server side initialization + Init(udpManager, e->mIp, e->mPort); + + mStatus = cStatusConnected; + for (int j = 0; j < UdpManager::cEncryptPasses; j++) + mConnectionConfig.encryptMethod[j] = mUdpManager->mParams.encryptMethod[j]; + mConnectionConfig.crcBytes = mUdpManager->mParams.crcBytes; + mConnectionConfig.maxRawPacketSize = mUdpManager->mParams.maxRawPacketSize; + mConnectionConfig.encryptCode = (rand() << 16) | rand(); + SetupEncryptModel(); + + // steal the connect code out of the packet early such that ProcessRawPacket will think it's a valid connect packet instead of ignoring it + // plus, the AddConnection function needs to know our connect code + mConnectCode = UdpMisc::GetValue32(e->mBuffer + 6); + mUdpManager->AddConnection(this); + + ProcessRawPacket(e); + GiveTime(); +} + +void UdpConnection::Init(UdpManager *udpManager, UdpIpAddress destIp, int destPort) +{ + mRefCount = 1; + mUdpManager = udpManager; + mIp = destIp; + mPort = destPort; + + mFlaggedPortUnreachable = false; + + mLastPortAliveTime = mLastSendTime = 0; // makes it send out the first connect packet immediately (if we are in negotiating mode) + mLastReceiveTime = UdpMisc::Clock(); + mLastClockSyncTime = 0; + mDataHoldTime = 0; + mGettingTime = false; + mHandler = NULL; + + mNoDataTimeout = mUdpManager->mParams.noDataTimeout; + mKeepAliveDelay = mUdpManager->mParams.keepAliveDelay; + + mMultiBufferData = new uchar[mUdpManager->mParams.maxRawPacketSize]; + mMultiBufferPtr = mMultiBufferData; + + mDisconnectPendingNextConnection = NULL; + mNextConnection = NULL; + mPrevConnection = NULL; + mIcmpErrorRetryStartStamp = 0; // when the timer started for ICMP error retry delay (gets reset on a successful packet receive) + mPortRemapRequestStartStamp = 0; + + mEncryptXorBuffer = NULL; + mEncryptExpansionBytes = 0; + mOrderedCountOutgoing = 0; + mOrderedCountOutgoing2 = 0; + mOrderedStampLast = 0; + mOrderedStampLast2 = 0; + mDisconnectReason = cDisconnectReasonNone; + mOtherSideDisconnectReason = cDisconnectReasonNone; + + mConnectionCreateTime = UdpMisc::Clock(); + mSimulateQueueBytes = 0; + mPassThroughData = NULL; + mSilentDisconnect = false; + + mLastSendBin = 0; + mLastReceiveBin = 0; + mOutgoingBytesLastSecond = 0; + mIncomingBytesLastSecond = 0; + memset(mSendBin, 0, sizeof(mSendBin)); + memset(mReceiveBin, 0, sizeof(mReceiveBin)); + + PingStatReset(); + mSyncTimeDelta = 0; + memset(mChannel, 0, sizeof(mChannel)); + memset(&mConnectionStats, 0, sizeof(mConnectionStats)); +} + +UdpConnection::~UdpConnection() +{ + if (mUdpManager != NULL) + InternalDisconnect(0, mDisconnectReason); + + for (int i = 0; i < UdpManager::cReliableChannelCount; i++) + delete mChannel[i]; + delete[] mMultiBufferData; + delete[] mEncryptXorBuffer; +} + +void UdpConnection::PortUnreachable() +{ + if (!mUdpManager->mParams.processIcmpErrors) + return; + + if (!mUdpManager->mParams.processIcmpErrorsDuringNegotiating) + { + if (mStatus == cStatusNegotiating) // during negotiating phase, ignore port unreachable errors, since it may be a case of the client starting up first + return; + } + + if (mUdpManager->mParams.icmpErrorRetryPeriod != 0) + { + if (mIcmpErrorRetryStartStamp == 0) + { + mIcmpErrorRetryStartStamp = UdpMisc::Clock(); // start timer on how long we will ignore ICMP errors + return; + } + + if (UdpMisc::ClockElapsed(mIcmpErrorRetryStartStamp) < mUdpManager->mParams.icmpErrorRetryPeriod) + { + return; // ignoring ICMP errors for a period of time + } + } + + InternalDisconnect(0, cDisconnectReasonIcmpError); +} + +void UdpConnection::InternalDisconnect(int flushTimeout, DisconnectReason reason) +{ + mDisconnectReason = reason; + Status startStatus = mStatus; + UdpManager *startUdpManager = mUdpManager; + + // if we are in a negotiating state, then you can't have a flushTimeout, any disconnect will occur immediately + if (mStatus == cStatusNegotiating) + flushTimeout = 0; + + if (mUdpManager != NULL) + { + if (flushTimeout > 0) + { + FlushMultiBuffer(); + mDisconnectFlushStamp = UdpMisc::Clock(); + mDisconnectFlushTimeout = flushTimeout; + ScheduleTimeNow(); + + if (mStatus != cStatusDisconnectPending) + { + mStatus = cStatusDisconnectPending; + mUdpManager->KeepUntilDisconnected(this); + } + return; + } + + // send a termination packet to the other side + // do not send a termination packet if we are still negotiating (we are not allowed to send any packets while negotiating) + // if you attempt to send a packet while negotiating, then it will potentially attempt to encrypt it before an encryption + // method is determined, resulting in a function call through an invalid pointer + if (!mSilentDisconnect) + { + if (mStatus == cStatusConnected || mStatus == cStatusDisconnectPending) + { + SendTerminatePacket(mConnectCode, mDisconnectReason); + } + } + + mUdpManager->RemoveConnection(this); + mUdpManager = NULL; + } + mStatus = cStatusDisconnected; + + if (startStatus != cStatusDisconnected && startUdpManager != NULL) + { + if (mHandler != NULL) + mHandler->OnTerminated(this); + } +} + +void UdpConnection::SendTerminatePacket(int connectCode, DisconnectReason reason) +{ + uchar buf[256]; + buf[0] = 0; + buf[1] = cUdpPacketTerminate; + UdpMisc::PutValue32(buf + 2, connectCode); + UdpMisc::PutValue16(buf + 6, (ushort)reason); + PhysicalSend(buf, 8, true); +} + + +void UdpConnection::SetSilentDisconnect(bool silent) +{ + // this function tells the connection to disconnect silently, meaning that it should + // not send a packet to the other side telling it that the connection is being terminated + mSilentDisconnect = silent; +} + +bool UdpConnection::Send(UdpChannel channel, const void *data, int dataLen) +{ + assert(dataLen >= 0); + assert(channel >= 0 && channel < cUdpChannelCount); + assert(mStatus != cStatusNegotiating); // you are not allowed to start sending data on a connection that is still in the process of negotiating (only applicable client-side obviously since servers never have connections in this state) + + if (mStatus != cStatusConnected) // if we are no longer connected (not allowed to send more when we are pending disconnect either) + return(false); + if (dataLen == 0) // zero length packets are ignored + return(false); + + assert(data != NULL); // can't send a null packet + + mUdpManager->mManagerStats.applicationPacketsSent++; + mConnectionStats.applicationPacketsSent++; + + // zero-escape application packets that start with 0 + if ((*(const uchar *)data) == 0) + { + uchar hold = 0; + return(InternalSend(channel, &hold, 1, (const uchar *)data, dataLen)); + } + + return(InternalSend(channel, (const uchar *)data, dataLen)); +} + +bool UdpConnection::Send(UdpChannel channel, const LogicalPacket *packet) +{ + assert(packet != NULL); // can't send a null packet + assert(channel >= 0 && channel < cUdpChannelCount); + assert(mStatus != cStatusNegotiating); // you are not allowed to start sending data on a connection that is still in the process of negotiating (only applicable client-side obviously since servers never have connections in this state) + + if (mStatus != cStatusConnected) // if we are no longer connected + return(false); + int dataLen = packet->GetDataLen(); + if (dataLen == 0) + return(false); + + mUdpManager->mManagerStats.applicationPacketsSent++; + mConnectionStats.applicationPacketsSent++; + + // zero-escape application packets that start with 0 + const uchar *data = (const uchar *)packet->GetDataPtr(); + if (!packet->IsInternalPacket() && data[0] == 0) + { + uchar hold = 0; + return(InternalSend(channel, &hold, 1, data, dataLen)); + } + + return(InternalSend(channel, packet)); +} + +bool UdpConnection::InternalSend(UdpChannel channel, const uchar *data, int dataLen, const uchar *data2, int dataLen2) +{ + // promote unreliable packets that are larger than maxRawPacketSize to be reliable + int totalDataLen = dataLen + dataLen2; + + int rawDataBytesMax = (mConnectionConfig.maxRawPacketSize - mConnectionConfig.crcBytes - mEncryptExpansionBytes); + if ((channel == cUdpChannelUnreliable || channel == cUdpChannelUnreliableUnbuffered) && totalDataLen > rawDataBytesMax) + channel = cUdpChannelReliable1; + else if ((channel == cUdpChannelOrdered || channel == cUdpChannelOrderedUnbuffered) && totalDataLen > rawDataBytesMax - cUdpPacketOrderedSize) + channel = cUdpChannelReliable1; + + uchar tempBuffer[UdpManager::cHardMaxRawPacketSize]; + switch(channel) + { + case cUdpChannelUnreliable: + BufferedSend(data, dataLen, data2, dataLen2, false); + return(true); + break; + case cUdpChannelUnreliableUnbuffered: + { + uchar *bufPtr = tempBuffer; + memcpy(bufPtr, data, dataLen); + if (data2 != NULL) + memcpy(bufPtr + dataLen, data2, dataLen2); + PhysicalSend(bufPtr, totalDataLen, true); + return(true); + break; + } + case cUdpChannelOrdered: + { + uchar *bufPtr = tempBuffer; + bufPtr[0] = 0; + bufPtr[1] = cUdpPacketOrdered2; + UdpMisc::PutValue16(bufPtr + 2, (ushort)(++mOrderedCountOutgoing2 & 0xffff)); + memcpy(bufPtr + 4, data, dataLen); + if (data2 != NULL) + memcpy(bufPtr + 4 + dataLen, data2, dataLen2); + BufferedSend(bufPtr, totalDataLen + 4, NULL, 0, true); + return(true); + break; + } + case cUdpChannelOrderedUnbuffered: + { + uchar *bufPtr = tempBuffer; + bufPtr[0] = 0; + bufPtr[1] = cUdpPacketOrdered2; + UdpMisc::PutValue16(bufPtr + 2, (ushort)(++mOrderedCountOutgoing2 & 0xffff)); + memcpy(bufPtr + 4, data, dataLen); + if (data2 != NULL) + memcpy(bufPtr + 4 + dataLen, data2, dataLen2); + PhysicalSend(bufPtr, totalDataLen + 4, true); + return(true); + break; + } + case cUdpChannelReliable1: + case cUdpChannelReliable2: + case cUdpChannelReliable3: + case cUdpChannelReliable4: + { + int num = channel - cUdpChannelReliable1; + if (mChannel[num] == NULL) + mChannel[num] = new UdpReliableChannel(num, this, &mUdpManager->mParams.reliable[num]); + mChannel[num]->Send(data, dataLen, data2, dataLen2); + return(true); + break; + } + default: + break; + } + return(false); +} + +bool UdpConnection::InternalSend(UdpChannel channel, const LogicalPacket *packet) +{ + switch(channel) + { + case cUdpChannelReliable1: + case cUdpChannelReliable2: + case cUdpChannelReliable3: + case cUdpChannelReliable4: + { + int num = channel - cUdpChannelReliable1; + if (mChannel[num] == NULL) + mChannel[num] = new UdpReliableChannel(num, this, &mUdpManager->mParams.reliable[num]); + mChannel[num]->Send(packet); + return(true); + break; + } + default: // unhandled members (cUdpChannelUnreliable, etc...) + { + // 3 April 2002 - jrandall + // moved this from beginning of statement to + // 1) satisfy compiler warnings about unhandled enum member + // 2) avoid the additional branch unnecessarily incurred + // when sending reliable messages + if (channel < cUdpChannelReliable1) // if going unreliably + return(InternalSend(channel, (const uchar *)packet->GetDataPtr(), packet->GetDataLen())); + break; + } + } + return(false); +} + +void UdpConnection::PingStatReset() +{ + mLastClockSyncTime = 0; // tells it to resync the clock pronto + mSyncStatMasterFixupTime = 0; + mSyncStatMasterRoundTime = 0; + mSyncStatLow = 0; + mSyncStatHigh = 0; + mSyncStatLast = 0; + mSyncStatTotal = 0; + mSyncStatCount = 0; + mConnectionStats.averagePingTime = 0; + mConnectionStats.highPingTime = 0; + mConnectionStats.lowPingTime = 0; + mConnectionStats.lastPingTime = 0; + mConnectionStats.masterPingTime = 0; +} + +void UdpConnection::GetStats(UdpConnectionStatistics *cs) const +{ + assert(cs != NULL); + + if (mUdpManager == NULL) + return; + *cs = mConnectionStats; + + if (mUdpManager->mParams.clockSyncDelay == 0) + cs->masterPingAge = -1; + else + cs->masterPingAge = UdpMisc::ClockElapsed(mSyncStatMasterFixupTime); + + cs->percentSentSuccess = 1.0; + cs->percentReceivedSuccess = 1.0; + if (cs->syncOurSent > 0) + cs->percentSentSuccess = (float)cs->syncTheirReceived / (float)cs->syncOurSent; + if (cs->syncTheirSent > 0) + cs->percentReceivedSuccess = (float)cs->syncOurReceived / (float)cs->syncTheirSent; + cs->reliableAveragePing = 0; + if (mChannel[0] != NULL) + cs->reliableAveragePing = mChannel[0]->GetAveragePing(); +} + +void UdpConnection::ProcessRawPacket(const UdpManager::PacketHistoryEntry *e) +{ + if (mUdpManager == NULL) + return; + + if (e->mBuffer[0] != 0 || e->mBuffer[1] != cUdpPacketUnreachableConnection) + { + // if we get any type of packet other than an unreachable-connection packet, then we can assume that our remapping + // request succeeded, and clear the timer for how long we should attempt to do the remapping. The reason we need + // to send requests for a certain amount of time, is the server may already have dozens of unreachable-connection packets + // on the wire on the way to us, before we manage to request that the remapping occur. + mPortRemapRequestStartStamp = 0; + } + + mIcmpErrorRetryStartStamp = 0; // we received a packet successfully, so assume we have recovered from any ICMP error state we may have been in, so we can reset the timer + mLastReceiveTime = UdpMisc::Clock(); + mConnectionStats.totalPacketsReceived++; + mConnectionStats.totalBytesReceived += e->mLen; + + // track incoming data rate + mLastReceiveBin = ExpireReceiveBin(); + mReceiveBin[mLastReceiveBin % cBinCount] += e->mLen; + mIncomingBytesLastSecond += e->mLen; + + if (e->mBuffer[0] == 0 && e->mBuffer[1] == cUdpPacketKeepAlive) + { + // encryption can't mess up the first two bytes of an internal packet, so this is safe to check + // if it is a keep alive packet, then we don't need to do any more processing beyond setting + // the mLastReceiveTime. We do this check here instead of letting it pass on through harmlessly + // like we used to do in order to avoid getting rescheduled in the priority queue. There is absolutely + // no reason to reschedule us due to an incoming keep alive packet since the keep-alive packet has the + // longest rescheduling of anything that needs time, so the worst thing that might happen is we might + // end up getting sheduled time sooner than we might otherwise need to. And obviously scheduling + // ourselves for immediate-time is even sooner than that, so there is no point. + // This turns out to be important for applications that have lots of connections (tens of thousands) + // that rarely talk but send keep alives...no reason to make the server do a lot work over these things. + return; + } + + // whenever we receive a packet, it could potentially change when we want time scheduled again + // so effectively we should reprioritize ourself to the top. By doing it this way instead of + // simply giving time and recalculating, we can effectively avoid giving ourself time and reprioritizing + // ourself over and over again as more and more packets arrive in rapid succession + // note: this cannot happen while we are in our UdpConnection::GiveTime function, so there is no need to squeltch check + // it like we do the others. + // note: this was moved to the top of the function from the bottom. This doesn't effect anything as it doesn't matter + // when we schedule ourself for future processing. Moving it to the top allowed us to get scheduled even if the packet + // we processed got rejected for some reason (crc mismatch or bad size). + ScheduleTimeNow(); + + if (e->mLen < 1) + { + CallbackCorruptPacket(e->mBuffer, e->mLen, cCorruptionReasonZeroLengthPacket); + return; // invalid packet len + } + + // first see if we are a special connect/confirm/unreachable packet, if so, process us immediately + if (IsNonEncryptPacket(e->mBuffer)) + { + ProcessCookedPacket(e->mBuffer, e->mLen); + } + else + { + // if we are still awaiting confirmation packet, then we must ignore any other incoming data packets + // this can happen if the confirm packet is lost and the server has dumped a load of data on the newly created connection + if (mStatus == cStatusNegotiating) + return; + + uchar *finalStart = e->mBuffer; + int finalLen = e->mLen; + + if (mConnectionConfig.crcBytes > 0) + { + if (finalLen < mConnectionConfig.crcBytes) + { + CallbackCorruptPacket(e->mBuffer, e->mLen, cCorruptionReasonPacketShorterThanCrcBytes); + return; // invalid packet len + } + + uchar *crcPtr = finalStart + (finalLen - mConnectionConfig.crcBytes); + int actualCrc = UdpMisc::Crc32(finalStart, finalLen - mConnectionConfig.crcBytes, mConnectionConfig.encryptCode); + int wantCrc = 0; + switch(mConnectionConfig.crcBytes) + { + case 1: + wantCrc = *crcPtr; + actualCrc &= 0xff; + break; + case 2: + wantCrc = UdpMisc::GetValue16(crcPtr); + actualCrc &= 0xffff; + break; + case 3: + wantCrc = UdpMisc::GetValue24(crcPtr); + actualCrc &= 0xffffff; + break; + case 4: + wantCrc = UdpMisc::GetValue32(crcPtr); + break; + } + if (wantCrc != actualCrc) + { + mConnectionStats.crcRejectedPackets++; + mUdpManager->mManagerStats.crcRejectedPackets++; + if (mHandler != NULL) + mHandler->OnCrcReject(this, e->mBuffer, e->mLen); + return; + } + finalLen -= mConnectionConfig.crcBytes; + } + + uchar tempDecryptBuffer[2][UdpManager::cHardMaxRawPacketSize]; + + for (int j = UdpManager::cEncryptPasses - 1; j >= 0; j--) + { + if (mConnectionConfig.encryptMethod[j] != UdpManager::cEncryptMethodNone) + { + // connect/confirm/unreachable packets are not encrypted, other packets are encrypted from the second or third byte on as appropriate + uchar *decryptPtr = tempDecryptBuffer[j % 2]; + *decryptPtr++ = finalStart[0]; + + if (finalStart[0] == 0) + { + if (finalLen < 2) + { + CallbackCorruptPacket(e->mBuffer, e->mLen, cUdpCorruptionInternalPacketTooShort); + return; // invalid packet len + } + + *decryptPtr++ = finalStart[1]; + int len = (this->*(mDecryptFunction[j]))(decryptPtr, finalStart + 2, finalLen - 2); + if (len == -1) + { + CallbackCorruptPacket(e->mBuffer, e->mLen, cUdpCorruptionDecryptFailed); + return; // decrypt failed, throw away packet + } + decryptPtr += len; + } + else + { + int len = (this->*(mDecryptFunction[j]))(decryptPtr, finalStart + 1, finalLen - 1); + if (len == -1) + { + CallbackCorruptPacket(e->mBuffer, e->mLen, cUdpCorruptionDecryptFailed); + return; // decrypt failed, throw away packet + } + decryptPtr += len; + } + + finalStart = tempDecryptBuffer[j % 2]; + finalLen = decryptPtr - finalStart; + } + } + + ProcessCookedPacket(finalStart, finalLen); + } +} + +void UdpConnection::CallbackRoutePacket(const uchar *data, int dataLen) +{ + if (mStatus == cStatusConnected) + { + mUdpManager->mManagerStats.applicationPacketsReceived++; + mConnectionStats.applicationPacketsReceived++; + + if (mHandler != NULL) + mHandler->OnRoutePacket(this, data, dataLen); + } +} + +void UdpConnection::CallbackCorruptPacket(const uchar *data, int dataLen, UdpCorruptionReason reason) +{ + mConnectionStats.corruptPacketErrors++; + mUdpManager->mManagerStats.corruptPacketErrors++; + if (mHandler != NULL) + mHandler->OnPacketCorrupt(this, data, dataLen, reason); +} + +void UdpConnection::ProcessCookedPacket(const uchar *data, int dataLen) +{ + uchar buf[256]; + uchar *bufPtr; + if (mUdpManager == NULL) + return; + + if (data[0] == 0 && dataLen > 1) + { + // internal packet, so process it internally + switch(data[1]) + { + case cUdpPacketConnect: + { + int connectCode = UdpMisc::GetValue32(data + 6); + + if (mStatus == cStatusNegotiating) + { + // why are we receiving a connect-request coming from the guy we ourselves are currently + // in the process of trying to connect to? Odds are very high that what is actually + // happening is we are trying to connect to ourself. In either case, we should reply + // back telling them they are terminated. + if (connectCode == mConnectCode) + SendTerminatePacket(connectCode, cDisconnectReasonConnectingToSelf); + else + SendTerminatePacket(connectCode, cDisconnectReasonMutualConnectError); + return; + } + + if (connectCode == mConnectCode) + { + mConnectionConfig.maxRawPacketSize = udpMin((int)UdpMisc::GetValue32(data + 10), mConnectionConfig.maxRawPacketSize); + + // send confirm packet (if our connect code matches up) + // prepare UdpPacketConnect packet + bufPtr = buf; + *bufPtr++ = 0; + *bufPtr++ = cUdpPacketConfirm; + bufPtr += UdpMisc::PutValue32(bufPtr, mConnectCode); + bufPtr += UdpMisc::PutValue32(bufPtr, mConnectionConfig.encryptCode); + *bufPtr++ = (uchar)mConnectionConfig.crcBytes; + for (int j = 0; j < UdpManager::cEncryptPasses; j++) + *bufPtr++ = (uchar)mConnectionConfig.encryptMethod[j]; + bufPtr += UdpMisc::PutValue32(bufPtr, mConnectionConfig.maxRawPacketSize); + RawSend(buf, bufPtr - buf); + } + else + { + // ok, we got a connect-request packet from the ip/port of something we thought we already had a connection to. + // Additionally, the connect-request packet has a different code, meaning it is not just a stragling connect-request + // packet that got sent after we accepted the connection. + // This means that the other side has probably terminated the connection and is attempting to connect again. + // if we just ignore the new connect-request, it will actually result in the new connection-attempt effectively + // keeping this connection object alive. So, instead, when we get this situation, we will terminate this connection + // and ignore the connect-request packet. The connect-request packet will be sent again 1 second later by the client + // at which time we won't exist and out UdpManager will establish a new connection object for it. + InternalDisconnect(0, cDisconnectReasonNewConnectionAttempt); + return; + } + break; + } + case cUdpPacketConfirm: + { + // unpack UdpPacketConfirm packet + Configuration config; + int connectCode = UdpMisc::GetValue32(data + 2); + config.encryptCode = UdpMisc::GetValue32(data + 6); + config.crcBytes = *(data + 10); + for (int j = 0; j < UdpManager::cEncryptPasses; j++) + config.encryptMethod[j] = (UdpManager::EncryptMethod)*(data + 11 + j); + config.maxRawPacketSize = UdpMisc::GetValue32(data + 11 + UdpManager::cEncryptPasses); + + if (mStatus == cStatusNegotiating && mConnectCode == connectCode) + { + mConnectionConfig = config; + SetupEncryptModel(); + mStatus = cStatusConnected; + if (mHandler != NULL) + mHandler->OnConnectComplete(this); + } + break; + } + case cUdpPacketRequestRemap: + { + // if a request remap packet managed to get routed to our connection, it is because + // the mapping is already correct, so we can just ignore this packet at this point + // this will happen when the client sends multiple remap-requests, the first one will + // cause the actual remapping to occur, and the subsequent ones will manage to make + // it into here + break; + } + case cUdpPacketZeroEscape: + { + CallbackRoutePacket(data + 1, dataLen - 1); + break; + } + case cUdpPacketOrdered: + { + ushort orderedStamp = UdpMisc::GetValue16(data + 2); + int diff = (int)orderedStamp - (int)mOrderedStampLast; + if (diff <= 0) // equal here makes it strip dupes too + diff += 0x10000; + if (diff < 30000) + { + mOrderedStampLast = orderedStamp; + CallbackRoutePacket(data + cUdpPacketOrderedSize, dataLen - cUdpPacketOrderedSize); + } + else + { + mConnectionStats.orderRejectedPackets++; + mUdpManager->mManagerStats.orderRejectedPackets++; + } + break; + } + case cUdpPacketOrdered2: + { + ushort orderedStamp = UdpMisc::GetValue16(data + 2); + int diff = (int)orderedStamp - (int)mOrderedStampLast2; + if (diff <= 0) // equal here makes it strip dupes too + diff += 0x10000; + if (diff < 30000) + { + mOrderedStampLast2 = orderedStamp; + CallbackRoutePacket(data + cUdpPacketOrderedSize, dataLen - cUdpPacketOrderedSize); + } + else + { + mConnectionStats.orderRejectedPackets++; + mUdpManager->mManagerStats.orderRejectedPackets++; + } + break; + } + case cUdpPacketTerminate: + { + int connectCode = UdpMisc::GetValue32(data + 2); + if (dataLen >= 8) // to remain protocol compatible with previous version, the other side disconnect reason is an optional field on this packet + { + mOtherSideDisconnectReason = (DisconnectReason)UdpMisc::GetValue16(data + 6); + } + + if (mConnectCode == connectCode) + { + // since other side explicitly told us they had terminated, there is no reason for us to send a terminate + // packet back to them as well (as it will almost always result in some for of unreachable-destination reply) + // so, put ourselves in silent-disconnect mode when this happens + SetSilentDisconnect(true); + InternalDisconnect(0, cDisconnectReasonOtherSideTerminated); + return; + } + break; + } + case cUdpPacketUnreachableConnection: + { + if (mUdpManager->mParams.allowPortRemapping) + { + if (mPortRemapRequestStartStamp == 0) + { + mPortRemapRequestStartStamp = UdpMisc::Clock(); + } + + enum { cMaximumTimeAllowedForPortRemapping = 5000 }; + if (UdpMisc::ClockElapsed(mPortRemapRequestStartStamp) < cMaximumTimeAllowedForPortRemapping) + { + bufPtr = buf; + *bufPtr++ = 0; + *bufPtr++ = cUdpPacketRequestRemap; + bufPtr += UdpMisc::PutValue32(bufPtr, mConnectCode); + bufPtr += UdpMisc::PutValue32(bufPtr, mConnectionConfig.encryptCode); + RawSend(buf, bufPtr - buf); // since the destination doesn't have an associated connection for us to decrypt us, we must be sent unencrypted + break; + } + } + + InternalDisconnect(0, cDisconnectReasonUnreachableConnection); + return; + break; + } + case cUdpPacketMulti: + { + const uchar *ptr = data + 2; + const uchar *endPtr = data + dataLen; + while (ptr < endPtr) + { + int len = *(const uchar *)ptr++; + const uchar *nextPtr = ptr + len; + if (nextPtr > endPtr) + { + // multi-packet lengths didn't properly add up to total packet length + // meaning we likely got a corrupt packet. If you have CRC bytes enabled, it seems + // quite unlikely this could ever occur. Odds are it has happened because the application + // (while processing this packet) ended up touching the packet-data and corrupting the next + // packet in the multi-sequence. + CallbackCorruptPacket(data, dataLen, cUdpCorruptionMultiPacket); + } + else + { + ProcessCookedPacket(ptr, len); + } + ptr = nextPtr; + } + break; + } + case cUdpPacketClockSync: + { + if (mUdpManager->mProcessingInducedLag > 1000) // if it has been over a second since our manager got processing time, then we should ignore clock-sync packets as we will have introduced too much lag ourselves. + break; + + // unpacket UdpPacketClockSync packet + UdpPacketClockSync pp; + pp.zeroByte = *data; + pp.packetType = *(data + 1); + pp.timeStamp = UdpMisc::GetValue16(data + 2); + pp.masterPingTime = UdpMisc::GetValue32(data + 4); + pp.averagePingTime = UdpMisc::GetValue32(data + 8); + pp.lowPingTime = UdpMisc::GetValue32(data + 12); + pp.highPingTime = UdpMisc::GetValue32(data + 16); + pp.lastPingTime = UdpMisc::GetValue32(data + 20); + pp.ourSent = UdpMisc::GetValue64(data + 24); + pp.ourReceived = UdpMisc::GetValue64(data + 32); + + // prepare UdpPacketClockReflect packet + bufPtr = buf; + *bufPtr++ = 0; + *bufPtr++ = cUdpPacketClockReflect; + bufPtr += UdpMisc::PutValue16(bufPtr, pp.timeStamp); // timeStamp + bufPtr += UdpMisc::PutValue32(bufPtr, UdpMisc::LocalSyncStampLong()); // serverSyncStampLong + bufPtr += UdpMisc::PutValue64(bufPtr, pp.ourSent); // yourSent + bufPtr += UdpMisc::PutValue64(bufPtr, pp.ourReceived); // yourReceived + bufPtr += UdpMisc::PutValue64(bufPtr, mConnectionStats.totalPacketsSent); // ourSent + bufPtr += UdpMisc::PutValue64(bufPtr, mConnectionStats.totalPacketsReceived); // ourReceived + PhysicalSend(buf, bufPtr - buf, true); + + mConnectionStats.averagePingTime = pp.averagePingTime; + mConnectionStats.highPingTime = pp.highPingTime; + mConnectionStats.lowPingTime = pp.lowPingTime; + mConnectionStats.lastPingTime = pp.lastPingTime; + mConnectionStats.masterPingTime = pp.masterPingTime; + mConnectionStats.syncOurReceived = mConnectionStats.totalPacketsReceived; + mConnectionStats.syncOurSent = mConnectionStats.totalPacketsSent - 1; // minus 1 since we should not count the packet we just sent + mConnectionStats.syncTheirReceived = pp.ourReceived; + mConnectionStats.syncTheirSent = pp.ourSent; + break; + } + case cUdpPacketClockReflect: + { + if (mUdpManager->mProcessingInducedLag > 1000) // if it has been over a second since our manager got processing time, then we should ignore clock-sync packets as we will have introduced too much lag ourselves. + break; + + UdpPacketClockReflect pp; + pp.zeroByte = *data; + pp.packetType = *(data + 1); + pp.timeStamp = UdpMisc::GetValue16(data + 2); + pp.serverSyncStampLong = UdpMisc::GetValue32(data + 4); + pp.yourSent = UdpMisc::GetValue64(data + 8); + pp.yourReceived = UdpMisc::GetValue64(data + 16); + pp.ourSent = UdpMisc::GetValue64(data + 24); + pp.ourReceived = UdpMisc::GetValue64(data + 32); + + ushort curStamp = UdpMisc::LocalSyncStampShort(); + int roundTime = UdpMisc::SyncStampShortDeltaTime(pp.timeStamp, curStamp); + + mSyncStatCount++; + mSyncStatTotal += roundTime; + if (mSyncStatLow == 0 || roundTime < mSyncStatLow) + mSyncStatLow = roundTime; + if (roundTime > mSyncStatHigh) + mSyncStatHigh = roundTime; + mSyncStatLast = roundTime; + + // see if we should use this sync to reset the master sync time + // if have better (or close to better) round time or it has been a while + int elapsed = UdpMisc::ClockElapsed(mSyncStatMasterFixupTime); + if (roundTime <= mSyncStatMasterRoundTime + 20 || elapsed > 120000) + { + // resync on this packet unless this packet is a real loser (unless it just been a very long time, then sync up anyhow) + if (roundTime < mSyncStatMasterRoundTime * 2 || elapsed > 240000) + { + mSyncTimeDelta = (pp.serverSyncStampLong - UdpMisc::LocalSyncStampLong()) + (uint)(roundTime / 2); + mSyncStatMasterFixupTime = UdpMisc::Clock(); + mSyncStatMasterRoundTime = roundTime; + } + } + + // update connection statistics + mConnectionStats.averagePingTime = (mSyncStatCount > 0) ? (mSyncStatTotal / mSyncStatCount) : 0; + mConnectionStats.highPingTime = mSyncStatHigh; + mConnectionStats.lowPingTime = mSyncStatLow; + mConnectionStats.lastPingTime = roundTime; + mConnectionStats.masterPingTime = mSyncStatMasterRoundTime; + mConnectionStats.syncOurReceived = pp.yourReceived; + mConnectionStats.syncOurSent = pp.yourSent; + mConnectionStats.syncTheirReceived = pp.ourReceived; + mConnectionStats.syncTheirSent = pp.ourSent; + break; + } + case cUdpPacketKeepAlive: + break; + case cUdpPacketReliable1: + case cUdpPacketReliable2: + case cUdpPacketReliable3: + case cUdpPacketReliable4: + case cUdpPacketFragment1: + case cUdpPacketFragment2: + case cUdpPacketFragment3: + case cUdpPacketFragment4: + { + int num = (data[1] - cUdpPacketReliable1) % UdpManager::cReliableChannelCount; + if (mChannel[num] == NULL) + mChannel[num] = new UdpReliableChannel(num, this, &mUdpManager->mParams.reliable[num]); + mChannel[num]->ReliablePacket(data, dataLen); + break; + } + case cUdpPacketAck1: + case cUdpPacketAck2: + case cUdpPacketAck3: + case cUdpPacketAck4: + { + int num = data[1] - cUdpPacketAck1; + if (mChannel[num] != NULL) + mChannel[num]->AckPacket(data, dataLen); + break; + } + case cUdpPacketAckAll1: + case cUdpPacketAckAll2: + case cUdpPacketAckAll3: + case cUdpPacketAckAll4: + { + int num = data[1] - cUdpPacketAckAll1; + if (mChannel[num] != NULL) + mChannel[num]->AckAllPacket(data, dataLen); + break; + } + case cUdpPacketGroup: + { + const uchar *ptr = data + 2; + const uchar *endPtr = data + dataLen; + while (ptr < endPtr) + { + uint len; + ptr += UdpMisc::GetVariableValue(ptr, &len); + ProcessCookedPacket(ptr, len); + ptr += len; + } + break; + } + } + } + else + { + CallbackRoutePacket(data, dataLen); + } +} + +void UdpConnection::FlushChannels() +{ + AddRef(); // in case application tries to delete us during this give time (could only occur due to a ConnectComplete callback timeout) + GiveTime(); // gives our reliable channels time to send any data recently added to their queues. Reschedules us as well, which is ok. + FlushMultiBuffer(); + Release(); +} + +void UdpConnection::FlagPortUnreachable() +{ + mFlaggedPortUnreachable = true; +} + +void UdpConnection::GiveTime() +{ + if (mUdpManager == NULL) + return; + UdpManager *myManager = mUdpManager; + + myManager->AddRef(); // hold a reference to the UdpManager so it doesn't disappear while we are inside our GiveTime + mGettingTime = true; // lets the internal code know we are in the process of getting time. We do this so when actual packets are sent while we are getting time, we don't reprioritize ourselves to 0 + + InternalGiveTime(); + + mGettingTime = false; + myManager->Release(); +} + +void UdpConnection::InternalGiveTime() +{ + uchar buf[256]; + uchar *bufPtr; + + int nextSchedule = 10 * 60 * 1000; // give us time in 10 minutes (unless somebody wants it sooner) + mConnectionStats.iterations++; + + if (mFlaggedPortUnreachable) + { + mFlaggedPortUnreachable = false; + PortUnreachable(); + } + + switch(mStatus) + { + case cStatusNegotiating: + { + if (mConnectAttemptTimeout > 0 && ConnectionAge() > mConnectAttemptTimeout) + { + InternalDisconnect(0, cDisconnectReasonConnectFail); + return; + break; + } + + if (UdpMisc::ClockElapsed(mLastSendTime) >= mUdpManager->mParams.connectAttemptDelay) + { + // prepare UdpPacketConnect packet + bufPtr = buf; + *bufPtr++ = 0; + *bufPtr++ = cUdpPacketConnect; + bufPtr += UdpMisc::PutValue32(bufPtr, UdpManager::cProtocolVersion); + bufPtr += UdpMisc::PutValue32(bufPtr, mConnectCode); + bufPtr += UdpMisc::PutValue32(bufPtr, mUdpManager->mParams.maxRawPacketSize); + RawSend(buf, bufPtr - buf); + nextSchedule = udpMin(nextSchedule, mUdpManager->mParams.connectAttemptDelay); + } + break; + } + case cStatusConnected: + case cStatusDisconnectPending: + { + // sync clock if required + if (mUdpManager->mParams.clockSyncDelay > 0) + { + // sync periodically. If our current master round time is very bad, then sync more frequently (this is important to quickly get a sync up and running) + int elapsed = UdpMisc::ClockElapsed(mLastClockSyncTime); + if (elapsed > mUdpManager->mParams.clockSyncDelay + || (mSyncStatMasterRoundTime > 3000 && elapsed > 2000) + || (mSyncStatMasterRoundTime > 1000 && elapsed > 5000) + || (mSyncStatCount < 2 && elapsed > 10000)) + { + // send a clock-sync packet + int averagePing = (mSyncStatCount > 0) ? (mSyncStatTotal / mSyncStatCount) : 0; + + bufPtr = buf; + *bufPtr++ = 0; + *bufPtr++ = cUdpPacketClockSync; + bufPtr += UdpMisc::PutValue16(bufPtr, UdpMisc::LocalSyncStampShort()); // timeStamp + bufPtr += UdpMisc::PutValue32(bufPtr, mSyncStatMasterRoundTime); // masterPingTime + bufPtr += UdpMisc::PutValue32(bufPtr, averagePing); // averagePingTime + bufPtr += UdpMisc::PutValue32(bufPtr, mSyncStatLow); // lowPingTime + bufPtr += UdpMisc::PutValue32(bufPtr, mSyncStatHigh); // highPingTime + bufPtr += UdpMisc::PutValue32(bufPtr, mSyncStatLast); // lastPingTime + bufPtr += UdpMisc::PutValue64(bufPtr, mConnectionStats.totalPacketsSent + 1); // ourSent (add 1 to include this packet we are about to send since other side will count it as received before getting it) + bufPtr += UdpMisc::PutValue64(bufPtr, mConnectionStats.totalPacketsReceived); // ourReceived + PhysicalSend(buf, bufPtr - buf, true); // don't buffer this, we need it to be as timely as possible, it still needs to be encrypted though, so don't raw send it. + + mLastClockSyncTime = UdpMisc::Clock(); + elapsed = 0; + } + + // schedule us next time for a clock-sync packet + nextSchedule = udpMin(nextSchedule, mUdpManager->mParams.clockSyncDelay - elapsed); + } + + // give reliable channels processing time and see when they want more time + int totalPendingBytes = 0; + for (int i = 0; i < UdpManager::cReliableChannelCount; i++) + { + if (mChannel[i] != NULL) + { + totalPendingBytes += mChannel[i]->TotalPendingBytes(); + int myNext = mChannel[i]->GiveTime(); + if (mUdpManager == NULL) + return; // giving the reliable channel time caused it to callback the application which may disconnect us + nextSchedule = udpMin(nextSchedule, myNext); + } + } + + if (mUdpManager->mParams.reliableOverflowBytes != 0 && totalPendingBytes >= mUdpManager->mParams.reliableOverflowBytes) + { + InternalDisconnect(0, cDisconnectReasonReliableOverflow); + return; + } + + // if we have multi-buffer data + if (mMultiBufferPtr - mMultiBufferData > 2) + { + int elapsed = UdpMisc::ClockElapsed(mDataHoldTime); + if (elapsed >= mUdpManager->mParams.maxDataHoldTime) + FlushMultiBuffer(); // having just sent it, there is no data in the buffer so no reason to adjust the schedule for when it may be needed again + else + nextSchedule = udpMin(nextSchedule, mUdpManager->mParams.maxDataHoldTime - elapsed); // schedule us processing time for when it does need to be sent + } + + // see if we need to keep connection alive + int elapsed = UdpMisc::ClockElapsed(mLastSendTime); + if (mKeepAliveDelay > 0) + { + if (elapsed >= mKeepAliveDelay) + { + // send keep-alive packet + bufPtr = buf; + *bufPtr++ = 0; + *bufPtr++ = cUdpPacketKeepAlive; + PhysicalSend(buf, bufPtr - buf, true); + elapsed = 0; + } + + // schedule us next time for a keep-alive packet + nextSchedule = udpMin(nextSchedule, mKeepAliveDelay - elapsed); + } + + // see if we need to keep the port alive + if (mUdpManager->mParams.portAliveDelay > 0) + { + int portElapsed = UdpMisc::ClockElapsed(mLastPortAliveTime); + if (portElapsed >= mUdpManager->mParams.portAliveDelay) + { + mLastPortAliveTime = UdpMisc::Clock(); + mUdpManager->SendPortAlive(mIp, mPort); + portElapsed = 0; + } + + // schedule us next time for a keep-alive packet + nextSchedule = udpMin(nextSchedule, mUdpManager->mParams.portAliveDelay - portElapsed); + } + + if (mStatus == cStatusDisconnectPending) + { + int timeLeft = mDisconnectFlushTimeout - UdpMisc::ClockElapsed(mDisconnectFlushStamp); + if (timeLeft < 0 || TotalPendingBytes() == 0) + { + InternalDisconnect(0, mDisconnectReason); + return; + } + else + { + nextSchedule = udpMin(nextSchedule, timeLeft); + } + } + + if (mNoDataTimeout > 0) + { + int lrt = LastReceive(); + if (lrt >= mNoDataTimeout) + { + InternalDisconnect(0, cDisconnectReasonTimeout); + return; + } + else + { + nextSchedule = udpMin(nextSchedule, mNoDataTimeout - lrt); + } + } + + break; + } + default: + break; + } + + if (mUdpManager != NULL) + { + // safety to prevent us for scheduling ourselves for a time period that has already passed, + // as doing so could result in infinite looping in the priority queue processing. + // in theory this cannot happen, I should likely assert here just to make sure... + if (nextSchedule < 0) + nextSchedule = 0; + + mUdpManager->SetPriority(this, UdpMisc::Clock() + nextSchedule + 5); // add 5ms to ensure that we are indeed slightly past the scheduled time + } +} + +int UdpConnection::TotalPendingBytes() const +{ + int total = 0; + for (int i = 0; i < UdpManager::cReliableChannelCount; i++) + { + if (mChannel[i] != NULL) + total += mChannel[i]->TotalPendingBytes(); + } + return(total); +} + +void UdpConnection::RawSend(const uchar *data, int dataLen) +{ + // raw send resets last send time, so we need to potentially recalculate when we need time again + // sends the actual physical packet (usually just after it has be prepped by PacketSend, but for connect/confirm/unreachable packets are bypass that step) + mUdpManager->ActualSend(data, dataLen, mIp, mPort); + mConnectionStats.totalPacketsSent++; + mConnectionStats.totalBytesSent += dataLen; + mLastPortAliveTime = mLastSendTime = UdpMisc::Clock(); + + // track data rate + mLastSendBin = ExpireSendBin(); + mSendBin[mLastSendBin % cBinCount] += dataLen; + mOutgoingBytesLastSecond += dataLen; + ScheduleTimeNow(); +} + +int UdpConnection::ExpireSendBin() +{ + int curBin = abs((int)(UdpMisc::Clock() / cBinResolution)); + int binDiff = curBin - mLastSendBin; + if (binDiff > cBinCount) + { + memset(mSendBin, 0, sizeof(mSendBin)); + mOutgoingBytesLastSecond = 0; + } + else + { + for (int i = 0; i < binDiff; i++) + { + int clearBin = (curBin + i) % cBinCount; + mOutgoingBytesLastSecond -= mSendBin[clearBin]; + mSendBin[clearBin] = 0; + } + } + return(curBin); +} + +int UdpConnection::ExpireReceiveBin() +{ + int curBin = abs((int)(UdpMisc::Clock() / cBinResolution)); + int binDiff = curBin - mLastReceiveBin; + if (binDiff > cBinCount) + { + memset(mReceiveBin, 0, sizeof(mReceiveBin)); + mIncomingBytesLastSecond = 0; + } + else + { + for (int i = 0; i < binDiff; i++) + { + int clearBin = (curBin + i) % cBinCount; + mIncomingBytesLastSecond -= mReceiveBin[clearBin]; + mReceiveBin[clearBin] = 0; + } + } + return(curBin); +} + +void UdpConnection::PhysicalSend(const uchar *data, int dataLen, bool appendAllowed) +{ + if (mUdpManager == NULL) + return; + + // if we attempt to do a physical send (ie. encrypt/compress/crc a packet) while we are not connected + // (especially if we are cStatusNegotiating), then it will potentially crash, because in the case of + // cStatusNegotiating, we don't have the encryption method function pointer initialized yet, as the method + // is part of the negotiations + if (mStatus != cStatusConnected && mStatus != cStatusDisconnectPending) + return; + + // this is physical packet send routine that compressed, encrypts the packet, and adds crc bytes to it as appropriate + // no need to make sure we don't encrypt a connect/confirm/unreachable packet because those go directly to RawSend. + uchar tempEncryptBuffer[2][UdpManager::cHardMaxRawPacketSize + sizeof(int)]; + const uchar *finalStart = data; + int finalLen = dataLen; + for (int j = 0; j < UdpManager::cEncryptPasses; j++) + { + if (mConnectionConfig.encryptMethod[j] != UdpManager::cEncryptMethodNone) + { + uchar *destStart = tempEncryptBuffer[j % 2]; + *(int *)(destStart + finalLen + mEncryptExpansionBytes) = (int)0xcececece; // overwrite debug signature + + uchar *destPtr = destStart; + *destPtr++ = finalStart[0]; + if (finalStart[0] == 0) + { + // we know this internal packet will not be a connect or confirm packet since they are sent directly to RawSend to avoid getting encrypted + *destPtr++ = finalStart[1]; + int len = (this->*(mEncryptFunction[j]))(destPtr, finalStart + 2, finalLen - 2); + + // if this assert triggers, it means the encryption pass expanded the size of the encrypted + // data more than was specified by the userSuppliedEncryptExpansionBytes setting, or at least + // tampered with the destination buffer past that length. This is considered a buffer overwrite + // and will potentially cause bugs. + assert(*(int *)(destStart + finalLen + mEncryptExpansionBytes) == (int)0xcececece); + + if (len == -1) + return; // would be really odd for encryption to return an error, but if it does, throw it away + destPtr += len; + } + else + { + int len = (this->*(mEncryptFunction[j]))(destPtr, finalStart + 1, finalLen - 1); + + // if this assert triggers, it means the encryption pass expanded the size of the encrypted + // data more than was specified by the userSuppliedEncryptExpansionBytes setting, or at least + // tampered with the destination buffer past that length. This is considered a buffer overwrite + // and will potentially cause bugs. + assert(*(int *)(destStart + finalLen + mEncryptExpansionBytes) == (int)0xcececece); + + if (len == -1) + return; // would be really odd for encryption to return an error, but if it does, throw it away + destPtr += len; + } + + finalStart = destStart; + finalLen = destPtr - finalStart; + appendAllowed = true; + } + } + + if (mConnectionConfig.crcBytes > 0) + { + if (!appendAllowed) + { + // if the buffer we are going to append onto was our original (ie. no encryption took place) + // then we have to copy it all over to a temp buffer since we can't modify the original + memcpy(tempEncryptBuffer[0], finalStart, finalLen); + finalStart = tempEncryptBuffer[0]; + } + + int crc = UdpMisc::Crc32(finalStart, finalLen, mConnectionConfig.encryptCode); + uchar *crcPtr = const_cast(finalStart) + finalLen; // safe cast, since we make a copy of the data above if we would have ended up appending to the original + switch(mConnectionConfig.crcBytes) + { + case 1: + *crcPtr = (uchar)(crc & 0xff); + break; + case 2: + UdpMisc::PutValue16(crcPtr, (ushort)(crc & 0xffff)); + break; + case 3: + UdpMisc::PutValue24(crcPtr, crc & 0xffffff); + break; + case 4: + UdpMisc::PutValue32(crcPtr, crc); + break; + } + finalLen += mConnectionConfig.crcBytes; + } + + RawSend(finalStart, finalLen); +} + + // returns where it placed the data in the buffer (if it ended up in the buffer), such that the InternalAckSend + // function can do its job +uchar *UdpConnection::BufferedSend(const uchar *data, int dataLen, const uchar *data2, int dataLen2, bool appendAllowed) +{ + if (mUdpManager == NULL) + return(NULL); + int used = mMultiBufferPtr - mMultiBufferData; + + int actualMaxDataHoldSize = udpMin(mUdpManager->mParams.maxDataHoldSize, mConnectionConfig.maxRawPacketSize); + + int totalDataLen = dataLen + dataLen2; + if (totalDataLen > 255 || (totalDataLen + 3) > actualMaxDataHoldSize) + { + // too long of data to even attempt a multi-buffer of this packet, so let's just send it unbuffered + // but first, to ensure the packet-order integrity is somewhat maintained, flush the multi-buffer + // if it currently has something in it + if (used > 2) + FlushMultiBuffer(); + + // now send it (the multi-buffer is empty if you need to use it temporarily to concatenate two data chunks -- it is large enough to hold the largest raw packet) + if (data2 != NULL) + { + memcpy(mMultiBufferData, data, dataLen); + memcpy(mMultiBufferData + dataLen, data2, dataLen2); + PhysicalSend(mMultiBufferData, totalDataLen, true); + } + else + PhysicalSend(data, dataLen, appendAllowed); + return(NULL); + } + + // if this data will not fit into buffer + // note: we allow the multi-packet to grow as large as maxRawPacketSize, but down below we will flush it + // as soon as it gets larger than maxDataHoldSize. + if (used + totalDataLen + 1 > (mConnectionConfig.maxRawPacketSize - mConnectionConfig.crcBytes - mEncryptExpansionBytes)) + { + FlushMultiBuffer(); + used = 0; + } + + // add data to buffer + if (used == 0) + { + // no buffered data yet, create multi-packet header + *mMultiBufferPtr++ = 0; + *mMultiBufferPtr++ = cUdpPacketMulti; + + // new multi-buffer started, so we need to potentially recalculate when we need time again + mDataHoldTime = UdpMisc::Clock(); // set data hold time to when the first piece of data is stuck in the multi-buffer + ScheduleTimeNow(); + } + + *(uchar *)mMultiBufferPtr++ = (uchar)totalDataLen; + uchar *placementPtr = mMultiBufferPtr; + memcpy(mMultiBufferPtr, data, dataLen); + mMultiBufferPtr += dataLen; + if (data2 != NULL) + { + memcpy(mMultiBufferPtr, data2, dataLen2); + mMultiBufferPtr += dataLen2; + } + + if ((mMultiBufferPtr - mMultiBufferData) >= actualMaxDataHoldSize) + { + FlushMultiBuffer(); + placementPtr = NULL; // it got flushed + } + return(placementPtr); +} + +uchar *UdpConnection::InternalAckSend(uchar *bufferedAckPtr, const uchar *ackPtr, int ackLen) +{ + if (bufferedAckPtr != NULL) + { + memcpy(bufferedAckPtr, ackPtr, ackLen); + return(bufferedAckPtr); + } + + BufferedSend(ackPtr, ackLen, NULL, 0, false); + return(NULL); // FIX THIS +} + +void UdpConnection::FlushMultiBuffer() +{ + int len = mMultiBufferPtr - mMultiBufferData; + if (len > 2) + { + if ((int)((uchar)mMultiBufferData[2]) + 3 == len) + PhysicalSend(mMultiBufferData + 3, len - 3, true); // only one packet so don't send it as a multi-packet + else + PhysicalSend(mMultiBufferData, len, true); + + // notify all the reliable channels to clear their buffered acks + for (int i = 0; i < UdpManager::cReliableChannelCount; i++) + { + if (mChannel[i] != NULL) + { + mChannel[i]->ClearBufferedAck(); + } + } + + } + mMultiBufferPtr = mMultiBufferData; +} + +int UdpConnection::EncryptNone(uchar *destData, const uchar *sourceData, int sourceLen) +{ + memcpy(destData, sourceData, sourceLen); + return(sourceLen); +} + +int UdpConnection::DecryptNone(uchar *destData, const uchar *sourceData, int sourceLen) +{ + memcpy(destData, sourceData, sourceLen); + return(sourceLen); +} + +int UdpConnection::EncryptUserSupplied(uchar *destData, const uchar *sourceData, int sourceLen) +{ + UdpManagerHandler *manHandler = mUdpManager->GetHandler(); + if (manHandler != NULL) + return(manHandler->OnUserSuppliedEncrypt(this, destData, sourceData, sourceLen)); + assert(0); // if user-supplied encryption is specified, then you must have a manager handler installed to provide the routines + return(0); +} + +int UdpConnection::DecryptUserSupplied(uchar *destData, const uchar *sourceData, int sourceLen) +{ + UdpManagerHandler *manHandler = mUdpManager->GetHandler(); + if (manHandler != NULL) + return(manHandler->OnUserSuppliedDecrypt(this, destData, sourceData, sourceLen)); + assert(0); // if user-supplied encryption is specified, then you must have a manager handler installed to provide the routines + return(0); +} + +int UdpConnection::EncryptUserSupplied2(uchar *destData, const uchar *sourceData, int sourceLen) +{ + UdpManagerHandler *manHandler = mUdpManager->GetHandler(); + if (manHandler != NULL) + return(manHandler->OnUserSuppliedEncrypt2(this, destData, sourceData, sourceLen)); + assert(0); // if user-supplied encryption is specified, then you must have a manager handler installed to provide the routines + return(0); +} + +int UdpConnection::DecryptUserSupplied2(uchar *destData, const uchar *sourceData, int sourceLen) +{ + UdpManagerHandler *manHandler = mUdpManager->GetHandler(); + if (manHandler != NULL) + return(manHandler->OnUserSuppliedDecrypt2(this, destData, sourceData, sourceLen)); + assert(0); // if user-supplied encryption is specified, then you must have a manager handler installed to provide the routines + return(0); +} + +int UdpConnection::EncryptXorBuffer(uchar *destData, const uchar *sourceData, int sourceLen) +{ + uchar *destPtr = destData; + const uchar *walkPtr = sourceData; + const uchar *endPtr = sourceData + sourceLen; + uchar *encryptPtr = mEncryptXorBuffer; + int prev = mConnectionConfig.encryptCode; + while ((walkPtr + sizeof(int)) <= endPtr) + { + *(int *)destPtr = *(const int *)walkPtr ^ *(int *)encryptPtr ^ prev; + prev = *(int *)destPtr; + walkPtr += sizeof(int); + destPtr += sizeof(int); + encryptPtr += sizeof(int); + } + + while (walkPtr != endPtr) + { + *destPtr = (uchar)(*walkPtr ^ *encryptPtr); + destPtr++; + walkPtr++; + encryptPtr++; + } + return(sourceLen); +} + +int UdpConnection::DecryptXorBuffer(uchar *destData, const uchar *sourceData, int sourceLen) +{ + const uchar *walkPtr = sourceData; + const uchar *endPtr = sourceData + sourceLen; + uchar *encryptPtr = mEncryptXorBuffer; + uchar *destPtr = destData; + int hold; + int prev = mConnectionConfig.encryptCode; + while ((walkPtr + sizeof(int)) <= endPtr) + { + hold = *(const int *)walkPtr; + *(int *)destPtr = *(const int *)walkPtr ^ prev ^ *(int *)encryptPtr; + prev = hold; + walkPtr += sizeof(int); + destPtr += sizeof(int); + encryptPtr += sizeof(int); + } + + while (walkPtr != endPtr) + { + *destPtr = (uchar)(*walkPtr ^ *encryptPtr); + walkPtr++; + destPtr++; + encryptPtr++; + } + return(sourceLen); +} + +int UdpConnection::EncryptXor(uchar *destData, const uchar *sourceData, int sourceLen) +{ + uchar *destPtr = destData; + const uchar *walkPtr = sourceData; + const uchar *endPtr = sourceData + sourceLen; + int prev = mConnectionConfig.encryptCode; + while ((walkPtr + sizeof(int)) <= endPtr) + { + *(int *)destPtr = *(const int *)walkPtr ^ prev; + prev = *(int *)destPtr; + walkPtr += sizeof(int); + destPtr += sizeof(int); + } + + while (walkPtr != endPtr) + { + *destPtr = (uchar)(*walkPtr ^ prev); + destPtr++; + walkPtr++; + } + return(sourceLen); +} + +int UdpConnection::DecryptXor(uchar *destData, const uchar *sourceData, int sourceLen) +{ + const uchar *walkPtr = sourceData; + const uchar *endPtr = sourceData + sourceLen; + uchar *destPtr = destData; + int hold; + int prev = mConnectionConfig.encryptCode; + while ((walkPtr + sizeof(int)) <= endPtr) + { + hold = *(const int *)walkPtr; + *(int *)destPtr = *(const int *)walkPtr ^ prev; + prev = hold; + walkPtr += sizeof(int); + destPtr += sizeof(int); + } + + while (walkPtr != endPtr) + { + *destPtr = (uchar)(*walkPtr ^ prev); + walkPtr++; + destPtr++; + } + return(sourceLen); +} + +void UdpConnection::SetupEncryptModel() +{ + mEncryptExpansionBytes = 0; + for (int j = 0; j < UdpManager::cEncryptPasses; j++) + { + switch(mConnectionConfig.encryptMethod[j]) + { + default: + assert(0); // unknown encryption method specified during in UdpManager construction + break; + case UdpManager::cEncryptMethodNone: + { + // point to method functions + mDecryptFunction[j] = &UdpConnection::DecryptNone; + mEncryptFunction[j] = &UdpConnection::EncryptNone; + mEncryptExpansionBytes += 0; + break; + } + case UdpManager::cEncryptMethodUserSupplied: + { + // point to method functions + mDecryptFunction[j] = &UdpConnection::DecryptUserSupplied; + mEncryptFunction[j] = &UdpConnection::EncryptUserSupplied; + mEncryptExpansionBytes += mUdpManager->mParams.userSuppliedEncryptExpansionBytes; + break; + } + case UdpManager::cEncryptMethodUserSupplied2: + { + // point to method functions + mDecryptFunction[j] = &UdpConnection::DecryptUserSupplied2; + mEncryptFunction[j] = &UdpConnection::EncryptUserSupplied2; + mEncryptExpansionBytes += mUdpManager->mParams.userSuppliedEncryptExpansionBytes2; + break; + } + case UdpManager::cEncryptMethodXorBuffer: + { + // point to method functions + mDecryptFunction[j] = &UdpConnection::DecryptXorBuffer; + mEncryptFunction[j] = &UdpConnection::EncryptXorBuffer; + mEncryptExpansionBytes += 0; + + // set up encrypt buffer (random numbers generated based on seed) + if (mEncryptXorBuffer == NULL) + { + int len = ((mUdpManager->mParams.maxRawPacketSize + 1) / 4) * 4; + mEncryptXorBuffer = new uchar[len]; + int seed = mConnectionConfig.encryptCode; + uchar *sptr = mEncryptXorBuffer; + for (int i = 0; i < len; i++) + *sptr++ = (uchar)(UdpMisc::Random(&seed) & 0xff); + } + break; + } + case UdpManager::cEncryptMethodXor: + { + // point to method functions + mDecryptFunction[j] = &UdpConnection::DecryptXor; + mEncryptFunction[j] = &UdpConnection::EncryptXor; + mEncryptExpansionBytes += 0; + break; + } + } + } +} + +void UdpConnection::GetChannelStatus(UdpChannel channel, ChannelStatus *channelStatus) const +{ + memset(channelStatus, 0, sizeof(*channelStatus)); + switch (channel) + { + case cUdpChannelReliable1: + case cUdpChannelReliable2: + case cUdpChannelReliable3: + case cUdpChannelReliable4: + if (mChannel[channel - cUdpChannelReliable1] != NULL) + { + mChannel[channel - cUdpChannelReliable1]->GetChannelStatus(channelStatus); + } + break; + default: + break; + } +} + +const char *UdpConnection::DisconnectReasonText(DisconnectReason reason) +{ + static bool sInitialized = false; + static char *sDisconnectReason[cDisconnectReasonCount]; + + if (!sInitialized) + { + sInitialized = true; + memset(sDisconnectReason, 0, sizeof(sDisconnectReason)); + sDisconnectReason[cDisconnectReasonNone] = "DisconnectReasonNone"; + sDisconnectReason[cDisconnectReasonIcmpError] = "DisconnectReasonIcmpError"; + sDisconnectReason[cDisconnectReasonTimeout] = "DisconnectReasonTimeout"; + sDisconnectReason[cDisconnectReasonOtherSideTerminated] = "DisconnectReasonOtherSideTerminated"; + sDisconnectReason[cDisconnectReasonManagerDeleted] = "DisconnectReasonManagerDeleted"; + sDisconnectReason[cDisconnectReasonConnectFail] = "DisconnectReasonConnectFail"; + sDisconnectReason[cDisconnectReasonApplication] = "DisconnectReasonApplication"; + sDisconnectReason[cDisconnectReasonUnreachableConnection] = "DisconnectReasonUnreachableConnection"; + sDisconnectReason[cDisconnectReasonUnacknowledgedTimeout] = "DisconnectReasonUnacknowledgedTimeout"; + sDisconnectReason[cDisconnectReasonNewConnectionAttempt] = "DisconnectReasonNewConnectionAttempt"; + sDisconnectReason[cDisconnectReasonConnectionRefused] = "DisconnectReasonConnectionRefused"; + sDisconnectReason[cDisconnectReasonMutualConnectError] = "DisconnectReasonConnectError"; + sDisconnectReason[cDisconnectReasonConnectingToSelf] = "DisconnectReasonConnectingToSelf"; + sDisconnectReason[cDisconnectReasonReliableOverflow] = "DisconnectReasonReliableOverflow"; + } + + return(sDisconnectReason[reason]); +} + + + + + ///////////////////////////////////////////////////////////////////////////////////////////////////// + // ReliableChannel implementation + ///////////////////////////////////////////////////////////////////////////////////////////////////// +UdpReliableChannel::UdpReliableChannel(int channelNumber, UdpConnection *con, UdpManager::ReliableConfig *config) +{ + mUdpConnection = con; + mChannelNumber = channelNumber; + mConfig = *config; + mConfig.maxOutstandingPackets = udpMin(mConfig.maxOutstandingPackets, (int)UdpManager::cHardMaxOutstandingPackets); + + mAveragePingTime = 800; // start out fairly high so we don't do a lot of resends on a bad connection to start out with + mTrickleLastSend = 0; + + int fragmentSize = mConfig.fragmentSize; + if (fragmentSize == 0 || fragmentSize > mUdpConnection->mConnectionConfig.maxRawPacketSize) + fragmentSize = mUdpConnection->mConnectionConfig.maxRawPacketSize; + mMaxDataBytes = (fragmentSize - UdpConnection::cUdpPacketReliableSize - mUdpConnection->mConnectionConfig.crcBytes - mUdpConnection->mEncryptExpansionBytes); + assert(mMaxDataBytes > 0); // fragment size/max-raw-packet-size set too small to allow for reliable deliver + if (mConfig.trickleSize != 0) + mMaxDataBytes = udpMin(mMaxDataBytes, mConfig.trickleSize); + + mMaxCoalesceAttemptBytes = -1; + if (mConfig.coalesce) + { + mMaxCoalesceAttemptBytes = mMaxDataBytes - 5; // 2 bytes for group-header, 3 bytes for length of packet + } + + mReliableIncomingId = 0; + mReliableOutgoingId = 0; + mReliableOutgoingPendingId = 0; + mReliableOutgoingBytes = 0; + + mLogicalPacketsQueued = 0; + mLogicalBytesQueued = 0; + + mCoalescePacket = NULL; + mCoalesceStartPtr = NULL; + mCoalesceEndPtr = NULL; + mCoalesceCount = 0; + + mBufferedAckPtr = NULL; + + mStatDuplicatePacketsReceived = 0; + mStatResentPacketsAccelerated = 0; + mStatResentPacketsTimedOut = 0; + + mCongestionWindowMinimum = udpMax(mMaxDataBytes, mConfig.congestionWindowMinimum); + mCongestionWindowStart = udpMin(4 * mMaxDataBytes, udpMax(2 * mMaxDataBytes, 4380)); + mCongestionWindowStart = udpMax(mCongestionWindowStart, mCongestionWindowMinimum); + mCongestionSlowStartThreshhold = udpMin(mConfig.maxOutstandingPackets * mMaxDataBytes, mConfig.maxOutstandingBytes); + mCongestionWindowLargest = mCongestionWindowSize = mCongestionWindowStart; + + mBigDataLen = 0; + mBigDataTargetLen = 0; + mBigDataPtr = NULL; + mFragmentNextPos = 0; + mLastTimeStampAcknowledged = 0; + mMaxxedOutCurrentWindow = false; + mNextNeedTime = 0; + + mPhysicalPackets = new PhysicalPacket[mConfig.maxOutstandingPackets]; + mReliableIncoming = new IncomingQueueEntry[mConfig.maxInstandingPackets]; + + mLogicalRoot = NULL; + mLogicalEnd = NULL; +} + +UdpReliableChannel::~UdpReliableChannel() +{ + if (mCoalescePacket != NULL) + { + mCoalescePacket->Release(); + mCoalescePacket = NULL; + } + + const LogicalPacket *cur = mLogicalRoot; + while (cur != NULL) + { + const LogicalPacket *next = cur->mReliableQueueNext; + cur->mReliableQueueNext = NULL; // make sure and mark it available so others could possibly use it (if it is a shared logical packet) + cur->Release(); + if (cur == next) // pointing to self, so this is end of list + break; + cur = next; + } + + delete[] mPhysicalPackets; // destructor will release any logical packets as appropriate + delete[] mReliableIncoming; // destructor will delete any data as appropriate + + // delete any big packet that might be under construction + delete[] mBigDataPtr; +} + +void UdpReliableChannel::Send(const uchar *data, int dataLen, const uchar *data2, int dataLen2) +{ + if (mLogicalPacketsQueued == 0) + { + // if we are adding something to a previously empty logical queue, then it is possible that + // we may be able to send it, so mark ourselves to take time the next time it is offered + mNextNeedTime = 0; + mUdpConnection->ScheduleTimeNow(); + } + + if (dataLen + dataLen2 <= mMaxCoalesceAttemptBytes) + { + SendCoalesce(data, dataLen, data2, dataLen2); + } + else + { + FlushCoalesce(); + + LogicalPacket *packet = mUdpConnection->mUdpManager->CreatePacket(data, dataLen, data2, dataLen2); + QueueLogicalPacket(packet); + packet->Release(); + } + + if (mConfig.processOnSend) // make it give our channel time right now to determine outstanding and send this packet if there is room + GiveTime(); +} + +void UdpReliableChannel::Send(const LogicalPacket *packet) +{ + if (mLogicalPacketsQueued == 0) + { + // if we are adding something to a previously empty logical queue, then it is possible that + // we may be able to send it, so mark ourselves to take time the next time it is offered + mNextNeedTime = 0; + mUdpConnection->ScheduleTimeNow(); + } + + if (packet->GetDataLen() <= mMaxCoalesceAttemptBytes) + { + SendCoalesce((const uchar *)packet->GetDataPtr(), packet->GetDataLen()); + } + else + { + FlushCoalesce(); + QueueLogicalPacket(packet); + } + + if (mConfig.processOnSend) // make it give our channel time right now to determine outstanding and send this packet if there is room + GiveTime(); +} + +void UdpReliableChannel::FlushCoalesce() +{ + if (mCoalescePacket != NULL) + { + if (mCoalesceCount == 1) + { + int firstLen; + int skipLen = UdpMisc::GetVariableValue(mCoalesceStartPtr + 2, (uint *)&firstLen); + LogicalPacket *lp = mUdpConnection->mUdpManager->CreatePacket(mCoalesceStartPtr + 2 + skipLen, firstLen); + QueueLogicalPacket(lp); + lp->Release(); + } + else + { + mCoalescePacket->SetDataLen(mCoalesceEndPtr - mCoalesceStartPtr); + QueueLogicalPacket(mCoalescePacket); + } + + mCoalescePacket->Release(); + mCoalescePacket = NULL; + } +} + +void UdpReliableChannel::SendCoalesce(const uchar *data, int dataLen, const uchar *data2, int dataLen2) +{ + int totalLen = dataLen + dataLen2; + if (mCoalescePacket == NULL) + { + mCoalescePacket = mUdpConnection->mUdpManager->CreatePacket(NULL, mMaxDataBytes); + mCoalesceEndPtr = mCoalesceStartPtr = (uchar *)mCoalescePacket->GetDataPtr(); + *mCoalesceEndPtr++ = 0; + *mCoalesceEndPtr++ = UdpConnection::cUdpPacketGroup; + mCoalesceCount = 0; + } + else + { + int spaceLeft = mMaxDataBytes - (mCoalesceEndPtr - mCoalesceStartPtr); + if (totalLen + 3 > spaceLeft) // 3 bytes to ensure PutVariableValue has room for the length indicator (this limits us to 64k coalescing, which is ok, since fragments can't get that big) + { + FlushCoalesce(); + SendCoalesce(data, dataLen, data2, dataLen2); + return; + } + } + + // append on end of coalesce + mCoalesceCount++; + mCoalesceEndPtr += UdpMisc::PutVariableValue(mCoalesceEndPtr, totalLen); + if (data != NULL) + memcpy(mCoalesceEndPtr, data, dataLen); + mCoalesceEndPtr += dataLen; + if (data2 != NULL) + memcpy(mCoalesceEndPtr, data2, dataLen2); + mCoalesceEndPtr += dataLen2; +} + +void UdpReliableChannel::QueueLogicalPacket(const LogicalPacket *packet) +{ + mLogicalPacketsQueued++; + mLogicalBytesQueued += packet->GetDataLen(); + + if (packet->mReliableQueueNext != NULL) + { + packet = mUdpConnection->mUdpManager->WrappedBorrow(packet); + } + else + { + packet->AddRef(); + } + + packet->mReliableQueueNext = packet; // have it point to itself to signify that the mNext pointer is taken (ie. reserve it), yet still represents the end of the list + if (mLogicalEnd != NULL) + { + mLogicalEnd->mReliableQueueNext = packet; + } + mLogicalEnd = packet; + + if (mLogicalRoot == NULL) + { + mLogicalRoot = packet; + } +} + +bool UdpReliableChannel::PullDown(int windowSpaceLeft) +{ + // the pull-down on-demand method will give us the opportunity to late-combine as many tiny logical packets as we can, + // reducing the number of tracked-packets that are required. This effectively reduces the number of acks that we are + // going to get back, which can be substantial in situations where there are a LOT of tiny reliable packets being sent. + // operating with fewer outstanding physical-packets to track is more CPU efficient as well. + + // (NOTE: as of this writing, the below implementation does not do the late-combine techique yet) + // (NOTE: we could also combine-on-send if we so desired, I am not sure which way we will go) + // (NOTE: doing it on send could allow us to avoid LogicalPacket allocations...) + + bool pulledDown = false; + int physicalCount = (int)(mReliableOutgoingId - mReliableOutgoingPendingId); + while (windowSpaceLeft > 0 && physicalCount < mConfig.maxOutstandingPackets) + { + if (mLogicalRoot == NULL) + { + FlushCoalesce(); // this is guaranteed to stick + if (mLogicalRoot == NULL) + break; // nothing flushed, so we are done + } + + int nextSpot = (int)(mReliableOutgoingId % mConfig.maxOutstandingPackets); + + // ok, we can move something down, even if it is only a fragment of the logical packet + PhysicalPacket *entry = &mPhysicalPackets[nextSpot]; + entry->mParent = mLogicalRoot; + entry->mParent->AddRef(); // add ref from physical packet + entry->mFirstTimeStamp = 0; + entry->mLastTimeStamp = 0; + + // calculate how much we can send based on our starting position (mFragmentNextPos) in the logical packet. + // if we can't send it the rest of data to end of packet, then send the fragment portion and addref, otherwise send the whole thing and pop the logical packet + int dataLen = entry->mParent->GetDataLen(); + const uchar *data = (const uchar *)entry->mParent->GetDataPtr(); + int bytesLeft = dataLen - mFragmentNextPos; + int bytesToSend = udpMin(bytesLeft, mMaxDataBytes); + + entry->mDataPtr = data + mFragmentNextPos; + + // if not sending entire packet + if (bytesToSend != dataLen) + { + // mark it as a fragment + if (mFragmentNextPos == 0) + bytesToSend -= sizeof(int); // fragment start has a 4 byte header specifying size of following large data, so make room for it so we don't exceed max raw packet size + } + entry->mDataLen = bytesToSend; + mReliableOutgoingBytes += bytesToSend; + + if (bytesToSend == bytesLeft) + { + mFragmentNextPos = 0; + mLogicalPacketsQueued--; + const LogicalPacket *lp = mLogicalRoot; + mLogicalRoot = mLogicalRoot->mReliableQueueNext; + if (mLogicalRoot == lp) // ie, we were pointing to ourself, meaning we were the end of the list + { + mLogicalRoot = NULL; + mLogicalEnd = NULL; + } + lp->mReliableQueueNext = NULL; // clear our next link since we are no longer using it (so somebody else can use it potentially) + lp->Release(); // release from logical queue + } + else + { + mFragmentNextPos += bytesToSend; + } + + mLogicalBytesQueued -= bytesToSend; // as fragments are sent, decrease the number of logical bytes queued + mReliableOutgoingId++; + physicalCount++; + windowSpaceLeft -= bytesToSend; + pulledDown = true; + } + return(pulledDown); +} + +int UdpReliableChannel::GiveTime() +{ + uchar buf[256]; + uchar *bufPtr; + + UdpMisc::ClockStamp hotClock = UdpMisc::Clock(); + + if (hotClock < mNextNeedTime) + return(UdpMisc::ClockDiff(hotClock, mNextNeedTime)); + + // if we are a trickle channel, then don't try sending more until trickleRate has expired. We are only allowed + // to send up to trickleBytes at a time every trickleRate milliseconds; however, if we don't send the full trickleBytes + // in one GiveTime call, then it won't get to try sending more bytes until this timer has expired, even if we had not used + // up the entire trickleBytes allotment the last time we were in here...this should not cause any significant problems + if (mConfig.trickleRate > 0) + { + int nextAllowedSendTime = mConfig.trickleRate - UdpMisc::ClockDiff(mTrickleLastSend, hotClock); + if (nextAllowedSendTime > 0) + return(nextAllowedSendTime); + } + + + // lot a tweaking goes into calculating the optimal resend time. Set it too large and you can stall the pipe + // at the beginning of the connection fairly easily + int optimalResendDelay = (mAveragePingTime * mConfig.resendDelayPercent / 100) + mConfig.resendDelayAdjust; // percentage of average ping plus a fixed amount + optimalResendDelay = udpMin(mConfig.resendDelayCap, optimalResendDelay); // never let the resend delay get over max + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // see if any of the physical packets can actually be sent (either resends, or initial sends, whatever + // if not, calculate when exactly somebody is expected to need sending + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + mMaxxedOutCurrentWindow = false; + int outstandingNextSendTime = 10 * 60000; + // if we have something to do + + // this next branch was replaced by JeffP in the latest UdpLibrary drop. Please integrate + // that. If something catestrophic happens with reliable channels, uncomment this next line to + // replace the existing branch + //if (mReliableOutgoingId < mReliableOutgoingPendingId || mLogicalRoot != NULL || mCoalescePacket != NULL) + + if (mReliableOutgoingPendingId < mReliableOutgoingId || mLogicalRoot != NULL || mCoalescePacket != NULL) + { + // first, let's calculate how many bytes we figure is outstanding based on who is still waiting for an ack-packet + UdpMisc::ClockStamp oldestResendTime = udpMax(hotClock - optimalResendDelay, mLastTimeStampAcknowledged); // anything older than this, we need to resend + + int useMaxOutstandingBytes = udpMin(mConfig.maxOutstandingBytes, mCongestionWindowSize); + int outstandingBytes = 0; + PhysicalPacket *readyQueue[10000]; + PhysicalPacket **readyEnd = readyQueue + (sizeof(readyQueue) / sizeof(PhysicalPacket *)); + PhysicalPacket **readyPtr = readyQueue; + + int windowSpaceLeft = useMaxOutstandingBytes; + for (udp_int64 i = mReliableOutgoingPendingId; i <= mReliableOutgoingId; i++) + { + if (i == mReliableOutgoingId) + { + // this packet is not really here yet, we need to pull it down if possible + // if not possible, we need to break out of the loop as we are done + if (!PullDown(windowSpaceLeft)) + break; + } + + // if this packet has not been acked and it is NOT ready to be sent (was recently sent) then we consider it outstanding + // note: packets needing re-sending probably got lost and are therefore not outstanding + PhysicalPacket *entry = &mPhysicalPackets[i % mConfig.maxOutstandingPackets]; + if (entry->mDataPtr != NULL) // acked packets set the dataPtr to NULL + { + // if this packet is ready to be sent (ie: needs time now, or some later packet has already been ack'ed) + windowSpaceLeft -= entry->mDataLen; // window-space is effectively taken whether we have sent it yet or not + if (entry->mLastTimeStamp < oldestResendTime) + { + if (readyPtr < readyEnd) // if we have queue space + *readyPtr++ = entry; + } + else + { + outstandingBytes += entry->mDataLen; + outstandingNextSendTime = udpMin(outstandingNextSendTime, optimalResendDelay - UdpMisc::ClockDiff(entry->mLastTimeStamp, hotClock)); + } + + // if we have reached a point in the queue where there are no sent packets + // and our outstanding bytes plus how much we intend to send is greater than the window we have + // then we can quit step-2, since there is nothing else to be gained by continuing. + if (entry->mFirstTimeStamp == 0 && (windowSpaceLeft <= 0)) + break; + } + } + + + // second, send ready entries until the max outstanding is reached + int trickleSent = 0; + PhysicalPacket **readyWalk = readyQueue; + PhysicalPacket *pendingReliableBasePtr = &mPhysicalPackets[mReliableOutgoingPendingId % mConfig.maxOutstandingPackets]; + while (readyWalk < readyPtr && outstandingBytes < useMaxOutstandingBytes) + { + // prepare packet and send it + PhysicalPacket *entry = *readyWalk++; + + // prepare reliable header and send it with data + const uchar *parentBase = (const uchar *)entry->mParent->GetDataPtr(); + + bool fragment = false; + if (entry->mDataPtr != parentBase || entry->mDataLen != entry->mParent->GetDataLen()) + fragment = true; + + // we can calculate what our reliableId should be based on our position in the array + // need to handle the case where we wrap around the end of the array + udp_int64 reliableId; + if (entry >= pendingReliableBasePtr) + { + reliableId = mReliableOutgoingPendingId + (entry - pendingReliableBasePtr); + } + else + { + reliableId = mReliableOutgoingPendingId + (&mPhysicalPackets[mConfig.maxOutstandingPackets] - pendingReliableBasePtr) + (entry - &mPhysicalPackets[0]); + } + + // prep the actual packet and send it + bufPtr = buf; + *bufPtr++ = 0; + *bufPtr++ = (uchar)(((fragment) ? UdpConnection::cUdpPacketFragment1 : UdpConnection::cUdpPacketReliable1) + mChannelNumber); // mark us as a fragment if we are one + bufPtr += UdpMisc::PutValue16(bufPtr, (ushort)(reliableId & 0xffff)); + if (fragment && entry->mDataPtr == parentBase) + bufPtr += UdpMisc::PutValue32(bufPtr, entry->mParent->GetDataLen()); // first fragment has a total-length byte after the reliable header + mUdpConnection->BufferedSend(buf, bufPtr - buf, entry->mDataPtr, entry->mDataLen, false); + + // update state information + if (entry->mFirstTimeStamp == 0) + { + entry->mFirstTimeStamp = hotClock; + } + else + { + // trying to send the packet again, let's see how long we have been trying to send this packet. If we + // have an unacknowledged timeout set and it is older than that, then terminate the connection. + // note: we only check for the oldest unacknowledged age against the timeout at the point in time + // that we are considering sending the packet again. This can technically cause it to wait slightly + // longer than the specified timeout setting before disconnecting the connection, but should be + // close enough for all practical purposes and allows for more efficient processing of this setting internally. + if (mUdpConnection->mUdpManager->mParams.oldestUnacknowledgedTimeout > 0) + { + int age = UdpMisc::ClockDiff(entry->mFirstTimeStamp, hotClock); + if (age > mUdpConnection->mUdpManager->mParams.oldestUnacknowledgedTimeout) + { + mUdpConnection->InternalDisconnect(0, UdpConnection::cDisconnectReasonUnacknowledgedTimeout); + return(0); + } + } + + // were we resent because of a later ack came in? or because we timed out? + if (entry->mLastTimeStamp < mLastTimeStampAcknowledged) + { + // we are resending this packet due to an accelleration (receiving a later packet ack) + // so recalc slow start threshhold and congestion window size as per Reno fast-recovery algorithm + mCongestionWindowSize = mCongestionWindowSize * 2 / 3; + mCongestionWindowSize = udpMax(mCongestionWindowMinimum, mCongestionWindowSize); // never let congestion window get smaller than a single packet + mCongestionSlowStartThreshhold = mCongestionWindowSize; + useMaxOutstandingBytes = udpMin(mConfig.maxOutstandingBytes, mCongestionWindowSize); + + mStatResentPacketsAccelerated++; + mUdpConnection->mConnectionStats.resentPacketsAccelerated++; + mUdpConnection->mUdpManager->mManagerStats.resentPacketsAccelerated++; + } + else + { + // we are resending this packet due to a timeout, so we are seriously overloading things probably + // so recalc slow start threshhold and congestion window size as per Tahoe algorithm + mCongestionSlowStartThreshhold = udpMax(mMaxDataBytes * 2, outstandingBytes / 2); + mCongestionWindowSize = mCongestionWindowStart; + useMaxOutstandingBytes = udpMin(mConfig.maxOutstandingBytes, mCongestionWindowSize); + + // because a resend has occurred due to a timeout, slow down the resend times slightly + // when things start flowing again, it will fix itself up quickly anyways + mAveragePingTime += 100; + + // When a connection goes temporarily dead, everything that is in the current + // window will end up getting timedout. If the window were large, this could result in the + // mAveragePingTime growing quite large and creating very long stalls in the pipe once it does + // start moving again. To prevent this, we cap mAveragePingTime when these events occur to prevent + // long stalls when the pipe finally reopens. + mAveragePingTime = udpMin(mConfig.resendDelayCap, mAveragePingTime); + + mStatResentPacketsTimedOut++; + mUdpConnection->mConnectionStats.resentPacketsTimedOut++; + mUdpConnection->mUdpManager->mManagerStats.resentPacketsTimedOut++; + } + } + + entry->mLastTimeStamp = hotClock; + + outstandingNextSendTime = udpMin(outstandingNextSendTime, optimalResendDelay); // this packet is now outstanding, so factor it into the outstandingNextSendTime calculation + outstandingBytes += entry->mDataLen; + mTrickleLastSend = hotClock; + trickleSent += entry->mDataLen; + + if (mConfig.trickleSize != 0 && trickleSent >= mConfig.trickleSize) + break; + } + + if (outstandingBytes >= useMaxOutstandingBytes) + mMaxxedOutCurrentWindow = true; + } + else + { + // we have nothing in the pipe at all, reset the congestion window (this means everything has been acked, so the pipe is totally empty + // we need to reset the window back to prevent a sudden flood next time a large chunk of data is sent. + // we also need to avoid having the slowly sent reliable packets constantly increase the window size (since none will ever get lost) + // such that when it does come time to send a big chunk of data, it thinks the window-size is enormous. + // resetting the window back to small will only have an effect if a large chunk of data is then sent, at which time it will quickly + // ramp up with the slow-start method. + // we will reset the slow-start threshhold to maximum level that the congestion window has ever been allowed to grow. This effectively + // allows the threshhold to increase after getting smaller, something it otherwise has not been able to do. + mCongestionWindowSize = mCongestionWindowStart; + mCongestionSlowStartThreshhold = mCongestionWindowLargest; + } + + int nextAllowedSendTime = mConfig.trickleRate - UdpMisc::ClockDiff(mTrickleLastSend, hotClock); + nextAllowedSendTime = udpMax(0, udpMax(nextAllowedSendTime, outstandingNextSendTime)); + mNextNeedTime = hotClock + nextAllowedSendTime; + return(nextAllowedSendTime); +} + +void UdpReliableChannel::GetChannelStatus(UdpConnection::ChannelStatus *channelStatus) const +{ + int coalesceBytes = 0; + if (mCoalescePacket != NULL) + coalesceBytes = mCoalesceEndPtr - mCoalesceStartPtr; + + channelStatus->totalPendingBytes = mLogicalBytesQueued + mReliableOutgoingBytes + coalesceBytes; + channelStatus->queuedPackets = mLogicalPacketsQueued; + channelStatus->queuedBytes = mLogicalBytesQueued; + channelStatus->incomingLargeTotal = mBigDataTargetLen; + channelStatus->incomingLargeSoFar = mBigDataLen; + channelStatus->duplicatePacketsReceived = mStatDuplicatePacketsReceived; + channelStatus->resentPacketsAccelerated = mStatResentPacketsAccelerated; + channelStatus->resentPacketsTimedOut = mStatResentPacketsTimedOut; + channelStatus->congestionSlowStartThreshhold = mCongestionSlowStartThreshhold; + channelStatus->congestionWindowSize = mCongestionWindowSize; + channelStatus->ackAveragePing = mAveragePingTime; + channelStatus->oldestUnacknowledgedAge = 0; + + if (mReliableOutgoingPendingId < mReliableOutgoingId) + { + // oldest pending packet will be definition be the oldestUnacknowledged, since it is impossible + // for any packet after it to have possibly been sent before it was sent for its first time + // since queue is effectively in first-send order. It is also impossible for this packet to have + // been acknowledged, since the pending id advances the moment the oldest is acknowledged, meaning + // that the pendingId is always pointing to something that has either been sent (or not sent at all) + PhysicalPacket *entry = &mPhysicalPackets[mReliableOutgoingPendingId % mConfig.maxOutstandingPackets]; + if (entry->mFirstTimeStamp != 0) // if has been sent (we know it hasn't been acknowledged or we couldn't possibly be pointing at it as pending) + { + channelStatus->oldestUnacknowledgedAge = UdpMisc::ClockElapsed(entry->mFirstTimeStamp); + } + } +} + +void UdpReliableChannel::ReliablePacket(const uchar *data, int dataLen) +{ + uchar buf[256]; + uchar *bufPtr; + + if (dataLen <= UdpConnection::cUdpPacketReliableSize) + { + mUdpConnection->CallbackCorruptPacket(data, dataLen, cUdpCorruptionReliablePacketTooShort); + return; + } + + int packetType = data[1]; + ushort reliableStamp = UdpMisc::GetValue16(data + 2); + udp_int64 reliableId = GetReliableIncomingId(reliableStamp); + + if (reliableId >= mReliableIncomingId + mConfig.maxInstandingPackets) + return; // if we do not have buffer space to hold onto this packet, then we simply must pretend like it was lost + + if (reliableId >= mReliableIncomingId) + { + ReliablePacketMode mode = (ReliablePacketMode)((packetType - UdpConnection::cUdpPacketReliable1) / UdpManager::cReliableChannelCount); + + // is this the packet we are waiting for + if (mReliableIncomingId == reliableId) + { + // if so, process it immediately + ProcessPacket(mode, data + UdpConnection::cUdpPacketReliableSize, dataLen - UdpConnection::cUdpPacketReliableSize); + mReliableIncomingId++; + + // process other packets that have arrived + while (mReliableIncoming[mReliableIncomingId % mConfig.maxInstandingPackets].mPacket != NULL) + { + int spot = (int)(mReliableIncomingId % mConfig.maxInstandingPackets); + if (mReliableIncoming[spot].mMode != cReliablePacketModeDelivered) + { + ProcessPacket(mReliableIncoming[spot].mMode, (uchar *)mReliableIncoming[spot].mPacket->GetDataPtr(), mReliableIncoming[spot].mPacket->GetDataLen()); + } + + mReliableIncoming[spot].mPacket->Release(); + mReliableIncoming[spot].mPacket = NULL; + mReliableIncomingId++; + } + } + else + { + // not the one we need next, but it is later than the one we need , so store it in our buffer until it's turn comes up + int spot = (int)(reliableId % mConfig.maxInstandingPackets); + if (mReliableIncoming[spot].mPacket == NULL) // only make the copy of it if we don't already have it in our buffer (in cases where it was sent twice, there would be no harm in the copy again since it must be the same packet, it's just inefficient) + { + mReliableIncoming[spot].mMode = mode; + mReliableIncoming[spot].mPacket = mUdpConnection->mUdpManager->CreatePacket(data + UdpConnection::cUdpPacketReliableSize, dataLen - UdpConnection::cUdpPacketReliableSize); + + // on out of order deliver, we need to keep a copy of it as if we were doing ordered-delivery in order to prevent duplicates + // we will mark the packet in the queue as already delivered to prevent it from getting delivered a second time when the stalled packet + // arrives and unwinds the queue + if (mode == cReliablePacketModeReliable && mConfig.outOfOrder) + { + ProcessPacket(cReliablePacketModeReliable, (uchar *)mReliableIncoming[spot].mPacket->GetDataPtr(), mReliableIncoming[spot].mPacket->GetDataLen()); + mReliableIncoming[spot].mMode = cReliablePacketModeDelivered; + } + } + else + { + mStatDuplicatePacketsReceived++; + mUdpConnection->mConnectionStats.duplicatePacketsReceived++; + mUdpConnection->mUdpManager->mManagerStats.duplicatePacketsReceived++; + } + } + } + else + { + mStatDuplicatePacketsReceived++; + mUdpConnection->mConnectionStats.duplicatePacketsReceived++; + mUdpConnection->mUdpManager->mManagerStats.duplicatePacketsReceived++; + } + + bufPtr = buf; + *bufPtr++ = 0; + if (mReliableIncomingId > reliableId) + { + // ack everything up to the current head of our chain (minus one since the stamp represents the next one we want to get) + *bufPtr++ = (uchar)(UdpConnection::cUdpPacketAckAll1 + mChannelNumber); + bufPtr += UdpMisc::PutValue16(bufPtr, (ushort)((mReliableIncomingId - 1) & 0xffff)); + } + else + { + // a simple ack for us only + *bufPtr++ = (uchar)(UdpConnection::cUdpPacketAck1 + mChannelNumber); + bufPtr += UdpMisc::PutValue16(bufPtr, (ushort)(reliableId & 0xffff)); + mBufferedAckPtr = NULL; // not allowed to replace an old one with a selective-ack + } + + if (mBufferedAckPtr != NULL && mConfig.ackDeduping) + { + memcpy(mBufferedAckPtr, buf, bufPtr - buf); + } + else + { + mBufferedAckPtr = mUdpConnection->BufferedSend(buf, bufPtr - buf, NULL, 0, true); // safe to append on our data, it is stack data + } +} + +void UdpReliableChannel::ProcessPacket(ReliablePacketMode mode, const uchar *data, int dataLen) +{ + assert(dataLen > 0); + + // if we have a big packet under construction already, or we are a fragment and thus need to be constructing one, then append this on the end (will create new if it is the first fragment) + if (mode == cReliablePacketModeReliable) + { + // we are not a fragment, nor was there a fragment in progress, so we are a simple reliable packet, just send it to the app + mUdpConnection->ProcessCookedPacket(data, dataLen); + } + else if (mode == cReliablePacketModeFragment) + { + // append onto end of big packet (or create new big packet if not existing already) + if (mBigDataPtr == NULL) + { + mBigDataTargetLen = UdpMisc::GetValue32(data); // first fragment has a total-length int header on it. + mBigDataPtr = new uchar[mBigDataTargetLen]; + mBigDataLen = 0; + data += sizeof(int); + dataLen -= sizeof(int); + } + + int safetyMax = udpMin(mBigDataTargetLen - mBigDataLen, dataLen); // can't happen in theory since they should add up exact, but protect against it if it does + + if(safetyMax != dataLen) + { + throw UdpLibraryException(); + } + + + memcpy(mBigDataPtr + mBigDataLen, data, safetyMax); + mBigDataLen += safetyMax; + + if (mBigDataTargetLen == mBigDataLen) + { + // send big-packet off to application + mUdpConnection->ProcessCookedPacket(mBigDataPtr, mBigDataLen); + + // delete big packet, and reset + delete[] mBigDataPtr; + mBigDataLen = 0; + mBigDataTargetLen = 0; + mBigDataPtr = NULL; + } + } +} + +void UdpReliableChannel::AckAllPacket(const uchar *data, int /*dataLen*/) +{ + udp_int64 reliableId = GetReliableOutgoingId((ushort)UdpMisc::GetValue16(data + 2)); + + if (mReliableOutgoingPendingId > reliableId) + { + // if we ackall'ed a packet and everything before the ackall address had already been acked, then we know + // for certainty that we sent a packet over again that did not need to be sent over again (ie. wasn't lost, just slow) + // so adjust the mAveragePingTime upward to slow down future resends + mAveragePingTime += 400; + mAveragePingTime = udpMin(mConfig.resendDelayCap, mAveragePingTime); + } + + for (udp_int64 i = mReliableOutgoingPendingId; i <= reliableId; i++) + Ack(i); +} + +void UdpReliableChannel::Ack(udp_int64 reliableId) +{ + // if packet being acknowledged is possibly in our resend queue, then check for it + if (reliableId >= mReliableOutgoingPendingId && reliableId < mReliableOutgoingId) + { + int pos = (int)(reliableId % mConfig.maxOutstandingPackets); + PhysicalPacket *entry = &mPhysicalPackets[pos]; + + if (entry->mDataPtr != NULL) // if this packet has not been acknowledged yet (sometimes we get back two acks for the same packet) + { + mNextNeedTime = 0; // something got acked, so we actually need to take the time next time it is offered + + // if the last time we gave this reliable channel processing time, it filled up the entire sliding window + // then go ahead and increase the window-size when incoming acks come in. However, if the window wasn't full + // then don't increase the window size. The problem is, a game application is likely to send reliable data + // at a relatively slow rate (2k/second for example), never filling the window. The net result would be that + // every acknowledged packet would increase the window size, giving the reliable channel the impression that + // it's window can be very very large, when in fact, it is only not losing packets because the application is pacing + // itself. The window could grow enormous, even 200k for a modem. Then, if the application were to dump a load + // of data onto us all at once, it would flush it all out at once thinking it had a big window. By only increasing + // the window size when we have high enough volume to fill the window, we ensure this does not happen. TCP does a similar + // thing, but what they do is reset the window if there has been a long stall. We do that too, but because we are a game + // application that is likely to pace the data at the application level, we have a unique circumstances that need addressing. + if (mMaxxedOutCurrentWindow) + { + if (mCongestionWindowSize < mCongestionSlowStartThreshhold) + mCongestionWindowSize += mMaxDataBytes; // slow-start mode + else + mCongestionWindowSize += udpMax(1, (mMaxDataBytes * mMaxDataBytes / mCongestionWindowSize)); // congestion mode + + mCongestionWindowLargest = udpMax(mCongestionWindowLargest, mCongestionWindowSize); + } + + if (entry->mLastTimeStamp == entry->mFirstTimeStamp) + { + + + // if the packet that is being acknowledged was only sent once, then we can safely use + // the round-trip time as an accurate measure of ping time. By knowing ping time, we can + // better (more agressively) schedule resends of lost packets. We will use a moving average + // that weights the current packet as 1/4 the average. + int thisPingTime = UdpMisc::ClockElapsed(entry->mFirstTimeStamp); + mAveragePingTime = (mAveragePingTime * 3 + thisPingTime) / 4; + } + + // what this is doing is if we receive an ACK for a packet that was sent at TIME_X + // we can assume that all packets sent before TIME_X that are not yet acknowledge were lost + // and we can resend them immediately + // since we do not know whether this ack is for last packet we sent, we have to assume it is for the first time this packet was sent (if sent multiple times) + // otherwise, we could resend it, then receive the ack from the first packet, and think our last-ack time is the time of the second outgoing packet + // which would cause about every packet in the queue to resend, even if they had just been sent + // in situations where the first packet truely was lost and this is an ACK of the second packet, then the only + // harm done is that the we may not resend some of the earlier sent packets quite as quickly. This will only + // happen in situations where a packet that was truely lost gets acked on it's second attempt...we just + // won't be using that ack for the purposes of accelerating other resends...since odds are a non-lost packet + // will accelerate those other resends shortly anyhow, there really is no loss + // (note: we used to only set this value forward for packets that were never lost (one time sends); however, if this stamp + // ever got set way high for some reason (in theory it can't happen), then we would get into a situation where it would + // rapidly resend and possibly never get reset, causing infinite rapid resends, so we now set it every time to the first-stamp) + // which will be safe, even if the packet were resent.) + mLastTimeStampAcknowledged = entry->mFirstTimeStamp; + + + // this packet we have queued has been acknowledged, so delete it from queue + mReliableOutgoingBytes -= entry->mDataLen; + entry->mDataLen = 0; + entry->mDataPtr = NULL; + entry->mParent->Release(); + entry->mParent = NULL; + + // advance the pending ptr until it reaches outgoingId or an entry that has yet to acknowledged + while (mReliableOutgoingPendingId < mReliableOutgoingId) + { + if (mPhysicalPackets[mReliableOutgoingPendingId % mConfig.maxOutstandingPackets].mDataPtr != NULL) + break; + mReliableOutgoingPendingId++; + } + } + else + { + // we got an ack for a packet that has already been acked. This could be due to an ack-all packet that covered us so statistically + // we can't do much with this information. + } + } + + // we don't need to try rescheduling ourself here, since our connection object reschedules to go immediately whenever any type + // of packet arrives (including ack packets) +} + + + + + +UdpReliableChannel::IncomingQueueEntry::IncomingQueueEntry() +{ + mPacket = NULL; + mMode = UdpReliableChannel::cReliablePacketModeReliable; +} + +UdpReliableChannel::IncomingQueueEntry::~IncomingQueueEntry() +{ + if (mPacket != NULL) + mPacket->Release(); +} + + +UdpReliableChannel::PhysicalPacket::PhysicalPacket() +{ + mParent = NULL; +} + +UdpReliableChannel::PhysicalPacket::~PhysicalPacket() +{ + if (mParent != NULL) + mParent->Release(); +} + + + + ///////////////////////////////////////////////////// + // LogicalPacket implementation + ///////////////////////////////////////////////////// +LogicalPacket::LogicalPacket() +{ + mRefCount = 1; + mReliableQueueNext = NULL; +} + +LogicalPacket::~LogicalPacket() +{ +} + +void LogicalPacket::AddRef() const +{ + mRefCount++; +} + +void LogicalPacket::Release() const +{ + if (--mRefCount == 0) + delete this; +} + +int LogicalPacket::GetRefCount() const +{ + return(mRefCount); +} + +bool LogicalPacket::IsInternalPacket() const +{ + return(false); +} + + ///////////////////////////////////////////////////// + // SimpleLogicalPacket implementation + ///////////////////////////////////////////////////// +SimpleLogicalPacket::SimpleLogicalPacket(const void *data, int dataLen) +{ + mDataLen = dataLen; + mData = new uchar[mDataLen]; + if (data != NULL) + memcpy(mData, data, mDataLen); +} + +SimpleLogicalPacket::~SimpleLogicalPacket() +{ + delete[] mData; +} + +void *SimpleLogicalPacket::GetDataPtr() +{ + return(mData); +} + +const void *SimpleLogicalPacket::GetDataPtr() const +{ + return(mData); +} + +int SimpleLogicalPacket::GetDataLen() const +{ + return(mDataLen); +} + +void SimpleLogicalPacket::SetDataLen(int len) +{ + assert(len <= mDataLen); + mDataLen = len; +} + ///////////////////////////////////////////////////// + // GroupLogicalPacket implementation + ///////////////////////////////////////////////////// +GroupLogicalPacket::GroupLogicalPacket() : LogicalPacket() +{ + mDataLen = 0; + mData = NULL; +} + +GroupLogicalPacket::~GroupLogicalPacket() +{ + UdpMisc::SmartResize(mData, 0); // free it +} + +void GroupLogicalPacket::AddPacket(const LogicalPacket *packet) +{ + assert(packet != NULL); + AddPacketInternal(packet->GetDataPtr(), packet->GetDataLen(), packet->IsInternalPacket()); +} + +void GroupLogicalPacket::AddPacket(const void *data, int dataLen) +{ + assert(data != NULL); + assert(dataLen >= 0); + AddPacketInternal(data, dataLen, false); +} + +void GroupLogicalPacket::AddPacketInternal(const void *data, int dataLen, bool isInternalPacket) +{ + if (dataLen == 0) + return; + mData = (uchar *)UdpMisc::SmartResize(mData, mDataLen + dataLen + 10, 512); // 7 is the most bytes that could be needed to specify the length of the data to follow, 2 is for internal header-bytes, 1 is for zero-escape if needed (if they need to be added on) + if (mDataLen == 0) + { + mData[0] = 0; + mData[1] = UdpConnection::cUdpPacketGroup; + mDataLen = 2; + } + + uchar *ptr = mData + mDataLen; + if (!isInternalPacket && *(const uchar *)data == 0) + { + ptr += UdpMisc::PutVariableValue(ptr, dataLen + 1); + *ptr++ = 0; // packet is not internal and starts with 0, so we need to zero-escape it so it knows it's an application packet + } + else + ptr += UdpMisc::PutVariableValue(ptr, dataLen); + + memcpy(ptr, data, dataLen); + ptr += dataLen; + mDataLen = ptr - mData; +} + +void *GroupLogicalPacket::GetDataPtr() +{ + return(mData); +} + +const void *GroupLogicalPacket::GetDataPtr() const +{ + return(mData); +} + +int GroupLogicalPacket::GetDataLen() const +{ + return(mDataLen); +} + +void GroupLogicalPacket::SetDataLen(int /*len*/) +{ + assert(0); // not allowed to set the len of a group logical packet +} + +bool GroupLogicalPacket::IsInternalPacket() const +{ + return(true); +} + + /////////////////////////////////////////////////////////////////////////////////////////// + // PooledLogicalPacket implementation + /////////////////////////////////////////////////////////////////////////////////////////// +PooledLogicalPacket::PooledLogicalPacket(UdpManager *manager, int len) +{ + mMaxDataLen = len; + mData = new uchar[mMaxDataLen]; + mDataLen = 0; + + mUdpManager = manager; + mAvailableNext = NULL; + mCreatedNext = NULL; + mCreatedPrev = NULL; + + mUdpManager->PoolCreated(this); +} + +PooledLogicalPacket::~PooledLogicalPacket() +{ + if (mUdpManager != NULL) + { + mUdpManager->PoolDestroyed(this); + } + + delete[] mData; +} + +void PooledLogicalPacket::AddRef() const +{ + LogicalPacket::AddRef(); +} + +void PooledLogicalPacket::Release() const +{ + if (mRefCount == 1 && mUdpManager != NULL) + mUdpManager->PoolReturn(const_cast(this)); // if pool wants to keep us, it will inc our ref count, preventing our destruction + // we cast off our const, as when we are added back to the pool, we can be modified + LogicalPacket::Release(); +} + +void *PooledLogicalPacket::GetDataPtr() +{ + return(mData); +} + +const void *PooledLogicalPacket::GetDataPtr() const +{ + return(mData); +} + +int PooledLogicalPacket::GetDataLen() const +{ + return(mDataLen); +} + +void PooledLogicalPacket::SetDataLen(int len) +{ + assert(len <= mMaxDataLen); + mDataLen = len; +} + +void PooledLogicalPacket::SetData(const void *data, int dataLen, const void *data2, int dataLen2) +{ + mDataLen = dataLen + dataLen2; + if (data != NULL) + memcpy(mData, data, dataLen); + if (data2 != NULL) + memcpy(mData + dataLen, data2, dataLen2); +} + + + + /////////////////////////////////////////////////////////////////////////////////////////// + // WrappedLogicalPacket implementation + /////////////////////////////////////////////////////////////////////////////////////////// +WrappedLogicalPacket::WrappedLogicalPacket(UdpManager *udpManager) +{ + mPacket = NULL; + mUdpManager = udpManager; + mAvailableNext = NULL; + mCreatedNext = NULL; + mCreatedPrev = NULL; + + mUdpManager->WrappedCreated(this); +} + +WrappedLogicalPacket::~WrappedLogicalPacket() +{ + if (mUdpManager != NULL) + { + mUdpManager->WrappedDestroyed(this); + } + + if (mPacket != NULL) + { + mPacket->Release(); + } +} + +void WrappedLogicalPacket::AddRef() const +{ + LogicalPacket::AddRef(); +} + +void WrappedLogicalPacket::Release() const +{ + if (mRefCount == 1 && mUdpManager != NULL) + mUdpManager->WrappedReturn(const_cast(this)); // if pool wants to keep us, it will inc our ref count, preventing our destruction + // we cast off our const, as when we are added back to the pool, we can be modified + LogicalPacket::Release(); +} + +void WrappedLogicalPacket::SetLogicalPacket(const LogicalPacket *packet) +{ + if (mPacket != NULL) + mPacket->Release(); + mPacket = packet; + if (mPacket != NULL) + mPacket->AddRef(); +} + +void *WrappedLogicalPacket::GetDataPtr() +{ + return(const_cast(mPacket->GetDataPtr())); // didn't have a choice really... +} + +const void *WrappedLogicalPacket::GetDataPtr() const +{ + return(mPacket->GetDataPtr()); +} + +int WrappedLogicalPacket::GetDataLen() const +{ + return(mPacket->GetDataLen()); +} + +void WrappedLogicalPacket::SetDataLen(int /*len*/) +{ + assert(0); // this should not be possible +} + + + + //////////////////////////////////////////////////////////////////////////////////////////////////// + // SimulateQueueEntry functions + //////////////////////////////////////////////////////////////////////////////////////////////////// +UdpManager::SimulateQueueEntry::SimulateQueueEntry(const uchar *data, int dataLen, UdpIpAddress ip, int port) +{ + mData = new uchar[dataLen]; + mDataLen = dataLen; + memcpy(mData, data, dataLen); + mIp = ip; + mPort = port; + mNext = NULL; +} + +UdpManager::SimulateQueueEntry::~SimulateQueueEntry() +{ + delete[] mData; +} + + + + //////////////////////////////////////////////////////////////////////////////////////////////////// + // UdpMisc functions + //////////////////////////////////////////////////////////////////////////////////////////////////// + +UdpMisc::ClockStamp UdpMisc::Clock() +{ +#if defined(WIN32) + static int sGlobalHigh = 0; + static unsigned sGlobalLow = 0; + + unsigned low = GetTickCount(); + if (low < sGlobalLow) + { + sGlobalHigh++; // not entirely safe, if the once every 47 days we happen to thread-slice right at this spot and some other thread called this, time could get messed for this call...I can live with those odds + } + sGlobalLow = low; + return(((ClockStamp)sGlobalHigh << 32) | sGlobalLow); +#else + // this implementation has the same sort of threading issues the windows version used to have + // before I started using thread-local-storage to solve them. The worst thing that can happen + // is the time-correction will occur more than once from seperate threads, which would result + // in time appearing to jump forward, which isn't that big of a deal. + static ClockStamp sLastStamp = 0; + static ClockStamp sCurrentCorrection = 0; + struct timeval tv; + gettimeofday(&tv, NULL); + UdpMisc::ClockStamp cs = static_cast(tv.tv_sec) * 1000 + static_cast(tv.tv_usec / 1000); + cs += sCurrentCorrection; + if (cs < sLastStamp) + { + // clock moved backwards (somebody changed it), don't ever let this happen + // if clock moves forward, there is no way we can recognize it, code will just + // have to deal with it. In the case of the UdpLibrary, it will likely result + // in a ton of pending packets thinking they have gotten lost and being sent, fairly harmless. + sCurrentCorrection += (sLastStamp - cs); + cs = sLastStamp; + } + sLastStamp = cs; + return(cs); +#endif +} + +int UdpMisc::SyncStampShortDeltaTime(ushort stamp1, ushort stamp2) +{ + ushort delta = (ushort)(stamp1 - stamp2); + if (delta > 0x7fff) + return((int)(0xffff - delta)); + return((int)delta); +} + +int UdpMisc::SyncStampLongDeltaTime(uint stamp1, uint stamp2) +{ + uint delta = stamp1 - stamp2; + if (delta > 0x7fffffff) + return((int)(0xffffffff - delta)); + return((int)delta); +} + +int UdpMisc::Random(int *seed) +{ + int hi = *seed / 127773; + int lo = *seed % 127773; + int t = lo * 16807 - hi * 2836 + 123; + *seed = (t > 0) ? t : (t + 2147483647); + return(*seed); +} + +int UdpMisc::Crc32(const void *buffer, int bufferLen, int encryptValue) +{ + static unsigned crc32_table[256] = { + 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, + 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, + 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, + 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, + 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, + 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, + 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, + 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, + 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, + 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, + 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106, + 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, + 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, + 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, + 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, + 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, + 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, + 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, + 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, + 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, + 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, + 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, + 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, + 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, + 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, + 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, + 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E, + 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, + 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, + 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, + 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, + 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, + 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, + 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, + 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242, + 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, + 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, + 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, + 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, + 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, + 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, + 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, + 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D}; + + int crc = 0xffffffff; + crc = ((crc >> 8) & 0x00FFFFFFL) ^ crc32_table[(crc ^ (encryptValue & 0xff)) & 0x000000FFL]; + crc = ((crc >> 8) & 0x00FFFFFFL) ^ crc32_table[(crc ^ ((encryptValue >> 8) & 0xff)) & 0x000000FFL]; + crc = ((crc >> 8) & 0x00FFFFFFL) ^ crc32_table[(crc ^ ((encryptValue >> 16) & 0xff)) & 0x000000FFL]; + crc = ((crc >> 8) & 0x00FFFFFFL) ^ crc32_table[(crc ^ ((encryptValue >> 24) & 0xff)) & 0x000000FFL]; + + const uchar *bufPtr = (const uchar *)buffer; + const uchar *endPtr = (const uchar *)buffer + bufferLen; + while (bufPtr < endPtr) + { + crc = ((crc >> 8) & 0x00FFFFFFL) ^ crc32_table[(crc ^ *bufPtr) & 0x000000FFL]; + bufPtr++; + } + crc = ~crc; + + return(crc); +} + +void *UdpMisc::SmartResize(void *ptr, int bytes, int round) +{ + enum { cAlignment = sizeof(int) }; + + // rounding stuff for speed + bytes = ((bytes + round - 1) / round) * round; + + if (bytes == 0) + { + if (ptr != NULL) + { + free((uchar *)ptr - cAlignment); + } + return(NULL); + } + + uchar *ptr2; + if (ptr == NULL) + { + ptr2 = (uchar *)malloc(bytes + cAlignment); + if (ptr2 == NULL) + return(NULL); + *(int *)ptr2 = bytes; + return(ptr2 + cAlignment); + } + + int oldBytes = *((int *)((uchar *)ptr - cAlignment)); + if (bytes == oldBytes) + return(ptr); + + ptr2 = (uchar *)realloc((uchar *)ptr - cAlignment, bytes + cAlignment); + if (ptr2 == NULL) + return(NULL); + + *(int *)ptr2 = bytes; + return(ptr2 + cAlignment); +} + +uint UdpMisc::PutVariableValue(void *buffer, uint value) +{ + uchar *bufptr = (uchar *)buffer; + uint store = (uint)value; + if (store < 254) + { + *bufptr = (uchar)store; + return(1); + } + else if (store < 0xffff) + { + *bufptr = 0xff; + *(bufptr + 1) = (uchar)(store >> 8); + *(bufptr + 2) = (uchar)(store & 0xff); + return(3); + } + else + { + *bufptr = 0xff; + *(bufptr + 1) = 0xff; + *(bufptr + 2) = 0xff; + *(bufptr + 3) = (uchar)(store >> 24); + *(bufptr + 4) = (uchar)((store >> 16) & 0xff); + *(bufptr + 5) = (uchar)((store >> 8) & 0xff); + *(bufptr + 6) = (uchar)(store & 0xff); + return(7); + } +} + +uint UdpMisc::GetVariableValue(const void *buffer, uint *value) +{ + const uchar *bufptr = (const uchar *)buffer; + if (*bufptr == 0xff) + { + if (*(bufptr + 1) == 0xff && *(bufptr + 2) == 0xff) + { + *value = (uint)((*(bufptr + 3) << 24) | (*(bufptr + 4) << 16) | (*(bufptr + 5) << 8) | *(bufptr + 6)); + return(7); + } + + *value = (uint)((*(bufptr + 1) << 8) | *(bufptr + 2)); + return(3); + } + + *value = (uint)*bufptr; + return(1); +} + +LogicalPacket *UdpMisc::CreateQuickLogicalPacket(const void *data, int dataLen, const void *data2, int dataLen2) +{ + enum { cQuickFactor = 128 }; + int totalDataLen = dataLen + dataLen2; + int q = (totalDataLen - 1) / cQuickFactor; + LogicalPacket *tlp; + switch(q) + { + case 0: + tlp = new FixedLogicalPacket(NULL, totalDataLen); + break; + case 1: + tlp = new FixedLogicalPacket(NULL, totalDataLen); + break; + case 2: + case 3: + tlp = new FixedLogicalPacket(NULL, totalDataLen); + break; + case 4: + case 5: + case 6: + case 7: + tlp = new FixedLogicalPacket(NULL, totalDataLen); + break; + default: + tlp = new SimpleLogicalPacket(NULL, totalDataLen); + break; + } + + uchar *dest = (uchar *)tlp->GetDataPtr(); + if (data != NULL) + memcpy(dest, data, dataLen); + if (data2 != NULL) + memcpy(dest + dataLen, data2, dataLen2); + return(tlp); +} + +UdpIpAddress UdpMisc::GetHostByName(const char *hostName) +{ + InitializeOperatingSystem(); + + unsigned long address = inet_addr(hostName); + if (address == INADDR_NONE) + { + struct hostent * lphp; + lphp = gethostbyname(hostName); + if (lphp == NULL) + { + address = 0; + } + else + { + address = ((struct in_addr *)(lphp->h_addr))->s_addr; + } + } + + TerminateOperatingSystem(); + return(UdpIpAddress(address)); +} + +void UdpMisc::Sleep(int milliseconds) +{ +#if defined(WIN32) + ::Sleep((DWORD)milliseconds); +#else + struct timeval tv; + tv.tv_sec = milliseconds / 1000; + tv.tv_usec = (milliseconds % 1000) * 1000; + select(0, 0, 0, 0, &tv); +#endif +} + +UdpLibraryException::UdpLibraryException() +{ +} + +UdpLibraryException::~UdpLibraryException() +{ +} + + diff --git a/external/3rd/library/udplibrary/UdpLibrary.doc b/external/3rd/library/udplibrary/UdpLibrary.doc new file mode 100644 index 0000000000000000000000000000000000000000..97735470298f5428c1bec64b40b9544a45dbd88f GIT binary patch literal 230400 zcmeFaU5unzn%`Bsw(;(=1#2%XVzF1D^{&YpS9W#xeC)VJ0#{de&(ylRtLV(8J!|7t zWM))mOl3x-BQmX-l@S-n)4Mh+VT6$YA%T$j5EAyH#RwsU4fcgC*%A_qbDr<7bIxD=E5GA6{{8>_C;yLITmStF zw|>j5-~8?m-}=Y8&wnew|F=JoAN<&@Tfffl`tvux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7&wlm zqHAHCu*8|g@6P_CgI^V*cV0E3TU~YHLPC5Wo!8==y*3dxpbl{nLxhNEEKCmfM1@G6 zDZakH8?YgwL^z!CNLCuLF^$=TCj5;}*^IbIvISf6cbc*ljw(H*+2bDBmhIS{9oUiP z?8MIOLR{S0jopcZ$`U62#@j@kMj>E@dQuu z6i+jmfAS2^@*L0e0x$9s|Kep{VG6JE8m}{zX}rO7-sCOb<{f76Z{FoS-sb~86{zp{YeSjg`zBF=TQC`UFf zX{E$&OjV#Fm8eV=s!|QH8dKG&K`u3^MQzrm4s}@veV|kIS(o)#pAFcMjc7na8nH2r z*@PyD<(S%(&DfkR*pk1~l&xsS)@;MJY{&NOz>YL$Cw68Rc4aqqXAfGiCws9sE!l@w z>`QC*!@X3g{Sieb+2#LM-HJAhtiqDIGiKsg1h8XT{((w98Gt6a11>; zmR>}smfjpsANnF{O{za4)}#h9h{2q|5KiPIPG%^la4M&9I%jYuXOYL*oWr>cV>lx? zkMp^J3%Q7kxr9r(jFDW<6ZI z^Qvo)kzPLKj99^A$>KFR`9Jw7EqLeuffw#~`s5kUWKFjG3%~dK9Txz*`)%u)8E$#r zRJZr4XoMW19LqB9fu?^$?7+R!p=_4Cuv75upZ~wE=~pmVHp@EwT*EdRTCkjQ963Y) zpSZvq%YHv=;aF>AaFUV^$WwYb^w>(4fBEH@ex=AR3gMMg;*!?#m8W@f$va&GmTmbb z6E2*-khA8tpJSP}YQgczSIhHXtreVpdP)UTjSGE49M8qJ(06$z?^ri0OLa=teQE1I z|Lh-z_vrXc*1-DvCiTDQ1ca|$;kqCBR9S)fkD9l3VIiXIW&6R!_4j}6osh +#include "UdpHandler.hpp" +#include "priority.hpp" +#include "hashtable.hpp" + +#if defined(WIN32) + typedef unsigned int SOCKET; // avoids us having to include winsock.h just for this + typedef __int64 udp_int64; +#else + typedef int SOCKET; + typedef long long udp_int64; +#endif + +class UdpReliableChannel; +class SimpleLogicalPacket; +class GroupLogicalPacket; + +enum UdpChannel { cUdpChannelUnreliable // unreliable/unordered/buffered + , cUdpChannelUnreliableUnbuffered // unreliable/unordered/unbuffered + , cUdpChannelOrdered // unreliable/ordered/buffered + , cUdpChannelOrderedUnbuffered // unreliable/ordered/unbuffered + , cUdpChannelReliable1 // reliable (as per channel config) + , cUdpChannelReliable2 // reliable (as per channel config) + , cUdpChannelReliable3 // reliable (as per channel config) + , cUdpChannelReliable4 // reliable (as per channel config) + , cUdpChannelCount // count of number of channels + }; + +struct UdpManagerStatistics +{ + udp_int64 bytesSent; + udp_int64 packetsSent; + udp_int64 bytesReceived; + udp_int64 packetsReceived; + udp_int64 connectionRequests; + udp_int64 crcRejectedPackets; + udp_int64 orderRejectedPackets; + udp_int64 duplicatePacketsReceived; + udp_int64 resentPacketsAccelerated; // number of times we have resent a packet due to receiving a later packet in the series + udp_int64 resentPacketsTimedOut; // number of times we have resent a packet due to the ack-timeout expiring + udp_int64 priorityQueueProcessed; // cumulative number of times a priority-queue entry has received processing time + udp_int64 priorityQueuePossible; // cumalative number of priority-queue entries that could have received processing time + udp_int64 applicationPacketsSent; + udp_int64 applicationPacketsReceived; + udp_int64 iterations; // number of times GiveTime has been called + udp_int64 corruptPacketErrors; // number of misformed/corrupt packets + udp_int64 socketOverflowErrors; // number of times the socket buffer was full when a send was attempted. + int poolCreated; // number of packets created in the pool + int poolAvailable; // number of packets available in the pool + int elapsedTime; // how long these statistics have been gathered (in milliseconds), useful for figuring out averages +}; + +struct UdpConnectionStatistics +{ + ///////////////////////////////////////////////////////////////////////// + // these statistics are valid even if clock-sync is not used + // these statistics are never reset and should not be as the negotiated + // packetloss stats would get messed up if they were + // as such, use UdpConnection::ConnectionAge to determine how long they have been accumulating + ///////////////////////////////////////////////////////////////////////// + udp_int64 totalBytesSent; + udp_int64 totalBytesReceived; + udp_int64 totalPacketsSent; // total packets we have sent + udp_int64 totalPacketsReceived; // total packets we have received + udp_int64 crcRejectedPackets; // total packets on our connection that have been rejected due to a crc error + udp_int64 orderRejectedPackets; // total packets on our connection that have been rejected due to an order error (only applicable for ordered channel) + udp_int64 duplicatePacketsReceived; // total reliable packets that we received where we had already received it before and threw it away + udp_int64 resentPacketsAccelerated; // number of times we have resent a packet due to receiving a later packet in the series + udp_int64 resentPacketsTimedOut; // number of times we have resent a packet due to the ack-timeout expiring + udp_int64 applicationPacketsSent; + udp_int64 applicationPacketsReceived; + udp_int64 iterations; // number of times this connection has been given processing time + udp_int64 corruptPacketErrors; // number of misformed/corrupt packets + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // these statistics are only valid if clock-sync'ing is enabled (highly recommended) (will be valid on both client and server side) + // these statistics are reset by PingStatReset and are negotiated periodically by the clock-sync stuff (Params::clockSyncDelay) + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + int masterPingAge; // only valid (and applicable) on client side + int masterPingTime; + int averagePingTime; + int lowPingTime; + int highPingTime; + int lastPingTime; + int reliableAveragePing; // the average time (over last 3 acks) for a reliable packet to get acked (when packet is not lost) + udp_int64 syncOurSent; // total packets we have sent at time they reported their numbers + udp_int64 syncOurReceived; // total packets we have received at time they reported their numbers + udp_int64 syncTheirSent; // total packets they have sent + udp_int64 syncTheirReceived; // total packets they have received + float percentSentSuccess; + float percentReceivedSuccess; +}; + +class UdpIpAddress +{ + public: + UdpIpAddress(unsigned int ip = 0); + unsigned int GetAddress() const { return(mIp); } + char *GetAddress(char *buffer) const; + bool operator==(const UdpIpAddress& e) const { return(mIp == e.mIp); } + protected: + unsigned int mIp; +}; + +class UdpMisc +{ + //////////////////////////////////////////////////////////////////////////////////////////// + // This is a group of miscellaneous function used as part of the implementation + // The application is free to use these helper functions as well if finds them useful. + // they are stuck into this class so as to avoid conflicts with the application + //////////////////////////////////////////////////////////////////////////////////////////// + public: + + + // note: on these clock functions, ClockElapsed and ClockDiff are designed to provide a time difference that will safely not overflow an 'int'. + // overflowing can occur in situations where the initial stamp is 0. Since a ClockStamp is a 64-bit number, we also avoid promoting + // other application values involved in the calculation to the 64-bit level by using these helper-functions. If you don't mind doing everything + // in 64-bit values, then feel free to do simple math on the clock-stamps instead of using the functions. + typedef udp_int64 ClockStamp; + static ClockStamp Clock(); // returns a timestamp (most likely in milliseconds) + static int ClockElapsed(ClockStamp stamp); // returns a elapsed time since stamp in milliseconds (if elapsed is over 23 days, it returns 23 days) + static int ClockDiff(ClockStamp start, ClockStamp stop); // returns a time difference in milliseconds (if difference is over 23 days, it returns 23 days) + + static int Crc32(const void *buffer, int bufferLen, int encryptValue = 0); // calculate a 32-bit crc for a buffer (encrypt value simple scrambles the crc at the beginning so the same packet doesn't produce the same crc on different connections) + static int Random(int *seed); // random number generator + static void Sleep(int milliseconds); + + static ushort LocalSyncStampShort(); // gets a local-clock based sync-stamp. (only good for timings up to 32 seconds) + static uint LocalSyncStampLong(); // gets a local-clock based sync-stamp. (good for timings up to 23 days) + + // returns the time difference between two sync-stamps that are based on the same clock + // for example, two LocalSyncStampShort stamps can be compared. Only differences up to + // 32 seconds can be timed. There is also a UdpConnect::ServerSyncStampShort function that returns + // you a sync-stamp that you can compare with ServerSyncStampShort values generated on other + // machines that are synchronized against the same server. This ServerSyncStampShort function and + // this delta time function serve as the basis for calculating one-way travel times for packets. + static int SyncStampShortDeltaTime(ushort stamp1, ushort stamp2); + static int SyncStampLongDeltaTime(uint stamp1, uint stamp2); // same as Short version only no limit + + // used to alloc/resize/free an allocation previously created with this function + // rounding causes that it pre-allocates additional space up to the rounded buffer size + // this allows the function to be called over and over again as tiny members are added, yet + // only do an actual realloc periodically. A 4-byte header is invisibly prepended on the + // allocated block such that it can keep track of the actual size of the block. The built-in + // memory manager does a little bit of this, but if you know you have an allocation that is likely + // to grow quite a bit, you can set the round'ing size up to a fairly large number and avoid + // unnecessary reallocs at the cost of a little potentially wasted space + // initial allocations are done by passing in ptr==NULL, freeing is done by passing in bytes==0 + static void *SmartResize(void *ptr, int bytes, int round = 1); + + // the following two functions store values in the buffer as a variable length (B1, 0xffB2B1, 0xffffffB4B3B2B1) + // such that values under 254 take one byte, values under 65535 take three bytes, and larger values take seven bytes + static uint PutVariableValue(void *buffer, uint value); // returns the number of bytes it took to store the value in the buffer + static uint GetVariableValue(const void *buffer, uint *value); // returns the number of bytes it took to get the value from the buffer + + // these functions are used to aid in portability and serve to ensure that the packet-headers are interpretted in the same + // manner on all platforms + static int PutValue64(void *buffer, udp_int64 value); // puts a 64-bit value into the buffer in big-endian format, returns number of bytes used(8) + static int PutValue32(void *buffer, uint value); // puts a 32-bit value into the buffer in big-endian format, returns number of bytes used(4) + static int PutValue24(void *buffer, uint value); // puts a 24-bit value into the buffer in big-endian format, returns number of bytes used(4) + static int PutValue16(void *buffer, ushort value); // puts a 16-bit value into the buffer in big-endian format, returns number of bytes used(2) + + static udp_int64 GetValue64(const void *buffer); // gets a 64-bit value from the buffer in big-endian format + static uint GetValue32(const void *buffer); // gets a 32-bit value from the buffer in big-endian format + static uint GetValue24(const void *buffer); // gets a 24-bit value from the buffer in big-endian format + static ushort GetValue16(const void *buffer); // gets a 16-bit value from the buffer in big-endian format + + static LogicalPacket *CreateQuickLogicalPacket(const void *data, int dataLen, const void *data2 = NULL, int dataLen2 = 0); + + // looks up the specified name and translates it to an IP address + // this is a blocking call that can at times take a significant amount of time, but will generally be fast (less than 300ms) + static UdpIpAddress GetHostByName(const char *hostName); +}; + + + //////////////////////////////////////////////////////////////////////////////////////////////////////////// + // The purpose of the LogicalPacket class is to provide means whereby multiple connections can share + // a queued packet to save having each connection make its own copy. It is highly recommended that + // logical packets be used for optimal performance when sending reliable data as well, since internally + // it will just end up creating one for you anyhow (LogicalPackets are used by the reliable layer to + // hold onto the data until it is acknowledged) + // + // Logical packets passed in are never modified by the udp library. + // After calling Send, application should immediately Release the logical packet if they don't need it for + // something else (like sending to another connection). The udp library will addref it if it decided it + // wants to keep it around past the Send call (for reliable data, it will always hang onto it, for unreliable + // data, it will only hang onto it if it gets promoted to reliable due to size.) + // + // Application is encouraged to derive their own application-specific packet classes from the LogicalPacket + // The object is required to be able to provide a pointer to the raw packet data that will remain valid + // for as long as the LogicalPacket object exists. + // + // for example: + // class PlayerLoginPacket : public LogicalPacket + // { + // public: + // PlayerLoginPacket(char *userName, char *password) { mData.packetType = cPacketTypePlayerLogin; strcpy(mData.userName, userName); strcpy(mData.password, password); } + // virtual const void *GetDataPtr() const { return(&mData); } + // virtual int GetDataLen() const { return(sizeof(mData)); } + // protected: + // virtual ~PlayerLoginPacket() {}; + // struct + // { + // uchar packetType; + // char userName[32]; + // char password[32]; + // } mData; + // }; + //////////////////////////////////////////////////////////////////////////////////////////////////////////// +class LogicalPacket +{ + public: + LogicalPacket(); + virtual void AddRef() const; + virtual void Release() const; + virtual int GetRefCount() const; + + virtual void *GetDataPtr() = 0; + virtual const void *GetDataPtr() const = 0; + virtual int GetDataLen() const = 0; + virtual void SetDataLen(int len) = 0; + + // returns true if this logical packet is an internal packet + // by default this returns false, and applications should not override this function to do + // otherwise. Basically, if this returns true, it tells the udp library that the packet has + // an internal-packet header (starts with a zero byte) on it that should not be escaped when sent. + // The packet will ultimately be processed by the internal udp library code on the other side. + // The purpose of this is to support features such as GroupLogicalPacket, such that the application + // can prep grouped packets and then send them via traditional means (it's how the udp library + // knows that the packet is a group packet instead of just a application-packet that happens to start with 0) + virtual bool IsInternalPacket() const; + protected: + virtual ~LogicalPacket(); // protected since the class should only be deleted by Releasing it + + protected: + mutable int mRefCount; + + protected: + friend class UdpReliableChannel; + mutable const LogicalPacket *mReliableQueueNext; // owned/managed by the reliable channel object as an optimizations, do not touch + // NULL = unclaimed, point-to-self = claimed, but end of list +}; + +class SimpleLogicalPacket : public LogicalPacket +{ + // this class is a simply dynamically allocated packet. Applications are welcome to use it, but + // it was originally created to allow the internal code to handle reliable data that was sent + // via the Send(char *, int) api call. + public: + SimpleLogicalPacket(const void *data, int dataLen); // data can be NULL if you want to populate it after it is allocated (get the pointer and write to it) + virtual void *GetDataPtr(); + virtual const void *GetDataPtr() const; + virtual int GetDataLen() const; + virtual void SetDataLen(int len); + protected: + virtual ~SimpleLogicalPacket(); + uchar *mData; + int mDataLen; +}; + +class WrappedLogicalPacket : public LogicalPacket +{ + // this class is used internally by the library such that it can get a free and clear mNext pointer + // in cases where a logical packet is being shared by multiple connections (such that the packet + // can reside in the linked-list for the reliable channel queue) + public: + WrappedLogicalPacket(UdpManager *manager); // lives in a pool + + virtual void AddRef() const; + virtual void Release() const; + virtual void *GetDataPtr(); + virtual const void *GetDataPtr() const; + virtual int GetDataLen() const; + virtual void SetDataLen(int len); + protected: + virtual ~WrappedLogicalPacket(); + const LogicalPacket *mPacket; + + protected: + friend class UdpManager; + void SetLogicalPacket(const LogicalPacket *packet); + UdpManager *mUdpManager; + WrappedLogicalPacket *mAvailableNext; + + WrappedLogicalPacket *mCreatedNext; + WrappedLogicalPacket *mCreatedPrev; +}; + +template class FixedLogicalPacket : public LogicalPacket +{ + // this class is similar to the SimpleLogicalPacket in that it is designed to just store raw + // data of various sizes. The difference here is that this class may be created at compile + // time to be any size desired (via template parameters), and is more efficient than the + // SimpleLogicalPacket when used (it avoid the internal alloc as the data is inline). The + // UdpMisc::CreateQuickLogicalPacket function automatically attempts to use this for small + // packets and will fall back and use SimpleLogicalPackets for larger packets. + public: + FixedLogicalPacket(const void *data, int dataLen); + virtual void *GetDataPtr(); + virtual const void *GetDataPtr() const; + virtual int GetDataLen() const; + virtual void SetDataLen(int len); + protected: + uchar mData[t_quickSize]; + int mDataLen; +}; + +template class StructLogicalPacket : public LogicalPacket +{ + // this class is designed to turn any raw struct into a logical packet. The application + // can then access the struct through thisLogicalPacket->mStruct.structMember. The template + // will take care of all the wrappings necessary to turn the struct into a fully functional + // logical packet that can be sent to a connection. As a word of caution, sending struct's + // across the wire is not considered very portable. There are byte-ordering and structure-packing + // issues that are platform dependent. At a minimum, you will probably want to make sure that + // struct's you use in this way are packed to 1-byte boundaries, such that you are not sending + // wasteful information. Do not attempt to send the member-content of classes (particularly classes + // with virtual functions) via this method as they may contain hidden data-members (such as pointers + // to vtables). + public: + StructLogicalPacket(T *initData = NULL); + virtual void *GetDataPtr(); + virtual const void *GetDataPtr() const; + virtual int GetDataLen() const; + virtual void SetDataLen(int len); + protected: + T mStruct; +}; + +class GroupLogicalPacket : public LogicalPacket +{ + // this class is a helper object intended for use by the application (it is not used internally) + // It allows you to add multiple application-packets and them send them all as an autonomous unit. + // The receiving end will automatically split the packet up and send them to the application callback function + // as if the individual packets had all been sent one at at time. + // + // This facility is primarily intended for reliable data, though it can be used to group unreliable data as well. + // Grouping unreliable data together would effectively give you an all-or-nothing type of delivery system. Be + // mindful that if the group of packets gets larger than max-raw-packet-size, it will end up getting + // promoted to being a reliable-packet with the associated overhead involved in that. + // + // You might be wondering what the advantage would be of grouping packets together at the application level as opposed to + // just letting the internal multi-buffer take care of the problem for you. For starters, the internal multi-buffer + // is incapable of combining partial-packets, so you end up sending less than maximum-size packets if you let the + // internal layer take care of it. Additionally, there is additional overhead in that if you send 100 tiny logical + // packets, each one will need to be ack'ed by the receiving end (even though they are getting combined down at the physical-packet + // level in order to reduce UDP overhead). Finally, grouping them together at the higher level will allow the + // logical-packet compression helper routines to operate on larger chunks of data at a time, which tends to improve + // compression efficiency. The downside to combining is that none of the application packets get delivered until the + // entire group arrives, then all are delivered in one fell swoop...the net effect being that in order to gain these efficeincies, + // the first-packet takes longer to be delivered to the application than it otherwise would have (while the last packet will in theory + // get there is less time as there will be less overhead). + // + // Internal packets can be added to the group (though that is somewhat unlikely), which means you can add a group to a group + public: + GroupLogicalPacket(); + void AddPacket(const LogicalPacket *packet); // cannot add internal logical packets to the group + void AddPacket(const void *data, int dataLen); + virtual void *GetDataPtr(); + virtual const void *GetDataPtr() const; + virtual int GetDataLen() const; + virtual void SetDataLen(int len); + virtual bool IsInternalPacket() const; // returns true if this is considering an internal logical packet type + + protected: + virtual ~GroupLogicalPacket(); + void AddPacketInternal(const void *data, int dataLen, bool isInternalPacket); + + uchar *mData; + int mDataLen; +}; + +class PooledLogicalPacket : public LogicalPacket +{ + // a pooled logical packet is like other logical packets, only when it's refCount gets down to 1, it notifies + // its manager to add it back to the pool. (The manager keeps the last ref count on the packet) + public: + PooledLogicalPacket(UdpManager *manager, int len); + + virtual void AddRef() const; + virtual void Release() const; + virtual void *GetDataPtr(); + virtual const void *GetDataPtr() const; + virtual int GetDataLen() const; + virtual void SetDataLen(int len); + protected: + virtual ~PooledLogicalPacket(); + + uchar *mData; + int mDataLen; + int mMaxDataLen; + protected: + friend class UdpManager; + void SetData(const void *data, int dataLen, const void *data2 = NULL, int dataLen2 = 0); + UdpManager *mUdpManager; + PooledLogicalPacket *mAvailableNext; + PooledLogicalPacket *mCreatedNext; + PooledLogicalPacket *mCreatedPrev; +}; + + +class AddressHashTableMember : public HashTableMember +{ +}; + +class ConnectCodeHashTableMember : public HashTableMember +{ +}; + + //////////////////////////////////////////////////////////////////////////////////////////////////////////// + // The purpose of the UdpManager is to manage a set of connections that are coming in on a particular port. + // Typically an application will only have one UdpManager taking care of all incoming connections. The + // exception is if the application is talking to two distinct sets of individuals. For example, the leaf + // server application might have a UdpManager to manage the connections to all the users/players who will + // be connecting. It may then have a second UdpManager to manage its connection to a master server + // someplace (though in theory it could use one UdpManager for everything). + // + // The UdpManager owns the solitary socket that all data being sent/received by any of the managed connections uses. + // When the UdpManager is created, it is given a port-number that it uses for this purpose. The UdpManager is capable + // of establishing new connections to other UdpManager, or it is also capable of accepting new connections. + //////////////////////////////////////////////////////////////////////////////////////////////////////////// +class UdpManager +{ + public: + // encryption methods are allowed to change the packet-size, so raw packet level compression + // would actually be implemented as a new encryption-method if needed + // the user-supplied method requires that the both ends of the connection have the user-supplied + // encrypt handler functions setup to correspond to each other. + enum EncryptMethod { cEncryptMethodNone + , cEncryptMethodUserSupplied // use the UdpConnectionHandler::OnUserSuppliedEncrypt function + , cEncryptMethodUserSupplied2 // use the UdpConnectionHandler::OnUserSuppliedEncrypt2 function + , cEncryptMethodXorBuffer // slower xor method, but slightly more encrypted + , cEncryptMethodXor // faster using less memory, slightly less well encrypted, use this one as first choice typically + , cEncryptMethodCount }; + + enum ErrorCondition { cErrorConditionNone, cErrorConditionCouldNotAllocateSocket, cErrorConditionCouldNotBindSocket }; + + enum { cReliableChannelCount = 4 }; + enum { cEncryptPasses = 2 }; + enum { cProtocolVersion = 2 }; // protocol version must match on both ends, or connect packets are simply ignored by the server + enum { cHardMaxRawPacketSize = 16384 }; + enum { cHardMaxOutstandingPackets = 30000 }; // don't change this + + struct ReliableConfig + { + int maxOutstandingBytes; // maximum number of bytes that are allowed to be outstanding without an acknowledgement before more are sent (default=200k) + int maxOutstandingPackets; // maximum number of physical reliable packets that are allowed to be outstanding (default=400) + int maxInstandingPackets; // maximum number of incoming reliable packets it will queue for ordered delivery while waiting for the missing packet to arrive (should generally be same as maxOutstandingPackets setting on other side) (default=400) + int fragmentSize; // this is the size it should fragment large logical packets into (default=0=max allowed=maxRawPacketSize) + int trickleSize; // maximum number of bytes to send per trickleRate period of time (default=0=max allowed=fragmentSize) + int trickleRate; // how often trickleSize bytes are sent on the channel. (default=0=no trickle control) + int resendDelayAdjust; // amount of additional time (in ms) above the average ack-time before a packet should be deemed lost and resent (default=300) + int resendDelayPercent; // percent average ack-time it should use in calculating the resend delay (default = 125 or 125%) + int resendDelayCap; // maximum length of resend-delay that will ever be assigned to an outstanding packet. (default=5000) + int congestionWindowMinimum; // the minimum size to allow the congestion-window to shrink. This defaults to 0, though internally it the implementation will never let the window get smaller than a single raw packet (512 bytes by default). + // This setting is more intended to allow the application to set a higher minimum than that, effectively allowing the application to tell the connection to refuse to slow itself + // down as much. See release notes for more details. + bool outOfOrder; // whether incoming packets on this channel should be allowed to be delivered out of order (default=false) + bool processOnSend; // whether a packet sent on this channel should immediately be processed to determine if it can be sent, or whether it should wait for the next give time (default=false) + bool coalesce; // whether the reliable-channel should attempt to coalesce data to reduce ack's needed (note: rarely change this to false)(default=true) + bool ackDeduping; // whether ack-packets stuck into the low-level multi-buffer should be deduped (note: rarely change this to false)(default=true) + }; + + struct Params + { + Params(); // constructor merely sets default values for the structure members + + // instead of specifying a callbackConnectRequest pointer, you can also specify a handler object to receive + // the callback directly. To have the UdpManager call your object directly when connection requests come in, you + // simply need to derive your class (multiply if necessary) from UdpManagerHandler, then you can set this + // pointer equal to your object and the UdpManager will call it as appropriate. The UdpConnection object + // also has a handler mechanism that replaces the other callback functions below, see UdpConnection::SetHandler + // if a handler is specified, the callback function is ignored, even if specified. + // default = NULL (not used) + UdpManagerHandler *handler; + + // this is the maximum number of connections that can be established by this manager, any incoming/outgoing connections + // over this limit will be refused. On the client side, this typically only needs to be set to 1, though there + // is little harm in setting this number larger. + // default = 10 + int maxConnections; + + // this is the port number that this manager will use for all incoming and outgoing data. On the client side + // this is typically set to 0, which causes the manager object to randomly pick an available port. On the server + // side, this port should be set to a specific value as it will represent the port number that clients will use + // to connect to the server (ie. the listening port). It's generally a good idea to give the user on the client + // side the option of fixing this port number at a specific value as well as it is often necessary for them to + // do so in order to navigate company firewalls which may have specific port numbers open to them for this purpose. + // default = 0 + int port; + + // if port is not zero, setting this to a value greater than zero causes it to randomly pick a port in the range + // (port to port+portRange). This is desireable when you wish to have the manager bind to a random port within + // a specified range. If you specify a portRange to use, then port must not be 0 (since 0 means to let the OS + // pick the port to use). 0=don't use a range, bind only to the port specified in 'port' setting. + // default = 0 + int portRange; + + // the size of the outgoing socket buffer. The client will want to set this fairly small (32k or so), but the server + // will want to set this fairly large (512k) + // default = 64k + int outgoingBufferSize; + + // the size of the incoming socket buffer. The client will want to set this fairly small (32k or so), but the server + // will want to set this fairly large (512k) + // default = 64k + int incomingBufferSize; + + // the purpose of the packet history is to make debugging easier. Sometimes a processed packet will cause the server + // to crash (due to a bug or possibly just corruption). Typically the application will put an exception handler around + // the main loop and call UdpManager::DumpPacketHistory when it is triggered. This will dump a history of the last + // packets received such that they can be analyzed by hand to determine if one of them caused the problem and why. + // The packet history is done at the raw packet level (before logical packet re-assembly). This is the number of raw + // packets to buffer in the history. Typically this can be set fairly small (maybe 1000) since packets older than + // that have little debug value. (uses maxRawPacketSize * thisValue of memory) + // default = 100 + int packetHistoryMax; + + // how long a connection will wait before sending a keep-alive packet to the other side of the connection + // set this to 0 to never send keep-alive packets (typically the server will set this to 0 and never keep alive + // the client, but the client will typically set this some value that will ensure that the server will not kick them + // for inactivity. Keep alive packets are only sent if no other data is being sent. + // default = 0 + int keepAliveDelay; + + // this is very similar to the keep alive delay, but serves a completely different purpose and may not have the desired + // effect on all platforms. The purpose of the keepAliveDelay above is generally to keep data flowing to the server + // so it knows that the client is still there. In this manner if the server doesn't get data within a certain period + // of time, it can know that the connection is probably dead. Sometimes it is the case that the server does not need + // to be kept alive, or at least kept alive very often (like for a chat server perhaps where nobody is talking much). + // For some people, it may be necessary to send data more frequently in order to keep their NAT mapping fresh, or their + // firewall software happy. However, we don't want to be in a situation where our server is receiving a lot more data + // than it needs to just so these people can keep their port open. I have seen NAT's that lose mappings in as short + // as 20 seconds. What this feature does is a bit tricky. It changes the time-to-live (TTL) for a special keep-alive + // packet to some small value (4) which is enough for the packet to get past firewalls and NAT's, but not make it all + // the way to our server. In this manner, the port gets kept alive, but we don't waste bandwidth with these packets. + // These special packets are not counted statistically in any way and they do not reset any timers of any kind. Their + // sole purpose is to keep the port alive on the client side. Any other data (include standard keepAlive packets will + // reset the timer for this packet, so obviously the portAliveDelay must be smaller than the keepAliveDelay in order + // to be meaningful. + // default = 0 = off + int portAliveDelay; + + // whether this UdpManager should send back an unreachable-connection packet when it receives incoming data from + // a destination it does not have a UdpConnection object representing. This is the equivalent of a port-unreachable + // ICMP error packet, only taken up one level further to the virtual-connection object (UdpConnection). Imagine a + // server that has terminated a client's connection, but the client didn't get notice that such termination had occurred. + // The client would still think the connection was good and continue to try and send data to the server, but the data + // would simply be getting lost and queued up indefinately on the client. The client doesn't get port unreachable errors + // since the server is using the same port for lots of people. Having this true causes the server to notify the client + // that its connection is dead. If a client finds out it is terminated in this manner, it will set its disconnect reason + // to cDisconnectReasonUnreachableConnection. + // default = true + bool replyUnreachableConnection; + + // the UdpLibrary supports a feature whereby a connection can remap its ip/port on-the-fly. How it works is, if a client + // gets back a connection-unreachable error, it will send out a request for the server to remap its connection to the + // new port that it is coming from. This will happen if a NAT expires and the next outgoing packet causes a different + // port-mapping to be selected. The UdpLibrary can recover from this situation. The icmpErrorRetryPeriod must be + // set to a reasonably high value, such that the client has time to send data and request the remapping before icmp + // errors are processed. This value determines not only whether a server will honor a remapping request, but also + // whether the client will attempt a remapping request. The terms client as server are used loosely here, as it is + // possible for the server to request the remapping (though it is extremely unlikely that the server end + // of a connection will have its port changed on-the-fly). + // default = true + bool allowPortRemapping; + + // this is the same as allowPortRemapping, only it allows the full IP address to be remapped on-the-fly. I + // recommend that you do NOT enable this, as it represents a fairly serious security loophole, whereby a hacker could + // cause random people to be disconnected, and in theory possibly even hi-jack their connection. The odds of this + // actually being able to happen are incredibly rare unless the hacker has been snooping your packet stream, as they + // would effectively have to guess two 32-bit random numbers (one generated by the client and one generated by the server) + // default = false + bool allowAddressRemapping; + + + // how long (in milliseconds) the manager will ignore ICMP errors to a particular connection before it will act upon + // them and terminate the connection. When a packet is successfully received into the connection in question, the + // retry period is reset, such that the next time an ICMP error comes in, it has another period of time for it + // to resolve the issue. This servers a couple purpose, 1) it can allow for momentary outages that can be recovered + // from in short order, and 2) it is necessary for the port-remapping feature to work properly in situations where + // the server may send data to the old-port before the remapping negotiations are completed. In order for the + // remapping feature to work properly, this value should be set to larger than the longest time a connection typically + // goes without receiving data from the other side. (0=no grace period) + // default = 5000 + int icmpErrorRetryPeriod; + + // how long a connection will go without receiving any data before it will terminate the connection + // set this to 0 to never have the connection terminated automatically for receiving no data. + // the application will receive an OnTerminated callback when the connection is terminated due to this timeout + // this setting can be overridden on a per-connection basis by calling UdpConnection::SetNoDataTimeout + // default = 0 + int noDataTimeout; + + // when reliable data is sent, it remains in the queue until it is acknowledged by the other side. When you query + // the reliable channel status, you can find out the age of the oldest piece of data in the queue that has been sent + // yet has not been acknowledged yet. As a general rule, if things are operating correctly, it should be very rare + // for something that has been sent to not be acknowledged within a few seconds, even if resending had to occur. + // Eventually, the sender could use this statistic to determine that the other side is no longer talking and terminate + // the connection. In past version of the UdpLibrary, the sending-application has checked this statistic itself. This + // parameter will cause the UdpLibrary to monitor this for you and automatically change the connection to a disconnected + // state when the value goes over this setting (in milliseconds) (0 means do not perform this check at all) + // The default is set fairly liberally, on client-side connections, you could safely set this to as low as 30 seconds + // allowing for quicker realization of lost connections. Often times the connection will realize it is dead much quicker + // for other reasons. When disconnected due to this, the disconnect reason is set to cDisconnectReasonUnacknowledgedTimeout + // default = 90000 + int oldestUnacknowledgedTimeout; + + // maximum number of bytes that are allowed to be pending in all the reliable channels combined + // before it will terminate the connection with a cDisconnectReasonReliableOverflow. If this value is set to + // zero, then it will never overflow (it will run out of memory and crash first). If your application wants + // to do something other than disconnect on this condition, then the application will have to periodically check the status + // itself using UdpConnection::TotalPendingBytes and act as appropriate. + // default = 0 + int reliableOverflowBytes; + + // how long a connection will hold onto outgoing data in hopes of bundling together future outgoing data in the same + // raw packet (specified in milliseconds) + // setting this to 0 will cause it to effectively flush at the end of every frame. This is generally desireable in + // cases where frame-rates are slow (less than 10 fps), or for internal LAN connections. + // default = 50 + int maxDataHoldTime; + + // how much data a connection will hold onto before sending out a raw packet + // (0=no multi-packet buffering, all application sends result in immediate raw packet sends) + // (-1=use same value as maxRawPacketSize) + // this value will be effectively ignored if it is larger than the maxRawPacketSize specified below) + // default = -1 + int maxDataHoldSize; + + // maximum size that a raw packet is allowed to be, this must be set the same on both client and server side + // the reason for this is the incoming packets need some handling before the connection object they are associated + // with is determined. This means we can't have different connection objects using different sizes, so this + // effectively means all clients must be the same size as the server. Normally this won't be a problem, this value + // should likely be set to something like 496 or 1400 and then just kept there forever. + // the raw packet size won't have a significant effect on anything. Must be at least 64 bytes. + // default = 512 + int maxRawPacketSize; + + // how large the hash-table is for looking up connections. It takes 4*hashTableSize memory and it is recommended + // you set it fairly large to prevent collisions (10 times maximum number of connections should be fine) + // default = 100 + int hashTableSize; + + // whether a priority queue should be used. If a priority queue is not used, then everytime + // UdpManager::GiveTime is called, every UdpConnection object gets processing time as well. + // It is thought that if traffic is heavy enough, that managing the priority queue may end up being more + // cpu time than giving everybody time (as it is possible everybody would end up getting time anyways) + // if not using a priority queue, it is recommended that you GiveTime only periodically (every 50ms for example) + // the more often you GiveTime, the more critical the priority-queue is at reducing load compared to not using it + // default = false + bool avoidPriorityQueue; + + // how often the client synchronizes its timing-clock to the servers (0=never)(specified in ms). + // the server-side MUST specify this as 0, or else the server will attempt to synchronize it's clock with + // the client as well (which would just be a waste of packets generally, though would work) + // the client should generally always set this feature on by setting it to something like 45000 ms. + // the clock-sync is used to negotiate statistics as well, so if you are not using clock sync, then you + // will not be able to get packetloss/lag statistics for the connection. If you are using it, then you will + // be able to get these statistics from either end of the connection. + // default = 0 + int clockSyncDelay; + + + // these two values control the number of packets that the UdpManager will create in its packet pool. The packet pool + // is a means by which the UdpManager avoid allocating logical packets for every send, by instead using them from the pool. + // you need to specify the size of the packets that are in the pool. Then, when somebody calls UdpManager::CreatePacket, + // if the packet being created is small enough and there is room available, it grabs one from the pool, otherwise it + // creates a new non-pooled one. The largest the pool will ever grow is pooledPacketMax, and the memory used will be roughly + // pooledPacketMax * pooledPacketSize. You should be generous with the pool packet size and the pool max in order + // to avoid having to do allocations as much as possible + // pooledPacketMax default = 1000, (0 = don't use memory pooling at all) + int pooledPacketMax; + // pooledPacketInitial is the number of packets to allocate in the initial pool. The only reason to set this to + // something other than the default, which is 0, is to avoid having your memory fragmented as the pool grows on demand. + int pooledPacketInitial; + // as a general rule, ou should leave this at -1. This is critical. If the pooledPacketSize is smaller than the + // maxRawPacketSize, then all the coalescing that occurs in the reliable channel will result in allocations. You + // would just as well not have a pool if you don't set this at least as large as the maxRawPacketSize. If your application + // tends to send largish packets (larger than maxRawPacketSize), setting this large enough to cover those might buy you + // some speed as well. + // pooledPacketSize defaults to -1, which means use the same as maxRawPacketSize + int pooledPacketSize; + + // the maximum number of entries to allow in WrappedLogicalPacket pool before they start getting destroyed + // depending on the nature of the sending that you are doing, and the volume that you are doing, you may + // want to increase this value. WrappedLogicalPacket objects are used in situations where you send the + // same LogicalPacket object to multiple connections, and that LogicalPacket object is larger than the + // maxRawPacketSize. As you can imagine, this is a relatively rare event, so the pool doesn't need to be + // very large. Like hte standard pool, this is merely an optimization to avoid allocations, it will work + // either way. A server application may wish to to set this number a little bit higher. Unlike the + // packet-pool, we don't bother to even pre-create these things, they are just created for the first time + // when demanded. + // default = 1000 + int wrappedPoolMax; + + // whether ICMP error packets should be used to determine if a connection has gone dead. + // when the destination machine is not available, or there is no process on the destination machine + // talking on the port, then a ICMP error packet will sometimes be returned to the client when a packet is sent. + // Processing ICMP errors will often allow the client machine to quickly determine that the other end of the + // connection is no longer reachable, allowing it to quickly change to a disconnected state. The downside + // of having this feature enabled is that it is possible that if there is a network problem along the route, that + // the connection will be terminated, even though the hardware along the route may correct the problem by re-routing + // within a couple seconds. If you are having problems with clients getting disconnected for ICMP errors (see + // disconnect reason), and you know the server should have remained reachable the entire time, then you might + // want to set this setting to false. The only downside of setting this to false is that it might take the + // client a bit longer to realize when a server goes down. + // default = true + bool processIcmpErrors; + + // whether ICMP errors should be used to terminate connection negotiations. By default, this is set to + // false, since generally when you are trying to establish a new connection (ie. negotiating), you are + // are willing to wait for timeout specified in the EstablishConnection call, since it may be a case + // that the client process gets started slightly sooner than the server process. + // default = false + bool processIcmpErrorsDuringNegotiating; + + + // during connection negotiation, the client sends connect-attempt packets on a periodic basis until the + // server responds acknowledging the connection. This value represents how often the client sends those packets. + // By default this is set to 1000 or 1 second and generally should not be messed with. + int connectAttemptDelay; + + + // This settings allows you to bind the socket used by the library to a specific IP address on the machine. + // Normally, and by default, the library will bind the socket to any address in the machine. This setting should + // not be messed with unless you really know what you are doing. In multi-homed machines it is generally NOT + // necessary to bind to a particular address, even if there are firewall issues involved, and even if you want + // to limit traffic to a particular network (firewalls do a better job of serving that purpose). If you are having + // problems communicating with a server on a multi-homed machine and think this might solve the problem, think again. + // You most likely need to configure the OS to route data appropriately, or make sure that internal network clients + // are connecting to the machine via the machines internal IP address (or vice versa). + // by default this string is empty meaning the socket is bound to any address. To bind to specific IP address, it + // should be entered in a.b.c.d form (DNS names are not allowed). Figuring out what IP addresses are in the machine + // and which one should be bound to is left as an exercise for the user. + char bindIpAddress[32]; + + // you need to specify the characteristics of the various reliable channels here, generally you will want + // to make sure the client and server sides set these characteristics the same, though it is technically not + // required. Each channel decides locally whether it will accept out of order delivery on a particular channel or not. + // (note: out of order delivery is a tiny optimization that simply lets the channel deliver the packet the moment it + // arrives, even if previous packets have not yet arrived). Likewise trickle-rates are for outgoing data only obviously. + // reliable channel managers are not actually created internally until data is actually sent on the channel, so there + // is no overhead associated with channels that are not used, and you need not specify characteristics for channels + // that you know you will not be using. + // default = 400 packets in&out/200k outstanding, ordered, no trickle (all channels) + ReliableConfig reliable[cReliableChannelCount]; + + + // when user supplied encrypt and decrypt routines are specified, it becomes necessary to tell the UdpManager + // how many bytes the encryption process could possibly expand the chunk of data it was given. Often times this + // will simply be 0, but if the user supplied routines attempt compression, then it's possible that expansion could + // could actually occur. Typically I would have the compression routines put a leader-byte on the chunk of data + // specifying whether it was compressed or not. Then if the compression didn't pan out, it could alway abort and just + // prepend the byte in question and the rest of the data. In that sort of algorithm, you would set this value to 1 + // since the most it could ever expand beyond the original is the 1-byte indicator on the front. It's possible that + // a particular encryption routine might want to append a 4-byte encryption key on the end of the chunk of data, in + // which case you would need to reserve 4 bytes. This is necessary as it allows the udp library to guarantee that + // no packet will be larger than maxRawPacketSize, and at the same time ensures that the destination buffers supplied + // to the encrypt/decrypt functions will have enough room. Obviously this value is ignored if the encryptMethod + // is not set to cEncryptMethodUserSupplied. + // default = 0 + int userSuppliedEncryptExpansionBytes; + int userSuppliedEncryptExpansionBytes2; + + // the following parameters are used to simulate various line conditions that may occur. This allows the application + // program to test how well it performs under various conditions + // simulating an incoming byte-rate is simply done by internally not polling the port for a certain period of time + // after receiving a packet based on the amount of data in the last packet received + // simulating outgoing byte-rate is a bit more difficult as it requires queuing the data to be sent and slowly trickling + // it out to the socket. Simulating is very much akin to simulating lag; however, it is not exactly the same thing. It's + // possible to have low-bandwidth, yet good or bad ping times depending on how many hops away you are, but this should good + // enough for most testing. + // default = 0 + int simulateOutgoingLossPercent; // 0=no loss, 100=100% loss + int simulateIncomingLossPercent; // 0=no loss, 100=100% loss + int simulateIncomingByteRate; // simulates the incoming bandwidth specified (in bytes/second) (0=off) + + + // simulates the outgoing bandwidth specified (in bytes/second) (0=off) + // an interesting side-note of the outgoing byte-rate limit. In order to accomplish + // this we to queue stuff on the client and then pace the sending of it. When the client + // terminates the connection, the last thing it does is send a terminate packet to the other side + // The UdpManager is playing the role of the operating system when byte-limiting is on insofar as it + // is the UdpManager that is managing the virutal socket buffer. Most of the time when the client + // terminates, it also terminates the UdpManager. When you are using the outgoing byte rate simulator, + // terminating the UdpManager is akin to rebooting the box. The net effect is that any outgong packets + // in the queue are lost. This includes the ever important 'terminate-packet' that is sent to the server + // at the last moment. This means that when you are simulating an outgoing byte rate, that the server + // will never see the client properly terminate, UNLESS the client gives the UdpManager processing time + // for several seconds (long enough to flush the queue at the specified byte-rate) after the connection + // is destroyed, but before the UdpManager is destroyed. + // note: when using this setting, it is critical that you also set the simulateOutgoingOverloadLevel as + // well in order to simulate the limited-size socket buffer that the OS would have. + int simulateOutgoingByteRate; + + + // this is the number of bytes in the outgoing queue total before packets are simply lost + // this number is used to simulate the condition where you can't simply throw tons of data at a modem + // and expect it to not lose any of it. Normally this data would queue up in the socket buffer for however + // big your outgoing socket buffer is; however, if you are simulating limited outgoing bandwidth, then + // the socket buffer never actually has any data in it, instead all the queuing is taking place in the + // simulation queue. In order to handle this properly, we have to cap the outgoing simulation queue size + // total for all connection. + int simulateOutgoingOverloadLevel; + + + // this is the number of bytes in the outgoing queue/connection before packets are simply lost (0=infinite) + // this number is used to simulate the condition where some router or terminal-server down + // the line has a limited buffer size and once it gets overloaded it just throws away new incoming + // packets. The reason we have to do this is because otherwise our internal simulation queue + // would grow forever. The loss of packets is necessary for the flow control stuff to do its + // job. The incoming side uses the socket-buffer for this purpose and as such can simply + // set the incoming socket buffer to the desired size and the OS will throw away stuff that comes + // in yet doesn't fit. Since this parameter is supposed to simulate a line condition downward + // toward the destination and we may have multiple destinations in our queue, this handles it properly + // by tracking the amount of data queued for each destination ip/port address (it does this by actually + // back-link up to the UdpConnection object). + int simulateDestinationOverloadLevel; + + + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // the following values are ignored by connections initiated by this manager (ie. client side) since the server will tell + // the client the values that are in effect during the connection initialization process. + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + // how many additional bytes of CRC data is added to raw packets to ensure integrity (0 through 4 allowed) + // default = 0 + int crcBytes; + + // which encryption method is to be used (see enumeration) (this occurs at the raw packet level) + // default = cEncryptMethodNone + EncryptMethod encryptMethod[cEncryptPasses]; + }; + + UdpManager(const Params *params); + + void SetHandler(UdpManagerHandler *handler); + UdpManagerHandler *GetHandler() const; + void SetPassThroughData(void *passThroughData); + void *GetPassThroughData() const; + ErrorCondition GetErrorCondition() const; + + // standard AddRef/Release scheme + void AddRef(); + void Release(); + int GetRefCount() const; + + + // this function MUST be called on a regular basis in order to give the manager object time to service the socket + // and give time to various connection objects that may need processing time to send/resend packets, etc. + // + // If you set maxPollingTime to 0, it will not attempt to receive any packets out of the socket. If no + // packets are gotten from the socket, then it is impossible for any packets to be forwarded to the application; + // hence, this is a good way to give the manager processing time for outgoing packets in situations + // where the application does not want to have to worry about processing incoming packets. + // + // If you set maxPollingTime to -1, it will poll at most 1 packet out of the socket at a time and process it. You + // should know that a single packet on the socket can possibly result in multiple data-packets being delivered to the application. + // For example, the incoming packet could be a previous lost reliable packet, which would cause all the stalled reliable packets to be + // delivered. Multi-buffering could also result in multiple route-packet callbacks from one physical packet coming off the socket. + // The purpose of this 1-packet feature is primarily to support the EstablishConnection process. By processing only one packet at a + // time, it gives the application a chance to check the status of a negotiating connection before any data is possibly delivered to + // that connection. This allows the client-app to loop on GetStatus checking for the connection to get established, then create all + // the infrastructure necessary to support that connection once it is established, without fear of packets being delivered before said + // infrastructure is in place. This is safe since connection-confirmation packets do not multi-buffer up. + // + // The only reason you might want to avoid giving the connections time is if you are a fairly non-packet-time critical application + // and are using the avoidPriorityQueue setting (and want to avoid using up too much CPU time while at the same + // time you don't want to avoid processing the socket). + // returns true if any incoming packets were processed during this time slice, otherwise returns false + bool GiveTime(int maxPollingTime = 50, bool giveConnectionsTime = true); + + // used to establish a connection to a server that is listening at the specified address and port. + // the serverAddress will do a DNS lookup as appropriate. This call will block long enough to resolve + // the DNS lookup, but then will return a UdpConnection object that will be in a cStatusNegotiating + // state until the connection is actually established. The application must give the manager + // object time after calling EstablishConnection or else the negotiation process to establish the + // connection will never have time to actually occur. Typically the client establishing the connection + // will call EstablishConnection, then sit in a loop calling UdpManager::GiveTime and checking to see + // if the status of the returned UdpConnection object is changed from cStatusNegotiating. This allows + // the application to look for the ESC key or timeout an attempted connection. + // This function will return NULL if the manager object has exceeded its maximum number of connections + // or if the serverAddress cannot be resolved to an IP address + // as is noted in the declaration, it is the responsibility of the application establishing the connection to delete it + // setting the timeout value (in milliseconds) to something greater than 0 will cause the UdpConnection object to change + // from a cStatusNegotiating state to a cStatusDisconnected state after the timeout has expired. It will also cause + // the connect-complete callback to be called. + UdpConnection *EstablishConnection(const char *serverAddress, int serverPort, int timeout = 0); + + // gets statistical information about this manager, which covers all connections going through this manager + // (useful for getting server-wide statistics) + void GetStats(UdpManagerStatistics *stats) const; + void ResetStats(); + + // this function will dump the packet history out to the specified filename. If no packet history is being kept (see UdmManagerParams::packetHistoryMax) + // then this function does nothing. + void DumpPacketHistory(const char *filename) const; + + // returns the ip address of this machine. If the machine is multi-homed, this value may be blank. + UdpIpAddress GetLocalIp() const; + + // returns the port the manager is actually using. This value will be the same as is specified in Params::port (or if Params::port was set to 0, this will be the dynamically assigned port number) + int GetLocalPort() const; + + // returns how long it has been (in milliseconds) since this manager last received data + int LastReceive() const; + + // returns how long it has been (in milliseconds) since this manager last sent data + int LastSend() const; + + // manually forces all live connections to flush their multi buffers immediately + void FlushAllMultiBuffer(); + + // creates a logical packet and populates it with data. data can be NULL, in which case it gives you logical packet + // of the size specified, but copies no data into it. If you are using pool management (see Params::poolPacketMax), + // it will give you a packet out of the pool if possible, otherwise it will create a packet for you. When logical + // are packets are needed internally for various things (like reliable channel sends that use the (void *, int) interface) + // they are gotten from this function, so your application can likely take advantage of pooling, even if it never bothers + // to explicitly call this function. + LogicalPacket *CreatePacket(const void *data, int dataLen, const void *data2 = NULL, int dataLen2 = 0); + + protected: + friend class PooledLogicalPacket; + void PoolReturn(PooledLogicalPacket *packet); // so pooled packets can add themselves back to the pool + void PoolCreated(PooledLogicalPacket *packet); + void PoolDestroyed(PooledLogicalPacket *packet); + + protected: + friend class WrappedLogicalPacket; + + WrappedLogicalPacket *WrappedBorrow(const LogicalPacket *lp); // used by reliable channels + void WrappedReturn(WrappedLogicalPacket *wp); + + void WrappedCreated(WrappedLogicalPacket *wp); + void WrappedDestroyed(WrappedLogicalPacket *wp); + + protected: + friend class UdpConnection; + friend class UdpReliableChannel; + + class PacketHistoryEntry + { + public: + PacketHistoryEntry(int maxRawPacketSize); + ~PacketHistoryEntry(); + + public: + uchar *mBuffer; + UdpIpAddress mIp; + int mPort; + int mLen; + }; + + Params mParams; + PacketHistoryEntry **mPacketHistory; + int mPacketHistoryPosition; + + void *mPassThroughData; + + UdpConnection *mConnectionList; // linked listed of connections + int mConnectionListCount; + + UdpConnection *mDisconnectPendingList; // linked list of connections to be released when they go to cStatusDisconnected + + ObjectHashTable *mAddressHashTable; + ObjectHashTable *mConnectCodeHashTable; + PriorityQueue *mPriorityQueue; + UdpMisc::ClockStamp mMinimumScheduledStamp; // soonest anybody is allowed to schedule themselves for more processing time + + SOCKET mUdpSocket; + UdpMisc::ClockStamp mLastReceiveTime; + UdpMisc::ClockStamp mLastSendTime; + UdpMisc::ClockStamp mLastEmptySocketBufferStamp; + int mProcessingInducedLag; // how long the currently being processed packet could have possibly been sitting in the socket-buffer waiting for processing (which is effectively how long it has been since we last quit polling packets from the socket queue) + unsigned long int mStartTtl; // initial TTL for packets (used if it is changed by the application for port-alive packets) + ErrorCondition mErrorCondition; + + UdpManagerStatistics mManagerStats; + UdpMisc::ClockStamp mManagerStatsResetTime; + + // stuff for managing the outgoing packet-rate simulation + class SimulateQueueEntry + { + public: + SimulateQueueEntry(const uchar *data, int dataLen, UdpIpAddress ip, int port); + ~SimulateQueueEntry(); + public: + uchar *mData; + int mDataLen; + UdpIpAddress mIp; + int mPort; + SimulateQueueEntry *mNext; + }; + friend class SimulateQueueEntry; + int mSimulateQueueBytes; + SimulateQueueEntry *mSimulateQueueStart; + SimulateQueueEntry *mSimulateQueueEnd; + UdpMisc::ClockStamp mSimulateNextIncomingTime; + UdpMisc::ClockStamp mSimulateNextOutgoingTime; + + // pool management + int mPoolCreated; + int mPoolAvailable; + PooledLogicalPacket *mPoolAvailableRoot; // those available + PooledLogicalPacket *mPoolCreatedRoot; // all those created + + // pool management + int mWrappedCreated; + int mWrappedAvailable; + WrappedLogicalPacket *mWrappedAvailableRoot; // those available + WrappedLogicalPacket *mWrappedCreatedRoot; // those created (available or not) + + protected: // internal functions + int AddressHashValue(UdpIpAddress ip, int port) const; + UdpConnection *AddressGetConnection(UdpIpAddress ip, int port) const; + UdpConnection *ConnectCodeGetConnection(int connectCode) const; + + PacketHistoryEntry *ActualReceive(); + void ActualSend(const uchar *data, int dataLen, UdpIpAddress ip, int port); + void ActualSendHelper(const uchar *data, int dataLen, UdpIpAddress ip, int port); + void SendPortAlive(UdpIpAddress ip, int port); + void ProcessRawPacket(const PacketHistoryEntry *e); + void AddConnection(UdpConnection *con); + void RemoveConnection(UdpConnection *con); + void SetPriority(UdpConnection *con, UdpMisc::ClockStamp stamp); + void ProcessIcmpErrors(); + void KeepUntilDisconnected(UdpConnection *con); + void ProcessDisconnectPending(); + void CloseSocket(); + void CreateAndBindSocket(int usePort); + + private: + ~UdpManager(); // does not destroy UdpConnection objects since it does not own them; however, it has a pointer to all the active + // ones and it will notify them that it no longer exists and set their state to cStatusDisconnected + // typically it is recommended that all UdpConnection objects be destroyed before destroying this manager object + + int mRefCount; +}; + + //////////////////////////////////////////////////////////////////////////////////////////////////////////// + // The purpose of the UdpConnection is to manage a single logical connection + //////////////////////////////////////////////////////////////////////////////////////////////////////////// +class UdpConnection : public PriorityQueueMember, public AddressHashTableMember, public ConnectCodeHashTableMember +{ + public: + enum Status { cStatusNegotiating, cStatusConnected, cStatusDisconnected, cStatusDisconnectPending, cStatusCount }; + + enum DisconnectReason { cDisconnectReasonNone, cDisconnectReasonIcmpError, cDisconnectReasonTimeout + , cDisconnectReasonOtherSideTerminated, cDisconnectReasonManagerDeleted + , cDisconnectReasonConnectFail, cDisconnectReasonApplication + , cDisconnectReasonUnreachableConnection, cDisconnectReasonUnacknowledgedTimeout + , cDisconnectReasonNewConnectionAttempt, cDisconnectReasonConnectionRefused + , cDisconnectReasonMutualConnectError, cDisconnectReasonConnectingToSelf + , cDisconnectReasonReliableOverflow + , cDisconnectReasonCount }; + + // standard AddRef/Release scheme + void AddRef(); + void Release(); + int GetRefCount() const; + + // returns the current status of this connection + Status GetStatus() const; + + // returns the reason that a connection was disconnected. See the enum above for a list of all the reasons + DisconnectReason GetDisconnectReason() const; + DisconnectReason GetOtherSideDisconnectReason() const; + static const char *DisconnectReasonText(DisconnectReason reason); // text-description of disconnect reason to aid in logging + + // sets the handler object for this connection. If a handler object is specified, then the callback functions specified + // in UdpManager::Params are ignored for this connection and the handler is used for the callback instead. + // by default there is no handler. + void SetHandler(UdpConnectionHandler *handler); + UdpConnectionHandler *GetHandler() const; + + // set and get the pass-through data value. Typically the application will set the pass through data + // in the callback function for establishing a connection, then it will use the pass through data + // in the callback function for routing packets. + void SetPassThroughData(void *passThroughData); + void *GetPassThroughData() const; + + // when called this connection is marked as terminated. It is the responsibility of the application + // to explicitly destroy connections that are no longer connected. When this object is disconnected + // it calls the UdpManager and has itself removed from the list of active connections, at which point + // the only person having a pointer to this object is the application itself (which owns it) + // (if the UdpManager is deleted before all UdpConnections are destroyed, the UdpManager loops through + // all of the connections it has calling Disconnect on them such that they know that they no longer + // have a udp manager that they can send data through) + // + // setting a flushTimeout tells the connection to stay alive for that amount of time trying to send any pending + // reliable data before shutting down. Once the application calls Disconnect even with a flushTimeout, the application + // should not attempt to use the connection in any significant way (see docs and release notes for details) + // note: the notifyApplication parameter was removed from this function and the functionality of the library + // was changed such that ANYTIME the connection objects state changes to cStatusDisconnected, the OnTerminated callback + // function gets called. + void Disconnect(int flushTimeout = 0); + + // sends a logical packet on the specified channel, returns FALSE if packet could not be queued for sending (should never happen) + // Internally, a packet that starts with a 0 byte is considered an internal control packet. If a logical packet starts with a 0 + // byte, then there will an extra control byte of overhead in order to facilitate it. It is recommended that if packet size + // is critical, that you don't start the packet with a 0 byte. Typically an application will have a packet-type byte on the front + // of application packets; the application packet types should simply start at 1. + bool Send(UdpChannel channel, const void *data, int dataLen); + + // same as the regular Send only it takes a LogicalPacket instead. There are two huge advantages to having it take a + // LogicalPacket. First, we can send the same LogicalPacket to multiple locations and each connection will not necessarily + // have to make its own copy of the data at the time it is put into the send queue (instead each connection just increments + // the buffer ref-count). Second, it allows the application to pre-generate very large packets (like file update packets potentially) + // and hold onto them for the entire length of the application, then, whenever any player needs that chunk of data, it can send them + // the already formatted LogicalPacket. + bool Send(UdpChannel channel, const LogicalPacket *packet); + + // manually forces all channels to send-off any data they have queued up waiting for processing time to send + // this mainly applies to reliable channels. When you send a reliable packet, it actually only adds it to the reliable + // queue until the connection is given processing time by the manager object. This call forces it to attempt to + // send that queued data immediately (subject to normal flow control restrictions). This also flushes the multi-buffer + // for the channel. If you send reliable data and want to ensure that it goes out immediately after the send, this is the + // best call to make. + void FlushChannels(); + + // manually forces buffered data to be sent immediately + void FlushMultiBuffer(); + + // returns the number of bytes sent/received in the last second to this connection (accurate to within cBinResolution(25) milliseconds) + // these functions are not const as they expire the older bin data internally in order to calculate the number + int OutgoingBytesLastSecond(); + int IncomingBytesLastSecond(); + + // returns the total number of bytes outstanding in all reliable channels. When this is zero, you know for sure + // that all sent reliable data has arrived at destination and is confirmed. + int TotalPendingBytes() const; + + // returns how long has elapsed since this connection received data (in milliseconds) + int LastReceive() const; + + // returns how long has elapsed since this connection received data (in milliseconds), using useStamp as the current time (optimization) + int LastReceive(UdpMisc::ClockStamp useStamp) const; + + // returns how long has elapsed since this connection sent data (in milliseconds) + int LastSend() const; + + // returns how long this connection has been in existence (in milliseconds) + int ConnectionAge() const; + + // returns the UdpManager object that is managing this connection + // will return NULL if the connection has been disconnected for some reason (because disconnecting severes the link to UdpManager) + UdpManager *GetUdpManager() const; + + // returns the 32-bit encryption-code that was negotiated as part of the connection-establishment process. + // this is a randomly generated number that both the client and the server have in common. It is exposed + // via this interface primarily to allow user-supplied encrypt routines access to it. + // this code is generated by the server side in response to a connect request. + int GetEncryptCode() const; + + // this returns the connection-code. This is very similar to the encrypt-code in that it is randomly + // generated and both ends of the connection will report the same value. The difference is that this + // code's purpose is part of the internal protocol to ensure that old connections don't try to process + // new connection request packets. Unlike the encrypt-code, this value is generated by the client + // and is part of the connect-request packet. Nevertheless, since this number will be the same random + // number on both ends of the connection, it too can be used as a potential encryption key for the user + // supplied encrypt routines. It's not quite as secure as the encrypt code since this value in theory + // could be hacked to be something predictable on the client side. + int GetConnectCode() const; + + // returns a sync-stamp that can be compared to other ServerSyncStamp's generated on other machines + // in order to calculate the one-way travel time for a packet. It can only accurate calculate + // packet travel times under 32 seconds, would should be completely safe. You must use the + // UdpManager::SyncStampDeltaTime function in order to calculate the elapsed time between + // the two stamps. + ushort ServerSyncStampShort() const; + uint ServerSyncStampLong() const; + int ServerSyncStampShortElapsed(ushort syncStamp) const; + int ServerSyncStampLongElapsed(uint syncStamp) const; + + // returns the IP address/port this connection is linked to + UdpIpAddress GetDestinationIp() const; + int GetDestinationPort() const; + + // statistical functions + void GetStats(UdpConnectionStatistics *cs) const; + void PingStatReset(); // resets the ping-stat information, causing it to resync the clock etc (if in clock-sync mode). Generally this is not done, it was added for backward compatibility + + + // functions for manipulating the automatic no-data-disconnect stuff on a per-connection basis + void SetNoDataTimeout(int noDataTimeout); // 0=never timeout, otherwise in milliseconds (overrides UdpManager::Params::noDataTimeout setting, which is the default) + int GetNoDataTimeout() const; + + // functions for manipulating the keep-alive packet sending on a per-connection basis + void SetKeepAliveDelay(int keepAliveDelay); + int GetKeepAliveDelay() const; + + // configures whether this connection is in silent-disconnect mode or not. By default, the connection is not in silent + // disconnect mode, which means that when this connection is terminated, it will send a final terminate-packet to the + // other side telling them that we are disconnected, allowing them to quickly realize that the connection is now dead. + // In some circumstances, it may be desireable to not do this, and this can be accomplished by calling this function + // passing in 'true' to put it in silent mode. This may be desireable in cases where you are disconnecting a cheater + // and don't want them to have immediate notification that they did something bad. Or, if you are attempting to test + // timeout functionality on the other end and want to simulate a truly dead connection. Normally, you will not want + // to mess with this. It was added to the API to support some internal functionality, see its use in the source-code + // or release-notes for details. + void SetSilentDisconnect(bool silent); + + + // returns the current queue-status of the reliable channel specified. Unreliable channels will always report zero. + struct ChannelStatus + { + int totalPendingBytes; // total bytes of data in channel that have yet to be acknowledged (includes queuedBytes plus physical-packet bytes that have yet to be acknowledged) + int queuedPackets; // number of logical packets in the queue + int queuedBytes; // number of bytes in the logical queue (the logical queue does NOT include pending physical packets) + int incomingLargeTotal; // total number of bytes in the currently incoming logical packet (only meaningful obviously if a fragmented file is in tranist) + int incomingLargeSoFar; // number of bytes received so far in the currently incoming logical packet + int oldestUnacknowledgedAge; // age of the oldest unacknowledged (but sent) packet (in milliseconds) + int duplicatePacketsReceived; // number of times we received a packet that we had already received + int resentPacketsAccelerated; // number of times we have resent a packet due to receiving a later packet in the series + int resentPacketsTimedOut; // number of times we have resent a packet due to the ack-timeout expiring + int congestionSlowStartThreshhold; // current threshhold for slow-start algorithm + int congestionWindowSize; // current sliding window size + int ackAveragePing; // average time for a packet to be acknowledged (used in calculating optimal resend timeouts) + }; + void GetChannelStatus(UdpChannel channel, ChannelStatus *channelStatus) const; + + protected: + friend class UdpManager; + friend class UdpReliableChannel; + + // note: if connectPacket is NULL, that means this connection object is being created to establish + // a new connection to the specified ip/port (ie. the connection starts out in cStatusNegotiating mode) + // if connectPacket is non-NULL, that menas this connection object is being created to handle an + // incoming connect request and it will start out in cStatusConnected mode. + UdpConnection(UdpManager *udpManager, UdpIpAddress destIp, int destPort, int timeout); // starts connection-establishment protocol + UdpConnection(UdpManager *udpManager, const UdpManager::PacketHistoryEntry *e); // starts already connected, replying to connection request + + // gives this connection processing time (only given processing time by the manager object and then + // only when the connection has scheduled itself to receive processing time) + void GiveTime(); + void ProcessRawPacket(const UdpManager::PacketHistoryEntry *e); + void PortUnreachable(); + void FlagPortUnreachable(); + protected: + UdpConnection *mNextConnection; // used by UdpManager to maintain list of connections + UdpConnection *mPrevConnection; // used by UdpManager to maintain list of connections + UdpConnection *mDisconnectPendingNextConnection; // used by UdpManager to maintain list of connections pending disconnect + UdpIpAddress mIp; + int mPort; + int mSimulateQueueBytes; // used by UdpManager to track how many bytes are in it's simulation queue headed to each destination + + private: + friend class GroupLogicalPacket; + + ~UdpConnection(); + void Init(UdpManager *udpManager, UdpIpAddress destIp, int destPort); + + // note: BufferedSend is capable of optionally taking two chunks of data at once, which are then concatenated together as if they were one chunk of data + // into the multi-buffer. Providing this facility prevents the UdpReliableChannel object from having to make a copy of all the data it sends + // in order to stick a realiable header on it. + // we don't bother extending this down to the PhysicalSend (in case the BufferedSend does a pass through due to size) because the encryption code + // is incapable of sourcing from two different chunks and outputting to one chunk. It's not possible to change that either, since the encyption + // takes place 32 bits at a time and you could end up straddling boundaries between chunks. + void RawSend(const uchar *data, int dataLen); // nothing happens to the data here, it is given to the udpmanager and sent out the port + void PhysicalSend(const uchar *data, int dataLen, bool appendAllowed); // sends a physical packet (encrypts and adds crc bytes) + uchar *BufferedSend(const uchar *data, int dataLen, const uchar *data2, int dataLen2, bool appendAllowed); // buffers logical packets waiting til we have more data (makes multi-packets) + bool InternalSend(UdpChannel channel, const LogicalPacket *packet); + bool InternalSend(UdpChannel channel, const uchar *data, int dataLen, const uchar *data2 = NULL, int dataLen2 = 0); + + uchar *InternalAckSend(uchar *bufferedAckPtr, const uchar *ackPtr, int ackLen); // used by reliable channel to send acks (special send that allows for deduping ack feature) + + void InternalGiveTime(); + void InternalDisconnect(int flushTimeout, DisconnectReason reason); + void ProcessCookedPacket(const uchar *data, int dataLen); + void DecryptIt(const uchar *data, int dataLen); + void ScheduleTimeNow(); + int ExpireSendBin(); + int ExpireReceiveBin(); + void SendTerminatePacket(int connectCode, DisconnectReason reason); + void CallbackRoutePacket(const uchar *data, int dataLen); + void CallbackCorruptPacket(const uchar *data, int dataLen, UdpCorruptionReason reason); + bool IsNonEncryptPacket(const uchar *data) const; + + // these encrypt-method functions return the length of the encrypted/decrypted data + // new methods of encryption/compression can be easily added by simply creating the + // functions for them and changing the SetupEncryptModel function as appropriate + // since raw packets are encrypted in the first place and have a limited size + // the decrypted data will never be larger than a maxRawPacketSize. Both of encrypt + // and decrypt are guaranteed to have enough room in dest buffers to hold the results. + // Encryption function is allowed to expand the data at most the number of bytes + // it reserves for this purpose in the SetupEncryptModel function. + int EncryptNone(uchar *destData, const uchar *sourceData, int sourceLen); + int DecryptNone(uchar *destData, const uchar *sourceData, int sourceLen); + int EncryptXor(uchar *destData, const uchar *sourceData, int sourceLen); + int DecryptXor(uchar *destData, const uchar *sourceData, int sourceLen); + int EncryptXorBuffer(uchar *destData, const uchar *sourceData, int sourceLen); + int DecryptXorBuffer(uchar *destData, const uchar *sourceData, int sourceLen); + int EncryptUserSupplied(uchar *destData, const uchar *sourceData, int sourceLen); + int DecryptUserSupplied(uchar *destData, const uchar *sourceData, int sourceLen); + int EncryptUserSupplied2(uchar *destData, const uchar *sourceData, int sourceLen); + int DecryptUserSupplied2(uchar *destData, const uchar *sourceData, int sourceLen); + void SetupEncryptModel(); + + int mRefCount; + Status mStatus; + void *mPassThroughData; + UdpManager *mUdpManager; + int mConnectCode; + UdpConnectionStatistics mConnectionStats; + UdpMisc::ClockStamp mConnectionCreateTime; + int mConnectAttemptTimeout; + int mNoDataTimeout; + DisconnectReason mDisconnectReason; + DisconnectReason mOtherSideDisconnectReason; + bool mFlaggedPortUnreachable; + bool mSilentDisconnect; + + UdpReliableChannel *mChannel[UdpManager::cReliableChannelCount]; + + struct Configuration + { + int encryptCode; + int crcBytes; + UdpManager::EncryptMethod encryptMethod[UdpManager::cEncryptPasses]; + int maxRawPacketSize; // negotiated maxRawPacketSize (ie. smaller of what two sides are set to) + }; + + Configuration mConnectionConfig; + + UdpMisc::ClockStamp mLastClockSyncTime; + UdpMisc::ClockStamp mDataHoldTime; + UdpMisc::ClockStamp mLastSendTime; + UdpMisc::ClockStamp mLastReceiveTime; + UdpMisc::ClockStamp mLastPortAliveTime; + uchar* mMultiBufferData; + uchar* mMultiBufferPtr; + + int mOrderedCountOutgoing; + int mOrderedCountOutgoing2; + ushort mOrderedStampLast; + ushort mOrderedStampLast2; + + typedef int (UdpConnection::* IEncryptFunction)(uchar *destData, const uchar *sourceData, int sourceLen); + typedef int (UdpConnection::* IDecryptFunction)(uchar *destData, const uchar *sourceData, int sourceLen); + IEncryptFunction mEncryptFunction[UdpManager::cEncryptPasses]; + IDecryptFunction mDecryptFunction[UdpManager::cEncryptPasses]; + + uchar *mEncryptXorBuffer; + int mEncryptExpansionBytes; + + uint mSyncTimeDelta; + int mSyncStatTotal; + int mSyncStatCount; + int mSyncStatLow; + int mSyncStatHigh; + int mSyncStatLast; + int mSyncStatMasterRoundTime; + UdpMisc::ClockStamp mSyncStatMasterFixupTime; + + bool mGettingTime; + UdpConnectionHandler *mHandler; + + int mKeepAliveDelay; + + UdpMisc::ClockStamp mIcmpErrorRetryStartStamp; + UdpMisc::ClockStamp mPortRemapRequestStartStamp; + + UdpMisc::ClockStamp mDisconnectFlushStamp; + int mDisconnectFlushTimeout; + + + // data rate management functions + enum { cBinResolution = 25, cBinCount = 1000 / cBinResolution }; + int mLastSendBin; + int mLastReceiveBin; + int mOutgoingBytesLastSecond; + int mIncomingBytesLastSecond; + int mSendBin[cBinCount]; + int mReceiveBin[cBinCount]; + + + + // note: cUdpPacketReliable, cUdpPacketFragment both indicate a reliable-packet header. They are marked + // differently such that we can support large packets without any additional header overhead, a fragment marked packet means + // that the packet is part of a larger packet being assembled. The first fragment has an additional 4 bytes on the header specifying + // the length to follow. The order of those entries is important + enum UdpPacketType { cUdpPacketZeroEscape, cUdpPacketConnect, cUdpPacketConfirm, cUdpPacketMulti, cUdpPacketBig + , cUdpPacketTerminate, cUdpPacketKeepAlive + , cUdpPacketClockSync, cUdpPacketClockReflect + , cUdpPacketReliable1, cUdpPacketReliable2, cUdpPacketReliable3, cUdpPacketReliable4 + , cUdpPacketFragment1, cUdpPacketFragment2, cUdpPacketFragment3, cUdpPacketFragment4 + , cUdpPacketAck1, cUdpPacketAck2, cUdpPacketAck3, cUdpPacketAck4 + , cUdpPacketAckAll1, cUdpPacketAckAll2, cUdpPacketAckAll3, cUdpPacketAckAll4 + , cUdpPacketGroup, cUdpPacketOrdered, cUdpPacketOrdered2, cUdpPacketPortAlive + , cUdpPacketUnreachableConnection, cUdpPacketRequestRemap }; + + ////////////////////////////////////////////////////////////////////////////////////////////////////// + // The following structs represent what the internal packets look like. In practice, most of these + // structs are never used and exist only for documentation clarity. Internally packets are + // manually assembled such that struct packing and byte-ordering issues won't be an issue. + ////////////////////////////////////////////////////////////////////////////////////////////////////// + struct UdpPacketConnect + { + uchar zeroByte; + uchar packetType; + int protocolVersion; + int connectCode; + int maxRawPacketSize; + }; + + struct UdpPacketConfirm + { + uchar zeroByte; + uchar packetType; + int connectCode; + Configuration config; + int maxRawPacketSize; + }; + + struct UdpPacketTerminate + { + uchar zeroByte; + uchar packetType; + int connectCode; + }; + + struct UdpPacketKeepAlive + { + uchar zeroByte; + uchar packetType; + }; + + struct UdpPacketGroup + { + // this format is prepped by the GroupLogicalPacket object, which reports itself to the UdpConnection object as an internal packet + // type such that it doesn't get treated as an application-packet even though the application is the one sending it + uchar zeroByte; + uchar packetType; + // variableValue/data, repeated... + }; + + struct UdpPacketClockSync + { + uchar zeroByte; + uchar packetType; + ushort timeStamp; + int masterPingTime; + int averagePingTime; + int lowPingTime; + int highPingTime; + int lastPingTime; + udp_int64 ourSent; + udp_int64 ourReceived; + }; + + struct UdpPacketClockReflect + { + uchar zeroByte; + uchar packetType; + ushort timeStamp; + uint serverSyncStampLong; + udp_int64 yourSent; + udp_int64 yourReceived; + udp_int64 ourSent; + udp_int64 ourReceived; + }; + + struct UdpPacketReliable + { + uchar zeroByte; + uchar packetType; + ushort reliableStamp; + }; + + struct UdpPacketReliableFragmentStart + { + UdpPacketReliable reliable; + int length; + }; + + struct UdpPacketAck + { + uchar zeroByte; + uchar packetType; + ushort reliableStamp; + }; + + struct UdpPacketOrdered + { + uchar zeroByte; + uchar packetType; + ushort orderStamp; + }; + + + enum { cUdpPacketReliableSize = 4 }; + enum { cUdpPacketOrderedSize = 4 }; + enum { cUdpPacketConnectSize = 14 }; +}; + + ////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // The purpose of this class is to manage the reliable transmission of packets on top of the inherently + // unreliable UDP layer. This is an internal object and should not be manually created or talked to by the user. + ////////////////////////////////////////////////////////////////////////////////////////////////////////////// +class UdpReliableChannel +{ + protected: + friend class UdpConnection; + + UdpReliableChannel(int channelNumber, UdpConnection *connection, UdpManager::ReliableConfig *config); + ~UdpReliableChannel(); + void GetChannelStatus(UdpConnection::ChannelStatus *channelStatus) const; + int GetAveragePing() const; + int TotalPendingBytes() const; // returns total bytes outstanding + + void ReliablePacket(const uchar *data, int dataLen); + void Send(const LogicalPacket *packet); + void Send(const uchar *data, int dataLen, const uchar *data2, int dataLen2); + void AckPacket(const uchar *data, int dataLen); + void AckAllPacket(const uchar *data, int dataLen); + void ClearBufferedAck(); + int GiveTime(); + + protected: + enum ReliablePacketMode { cReliablePacketModeReliable, cReliablePacketModeFragment, cReliablePacketModeDelivered }; + + class PhysicalPacket + { + public: + PhysicalPacket(); + ~PhysicalPacket(); + + public: + UdpMisc::ClockStamp mFirstTimeStamp; + UdpMisc::ClockStamp mLastTimeStamp; + const LogicalPacket *mParent; // physical packets hold an addref on the logical packet. Once all of the logical packet data has been divied out to physical packets, the logical queue releases it + const uchar *mDataPtr; // within parent's data (it's possible it is not pointing to the beginning in the case of large packets) + int mDataLen; + }; + + class IncomingQueueEntry + { + public: + IncomingQueueEntry(); + ~IncomingQueueEntry(); + + public: + LogicalPacket *mPacket; + ReliablePacketMode mMode; + }; + friend class IncomingQueueEntry; + + udp_int64 GetReliableOutgoingId(int reliableStamp) const; + udp_int64 GetReliableIncomingId(int reliableStamp) const; + void Ack(udp_int64 reliableId); + void ProcessPacket(ReliablePacketMode mode, const uchar *data, int dataLen); + bool PullDown(int windowSpaceLeft); + void FlushCoalesce(); + void SendCoalesce(const uchar *data, int dataLen, const uchar *data2 = NULL, int dataLen2 = 0); + void QueueLogicalPacket(const LogicalPacket *packet); + + UdpManager::ReliableConfig mConfig; + UdpConnection *mUdpConnection; + UdpMisc::ClockStamp mLastTimeStampAcknowledged; + UdpMisc::ClockStamp mTrickleLastSend; + UdpMisc::ClockStamp mNextNeedTime; + int mChannelNumber; + udp_int64 mReliableOutgoingId; + udp_int64 mReliableOutgoingPendingId; + int mReliableOutgoingBytes; + int mLogicalBytesQueued; + int mLogicalPacketsQueued; + uchar *mBigDataPtr; + int mBigDataLen; + int mBigDataTargetLen; + int mAveragePingTime; + int mMaxDataBytes; + int mFragmentNextPos; + PhysicalPacket *mPhysicalPackets; + const LogicalPacket *mLogicalRoot; + const LogicalPacket *mLogicalEnd; + + int mCongestionWindowStart; + int mCongestionWindowSize; + int mCongestionWindowLargest; + int mCongestionSlowStartThreshhold; + int mCongestionWindowMinimum; + bool mMaxxedOutCurrentWindow; + + udp_int64 mReliableIncomingId; + IncomingQueueEntry *mReliableIncoming; + + LogicalPacket *mCoalescePacket; + uchar *mCoalesceStartPtr; + uchar *mCoalesceEndPtr; + int mCoalesceCount; + int mMaxCoalesceAttemptBytes; + + uchar *mBufferedAckPtr; + + int mStatDuplicatePacketsReceived; + int mStatResentPacketsAccelerated; + int mStatResentPacketsTimedOut; +}; + + + ///////////////////////////////////////////////////////////////////////// + // FixedLogicalPacket implementation + ///////////////////////////////////////////////////////////////////////// +template FixedLogicalPacket::FixedLogicalPacket(const void *data, int dataLen) +{ + mDataLen = dataLen; + if (data != NULL) + memcpy(mData, data, mDataLen); +} + +template void *FixedLogicalPacket::GetDataPtr() +{ + return(mData); +} + +template const void *FixedLogicalPacket::GetDataPtr() const +{ + return(mData); +} + +template int FixedLogicalPacket::GetDataLen() const +{ + return(mDataLen); +} + +template void FixedLogicalPacket::SetDataLen(int len) +{ + mDataLen = len; +} + + + ///////////////////////////////////////////////////////////////////////// + // StructLogicalPacket implementation + ///////////////////////////////////////////////////////////////////////// +template StructLogicalPacket::StructLogicalPacket(T *initData) +{ + if (initData != NULL) + mStruct = *initData; +} + +template void *StructLogicalPacket::GetDataPtr() +{ + return(&mStruct); +} + +template const void *StructLogicalPacket::GetDataPtr() const +{ + return(&mStruct); +} + +template int StructLogicalPacket::GetDataLen() const +{ + return(sizeof(mStruct)); +} + +template void StructLogicalPacket::SetDataLen(int len) +{ +} + + + ///////////////////////////////////////////////////////////////////////// + // inline implementations + ///////////////////////////////////////////////////////////////////////// + + // UdpMisc +inline int UdpMisc::ClockElapsed(ClockStamp stamp) +{ + ClockStamp t = (UdpMisc::Clock() - stamp); + if (t > 2000000000) // only time differences up to 23 days can be measured with this function + return(2000000000); + return((int)t); +} + +inline int UdpMisc::ClockDiff(ClockStamp start, ClockStamp stop) +{ + ClockStamp t = (stop - start); + if (t > 2000000000) // only time differences up to 23 days can be measured with this function + return(2000000000); + return((int)t); +} + +inline ushort UdpMisc::LocalSyncStampShort() +{ + return((ushort)(Clock() & 0xffff)); +} + +inline uint UdpMisc::LocalSyncStampLong() +{ + return((uint)(Clock() & 0xffffffff)); +} + + +inline int UdpMisc::PutValue64(void *buffer, udp_int64 value) +{ + uchar *bufptr = (uchar *)buffer; + *bufptr++ = (uchar)(value >> 56); + *bufptr++ = (uchar)((value >> 48) & 0xff); + *bufptr++ = (uchar)((value >> 40) & 0xff); + *bufptr++ = (uchar)((value >> 32) & 0xff); + *bufptr++ = (uchar)((value >> 24) & 0xff); + *bufptr++ = (uchar)((value >> 16) & 0xff); + *bufptr++ = (uchar)((value >> 8) & 0xff); + *bufptr = (uchar)(value & 0xff); + return(8); +} + +inline udp_int64 UdpMisc::GetValue64(const void *buffer) +{ + const uchar *bufptr = (const uchar *)buffer; + return(((udp_int64)*bufptr << 56) | ((udp_int64)*(bufptr + 1) << 48) | ((udp_int64)*(bufptr + 2) << 40) | ((udp_int64)*(bufptr + 3) << 32) | ((udp_int64)*(bufptr + 4) << 24) | ((udp_int64)*(bufptr + 5) << 16) | ((udp_int64)*(bufptr + 6) << 8) | (udp_int64)*(bufptr + 7)); +} + +inline int UdpMisc::PutValue32(void *buffer, uint value) +{ + uchar *bufptr = (uchar *)buffer; + *bufptr++ = (uchar)(value >> 24); + *bufptr++ = (uchar)((value >> 16) & 0xff); + *bufptr++ = (uchar)((value >> 8) & 0xff); + *bufptr = (uchar)(value & 0xff); + return(4); +} + +inline uint UdpMisc::GetValue32(const void *buffer) +{ + const uchar *bufptr = (const uchar *)buffer; + return((*bufptr << 24) | (*(bufptr + 1) << 16) | (*(bufptr + 2) << 8) | *(bufptr + 3)); +} + +inline int UdpMisc::PutValue24(void *buffer, uint value) +{ + uchar *bufptr = (uchar *)buffer; + *bufptr++ = (uchar)((value >> 16) & 0xff); + *bufptr++ = (uchar)((value >> 8) & 0xff); + *bufptr = (uchar)(value & 0xff); + return(3); +} + +inline uint UdpMisc::GetValue24(const void *buffer) +{ + const uchar *bufptr = (const uchar *)buffer; + return((*bufptr << 16) | (*(bufptr + 1) << 8) | *(bufptr + 2)); +} + +inline int UdpMisc::PutValue16(void *buffer, ushort value) +{ + uchar *bufptr = (uchar *)buffer; + *bufptr++ = (uchar)((value >> 8) & 0xff); + *bufptr = (uchar)(value & 0xff); + return(2); +} + +inline ushort UdpMisc::GetValue16(const void *buffer) +{ + const uchar *bufptr = (const uchar *)buffer; + return((ushort)((*bufptr << 8) | *(bufptr + 1))); +} + + // UdpManager +inline int UdpManager::LastReceive() const +{ + return(UdpMisc::ClockElapsed(mLastReceiveTime)); +} + +inline int UdpManager::LastSend() const +{ + return(UdpMisc::ClockElapsed(mLastSendTime)); +} + +inline int UdpManager::AddressHashValue(UdpIpAddress ip, int port) const +{ + return((int)((ip.GetAddress() ^ port) & 0x7fffffff)); +} + +inline void UdpManager::SetPriority(UdpConnection *con, UdpMisc::ClockStamp stamp) +{ + // do not ever let anybody schedule themselves for processing time sooner then mMinimumScheduledStamp + // otherwise, they could end up getting processing time multiple times in a single UdpManager::GiveTime iteration + // of the priority queue, which under odd circumstances could result in an infinite loop + if (stamp < mMinimumScheduledStamp) + stamp = mMinimumScheduledStamp; + + if (mPriorityQueue != NULL) + mPriorityQueue->Add(con, stamp); +} + +inline void UdpManager::SetHandler(UdpManagerHandler *handler) +{ + mParams.handler = handler; +} + +inline UdpManagerHandler *UdpManager::GetHandler() const +{ + return(mParams.handler); +} + +inline void UdpManager::PoolReturn(PooledLogicalPacket *packet) +{ + if (mPoolAvailable < mParams.pooledPacketMax) + { + packet->AddRef(); + mPoolAvailable++; + packet->mAvailableNext = mPoolAvailableRoot; + mPoolAvailableRoot = packet; + } +} + +inline void UdpManager::WrappedReturn(WrappedLogicalPacket *wp) +{ + wp->SetLogicalPacket(NULL); + + if (mWrappedAvailable < mParams.wrappedPoolMax) + { + wp->AddRef(); + mWrappedAvailable++; + wp->mAvailableNext = mWrappedAvailableRoot; + mWrappedAvailableRoot = wp; + } +} + +inline void UdpManager::AddRef() +{ + mRefCount++; +} + +inline void UdpManager::Release() +{ + mRefCount--; + if (mRefCount == 0) + delete this; +} + +inline int UdpManager::GetRefCount() const +{ + return(mRefCount); +} + +inline void UdpManager::SetPassThroughData(void *passThroughData) +{ + mPassThroughData = passThroughData; +} + +inline void *UdpManager::GetPassThroughData() const +{ + return(mPassThroughData); +} + + + + // UdpConnection +inline void UdpConnection::ScheduleTimeNow() +{ + // if we are current in our GiveTime function getting time, then there is no need to reprioritize to 0 when we send a raw packet, since + // the last thing we do in out GiveTime is do a scheduling calculation based on the last time a packet was sent. This little check + // prevents us from reprioritizing to 0, only to shortly thereafter be reprioritized to where we actually belong. + if (!mGettingTime) + { + if (mUdpManager != NULL) + mUdpManager->SetPriority(this, 0); + } +} + +inline void UdpConnection::SetHandler(UdpConnectionHandler *handler) +{ + mHandler = handler; +} + +inline UdpConnectionHandler *UdpConnection::GetHandler() const +{ + return(mHandler); +} + +inline void UdpConnection::AddRef() +{ + mRefCount++; +} + +inline void UdpConnection::Release() +{ + mRefCount--; + if (mRefCount == 0) + delete this; +} + +inline bool UdpConnection::IsNonEncryptPacket(const uchar *data) const +{ + if (data[0] == 0) + { + if (data[1] == cUdpPacketConnect || data[1] == cUdpPacketConfirm || data[1] == cUdpPacketUnreachableConnection || data[1] == cUdpPacketRequestRemap) + return(true); + } + return(false); +} + + +inline int UdpConnection::GetRefCount() const +{ + return(mRefCount); +} + +inline int UdpConnection::GetEncryptCode() const +{ + return(mConnectionConfig.encryptCode); +} + +inline int UdpConnection::GetConnectCode() const +{ + return(mConnectCode); +} + +inline int UdpConnection::LastReceive(UdpMisc::ClockStamp useStamp) const +{ + return(UdpMisc::ClockDiff(mLastReceiveTime, useStamp)); +} + +inline int UdpConnection::LastReceive() const +{ + return(UdpMisc::ClockElapsed(mLastReceiveTime)); +} + +inline int UdpConnection::ConnectionAge() const +{ + return(UdpMisc::ClockElapsed(mConnectionCreateTime)); +} + +inline int UdpConnection::LastSend() const +{ + return(UdpMisc::ClockElapsed(mLastSendTime)); +} + +inline ushort UdpConnection::ServerSyncStampShort() const +{ + return((ushort)(UdpMisc::LocalSyncStampShort() + (mSyncTimeDelta & 0xffff))); +} + +inline uint UdpConnection::ServerSyncStampLong() const +{ + return(UdpMisc::LocalSyncStampLong() + mSyncTimeDelta); +} + +inline int UdpConnection::ServerSyncStampShortElapsed(ushort syncStamp) const +{ + return(UdpMisc::SyncStampShortDeltaTime(syncStamp, ServerSyncStampShort())); +} + +inline int UdpConnection::ServerSyncStampLongElapsed(uint syncStamp) const +{ + return(UdpMisc::SyncStampLongDeltaTime(syncStamp, ServerSyncStampLong())); +} + +inline UdpManager *UdpConnection::GetUdpManager() const +{ + return(mUdpManager); +} + +inline UdpConnection::Status UdpConnection::GetStatus() const +{ + return(mStatus); +} + +inline UdpConnection::DisconnectReason UdpConnection::GetDisconnectReason() const +{ + return(mDisconnectReason); +} + +inline UdpConnection::DisconnectReason UdpConnection::GetOtherSideDisconnectReason() const +{ + return(mOtherSideDisconnectReason); +} + +inline int UdpConnection::OutgoingBytesLastSecond() +{ + ExpireSendBin(); + return(mOutgoingBytesLastSecond); +} + +inline int UdpConnection::IncomingBytesLastSecond() +{ + ExpireReceiveBin(); + return(mIncomingBytesLastSecond); +} + +inline void UdpConnection::SetPassThroughData(void *passThroughData) +{ + mPassThroughData = passThroughData; +} + +inline void *UdpConnection::GetPassThroughData() const +{ + return(mPassThroughData); +} + +inline UdpIpAddress UdpConnection::GetDestinationIp() const +{ + return(mIp); +} + +inline int UdpConnection::GetDestinationPort() const +{ + return(mPort); +} + +inline void UdpConnection::SetNoDataTimeout(int noDataTimeout) +{ + mNoDataTimeout = noDataTimeout; +} + +inline int UdpConnection::GetNoDataTimeout() const +{ + return(mNoDataTimeout); +} + +inline void UdpConnection::Disconnect(int flushTimeout) +{ + InternalDisconnect(flushTimeout, cDisconnectReasonApplication); +} + +inline void UdpConnection::SetKeepAliveDelay(int keepAliveDelay) +{ + mKeepAliveDelay = keepAliveDelay; +} + +inline int UdpConnection::GetKeepAliveDelay() const +{ + return(mKeepAliveDelay); +} + + + // UdpReliableChannel +inline void UdpReliableChannel::AckPacket(const uchar *data, int /*dataLen*/) +{ + Ack(GetReliableOutgoingId((ushort)UdpMisc::GetValue16(data + 2))); +} + +inline int UdpReliableChannel::GetAveragePing() const +{ + return(mAveragePingTime); +} + +inline int UdpReliableChannel::TotalPendingBytes() const +{ + return(mLogicalBytesQueued + mReliableOutgoingBytes); +} + +inline void UdpReliableChannel::ClearBufferedAck() +{ + mBufferedAckPtr = NULL; +} + +inline udp_int64 UdpReliableChannel::GetReliableOutgoingId(int reliableStamp) const +{ + // since we can never have anywhere close to 65000 packets outstanding, we only need to + // to send the low order word of the reliableId in the UdpPacketReliable and UdpPacketAck + // packets, because we can reconstruct the full id from that, we just need to take + // into account the wrap around issue. We calculate it based of the high-word of the + // next packet we are going to send. If it ends up being larger then we know + // we wrapped and can fix it up by simply subtracting 1 from the high-order word. + udp_int64 reliableId = reliableStamp | (mReliableOutgoingId & (~(udp_int64)0xffff)); + if (reliableId > mReliableOutgoingId) + reliableId -= 0x10000; + return(reliableId); +} + +inline udp_int64 UdpReliableChannel::GetReliableIncomingId(int reliableStamp) const +{ + // since we can never have anywhere close to 65000 packets outstanding, we only need to + // to send the low order word of the reliableId in the UdpPacketReliable and UdpPacketAck + // packets, because we can reconstruct the full id from that, we just need to take + // into account the wrap around issue. We basically prepend the last-known + // high-order word. If we end up significantly below the head of our chain, then we + // know we need to pick the entry 0x10000 higher. If we fall significantly above + // our previous high-end, then we know we need to go the other way. + udp_int64 reliableId = reliableStamp | (mReliableIncomingId & (~(udp_int64)0xffff)); + if (reliableId < mReliableIncomingId - UdpManager::cHardMaxOutstandingPackets) + reliableId += 0x10000; + if (reliableId > mReliableIncomingId + UdpManager::cHardMaxOutstandingPackets) + reliableId -= 0x10000; + return(reliableId); +} + +class UdpLibraryException +{ +public: + UdpLibraryException(); + ~UdpLibraryException(); +private: +}; + + +#endif + diff --git a/external/3rd/library/udplibrary/UdpLibraryRelease.txt b/external/3rd/library/udplibrary/UdpLibraryRelease.txt new file mode 100644 index 00000000..934ab022 --- /dev/null +++ b/external/3rd/library/udplibrary/UdpLibraryRelease.txt @@ -0,0 +1,1298 @@ +UdpLibrary Release Notes +================================================================================================================= + +----------------------------------------------------------------------------------------------------------------- +6/11/2003 +----------------------------------------------------------------------------------------------------------------- +- There are a lot of significant changes in this release. CPU usage, throughput ability, and memory-usage + have all been improved substantially, particularly for high-stress server-to-server type environments. + The optimizations are such that old settings that used up a lot of memory can be changed to more reasonable + values without adversely effecting throughput ability or CPU usage. To make things simpler in this regard, + below is a list of recommended settings for different types of connections. If you have connections of + different types, ideally you would setup your architecture such that the different types were managed by + different UdpManager's, such that optimal settings can be applied to both. As always, you should not have + more managers than is needed (perhaps one for all client connections and one for all interserver connections). + Settings not specified below should be left at their default. It is still important that you keep the GiveTime + frame rate reasonably high. For high-bandwidth server to server connections, I recommend at least 20, 8 minimum. + If your frame rate drops below that and you are sending more than 500k/sec of data, then you better seriously + consider putting the UdpManager into it's own thread. In a future release (next release probably), I plan on + including a threaded-wrapper version of the UdpLibrary to make this painless. Until then, you may wish to + talk to the SWG team, which has already done this, and may be able to provide you with some getting-started code. + + Server side settings for incoming player connections (ie. clients connecting are relatively low bandwidth) + (total bandwidth per connection is intended to be low) + ----------------------------------------------------------------------------------------------------------- + Params::maxConnections set as appropriate + Params::port set as appropriate + Params::outgoingBufferSize set to 1mb-4mb + Params::incomingBufferSize set to 1mb-4mb + Params::keepAliveDelay set to 15000 (15 second keep-alives generally do the trick) + Params::noDataTimeout set to 46000 (46 seconds, typically just over 3 times client side keepAliveDelay) + Params::reliableOverflowBytes set to 2mb (however much you are willing to let them have backlogged) + Params::hashTableSize set to 10 times max expected connections + Params::pooledPacketMax set to roughly 5 times max expected connections, but rarely over 10000 + Params::crcBytes set to 2 + Params::bindIpAddress[32] set as appropriate (I like to leave it blank) + + Params::clockSyncDelay leave at default of 0 (client side will do clock syncing) + Params::portRange leave at default + Params::packetHistoryMax leave at default + Params::portAliveDelay leave at default of 0 + Params::replyUnreachableConnection leave at default of true + Params::allowPortRemapping leave at default of true + Params::allowAddressRemapping leave at default of false + Params::icmpErrorRetryPeriod leave at default of 5000 + Params::oldestUnacknowledgedTimeout leave at default of 90000 (or set more aggressively even, down to 30000) + Params::maxDataHoldTime leave at default of 50 (or set longer up to 100 possibly) + Params::maxDataHoldSize leave at default of -1 (meaning same as raw packet size) + Params::maxRawPacketSize leave at default of 512 + Params::avoidPriorityQueue leave at default of false + Params::pooledPacketInitial leave at default of 0 (or set to pooledPacketMax for extra efficiency at cost of memory) + Params::pooledPacketSize leave at default of -1 (uses same size as raw packet size, this is critical) + Params::wrappedPoolMax leave at default of 1000 + Params::processIcmpErrors leave at default of true + Params::connectAttemptDelay leave at default (irrelevant for server side) + Params::reliable[0] leave all settings at defaults + + + Server-to-server settings for LAN connections (ie. connections are local on high-bandwidth) + (total bandwidth per connection is intended to be high) + (same settings used regardless of who intiates connection) + ----------------------------------------------------------------------------------------------------------- + Params::maxRawPacketSize set to 1460 + Params::maxConnections set as appropriate + Params::port set as appropriate + Params::outgoingBufferSize set to 4mb + Params::incomingBufferSize set to 4mb + Params::keepAliveDelay set to 15000 (15 second keep-alives generally do the trick) + Params::noDataTimeout set to 61000 (61 seconds, typically just over 4 times client side keepAliveDelay) + Params::reliableOverflowBytes set to 10mb (however much you are willing to let them have backlogged before disconnecting) + Params::hashTableSize set to 10 times max expected connections + Params::pooledPacketMax set to 5000 + Params::crcBytes set to 2 + Params::bindIpAddress[32] set as appropriate (I like to leave it blank) + Params::icmpErrorRetryPeriod set to 2000 (optional) + Params::allowPortRemapping set to false (optional) + Params::oldestUnacknowledgedTimeout set to 30000 (longer for debugging tolerance) + Params::maxDataHoldTime set to 0 (this is critical) + Params::reliable[].maxOutstandingBytes set to 2mb + Params::reliable[].maxOutstandingPackets set to 4000 + Params::reliable[].resendDelayAdjust set to 1500 + Params::reliable[].congestionWindowMinimum set to 50000 + + Params::reliable[] leave all other settings at defaults + Params::clockSyncDelay leave at default of 0 (client side will do clock syncing) + Params::portRange leave at default + Params::packetHistoryMax leave at default + Params::portAliveDelay leave at default of 0 + Params::replyUnreachableConnection leave at default of true + Params::allowAddressRemapping leave at default of false + Params::maxDataHoldSize leave at default of -1 (meaning same as raw packet size) + Params::avoidPriorityQueue leave at default of false + Params::pooledPacketInitial leave at default of 0 (or set to pooledPacketMax for extra efficiency at cost of memory) + Params::pooledPacketSize leave at default of -1 (uses same size as raw packet size, this is critical) + Params::wrappedPoolMax leave at default of 1000 + Params::processIcmpErrors leave at default of true + Params::connectAttemptDelay leave at default + + + Client side settings for player connections (ie. client is connecting over the internet to the server) + (total bandwidth per connection is intended to be low) + ----------------------------------------------------------------------------------------------------------- + Params::maxConnections set as appropropriate (probably something small like 5) + Params::port set to 0 (let client pick port) + Params::outgoingBufferSize set to 64k (default I believe) + Params::incomingBufferSize set to 64k (default I believe) + Params::keepAliveDelay set to 15000 (15 second keep-alives generally do the trick) + Params::noDataTimeout set to 46000 (46 seconds, typically just over 3 times client side keepAliveDelay) + Params::reliableOverflowBytes set to 512k (however much you are willing to let them have backlogged) + Params::hashTableSize set to 10 times max connections + Params::clockSyncDelay set to 60000 if you want packetloss stats or clock-sync features, otherwise 0 + Params::pooledPacketMax set to 50 + Params::crcBytes set to 2 + Params::maxDataHoldTime set to 20 (more agressive for client to server data generally) + Params::packetHistoryMax set to 5 (to save memory on client) + + Params::bindIpAddress[32] leave at default + Params::portRange leave at default + Params::portAliveDelay leave at default of 0 + Params::replyUnreachableConnection leave at default of true + Params::allowPortRemapping leave at default of true + Params::allowAddressRemapping leave at default of false + Params::icmpErrorRetryPeriod leave at default of 5000 + Params::oldestUnacknowledgedTimeout leave at default of 90000 (or set more aggressively even, down to 30000) + Params::maxDataHoldTime leave at default of 60 (or set longer up to 100 possibly) + Params::maxDataHoldSize leave at default of -1 (meaning same as raw packet size) + Params::maxRawPacketSize leave at default of 512 + Params::avoidPriorityQueue leave at default of false + Params::pooledPacketInitial leave at default of 0 (or set to pooledPacketMax for extra efficiency at cost of memory) + Params::pooledPacketSize leave at default of -1 (uses same size as raw packet size, this is critical) + Params::wrappedPoolMax leave at default of 1000 + Params::processIcmpErrors leave at default of true + Params::connectAttemptDelay leave at default + Params::reliable[0] leave all settings at defaults + + +- added reliable-channel level coalescing support. This is a major feature enhancements to the UdpLibrary. + Originally, the UdpLibrary only did coalescing at the low-level, such that it could combine together + unreliable, reliable, and internal data into the same physical packet. This is still a vital and key + feature of the UdpLibrary. Not coalescing up at the higher level of the reliable channel had its + problems: 1) it required that ACK packets on a per-logical packet basis, meaning that if + lots of tiny reliable packets were being sent, there was a bit more ack-overhead in the process. This + overhead is generally negligable except in the most extreme circumstances. 2) it required that the + reliable channel object track all those outstanding packets, which proved to be a bit of a performance + burden. 3) it required the reliable channel object allocated LogicalPacket objects to hold onto each + individual application packet (in the usual case where the application wasn't directly sending + LogicalPacket objects). To make matters worse in this regard, it ended up getting these objects from + the memory pool, which often effectively meant that tiny applications packets were stealing much + larger pooled packets and wasting memory. The new scheme has the advantage that many reliable sends + will now be performed without an allocation and without using up a pooled packet. + + One nice effect of this change will be that a much smaller pool needs to be reserved. Due to a + limitation in the low-level coalescing, if your application sends lots of medium sized packets + (256 to maxRawPacketSize bytes), then you may see a significant reduction in physical packets sent + per second. This change may sound like it would have a drastic effect on the protocol, but that is not + the case, we remain protocol compatible with old version. This is because the underlying protocol + has always supported the concept of a group logical packet. + + The benefits of this feature will be most evident in high-bandwidth server-to-server connections. You + should make sure you pooledPacketSize = maxRawPacketSize, and you should be able to reduce the size of + your pool substantially. + + Internally, the reliable channel will coalesce things up to the fragment size, which defaults to being + the same as the maxRawPacketSize. There is no hold-time for coalescing. The next time the connection + is given time (next time manager is given time), if there is flow-control-window space available, it + will flush the coalesce buffer in an attempt to use it up. So, it will effectively coalesce application + sends until there is an opening in the flow-control-window and GiveTime is called. + + The only disadvantage I can see to leaving this feature enabled (it is configurable in + UdpManager::Params::reliable[].coalesce) is for client-side low-bandwidth connections. In those situations, + it is important that the ACK traffic back to the server coalesce with application data. This will occur + even if this feature is enabled, but because the low-level coalescer is incapable of coalescing packets + larger than 256 bytes, it is possible the reliable-sends from the client headed to the server might + result in more physical packets per second. Even then, I don't see it being a performance issue, + so I would leave it enabled. The configurability was largely added such that the library could be + configured to operate as before, just in case there were some unforseen issues that cropped up with this. + + It's worth noting, that configuring the reliable channel to processOnSend will effectively circumvent + any coalescing that you might have been getting. + +- if reliable-channel coalescing is used (it defaults to ON), then the pooledPacketSize should be set + to the maxRawPacketSize. This is critical to performance. The previous recommendation of setting + it to the typical packet size won't work anymore, since it is now coalescing at the reliable channel + level, effectively increaseing the size of packet it will grab from the pool typically. + +- made substantial CPU-usage improvements to the reliable channel resend algorithm in situations where + thousands of outstanding packets needs to be supported to achieve server-to-server throughput. + This does not effect the protocol in any way, just uses slightly less memory to manage the process and + potentially substantially less CPU. + +- made a slight change to the reliable channel fast-recovery algorithm (based on the TCP Reno algorithm) + When we notice that packetloss is selective (ie. some packet after the lost packet is acked and serves + as a notice to do an accelerated resend on the other), we used to shrink the flow-control window + by 50% (as opposed to non-selective loss which completely resets it). We now only shrink the window + by 33%, in an attempt to make for an even faster recovery and less hiccup. I don't believe being this + aggressive will be an issue, as we know more about the nature of the circumstances than Reno does, since + we have selective acks. + +- fixed a nasty design flaw that was causing the maximum effective reliable logical packet transfer rate + to be limited to 256 times the number of GiveTime iterations per second. When small packets are being + sent, this can turn into a serious limitation, particularly on server-to-server communications where + these packet rates potentially get very high. The limit has been increased to 8192, which should be + plenty large for even the highest bandwidth connections. Note: this fix causes that an additional + 31k of stack space is used during the GiveTime call -- I am doubting this will cause anybody grief. + +- improved the pooled packet scheme. The old scheme used to fall back to standard SimpleLogicalPackets + after it has created the maximum number it was allowed in the pool. The new scheme always creates + packets that can go into the pool when they are returned (if possible size wise). It then releases + packets that are returned to the pool if the pool is already at its max. So as before when max + represent the max anywhere, the max now represents the max it will hold onto in the available queue. + This will make a big performance difference in situations where just one of many connections is + backlogging. In the old scheme, a backlogging connection could eat up the entire pool, leaving + all other connections left doing allocs and frees of their packets. With the new scheme, the + bad connection can't monopolize the pool, as when the bad connection finally clears up and release + all those packets, those packets will be destroyed even though they were gotten from the pool, because + the pool available will have likely grown as appropriate. Between this change and the reliable + coalescing, the pooledPacketMax setting should be set MUCH smaller than it previously was. I + wouldn't set it to over 5000, even for server-to-server connections. Additionally, I would set + pooledPacketInitial to under 1000, since the vast majority of the time I don't see this ever + being exceeded. + +- changed the default setting of pooledPacketMax down to 1000 (from 2000), in order to reflect the + recent optimizations noted above and the lack of need for a large pool most of the time. + +- changed the default setting of pooledPacketSize from 256 to -1. -1 means use whatever size the + maxRawPacketSize is set to. As a general rule, you should never change this setting from this. + Now that the reliable channel is coalescing, all allocations from the pool are of maxRawPacketSize. + If you set the pool size smaller than this, then odds are the pool will be completely worthless. + There are some odd cases where you may wish to set it higher, depending on the nature of the + packets your application sends. + +- got rid of the deque object that was used internally to track outstanding LogicalPackets that were + queued up on the reliable channel. Since we put in coalescing at the top, all packets smaller than + the maxRawPacketSize were getting coalesced into a larger logical packet anyways, meaning that the + logical packet that we owned was extremely unlikely to be shared. If we knew for a fact that all the + logical packets we had queued were not shared by other connections, then we could get rid of the deque + object and have a much faster and simpler linked-list scheme. Since shared packets are now so rare, + I decided it would be worthwhile to switch to a linked list and in situations where the logical packet + is shared, I create a WrapperLogicalPacket object to effectively make it appear unique from a linked-list + management point of view. This wrappers are actually gotten from a pool owned by the UdpManager. You + can set the size to allow this pool to grow with Params::wrappedPoolMax. I recommend leaving this + value at the default of 1000, though if you are a server doing a lot of large shared packets, then it + may be worth your while setting this to a larger value. This optimizations gets rid of the nasty + deque expansion problem that was eating up so much CPU when backlogging occurred. + + Note: This also means the PointerDeque.hpp has been removed from the UdpLibrary as it is no longer + used. You can remove this object from your build. + +- added ack-deduping support. This can be disabled (Params::reliable[].ackDeduping), but defaults to + enabled (true). I really can't think of any reason you would ever want to turn it off. What it does + is cause ack-packets in the low-level coalesce buffer to be deduped. There are no performance + implications (in fact, it is faster when enabled). How it works is if the library is sending an + ack-all packet and there is a previous ack-packet of some sort still sitting in the low-level + multi-buffer, then it will replace the existing ack instead of appending one onto the multi-buffer. + In high-bandwidth situations (or if reliable coalescing is disabled), this can result in an appreciable + bandwidth savings. The only situation where you might want to disable this is if for some reason you + were counting on the additional acks to intentionally overflow the multi-buffer and force it to send + rather than waiting for multi-buffer timeout. But, that doesn't make a whole lot of sense. + +- changed the default value of maxDataHoldSize to -1 (used to be 512). Setting it to -1 now causes it + to use the maxRawPacketSize for this. Generally you should always be wanting to coalesce to as + large of a packet as is allowed. + +- added new configuration parameter, icmpErrorRetryPeriod. This setting allows you to not terminate the + connection at the first sign of trouble (ie. an ICMP error), but instead wait for a period of time in + hopes that the situation will correct itself. Normally, a single ICMP error packet is sufficient cause + to terminate a connection. However, there are situations where it may be desireable to tolerate ICMP + errors for a few seconds as sometimes these situations resolve themselves. In particular, the new + port-remapping feature (see below) requires that some tolerance be set in this regard, as ICMP errors + will likely occur for a short period of time while remapping is occurring. Although it changes + previous behavior of the library, I have decided to make this new setting start out at 5000ms. This + means that ICMP errors for port unreachable and such will not occur for at least 5 seconds, whereas + before they may have occurred nearly instantly. If you want the previous behavior, setting this value + to 0 will cause it to disconnect on even a single ICMP error. Once the first ICMP error is received, + the timer is started. If a subsequent ICMP error comes in after the timeout period has expired, then + it will be processed and the connection will be closed. Once a successful packet is received on the + connection, the ICMP timeout period is reset, such that a subsequent ICMP error will be treated as if + it were the first. + +- added new configuration parameter, allowPortRemapping. This setting allows the UdpLibrary to effectively + change the port-number associated with a particular connection dynamically on-the-fly. Now, normally + this would never happen, but sometimes a user may be going through a NAT router that feels the need to + change the associated mapping mid-session. Normally when this occurs, connections are simply lost. + However, due to a bug in the #1 selling Linksys router whereby it does this remapping every 10 minutes, + we have decided to add this feature to deal with it. This feature is also generally useful in other + situations. For example, with the EQ channel-chat system, the user often goes for quite a while without + sending or receiving any messages. Many NAT routers expire the port-mappings fairly quickly if data + is not being sent. The usual solution to this problem is to configure it to send keep-alives, and + generally that works. In the case of the EQ channel-chat, we have keep-alives configured to go out + every 45 seconds, which keeps 99% of NAT routers alive and happy. The other 1% seem to expire after as + little as 30 seconds. We put in an override setting in EQ to have it send the keep alives every 15 + seconds instead, but it is a less than ideal solution as it requires the user changing things. We + don't want to configure everybody to send keep-alives that frequently, as there are 100,000 connections + to the server, and the keep-alives alone start to really add up. This port remapping system will + effectively allow the mapping to expire and the connection never miss a beat. See allowAddressRemapping + for a discussion of security concerns. This parameter defaults to 'true' or enabled. While this + feature adds some new packetting, it remains protocol compatible with old versions. You will need to + upgrade both ends of the connection for this feature to be available though. + +- added new configuration parameter, allowAddressRemapping. This setting is almost identical to the + allowPortRemapping setting, only it takes the concept one step further, and allows for the IP address + itself to be changed on-the-fly as well. Normally when only port remapping is enabled, if the IP address + is changed, it will not be able to pick up on it. By default, this parameter is 'false' or disabled. It + is highly unlikely that under any sort of normal operating conditions this will occur. About the only + reason I can think of for this is if you wanted to be so tolerant as to allow a modem user to disconnect + and reconnect from their ISP and maintain their connection. The biggest danger in enabling this feature + is the potential for somebody to hijack somebody elses connection. This is extremely unlikely however + as the UdpLibrary effectively uses a 64-bit randomly generated key to authenticate any request for + a remapping to occur. To hijack a connection, somebody would have to guess what that 64-bit key value + is for a particular IP address. + +- added a new static UdpConnection::DisconnectReasonText function that will translate a disconnect reason + into a displayable reason-string to make logging of disconnect reasons easier to read. + +- put in safety checks to try and identify packets that are corrupt for some reason. When one of these + packets is spotted, the application is notified via the UdpConnectionHandler::OnPacketCorrupt callback. + Statistics are also kept on the connection and on the manager regarding how many of these occur. If you + have CRC bytes enabled, then the odds of a corrupt packet making it through the check are extremely low. + In this case, odds are the actual cause of the corruption is intentional packet tampering by the user; + that is, they modify the packet and touchup the CRC so it passes. This is why we callback the application + as they may wish to log this event as a means of monitoring possible cheating. This new callback provides + the application with the packet data at the time the corruption is sensed. This means that if the + corruption is sensed after the decryption has occurred (say, by the multi-packet unpacker), then the + data passed to the application will reflect the decrypted packet. Additionally, the callback provides + a reason why the packet was deemed corrupt. If you are seeing a lot of corrupt packets for some reason, + it may be worth logging the reason and trying to find a pattern (I also like to log the IP of where it + came from, as often times bad packets tend to come from the same IP over and over, often due to + tampering). + +- added new configuration parameter, reliableOverflowBytes. Normally, you can send an unlimited amount + of data to a connection and it will never overflow, instead it will queue up the data until it runs + out of memory and crashes. In the past, it has been the applications responsibility to periodically + check how many bytes are pending using UdpConnection::TotalPendingBytes and take appropriate action. + This new parameter causes the UdpLibrary to do this check for the application, and if a connection + ever overflows this amount (all reliable channels added together), then the connection is disconnected + with a reason of cDisconnectReasonReliableOverflow. The default setting for this parameter is zero, + which disables this check and makes it operate as it always has. The check for an overflow condition + does not occur at send time as this would result in a callback to the application at an illegal time; + instead, the overflow condition is checked the next time the connection object is given time. + +- added a new sample-program to the distribution. This program is called 'udpstress' and simulates what + one might see in a backend server-to-server cluster implementation. Each udpstress client that starts + up connects to one of the existing udpstress clients that is already running, and effectively adds itself + to the cluster. Each node in the cluster transfers data to every other node in the cluster at a compile + time configurable rate. Since everybody is connecting to everybody else, this stress program operates as + both a client and a server. Unlike the other sample program, it fully takes advantage of the new + callbacks that were added and more accurately reflects the proper way of setting up an architecture. + This program is also portable to other platforms. + +- removed the old client/server test programs from the distribution. The udpstress program covers both + client and server examples, is far more flexible for testing, and is written to take advantage of the + latest features, providing a better example to follow. + +- moved the processing of the flagged port unreachable further up in the InternalGiveTime function. This + would have only effected the sparc version of the UdpLibrary and the way it handles ICMP errors. There + was a bug whereby it was going through the connected give-time logic when in fact it may have been + disconnected by a port unreachable. This likely didn't cause any problems, but I moved it just to be + safe. + +- added new UdpMisc::Sleep function. This is not used by the UdpLibrary, but is used by the sample + applications and in general provides a portable way for applications to sleep. + +- made some internal optimizations that allows the UdpLibrary to avoid making an extra copy of the data + (in most circumstances) in order to append CRC bytes. When coalescing is likely to occur and encryption + is not being used, this optimization will speed up the cost of CRC checking on the send side a fair amount. + +- created a new variation of the HashTable called an ObjectHashTable. The UdpLibrary now uses this variation + which is optimized to assume that the contents of the hash table are pointers to objects that are derived + from HashTableMember. This allows the hash table to avoid allocations/deallocations entirely when new + entries are added/removed. UdpConnection objects make use of this optimization. The new hash table object + has restrictions the old one does not, namely, its members can only ever be in one hash table, and then + only in it one time. For most applications, this is not an issue. The old version is no longer used by + the UdpLibrary but has been kept in the header file because some applications have started making use of it, + and we don't want to mess them up. + +- recently, the UdpLibrary has been seeing much heavier load as a server-to-server interprocess communications + protocol. While it was designed to handle this sort of thing, it did have one short-sighted limitation; it + was only capable of transferring 4 billion logical reliable packets per session before it would fall apart. + Even at a very high average trasfer rate of 1000 packets per second on a single connection, it would take over + a month to exceed this rate. It seemed at first that this might be a reasonable limitation, until Planetside + and SWG came along. These games have interprocess packet rates that are sometimes 10 times that high, meaning + that critical failure could occur in only a few days in extreme circumstances. Fixing this limitation did + not require a protocol change, merely some internal changes to the way packets are tracked. You need only + upgrade both sides of a connection simultaneously to maintain compatibility if that particular connection + will exceed 4 billion packets in a single session. There is now no limit on the amount of data that a single + session can send reliably. + +- for the Win32 version of the Clock function, I had come up with a rather clever technique to avoid threading + issues and clock-wrap. Turns out the thread-local-storage scheme I was using doesn't work if the UdpLibrary + is stuck in a DLL. The odds of a clock wrap that occurs once every 47 days thread-slicing between two + consecutive interger assignments and causing a problem are astronomically rare, so I have decided not to + worry about the potential issue. What I really need is a 64-bit interlocked increment, but Windows doesn't + add support for that until the next version (after XP). + +- added new configuration parameter, processIcmpErrorsDuringNegotiating. This boolean parameter defaults to + false, which was the default behavior of the UdpLibrary before this became configurable. Setting this to + true will cause a client side connection attempt to fail immediately if the server process is not running + (ie. doesn't have the port open). Usually you will want to leave this at the default as often times two + processes that talk to each other are started at the same time, and you don't want a race-condition to cause + the client to fail because it happened to startup quicker than the server. + +- put a hard cap on the maximum allowed maxOutstandingPackets configuration value. If this value were ever + set past 30000, things potentially could have fallen apart. I can't imagine there ever being a need for this + many outstanding packets. Eventually when I get the reliable-channel coalescing put in, this cap will become + even more meaningless. + +- made the maximum allowed resend delay configurable. This allows the application to put a cap on the longest + it will ever wait to resend a lost packet. Without a cap, lost-packet throttling can potentially cause the + resend delay to get excessively long. You should generally leave this value alone, but tweaking with it for + certain internal-network connections may be desireable to optimize performance. For example, if you know you + are on an internal network, then it may be safe to set this value to the larger of double the typical ping-time + and the longest GiveTime cycle time of either end of the connection. Like many values, unless you really + know what you are doing, you probably shouldn't mess with this. I would never set this value to less than + 1000 for anything, but setting it to a value like 2000 might be desireable for some internal server-to-server + connections. The default is 5000. This cap has always existed in the UdpLibrary, it has just been hard-coded + in the past. + +- got rid of the temporary encryption and decryption buffers in the UdpManager and replaced with stack-based + buffers. Doing this has two side effects. 1) more stack space is used, though it won't be very much. + 2) there is now a hard-limit on how large you can set the maxRawPacketSize. Currently that hard limit + is set to 16384 bytes. I can't imagine anybody in their right mind would want to set it anywhere near + that high. On some crazy chance that you did want to set it higher, you would simply need to change the + cHardMaxRawPacketSize constant in the header file. In theory it could never go over 65535 anyways, since + that is an IP protocol hard limit. The only implication to increasing this number is more stack space usage. + +- added debug signatures to the end of the encryption buffer and an assert to catch cases where user-supplied + encryption routines overflow the end of the destination buffer. Have yet to see this happen, but I am + learning more and more that the more stuff the UdpLibrary can do to help the application find bugs in its + usage of the library, the better. + +- made sure UdpManager constructor initialized mPassThroughData to NULL. + +- got rid of UdpMisc::FindAvailablePort function. The reason for getting rid of this is that any use of + it will almost surely be an unsafe/bad practice, as once it has found a port for you, it is entirely possible + that some other process on the machine may grab that port before you create a UdpManager and bind to it. We + found this out the hard way in EQ. + +- added a new configuration option, Params::portRange. This new parameter accomplished what FindAvailablePort + was supposed to do, only it does it in a safe manner. If you wanted a to bind to a random port between + 40000 and 40999, you would specify 'port' to be 40000 and portRange to be 1000. Upon construction, the + manager would then randomly pick a port in that range and try binding to it. It will try all ports in the + range before it completely fails and gives up. You cannot specify a portRange to use if you have 'port' + set to zero as this makes no sense. By default this parameter is 0, which causes it to act as it always has + previously. + +- against my better judgement, I added a GetRefCount function to the UdpConnection, UdpManager, + and LogicalPacket objects. I actually needed it on the UdpConnection object internally such that the + UdpManager could tell if the application had accepted a connection during an OnConnectRequest callback. + +- added a new disconnect reason, cDisconnectReasonConnectionRefused. You will never actually see this reason + returned from GetDisconnectReason, since the object that would return it will already be destroyed so you can't + ask it. Where you will actually see it is from the GetOtherSideDisconnectReason function. When a client + attempts to connect to a server and that server ignores their connect request (by simply not AddRef'ing + it during the OnConnectRequest callback), the server responds back to the client with a terminate-packet. The + client will see the disconnect reason as cDisconnectOtherSideTerminated, but can further query to see that the + server explicitly refused the connection. + +- added a new disconnect reason, cDisconnectReasonMutualConnectError. You will only ever see this from + GetDisconnectReasonOther and it is a form of connection-refusal as well. In this case, what it means is + that the guy you are trying to connect to is simulatanously in the process of trying to connect to you. The + odds of this happening are very rare, but since the code was able to distinguish it from other situations, I + figured it was worth clarifying if it ever did happen. + +- added a new disconnect reason, cDisconnectReasonConnectingToSelf. This is identical to the MutualConnectError + and occurs whenever you try to establish a connection to yourself. This is somewhat more likely, and in fact + was added to the library because it actually happened. For the sake of others who might run into this, I will + give a brief description of how we managed to get bit by it. We had enabled ICMP error processing during + connection-negotiation, such that we could instantly tell if the server were down. Our client application would + then fail the connection, and immediately try again. Each time it tried, it picked the next available port. + The client in this case was another server process that happened to be running on the same box as the server + it was trying to connect to. The server it was trying to connect to was down. The server it was trying to + connect to was supposed to be listening on port 44452. The application-level retry loop was very tight with + no delay, and eventually (30 seconds or so), it had managed to cycle through enough ports that the one it + randomly picked (next available) was 44452. This effectively made it connect to itself, which actually caused + the library to eventually crash. This was SWG btw. + +- fixed bug in CRC calculation whereby it was applying the encrypt code in a byte-order dependent manner. Nobody + actually found this bug since nobody is running on big-endian based hardware. There may be other endian issues + that have yet to be tested, but in general the library is supposed to handle endian issues. Internally, all + protocols and such are done in network-byte-order (motorola big-endian order that is) + +- added new api-function UdpConnection::GetOtherSideDisconnectReason(). When a connection is terminated, if the + GetDisconnectReason function returns to you the value cDisconnectReasonOtherSideTerminated, then you will be able to + call this new function and find out what the other-sides reason was for disconnecting. While this latest version of + the library remains backwards protocol compatible, this new API feature will only be available if both sides of the + connection are using the latest version of the library. If the other side is not using the latest version, then + this function will return cDisconnectReasonNone, meaning that it doesn't know. Likewise, if this function is ever + called when the primary disconnect reason is not OtherSideTerminated, then it will return None. This should make + tracking client-disconnects easier in situations where you don't have easy access to the client that is having the + problem. + +- added new api-function UdpConnection::SetSilentDisconnect(bool silent). This function allows the application to tell + a connection that it should not notify the other end of the connection when it terminates (by sending the usual terminate + packet to the other side). This was actually added for internal reasons such that we didn't reply to a terminate-packet + from the other side with a terminate packet of our own; however, it was thought that in some circumstances it may + be desireable to let the other side languish and have to time-out, rather than us explicitly telling them that we + have shutdown our side of the connection. An example of wanting to do this might be if you were testing timeout + functionality, or if you wanted to dump a cheater hard without telling them explicitly that they had been disconnected. + +- added statistics to track how many socket-overflow errors occurred. A socket overflow error can be caused if the + socket-buffer size is too small for the overall datarate that the manager is trying to send (which could very well + happen on a busy server if you gave it a small buffer. The net result is packetloss, which will cause extra resends + later on (for reliable data), etc. If you have a fairly big outgoing socket buffer size, then this may be caused + if the application is spamming too much unreliable data out the pipe. Remember, unreliable data is not flow-controlled + in any way, so if the server instantly dumps 500k of unreliable data to all connections combined and the server only + has a 256k outgoing socket buffer, then you are going to lose a healthy chunk of that data. This statistic can be + used to determine if you have this problem occurring on a frequent basis. + +- made UdpManagerHandler::OnConnectRequest non-pure-virtual (ie. provided a default implementation). It used to be the + case that the only reason you would want a UdpManagerHandler is if you were listening for connections, now it is quite + likely that you will want one for user-supplied encyrption on the client side, so it was thought that we really should + provide a default implementation that basically ignored incoming connection requests. + + +----------------------------------------------------------------------------------------------------------------- +3/7/2003 +----------------------------------------------------------------------------------------------------------------- +- Added new configuration parameter to reliable channels UdpManager::Params::reliable[].congestionWindowMinimum. It is very + unlikely that you would want to change this parameter from its default value of zero. What this parameter does is allow + you to control how small (in bytes) the flow-control congestion window is able to shrink. How flow control works is that + as connections get congested, they slow down sending data by controlling how much outstanding unacknowledged data there + is. The congestion-window itself represents how much data this is, and it changes constantly as appropriate. When packets + are getting lost it shrinks. This new parameter added here lets you control how small you will let it shrink in response + to bad connection conditions. The UdpLibrary will never let the congestion-window shrink below the size of a single + physical packet (512 bytes by default), so all values for this setting below maxRawPacketSize are meaningless. Instead, + the purpose of this setting is to allow you to set this minimum higher than that. Setting this number higher can result + in the connection getting flooded beyond its capacity, as it may not be able to slow itself down enough to meet + current line conditions. + + A situation where you may want to toy with this number is in a game (surprise), where you expect to be the only + process using bandwidth in the system, and you don't really care to be 'friendly' to other connections running + on the system. Normally, if a modem-user is playing the game and has an TCP transfer going on in the background, + then the TCP connection and the UdpLibrary connection will both effectively settle-in on using about half the available + bandwidth. That is, the flow-control algorithms used by the UdpLibrary and TCP are such that over a fairly short period + of time, they will both settle in on a happy split. However, if half-the-bandwidth was equal to a 2k window-size, and + the UdpLibrary was configured to have a minimum window size of 3k, then the UdpLibrary would not scale down. Because + the TCP connection is willing to scale down further, it would, and you would effectively end up with a 3k/1k split + with TCP kindly taking a backseat to the UdpLibrary (of course, if you had put two 3k-configured UdpLibrary connections + in this same environment, the net result would be massive/wasteful packetloss). + + The advantage of setting a higher minimum is just the scenario described above, you can effectively configure your + application to be a bandwidth pig at the expense of other connections in the system; like I said, sometimes this is + desireable. Another advantage of having a larger minimum window size is that it allows the connection to more + quickly scale-up to optimal speed in situations where packets are lost, or there are long stalls in the outgoing + packet stream. So, if the game is plugging away at 500 bytes per second and has a window size of a minimum of 3k, then + when a sudden burst of data transfer is needed for something, it can burst a good chunk of it all at once and not + wait for the flow control window to scale up via the slow-start algorithm (which really should be called the fast-start + algorithm). + + Another case where you might want to set this higher, is if you know for a fact that the connection is always going + to be very high quality (internal ethernet). In those situation, you may want to set the minimum window size to 30k + or so, again, such that transfers get up to full-speed quicker. + + Basically, you can look at this value as the guaranteed bandwidth that you want the connection to claim. The effects of + playing with this setting are going to be very difficult to quantify, but if anybody does decide to try playing with it + and notices an appreciable difference (good or bad), I would appreciate hearing about it, just so I know when others ask + me about it. + +- Added new helper function UdpMisc::FindAvailablePort(int startPortScan, int endPortScan), that will find you a randomly + available port in the specified range, or return 0 if no available port was found. This can be used in situations where + you want the server to be listening on a random port in a particular range. Sometimes it is nice to limit the random + port selection to a particular range as it allows easier configuration of firewalls. It's worth noting that when it + finds an available port, it does not reserve it, but rather releases it and returns which one it is. It is entirely + possible that the port could be claimed by somebody else before you effectively bind to it. A better way of handling + this situation would be to actually create UdpManager's giving them a random listenPort in the range and then checking + the UdpManager::GetErrorCondition to see if it successfully bound and try again if not. In this way, you don't have to + worry about your port getting stolen. This helper function was added merely to handle a situation that was unique to + Everquest where we did not have control of the underlying communications library in order to add this functionality. + +- API CHANGE: UdpManager::GetHostByName has been moved and made a static function of UdpMisc. This allows you to do this + function without creating a UdpManager. + +- API CHANGE: UdpConnection::Disconnect no longer gives the application the option of not being notified via OnTerminated for + the disconnect event. The application can only specify a flushTimeout. When the application does specify a flushTimeout + the OnTerminated callback does not occur until the actual connection is fully disconnected (by either timing out on the flush + time, or successfully sending all the pending data and terminating). While the connection is in a cStatusDisconnectPending + state, it is possible for some other event (like an ICMP error for example) to cause the connection to switch to a + cStatusDisconnected state before all pending data is sent. + +- API CHANGE: OnTerminated callback will now be called ANYTIME a UdpConnection object switches to a cStatusDisconnected state, + regardless of the reason. Once a UdpConnection is in a cStatusDisconnected state, it will always remain as such. Since + newly created UdpConnection objects start out in a non-disconnected state (they are either connected or negotiating to start + with), all UdpConnection objects created are guaranteed to eventually receive one and only-one OnTerminated callback. In + previous versions of the library, a connection that was still negotiating (trying to connect) would not call OnTerminated + if it failed, now it will. In this manner, it will be easier and more clear for an application to maintain a strict balance + between connection creation and destruction. Furthermore, connections that are explicitly terminated by an application + call to Disconnect will now callback via OnTerminated (if necessary). + +- API CHANGE: OnConnectComplete callback is now only called when a connection is successfully established (as initiated by an + EstablishConnection call). Previously, any negotiating connection would callback via this function when it left the + negotiating state (either succeed or fail). + +- API CHANGE: moved user-supplied encryption callbacks from the UdpConnectionHandler to the UdpManagerHandler. If you are + using the user-supplied encryption callbacks, then you will likely need to make some minor changes to your code, by creating + a handler object for the manager and moving the callback routines to there. This should in no way effect the type of + encryption you can do as you will still have access to the UdpConnection object. The purpose of this change was to deal + with a bug whereby the encryption routines could have been called on before the application had an opportunity to set + a connection-specific handler. + +- made API and library more const safe. There are likely a few other places where I could have used const that I did not, I + am sure they will all get tracked down eventually. + +- fixed bug where in theory an infinite loop situation could occur. This could have happened in a situation where one + connection was bumping another connection to the top of the priority queue and vice-versa during priority-queue processing. + This is the second time this issue has come up, so this time I put in a more comprehensive fix. Now, the priority queue + will refuse to let any connection schedule itself for processing time sooner than 1 ms after the current processing cutoff + line. This will also help to deal with issues where the clock might go backwards (though in theory that won't happen now + either). + +- documented the UdpHandler.hpp file more thoroughly to describe what the various callback functions do. + +- fixed a bug where the congestion-window used for flow control was able to shrink down to zero when certain rare packet-loss + patterns occurred. It turns out that if you are under heavy flow and the timing mechanism used by the library goes + backwards it can cause this to happen. This only happened on Linux which uses the real time clock for timing, and the system + clock was changed while the program was running. The fix prevents the congestion-window from dropping below the size of + a single packet. (Thanks Thomas Farthing for help in finding this one) + +- changed the Linux version of the UdpMisc::Clock function to compensate for a backwards moving clock caused by the system + time being changed mid-application. There is still and issue with time jumping forward when the clock is changed, but + that will tend to cause less problems. If anybody knows a relatively portable way of getting millisecond accurate timing + that isn't relative to the real-time clock under Linux (something equivalent to Windows GetTickCount), let me know. + (copied Vince Harron's time-correction routine for this purpose). + +- changed the Windows version of the Clock function to better handle clock-wrap in situations where there are multiple + threads in the process calling the function at the same time. (used thread-local-storage to solve the issue) + +- enhanced user-supplied encryption/decryption callback functions such that if they return -1 it is treated as an + error-condition and the packet is thrown away by the UdpLibrary. This could occur if the application detected during + decryption that something wasn't right. Perhaps somebody tampered with the packet, or it got corrupted on-route yet + still managed to make it through the UDP header checksum and any UdpLibrary CRC checks that might have been in place. + +- fixed bug in explicit bind where it was getting the network byte-ordering wrong (thanks Justin Randall for finding this one). + +- fixed a bug where the OnTerminated callback was not being called in situations where an existing connection was terminated + due to a subsequent new connect-request coming from the same IP and port. Basically, the InternalDisconnect function was + being called and told not to bother notifying the application of the event...I have no idea why I would have told it not + to notify the application of termination for this reason, but it is now fixed. + +- added direct winsock2 support. If you want the UdpLibrary to use version winsock v2.2, then you should define the + symbol UDPLIBRARY_WINSOCK2. This will cause it to use the winsock2.h header files and request version 2.2 of winsock + on WSAStartup. Since the library itself doesn't make use of any winsock2-only features, it really doesn't buy you much + to do this. The exception is if other parts of your program are using winsock2 functionality for some reason, you may + have to define this in order to successfully compile, since the winsock.h and winsock2.h headers conflict. + +- modified the hashtable template class and gave it the ability to iterate through its contents with WalkFirst and WalkNext. + This feature isn't actually used by the UdpLibrary, but some other stuff I am working on that shares that hash table object + needed it. + +- fixed some code issues that were causing level 4 warnings (all future releases will be checked against level 4 warnings now) + +- fixed problem where NULL was being implicitly converted to a UdpIpAddress inside of GetHostByName + + +----------------------------------------------------------------------------------------------------------------- +11/26/2002 +----------------------------------------------------------------------------------------------------------------- +- thanks to Ben Hulse for porting the library to Solaris/sparc. His changes have been rolled into the master distribution. + The ICMP error processing on this platform is done in yet another way from the other platforms. When you attempt + to send data to a destination that has previously returned and ICMP unreachable error, the sendto function fails. Given + that we use the same port for multiple destinations and that hundreds of outgoing packets to other connections could have + occurred before we send to the one that returned the error, I suspect this feature may not be working under Solaris. I + have seem some references on the web that indicate that UDP sockets do not receive ICMP errors unless they are connected + (which obviously we can't do). I didn't immediately see a way that this could work; though, I think it would be worth + trying to do it the basically the same way Windows does on the recvfrom call. + +- we have discovered that ICMP errors are not being forwarded up to the application under Windows NT and Windows 98. These + operating systems echo back ICMP errors as appropriate, but applications running on these platforms will not receive + them on the other end. Windows 2000/XP do not appear to have this problem. Our NT testing was using service-pack 6. + +- fixed bug in replyUnreachableConnection processing. The net result is that the library should more accurately + detect when a connection has gone dead in situations where the server has shutdown and restarted again fairly quickly. + +- optimized the code dealing with removing a connection from the manager. In previous versions of the library this required + a linear search through all connections as they were tracked with a single linked list. They are now tracked with a + double linked list such that they can be removed without the search. This change gets rid of the last linear operation in + the library relative to the number of the connections (in normal processing). + +- removed the UDPLIBRARY_SAME_ENDIAN flag. This turned out to be a source of confusion and in practice probably should + never have been used anyways. It was merely an optimization that allowed the integer packing routine to operate more + quickly in situation where both ends of the connection were guaranteed to be the same byte-order platform. + +- added function UdpManager::GetHostByName, which does a DNS lookup to translate a name into an IP address. Even though + functions such as EstablishConnection will automatically do the DNS lookup if necessary, it is recommended that the + application do this translation themselves on startup if the architecture is such that the connection might be established + multiple times. The reason for this is the lookup is a blocking call and it saves having to do it multiple times potentially. + +- added support for binding the socket used to a particular IP address (UdpManager::Params::bindIpAddress) + Normally, and by default, the library will bind the socket to any address in the machine. This setting should + not be messed with unless you really know what you are doing. In multi-homed machines it is generally NOT + necessary to bind to a particular address, even if there are firewall issues involved, and even if you want + to limit traffic to a particular network (firewalls do a better job of serving that purpose). If you are having + problems communicating with a server on a multi-homed machine and think this might solve the problem, think again. + You most likely need to configure the OS to route data appropriately, or make sure that internal network clients + are connecting to the machine via the machines internal IP address (or vice versa). The only reason I have come up + with to bind to a specific IP is for security reasons above and beyond those that would normally be provided by + a firewall. + +- added configuration parameter UdpManager::Params::processIcmpErrors to turn on/off ICMP error processing. By default + ICMP error processing will be enabled. If you are having problems with false ICMP errors causing disconnections, then + you may want to disable this option. False ICMP errors can occur when a router along the way momentarily goes down + but is traffic is quickly re-routed, such that termination of the connection wasn't really necessary (at least, that's + my theory). + +- added configuration parameter UdpManager::Params::connectAttemptDelay. During connection negotiation, the client will + periodically send the server connect-attempt packets until the server acknowledges the request for a connection. This + value represents how frequently the client sends the connect-attempt packets. By default this is set to 1000 and as a + general rule, should not be changed. The reason this configuration parameter was added was such that I could slow down + the attempt-rate used by clients of the EQ channel-server. At peak, the channel server will have upwards of 90,000 + simultaneous connections. Additionally, there will be up to 2000 people who are attempting to connect but cannot (most + likely due to a firewall issue on their end). These 2000 people will continously try to connect for 30 seconds, sending + the channel-server a packet once per second, resulting in an extra 2000 packets/second coming into the channel server from + people who will never be able to form a connection. Since packetloss is relatively rare and rapid connection establishment + is not critical, I wanted to slow down the rate at which these connection-attempts flooded the server. + +- included to get the definition of NULL. + +- added carriage return to end of some header files to prevent warnings from gcc compiler + +- removed an assert that could happen in normal processing after all. Turns out that some ICMP error packets will show + as coming from port 0 (presumeably when the entire IP address is not reachable?). We have no way of doing an IP-only + base connection query without looping through every connection, which we really don't want to do, so for now we are + ignoring ICMP error packets that have a port of 0. The appropriate behavior would probably be to terminate all + connections that came from that IP address. This doesn't appear to happen very often though; we had the EQ Channel Server + running for 3 hours with 80,000 connections before it happened for the first time. ICMP processing to accellerate + disconnection recognition is really a bit of a luxury, so I don't feel too bad ignoring this rare case. + + +----------------------------------------------------------------------------------------------------------------- +10/3/2002 +----------------------------------------------------------------------------------------------------------------- +- added new function UdpConnection::IncomingBytesLastSecond(), which returns the number of raw bytes the connection object + has received in the last second (accurate to within 25ms). + +- made sure ScheduleTimeNow was never executed during a GiveTime call. If it was, then the net result would potentially be + an immediate re-giving of time again, which I am thinking could possible create an infinite processing loop. + +- added new function UdpManager::GetErrorCondition(), which returns an enum ErrorCondition value (see header file). The + only error conditions that it currently checks for is the ability to create and bind the socket. If you are using a fixed + port and are concerned that somebody else (like another copy of the program running) might already have it, then you should + check the error condition after constructing the UdpManager object. + + +----------------------------------------------------------------------------------------------------------------- +9/18/2002 +----------------------------------------------------------------------------------------------------------------- +- fixed problem where it was unable to establish a new connection because the server-side thought it already had a connection. + This actually would not happen except under very unique circumstances. In particular, you had to establish a connection + to the server, then, using the same UdpManager object, establish another connection to the same server. It is illegal + to have multiple connections between the same two UdpManagers, as the resultant UdpConnection objects would effectively + have the same ip/port combo's, effectively making it impossible for the UdpManager to know who incoming data was coming + from. The problem arose when the client had actually terminated one of the connections, but the server was unaware of it. + Then, the next conenct-attempt would simply be ignored since the server would think it already had a connection to that + ip/port. You would think that the server would eventually timeout the old connection object, but the problem was that + the new connect-attempts (that were being ignored) were actually refreshing the no-data-timeout on the existing connection. + This effectively put it in a state where the client could never re-establish the connection. + + This would actually only occur if the second-connection attempt was using the same UdpManager as it had for the first + connection, or if the UdpManager it was using had been assigned the same port-number again (either by explicit override, + or by dumb-luck). The fix for this is to have the existing connection terminate if it gets a new connect-attempt that + is using a different encrypt-code. When this set of circumstances happens and is the cause for a connection to be + terminated, the disconnect-reason will be set to cDisconnectReasonNewConnectionAttempt. + + On a side note, since clients generally only have one outgoing connection for a given manager, it is generally a good + idea to have the UdpManager created and destroyed along with the connection. Each time the UdpManager is re-created, it + picks a new port to use, which effectively eliminates these issues. This is also a good reason not to explicitly + set the Params::port on client side connections, but instead leave it at 0, so the library will let the OS pick a random + port (that presumeably hasn't been used recently). + + The situation where this bug first appeared was one where the client was actually using the same UdpManager to connect + to dozens of servers simultaneously, so re-making the UdpManager wasn't really an option, since all the other connections + would still be operating. + +- got rid of C-style callback functions. If you are currently using this style of callback you will need to convert + your code over to using handlers. I don't think anybody was using user-supplied encryption/compression routines + yet, but those have been removed as C-style callbacks as well and added to the UdpConnectionHandler class. See the + UdpLibrary.doc file or the udpserver.cpp sample code for a description on how to use handler based callbacks. A quick + and dirty way of converting an existing C-style callback implementation into a handler based one would be to use the + following class: + + class MyHandler : public UdpManagerHandler, public UdpConnectionHandler + { + public: + inline virtual void OnConnectRequest(UdpConnection *con) { AppConnectRequest(NULL, con); } + inline virtual void OnRoutePacket(UdpConnection *con, const uchar *data, int dataLen) { AppRoutePacket(NULL, con, data, dataLen); } + inline virtual void OnConnectComplete(UdpConnection *con) { AppConnectComplete(NULL, con); } + inline virtual void OnTerminated(UdpConnection *con) { AppTerminated(NULL, con); } + }; + + Where the AppXXXXXXXXX functions are functions you had previously passed in as callback functions in the UdpManager::Params. + The only thing lost is there is not a UdpManager mainPassThrough pointer anymore (first parameter to the ConnectRequest + callback in the old scheme), but odds are it wasn't used anyways. Finally, you need to create an instance of the above + class and set it up as the UdpManager::Params::handler for the UdpManager, and call UdpConnection::SetHandler(...) for + each newly created connection as well (just after either EstablishConnection or on the server-side in the OnConnectRequest + function). + + The above dummy-handler is primarily intended to handle converting existing code that uses the old method. New code + should generally have one handler object for the UdpManager and a seperate handler object for each UdpConnection created. + Again, see the udpserver.cpp code for an example of this should be set up. + +- when reliable data is sent, it remains in the queue until it is acknowledged by the other side. When you query + the reliable channel status, you can find out the age of the oldest piece of data in the queue that has been sent + yet has not been acknowledged yet. As a general rule, if things are operating correctly, it should be very rare + for something that has been sent to not be acknowledged within a few seconds, even if resending had to occur. + Eventually, the sender could use this statistic to determine that the other side is no longer talking and terminate + the connection. In past version of the UdpLibrary, the sending-application has checked this statistic itself. A new + parameter has been added UdpManager::Params::oldestUnacknowledgedTimeout. This parameter will cause the UdpLibrary + to monitor this for you and automatically change the connection to a disconnected state when the value goes over + this setting (in milliseconds) (0 means do not perform this check at all) The default is set fairly liberally, + on client-side connections, you could safely set this to as low as 30 seconds (30000) allowing for quicker realization + of lost connections. Often times the connection will realize it is dead much quicker for other reasons. + When disconnected due to this, the disconnect reason is set to cDisconnectReasonUnacknowledgedTimeout + +- added UdpManager::Params::replyUnreachableConnection. When this parameter is set to true (the default), it causes the + UdpManager to reply back to destinations with unreachable-connection error packets when the server does not have a + UdpConnection object representing the destination. The purpose of this is to allow a client who has had its connection + terminated by the server to quickly realize that the server no longer considers the connection valid. Normally the + client would be told when the server terminated the connection, but that termination notice can get lost leaving a client + thinking it is connected for longer than it should. See the header file for this new parameter for more details. + (note: the client can sort of sense this is occurring now by monitoring the oldestUnacknowledgedAge in the reliable + channel status, this just makes it occur quickly instead of having to time-out). + + +----------------------------------------------------------------------------------------------------------------- +8/26/2002 +----------------------------------------------------------------------------------------------------------------- +- when I fixing the bug below, I also took the liberty of having the UdpConnection object hold a reference to the + UdpManager during processing, such that it would handle the case where the UdpManager was deleted during a callback + as well. The problem is, I made the same mistake that caused me to mess with the code in the first place, that is, + referencing the mUdpManager pointer after a callback. But the release still needs to take place, even if we lose + our link to the UdpManager due to a disconnect, so to fix it, I have the UdpConnection object hold its reference to + the UdpManager in a stack variable (effectively). + + +----------------------------------------------------------------------------------------------------------------- +8/23/2002 +----------------------------------------------------------------------------------------------------------------- +- fixed bug where during a UdpConnection::GiveTime call, the mUdpManager member could become NULL (if the application + did a hard-disconnect during a callback function), and the GiveTime function later on used the mUdpManager pointer + without checking it. I think in practice this would have only happened if the application did a hard Disconnect call + during the processing of an incoming reliable application packet. Whatever the case, I have went through the code + and verified that mUdpManager is checked for NULL everywhere it could possibly be used after a callback. + +- added the ability to modify the keep-alive delay on a per-connection basis (see UdpConnection::SetKeepAliveDelay) + +- fixed some compiler warnings/errors for the Linux compile (I don't actually compile the Linux version, so I count on + those who do to tell me when I break things :). + + +----------------------------------------------------------------------------------------------------------------- +8/19/2002 +----------------------------------------------------------------------------------------------------------------- +- added a UdpConnection::GetDisconnectReason function that returns the reason that a connection was terminated. + +- fixed bug where it wasn't tracking the duplicate-packet-received statistic correctly + +- added two new parameters for configuring reliable-channels. I now allow you to set the resendDelayAdjust and the + resendDelayPercent. These two values allow you to fine-tune the resending algorithm to make the channel either more or + less aggressive about correcting lost packets. On some channels, you may be willing to unnecessarily send the packet twice + to ensure that it gets there as soon as possible; whereas on other channels that are less critical, you may be willing + to tolerate a slower correction time to avoid sending something you might not need to. See header-file for details on these + parameter values. + +- changed several of the UdpMisc functions to take void* instead of uchar*. This was to make it easier for applications to + use these helper functions to hand-pack packets in order to be byte-ordering and structure-packing safe. + +- added string-hashing functions to the hashtable.hpp file, since I have found myself using the hash table object in a lot + of places for strings and it seemed generally useful. + +- added the ability to force the UdpManager to pre-allocate a certain portion of the packet pool. You might want to do this + if you are very concerned about memory fragmentation and you know for sure that you are going to be using a certain minimum + number of packets from the pool anyways. The new parameter is UdpManager::Params::pooledPacketInitial. + +- modified the ReliableChannel object to get temporary packets from the pool instead of allocating them. It uses temporary + LogicalPackets to hold onto incoming data that arrives out of order. Again, if you are not configuring the UdpManager to + have a pool and having your application use it as well, you are doing yourself a huge disservice. + +- changed the logical-packet queue to expand itself 64 packet-pointers at a time instead of 1024 at a time. This effectively + reduces the minimum connection footprint by almost 4k at the cost of having to expand this buffer on the fly if the number + of queued logical packets ever exceeds 64. Most of the time this expansion will not have to occur, if I find otherwise in + some application, I will make the expansion rate of this queue a setting instead of hardcoded. + +- added a slightly more efficient, though not quite as effective non-buffer based Xor encryption method. I ended up adding + this as I am trying to avoid the 512 bytes/connection overhead of the XorBuffer method. This should be good enough for + most situations, we are just trying to make the packet-stream look like work to hack to the casual hacker. This small amount + of memory saved actually turns out to be critical for the application I am doing as I am trying to get 200,000 players + connected to a single server. + + +----------------------------------------------------------------------------------------------------------------- +7/1/2002 +----------------------------------------------------------------------------------------------------------------- +- added a new 'interations' statistic to the UdpManagerStatistics. This represents how many times UdpManager::GiveTime has + been called. Divide this by elapsedTime to get an average number of times per second GiveTime is called. This should + generally be at least 10 or else you are likely creating too much processing-induced-lag (as I call it). A similar statistic + has been added to UdpConnectionStatistics + +- changed code to ignore ICMP port unreachable errors while in cStatusNegotiating mode. This was necessary in order to prevent + a client who happened to startup moments before the server did from aborting it's connection attempt because the port wasn't + open yet when the first packet went out. + +- added a new UdpManager::Params::portAliveDelay parameter which is intended to be used on the client side to keep the NAT or + firewall port alive without having to actually send the server unnecessary keep alive packets. This is very similar to the + keep alive delay, but serves a completely different purpose and may not have the desired effect on all platforms. The purpose + of the keepAliveDelay is generally to keep data flowing to the server so it knows that the client is still there. In this + manner if the server doesn't get data within a certain period of time, it can know that the connection is probably dead. + Sometimes it is the case that the server does not need to be kept alive, or at least kept alive very often (like for a chat + server perhaps where nobody is talking much). For some people, it may be necessary to send data more frequently in order to + keep their NAT mapping fresh, or their firewall software happy. However, we don't want to be in a situation where our server + is receiving a lot more data than it needs to just so these people can keep their port open. I have seen NAT's that lose + mappings in as short as 20 seconds. What this feature does is a bit tricky. It changes the time-to-live (TTL) for a special + keep-alive packet to some small value (5) which is enough for the packet to get past local firewalls and NAT's, but not make + it all the way to our server. In this manner, the port gets kept alive, but we don't waste bandwidth with these packets. + These special packets are not counted statistically in any way and they do not reset any timers of any kind. Their sole + purpose is to keep the port alive on the client side. Any other data (including standard keepAlive packets will reset the + timer for this packet, so obviously the portAliveDelay must be smaller than the keepAliveDelay in order to be meaningful. + By default this feature is off. It's worth noting that the client-side will receive-back an ICMP TTL-expired packet; however, + the client will simply ignore it. It is unknown as to whether all NAT's will ignore the ICMP return packet, it is assumed + that they will. You should not set this value too aggressively as the returning ICMP packets will be taking up valuable + incoming bandwidth on the client side, even though they have no effect on our end at all. + + (update: we put this live on EQ and it fixed the problem for a lot of people; however, it also caused a different small + set of players to start having problems...probably firewalls or NATs that interpret the ICMP error packet as a reason to + shut down the port/mapping. Anyways, I am not sure it is a viable option because of this. You could make it an advanced + user option, but clearly it isn't the fool-proof solution we had hoped it would be.) + +- added an optional parameter to UdpConnection::Disconnect as to whether it should callback the application via OnTerminated + to notify of it of this Disconnect call. In either case, the callback would only occur if connection had previously + been in a cStatusConnected state. This api enhancement was primarily added for internal purposes, though it will + allow the application to receive OnTerminated callbacks when it explicitly calls Disconnect itself (if it so desires) + +- added the ability for the library to auto-terminate connections that are not receiving data. This feature is supported + via the UdpManager::Params::noDataTimeout setting. The timeout can also be overridden/controlled on a per-connection + basis via the UdpConnection::SetNoDataTimeout and UdpConnection::GetNoDataTimeout functions. Setting the timeout to + zero (the default) causes the connections to never auto-disconnect due to not receiving data. + +- added UdpManager::GetHandler and UdpConnection::GetHandler calls (added for completeness, not currently used by anybody) + +- some minor changes to the non-windows version so it compiled cleaner. + +- made it so keep-alive packets do not cause UdpConnection objects to be rescheduled in the priority queue. They really + don't need to reschedule things since the worst thing that can happen is it might get processing time sooner than it + might otherwise need to. But scheduling it for immediate processing time just makes that worse if you think about it. + + +----------------------------------------------------------------------------------------------------------------- +5/15/2002 +----------------------------------------------------------------------------------------------------------------- +- added the ability to have a Disconnect'ed connection continue processing until there is no more data outstanding + on any of the reliable channels. The UdpConnection::Disconnect function now takes a 'flushTimeout' value, which is + how long (in milliseconds) the connection will attempt to finish sending queued reliable data before giving up. If + the application specifies a flushTimeout of zero (the default), then the connection is immediately terminated as before. + + When the application specifies a timeout, the connection status is changed to cStatusDisconnectPending. From the + applications point of view the connection should be thought of as disconnected the moment the call is made. The + connection will never callback the application again to deliver data and it will not allow the application to Send + any more data on the connection. About all the application can with the connection is query its various status', or + force an immediate disconnect by calling Disconnect again giving it no flush timeout. + + It is generally thought that the application will call Disconnect with a timeout and then immediately Release the + connection and have nothing else to do with it again. This is how it is intended to work. The Release by the application + will not actually free the connection if there is still data pending to be sent. + + Internally how this works is when the application calls Disconnect with a timeout, the status of the connection + is changed to cStatusDisconnectPending. The connection object registers itself with the UdpManager telling it + to keep a reference to the connection until the connection status changes to cStatusDisconnected. The connection will + be given scheduled time by the UdpManager as before and when the connection has no more reliable data to send, or when + its 'flushTime' expires, the connection will change itself to a cStatusDisconnected state, at which point the UdpManager + will 'Release' its reference to the connection and the connection will actually be deleted. So, take the following + code for example: + + void ApplicationFunction() + { + myConnection->Send(cUdpChannelReliable, data, 100000); // send 100,000 byte packet + myConnection->Disconnect(30000); // disconnect us, but give it up to 30 seconds to get any pending reliable data sent + myConnection->Release(); // we are finished with this connection + myConnection = NULL; + } + + The above code will not block and will deliver the 100k packet if it can manage to do it in the 30 second timeframe. + Releasing the UdpManager while there is pending disconnects will result in everybody being disconnected immediately + and the data will not be delivered. + +- added a new function UdpConnection::TotalPendingBytes(), which returns the total number of reliable-bytes that + are outstanding. When this returns zero, you can be assured that all data on all reliable channels has been successfully + delivered. (note: this is same as adding up ChannelStatus::totalPendingBytes on all reliable channels) + +- added a GetStatistics function to the internally used hash-table object (in case anybody is using it externally for their + own purposes. The UdpLibrary itself does not make use of the statistics. + + +----------------------------------------------------------------------------------------------------------------- +5/9/2002 +----------------------------------------------------------------------------------------------------------------- +- seeded the CRC algorithm with the negotiated encryption key, such that the CRC values calculated for packets cannot + be easily calculated by off the shelf CRC algorithms (to prevent packet tampering). This gives us some level of + protection without the huge disadvantage of encryption which is 1) potentially slow, and 2) encrypting of a typical + data stream reduces how effective modems hardware compress. In Infantry, we found turning encryption on (where the + data did not change size) significantly lagged modem users. For this reason, encryption should likely only be used + in conjunction with compression. General byte-level compression itself is probably sufficient encryption for out needs. + +- added OnCrcReject handler function. This function is only available if a handler object is used (there is no callback + equivalent right now...if somebody wants one, let me know; but, I would encourage everybody to switch over to handler based + callbacks instead). Whenever a packet is rejected due to a CRC error, this function is called to report the incident to + the application. Corrupt packets are fairly rare as they must first get through the 16 bit checksum that the UDP packet + inherently puts on it. If it makes it through that, then our CRC check will probably catch it. The reason why an + application would want to know this, is because there is a very good chance that the corruption was caused by somebody + intentionally trying to tamper with the packet data, but not touching up the CRC on the end. The application may want + to take note of the IP address and if too many corrupt packets come from the same IP, consider banning the player for + cheating. + +- made manager check the protocol version for compatibility before creating an associated UdpConnection object. This prevents + the application from seeing connections that may later be rejected because of protocol version incompatibility. + +- tweaked the flow control algorithm a bit. The two main changes were 1) selective acks now adjust the congestion window + differently similar to the TCP Reno algorithm, and 2) The slow start threshhold was reset when the pipe went dead (before + we only reset the congestion window, but this resulted in a circumstance whereby the slow-start threshhold once low would + never be able to go back up. + +- modified the resend-timer to not take into account the amount of outstanding data in the window. It turns out that + this adjustments is redundant in that the slow-start flow control mechanisms will ramp things up to speed in a manner + such that the average ack time will grow longer as the window gets bigger as appropriate. + +- Added a new parameter to the reliable channel configuration. ReliableConfig::processOnSend if 'true' will configure that + particular reliable channel to do processing on sent packets at the time they are sent instead of waiting until the next + UdpManager::GiveTime call. Normally the application will want to leave this at its default value of 'false' because it + is more efficient on CPU usage, since it can avoid doing all the ReliableChannel::GiveTime processing on a per-sent packet + basis by delaying it until the next global GiveTime. The downside of delaying is that it is often the case that the next + GiveTime simply causes the reliable packet to get moved into the multi-buffer inside the UdpConnection object. Then another + GiveTime a bit later finally sends it. + + Setting this value to 'true' is similar to issuing a FlushChannels command after each reliable Send (with the + exception that rather than giving every channel and the UdpConnection object time, it only has to give the channel + doing the send time). + + This feature is most useful when you are on the client side and the overhead of processing the packet on-send + will not add up to much, compared to what the server-side has to do managing thousands of connections. Most clients + should probably set this to 'true', since timeliness of sent packets generally exceeds any need for code efficiency on + the client side in this regard. + + It's worth noting that the reliable channel object ends up scheduling time for the connection the next GiveTime, + regardless of how this value is set. + + If you have the multi-buffer set to 0 (no multibuffering) and you have this parameter set to 'true', then the actual + send of a physical packet will end up occuring during your reliable-send call (assuming there is room in the reliable + flow control window for the packet to go out at that time). As a reminder, I don't recommend setting the multibuffer + to zero, even on the client side, because it effectively circumvents the mechanism by which ACK packets get combined + and piggy-backed on other packets (ie. every ACK would end up taking its own physical packet). A better approach I feel + is to have a multibuffer and then do explicit FlushMultiBuffer calls after sends. In this manner, ACK's still have some + chance of piggy-backing as they will wait around until the next send call (or the multibuffer timeout). + +- fixed a tiny problem whereby packets that were rejected due to CRC mismatches were not causing the connection receiving + them to schedule itself for processing time as it probably should. In practice this didn't really effect anything. + +- added a (UdpConnection *) parameter to the UdpConnectionHandler callback functions. This was done such that an application + could setup one object as the handler for multiple connections and be able to distinguish between which connection the + callback was coming from. You will have to change your handler functions appropriately to make them compile, and odds + are you will be able to simply ignore the incoming UdpConnection parameter. + +- added UdpManager::GetPassThroughData and UdpManager::SetPassThroughData calls to allow application to change the pass through + data after construction (makes it more consistent with the way UdpConnection objects work as well) + +- made it negotiate the maxRawPacketSize between the client and server side during connection establishment. It will actually + end up using the smaller of the two values specified on the client or server side. In this manner, the server can be + configured to support 1k packets (for example) and some client could be configured to only support 512 byte packets and they + would still be able to talk to each other (they would end up doing all talking with 512 byte packets in this case). This + will prevent the client and server from accidentally getting configured differently and then refusing to talk to each other. + It will also allow clients who are behind networks that can only handle tiny packets to be configured to talk in the smaller + chunks to support that. You will have to update both ends of the connection with this code in order for them to talk, since + this change effected the initial negotiation process. + + +----------------------------------------------------------------------------------------------------------------- +4/12/2002 +----------------------------------------------------------------------------------------------------------------- +- Removed the Handler base class declarations from being nested and put them in their own header file such that we could avoid + having to expose the entire UdpLibrary header everywhere (along with all it's baggage). You can now only include UdpHandler.hpp + to get these declarations, which have now been named UdpConnectionHandler and UdpManagerHandler. + +- Removed the nesting of ConnectionStatistics and ManagerStatistics, they are now UdpConnectionStatistics and UdpManagerStatistics. + +- Optimized the UdpLibrary.hpp header file to avoid including stuff we really didn't need to. In particular, with a few trivial + changes, we managed to be able to avoid including any other header files (including winsock.h). The only file we could not + get rid of including is stddef.h, which has the offsetof() macro that we need in the hashtable.hpp template implementation. + +- merged latest Linux implementation into main distribution. Hopefully I didn't change anything in the Linux implementation in + doing so as I have not tested it. + + +----------------------------------------------------------------------------------------------------------------- +4/9/2002 +----------------------------------------------------------------------------------------------------------------- +- switched UdpManager over to an AddRef/Release scheme as well. This is to allow the application to release/delete the UdpManager + during a callback as well. Internally the UdpManager keeps a reference to itself when it calls back the application, such + that the actual delete doesn't occur until the UdpManager is safely off the stack. + +- added UdpManager::SetHandler function. Normally the handler for the UdpManager is set in the UdpManager::Params and passed to the + constructor; however, I wanted to let the application change or get rid of the handler if needed. In particular, if the application + has set the handler to a particular object, and that object gets deleted before the manager, then that object should clear itself + from being the handler for the manager by setting the handler to NULL (presumeably). In particular, it is probably a good idea + to get in the habit of having handler objects set and clear themselves on construction and destruction as appropriate, for both + UdpConnection and UdpManager handlers. For example, this is the best way to set things up (similar things should be done + for UdpManagers): + + class Player : public UdpConnection::Handler; + + Player::Player(UdpConnection *con) + { + mConnection = con; + mConnection->AddRef(); + mConnection->SetHandler(this); + } + + Player::~Player() + { + mConnection->SetHandler(NULL); + mConnection->Disconnect(); + mConnection->Release(); + } + + Clearing the Handler on destruction is probably not necessary, but on the odd chance that somebody else has a reference to + your object and it lives on, it would seem to be a safe practice. In particular, during callbacks the connection will actually keep + a reference to itself momentarily. The Disconnect call should keep it from every trying to call you back, but clearing the + handler is a good safety step to be sure. + +- fixed bugged array delete in PriorityQueue. + +- fixed another bug in the ICMP handling that prevented the application from deleting the connection during that callback. + +- changed ICMP processing such that it would not quit trying to poll packets when it got an ICMP error return. I am not sure if + Windows avoids sending ICMP error results via recvfrom if there are legit packets in the queue or not, but just in case, this + change will make it handle the situation slightly better. + + +----------------------------------------------------------------------------------------------------------------- +4/8/2002 +----------------------------------------------------------------------------------------------------------------- +- added ICMP port-unreachable support. This will allow the sending end of a connection to quickly determine that the other side + has terminated servicing its port for some reason. Again, props to Justin for this idea. I tested it and it appears to work. + +- changed UdpConnection objects to be AddRef/Release based. This effectively meant we could get rid of the UDPGIFT ownership + passing paradigm because ownership is now a function of having a reference to it. This means that when you are called back + and given a UdpConnection object from a ConnectRequest callback, you must AddRef it if you intend to keep a hold of it; otherwise + it will be destroyed by the UdpManager when the callback returns. If you have an existing application you must remember to put + the AddRef in the callback, otherwise your connection will be destroyed immediately after it is created. The compiler will give you + errors for the destructors which need to be changed as noted below. + + Similarly, the application should Disconnect() and Release() the UdpConnection object when it is done using it and wants + it to go away. This change was needed such that the application would be free to delete the connection object + during packet processing inside a callback. The UdpManager keeps a reference to the object during packet processing + and releases it when done. The problem was that the callback to the application to route the packet came from deep + inside the UdpConnection object which was being destoryed. Because the UdpConnection object may be breaking apart + a multi-packet at the time the application releases the UdpConnection object, it will continue to route the remaining parts of the + multi-packet, as it has no idea that the application no longer wants to receive them. The application may have released its reference + to the UdpConnection, but the UdpManager maintains a reference until the raw packet is finished processing. What this means is that + if the application wants to ensure that when it releases the UdpConnection object that it also prevents all future callbacks from that + connection, it must also call UdpConnection::Disconnect before calling Release(). The bottom line is the proper way for the application + to destroy a connection during packet processing is to call Disconnect() and Release(). A Disconnected UdpConnection object will + never callback the application to route a packet. + +- fixed UdpMisc::Clock such that if different threads were calling it at the same time and got time sliced away, it wouldn't accidentally + double-increment or miscalculate in some way the next stamp. + +- Justin Randall modified library to compile properly under Linux and his changes have been included into official distribution. + The sample client/server programs are still WIN32 specific. + + +----------------------------------------------------------------------------------------------------------------- +4/2/2002 +----------------------------------------------------------------------------------------------------------------- +- made API const safe for most things. + +- added support for using object-based handler objects instead of callback functions for notifications. See the header file + under Params::handler and UdpConnection::SetHandler for more details on how these work. The UdpLibrary.doc file has been updated + to talk briefly about them as well. The UdpServer sample application included in the distribution has been modified to use + handlers instead of callbacks by default as well. + +- changed hashtable template interface slightly. It now has a simpler FindFirst/FindNext interface, avoiding the need to expose + the linked-list implementation details. + + +----------------------------------------------------------------------------------------------------------------- +3/24/2002 +----------------------------------------------------------------------------------------------------------------- +- added a pooled memory manager for logical packets to the UdpManager object. This should virtually eliminate + allocations for the vast majority of usage if configured properly. Using the UdpManager::CreatePacket is now the recommended + method for allocating logical packets to send since it will use the pool automatically if possible. + +- implemented function UdpConnection::OutgoingBytesLastSecond (replaces the unimplemented BytesInLastSecond) + +- added new FlushChannels function which allows the application to force all channels (particularly reliable ones) to send + out any queued data that they have time for immediately, instead of waiting for the next time they get processing time. + +- added new totalBytesPending statistic to the ChannelStatus, so you could tell for certainty that everything had made it + +- tweaked resend-time calculation...I will probably toy with this for a while til I am perfectly happy with it. + +- moved clock-sync packet sending ahead of reliable-queue packet processing to better the odds of getting a fast ping. + + +----------------------------------------------------------------------------------------------------------------- +3/22/2002 +----------------------------------------------------------------------------------------------------------------- +- optimized the flow-control window algorithm for our unique situation. Basically, I changed it so that the window-size does not + increase when the application is pacing the data rate itself such that the window is not getting filled. The problem was that + a slow stream from the application would give the flow-control mechanism the impression that packets were never being lost and it + would keep increasing it's window size forever. Then if the application suddenly dumped a huge chunk of data into the pipe, the large + window size would cause the client to get seriously over-flooded. This is one area where TCP falls flat on its face as well. + For more information on this flow-optimization, search for the mMaxxedOutCurrentWindow variable in the implementation and read the + associated comments. + +- fixed nasty bug where UdpReliableChannel::mLastTimeStampAcknowledged was not being initialized. This potentially caused the + first reliable packet sent to think that it had been ack-accelerated by a later packet and resent a second time. What made the bug + really nasty is that would resend this packet over and over again very very rapidly until it had been acknowledged. But because + the packet had been sent more than once, it would not use that packets last send stamp to reseed the accelleration stamp. This meant + that the next reliable packet sent experienced the same problem. The net effect was a ton of traffic and 80% packetloss to the destination. + I'm surprised this did not show up more in testing, but it should be fixed now. + +- fixed bug where it would crash if the timeout expired on EstablishConnection call (optional feature) +- fixed bug in trickle channels where they were sending faster than the specified trickle rate +- fixed bug in UdpConnection::LastReceive(...) (the version added last rev, accidentally compared the wrong stamp) +- added more ChannelStatus statistics (not real useful, but they have some gee-wiz value) + + +----------------------------------------------------------------------------------------------------------------- +3/12/2002 +----------------------------------------------------------------------------------------------------------------- +- enhanced UdpManager::GiveTime to allow it to be told to process only 1 physical packet out of the socket per call. See the docs + for GiveTime for an explanation of when this might be useful. + +- added support for reliable-channel specific fragment sizes (as opposed to using maxRawPacketSize for all channels). See the docs + for ReliableConfig::fragmentSize for details on when you might want to do this. + +- fixed bug whereby a client-side connection could possibly try and process a packet before it received the connection confirmation + This was a serious problem, but would only happen if the very first communication a client/server did originated from the server + side and the connection confirmation packet had been lost, and encryption was being used. + +- changed UdpMisc::CreateQuickLogicalPacket to allow it to take a NULL primary pointer (ie. just allocate space) + +- added a UdpConnection::LastReceive function that takes the current time as a parameter, this saves the connection having + to poll the clock to accomplish this, which can add up if you are looping through a ton of connections just to check this info + +- moved several functions into the header as inline implementations for performance + + +----------------------------------------------------------------------------------------------------------------- +3/8/2002 +----------------------------------------------------------------------------------------------------------------- +- Fixed bug in UdpConnection::GetLocalPort, it was incorrectly returning the port number in network-byte-order +- Added a UdpConnection::GetUdpManager call to get the UdpManager associated with a connection +- initialized UdpConnection's pass-through pointer to NULL +- got rid of UdpManager::Initialize and UdpManager::Terminate. These functions weren't needed after all since WSAStartup/WSACleanup do ref-counting. + You can now simply create the UdpManager immediately and it will take care of it. + + +----------------------------------------------------------------------------------------------------------------- +3/6/2002 +----------------------------------------------------------------------------------------------------------------- +- UdpManager::ManagerStatistics::elapsedTime changed to milliseconds from seconds. +- Added bytesSent tracking to UdpConnection::ConnectionStatistics +- changed all byte and packet count tracking to 64-bit values to prevent rollover. (cleaned up stat tracking code internally while at it) +- fixed an off-by-one bug in statistics where it was improperly counting the clock-reflect packet it had just sent when calculating packet-loss statistics +- added a UdpManager::GetLocalPort call to complement the GetLocalIp call (oversight, it should have been there all along). +- documentation continues to be fleshed out (still a lot of sections missing though, UdpManager::GiveTime section is worth reading) + + +----------------------------------------------------------------------------------------------------------------- +3/5/2002 +----------------------------------------------------------------------------------------------------------------- +- Added two new callback functions, one for when an EstablishConnection + call either succeeds or fails and one for when the connection is + is terminated by the other end. EstablishConnection function used for + establishing client-side connections now takes a timeout value for how + long it should attempt to establish the connection. Both are completely optional + if the application wishes to have callback notification of these events. + +- Fixed compiler incompatibility with MSVC 7.0 (template friend function related) +- Fixed compiler level 4 warnings for unused parameters +- Modified the resend protocol to throttle back resend times when packetloss + occurs to prevent unnecessary spamming on truely link-dead connections. + + +----------------------------------------------------------------------------------------------------------------- +2/26/2002 +----------------------------------------------------------------------------------------------------------------- +- Initial release of library to EQ2 team (only) + Documentation not yet finished, see header file comments for full documentation. + + +----------------------------------------------------------------------------------------------------------------- +Distribution Files +----------------------------------------------------------- +UdpLibrary.cpp - main source file +UdpLibrary.hpp - main include file +UdpHandler.hpp - include needed to be a handler-only +hashtable.hpp - include file needed internally +priority.hpp - include file needed internally +udpstress.cpp - sample application (acts as both client and server) +UdpLibrary.doc - documentation file +UdpLibraryRelease.txt - this file diff --git a/external/3rd/library/udplibrary/hashtable.hpp b/external/3rd/library/udplibrary/hashtable.hpp new file mode 100644 index 00000000..8d9e80b7 --- /dev/null +++ b/external/3rd/library/udplibrary/hashtable.hpp @@ -0,0 +1,619 @@ +#ifndef HASHTABLE_HPP +#define HASHTABLE_HPP + +#include +#include + + // This is a simple hash table template. Any type of object can be stored in this hash table + // provided it can be copied. Resizing the table while in use is generally not recommended. + // putting the same object in the table twice is generally not recommended as it makes the find ambiguous + + + //////////////////////////////////////////////////////// + // helper functions for the below hash table classes + //////////////////////////////////////////////////////// + +inline bool IsPrime(int value) // works on positive numbers up 62,710,561 +{ + static int sPrimeTab[] = + { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, + 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, + 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, + 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, + 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, + 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, + 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, + 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, + 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, + 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, + 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, + 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, + 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, + 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, + 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, + 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, + 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, + 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, + 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, + 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, + 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, + 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, + 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, + 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, + 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, + 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, + 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, + 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, + 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, + 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, + 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, + 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, + 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, + 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, + 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, + 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, + 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, + 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, + 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, + 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, + 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, + 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, + 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, + 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, + 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, + 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, + 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, + 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, + 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, + 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, + 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, + 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, + 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, + 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, + 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, + 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, + 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, + 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, + 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, + 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, + 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, + 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, + 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, + 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, + 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, + 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, + 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, + 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, + 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, + 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, + 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, + 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, + 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, + 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, + 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, + 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, + 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, + 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, + 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, + 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, + 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, + 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, + 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, + 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473, + 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, + 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, + 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, + 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, + 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, + 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, + 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, + 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, + 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, + 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, + 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, + 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, + 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, + 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, + 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, + 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919 + }; + + for (unsigned int j = 0; j < sizeof(sPrimeTab) / sizeof(int) && sPrimeTab[j] * sPrimeTab[j] < value; j++) + { + if (value % sPrimeTab[j] == 0) + return(false); + } + return(true); +} + +inline int NextPrime(int value) +{ + if (value % 2 == 0) + value++; + while (!IsPrime(value)) + value += 2; + return(value); +} + +inline int HashString(char *string) +{ + int h = 0; + while (*string != 0) + h = ((31 * h) + *string++) ^ (h >> 26); + return(h); +} + +inline int UpperHashString(char *string) +{ + int h = 0; + while (*string != 0) + { + h = ((31 * h) + (*string >= 'a' && *string <= 'z') ? (*string - 0x20) : *string) ^ (h >> 26); + string++; + } + return(h); +} + +struct HashTableStatistics +{ + int tableSize; + int usedSlots; + int totalEntries; +}; + + + ////////////////////////////////////////////////////////////////////////// + // HashTable + ////////////////////////////////////////////////////////////////////////// +template class HashTable +{ + public: + HashTable(int hashSize); + ~HashTable(); + + void Insert(T& obj, int hashValue); + bool Remove(T& obj, int hashValue); + void Reset(); // removes all entries from the table + + T *FindFirst(int hashValue) const; // returns NULL if not found + T *FindNext(T *prevResult) const; // returns NULL if not found + + T *WalkFirst() const; // returns NULL if not found + T *WalkNext(T *prevResult) const; // returns NULL if not found + + void Resize(int hashSize); // the actual hash size will be rounded up to the next larger prime number (very slow, not recommended) + void GetStatistics(HashTableStatistics *stats) const; + + protected: + struct HashEntry + { + T obj; + int hashValue; + HashEntry *nextEntry; + }; + + HashEntry **mTable; + int mTableSize; + int mEntryCount; + int mStatUsedSlots; +}; + + +template HashTable::HashTable(int hashSize) +{ + mTable = NULL; + mTableSize = 0; + mEntryCount = 0; + mStatUsedSlots = 0; + Resize(hashSize); +} + +template HashTable::~HashTable() +{ + Reset(); // deletes everything + delete[] mTable; +} + +template void HashTable::Insert(T& obj, int hashValue) +{ + HashEntry *entry = new HashEntry; + entry->obj = obj; + entry->hashValue = hashValue; + + int spot = ((unsigned)hashValue) % mTableSize; + if (mTable[spot] == NULL) + { + entry->nextEntry = NULL; + mTable[spot] = entry; + mStatUsedSlots++; + } + else + { + entry->nextEntry = mTable[spot]; + mTable[spot] = entry; + } + mEntryCount++; +} + +template bool HashTable::Remove(T& obj, int hashValue) +{ + int spot = ((unsigned)hashValue) % mTableSize; + HashEntry* next = mTable[spot]; + HashEntry** prev = &mTable[spot]; + while (next != NULL) + { + if (next->obj == obj && next->hashValue == hashValue) + { + *prev = next->nextEntry; + delete next; + mEntryCount--; + if (mTable[spot] == NULL) + mStatUsedSlots--; + return(true); + break; + + } + prev = &next->nextEntry; + next = next->nextEntry; + } + return(false); +} + +template void HashTable::Reset() +{ + for (int spot = 0; spot < mTableSize; spot++) + { + HashEntry *curr = mTable[spot]; + if (curr != NULL) + { + mStatUsedSlots--; + while (curr != NULL) + { + HashEntry *next = curr->nextEntry; + delete curr; + mEntryCount--; + curr = next; + } + mTable[spot] = NULL; + } + } +} + +template T *HashTable::FindFirst(int hashValue) const +{ + HashEntry *entry = mTable[((unsigned)hashValue) % mTableSize]; + while (entry != NULL) + { + if (entry->hashValue == hashValue) + return(&entry->obj); + entry = entry->nextEntry; + } + return(NULL); +} + +template T *HashTable::FindNext(T *prevResult) const +{ + HashEntry *entry = (HashEntry *)(((char *)prevResult) - offsetof(HashEntry, obj)); + int hashValue = entry->hashValue; + entry = entry->nextEntry; + while (entry != NULL) + { + if (entry->hashValue == hashValue) + return(&entry->obj); + entry = entry->nextEntry; + } + return(NULL); +} + +template T *HashTable::WalkFirst() const +{ + for (int bucket = 0; bucket < mTableSize; bucket++) + { + HashEntry *entry = mTable[bucket]; + if (entry != NULL) + return(&entry->obj); + } + return(NULL); +} + +template T *HashTable::WalkNext(T *prevResult) const +{ + HashEntry *entry = (HashEntry *)(((char *)prevResult) - offsetof(HashEntry, obj)); + int bucket = ((unsigned)entry->hashValue) % mTableSize; + + entry = entry->nextEntry; + if (entry != NULL) + return(&entry->obj); + + bucket++; // go onto next bucket + for (; bucket < mTableSize; bucket++) + { + HashEntry *entry = mTable[bucket]; + if (entry != NULL) + return(&entry->obj); + } + return(NULL); +} + +template void HashTable::Resize(int hashSize) +{ + HashEntry **oldTable = mTable; + int oldSize = mTableSize; + + // calculate next prime number greater than hashSize. + mTableSize = NextPrime(hashSize); + mTable = new HashEntry*[mTableSize]; + memset(mTable, 0, sizeof(HashEntry *) * mTableSize); + + mStatUsedSlots = 0; + + if (mEntryCount > 0) + { + for (int i = 0; i < oldSize; i++) + { + HashEntry* next = oldTable[i]; + while (next != NULL) + { + HashEntry* hold = next; + next = next->nextEntry; + + // insert hold into new table + int spot = ((unsigned)hold->hashValue) % mTableSize; + if (mTable[spot] == NULL) + { + hold->nextEntry = NULL; + mTable[spot] = hold; + mStatUsedSlots++; + } + else + { + hold->nextEntry = mTable[spot]; + mTable[spot] = hold; + } + } + } + } + + delete[] oldTable; +} + +template void HashTable::GetStatistics(HashTableStatistics *stats) const +{ + stats->totalEntries = mEntryCount; + stats->usedSlots = mStatUsedSlots; + stats->tableSize = mTableSize; +} + + + ////////////////////////////////////////////////////////////////////////////// + // ObjectHashTable + // + // similar to basic HashTable object, only this version requires the contents + // be pointers to objects derived from class HashTableMember. If you can use + // this version, it avoids allocations of HashEntry objects. + // making the cost of object inserts/deletes dirt-cheap + ////////////////////////////////////////////////////////////////////////////// +class HashTableMember +{ +#if defined(_MSC_VER) && (_MSC_VER >= 1300) // MSVC 7.0 is the first version to support friend templates + template friend class ObjectHashTable; + protected: +#else + public: +#endif + int mHashValue; + HashTableMember *mHashNextEntry; +}; + +template class ObjectHashTable +{ + public: + ObjectHashTable(int hashSize); + ~ObjectHashTable(); + + void Insert(T obj, int hashValue); + bool Remove(T obj, int hashValue); + void Reset(); // removes all entries from the table + + T FindFirst(int hashValue) const; // returns NULL if not found + T FindNext(T prevResult) const; // returns NULL if not found + + T WalkFirst() const; // returns NULL if not found + T WalkNext(T prevResult) const; // returns NULL if not found + + void Resize(int hashSize); // the actual hash size will be rounded up to the next larger prime number (very slow, not recommended) + void GetStatistics(HashTableStatistics *stats) const; + + protected: + T *mTable; + int mTableSize; + int mEntryCount; + int mStatUsedSlots; +}; + + +template ObjectHashTable::ObjectHashTable(int hashSize) +{ + mTable = NULL; + mTableSize = 0; + mEntryCount = 0; + mStatUsedSlots = 0; + Resize(hashSize); +} + +template ObjectHashTable::~ObjectHashTable() +{ + Reset(); + delete[] mTable; +} + +template void ObjectHashTable::Insert(T obj, int hashValue) +{ + obj->mHashValue = hashValue; + + int spot = ((unsigned)hashValue) % mTableSize; + if (mTable[spot] == NULL) + { + obj->mHashNextEntry = NULL; + mTable[spot] = obj; + mStatUsedSlots++; + } + else + { + obj->mHashNextEntry = mTable[spot]; + mTable[spot] = obj; + } + mEntryCount++; +} + +template bool ObjectHashTable::Remove(T obj, int hashValue) +{ + int spot = ((unsigned)hashValue) % mTableSize; + T next = mTable[spot]; + T *prev = &mTable[spot]; + while (next != NULL) + { + if (next == obj) + { + *prev = (T)next->mHashNextEntry; + next->mHashNextEntry = NULL; + mEntryCount--; + if (mTable[spot] == NULL) + mStatUsedSlots--; + return(true); + break; + + } + prev = (T *)&next->mHashNextEntry; + next = (T)next->mHashNextEntry; + } + return(false); +} + +template void ObjectHashTable::Reset() +{ + for (int spot = 0; spot < mTableSize; spot++) + { + T curr = mTable[spot]; + if (curr != NULL) + { + while (curr != NULL) + { + T next = (T)curr->mHashNextEntry; + curr->mHashNextEntry = NULL; + mEntryCount--; + curr = next; + } + mTable[spot] = NULL; + mStatUsedSlots--; + } + } +} + +template T ObjectHashTable::FindFirst(int hashValue) const +{ + T entry = mTable[((unsigned)hashValue) % mTableSize]; + while (entry != NULL) + { + if (entry->mHashValue == hashValue) + return(entry); + entry = (T)entry->mHashNextEntry; + } + return(NULL); +} + +template T ObjectHashTable::FindNext(T prevResult) const +{ + T entry = prevResult; + int hashValue = entry->mHashValue; + entry = (T)entry->mHashNextEntry; + while (entry != NULL) + { + if (entry->mHashValue == hashValue) + return(entry); + entry = (T)entry->mHashNextEntry; + } + return(NULL); +} + +template T ObjectHashTable::WalkFirst() const +{ + for (int bucket = 0; bucket < mTableSize; bucket++) + { + T entry = mTable[bucket]; + if (entry != NULL) + return(entry); + } + return(NULL); +} + +template T ObjectHashTable::WalkNext(T prevResult) const +{ + T entry = prevResult; + int bucket = ((unsigned)entry->mHashValue) % mTableSize; + + entry = (T)entry->mHashNextEntry; + if (entry != NULL) + return(entry); + + bucket++; // go onto next bucket + for (; bucket < mTableSize; bucket++) + { + entry = mTable[bucket]; + if (entry != NULL) + return(entry); + } + return(NULL); +} + +template void ObjectHashTable::Resize(int hashSize) +{ + T *oldTable = mTable; + int oldSize = mTableSize; + + // calculate next prime number greater than hashSize. + mTableSize = NextPrime(hashSize); + mTable = new T[mTableSize]; + memset(mTable, 0, sizeof(T) * mTableSize); + + mStatUsedSlots = 0; + + if (mEntryCount > 0) + { + for (int i = 0; i < oldSize; i++) + { + T next = oldTable[i]; + while (next != NULL) + { + T hold = next; + next = (T)next->mHashNextEntry; + + // insert hold into new table + int spot = ((unsigned)hold->mHashValue) % mTableSize; + if (mTable[spot] == NULL) + { + hold->mHashNextEntry = NULL; + mTable[spot] = hold; + mStatUsedSlots++; + } + else + { + hold->mHashNextEntry = mTable[spot]; + mTable[spot] = hold; + } + } + } + } + + delete[] oldTable; +} + +template void ObjectHashTable::GetStatistics(HashTableStatistics *stats) const +{ + stats->totalEntries = mEntryCount; + stats->usedSlots = mStatUsedSlots; + stats->tableSize = mTableSize; +} + +#endif + diff --git a/external/3rd/library/udplibrary/priority.hpp b/external/3rd/library/udplibrary/priority.hpp new file mode 100644 index 00000000..82751efa --- /dev/null +++ b/external/3rd/library/udplibrary/priority.hpp @@ -0,0 +1,225 @@ +#ifndef PRIORITY_HPP +#define PRIORITY_HPP + + +template class PriorityQueue; + + // classes that wish to be members of the priority queue below must derive themselves from this class + // in order to pull in the two member variables. Unlike normal priority queue classes which don't + // have this restriction, it is required in order to support Remove and reprioritize functionality + // in a timely manner (otherwise, in order to remove an entry that is not at the top, you would have + // to linearly scan the entire queue). This is accomplished by having the member itself contain + // a pointer to it's position in the queue (mPriorityQueuePosition). + // note: a restriction of this ability is that an object cannot participate in more than one priority + // queue at a time. +class PriorityQueueMember +{ + public: + PriorityQueueMember(); + +#if defined(_MSC_VER) && (_MSC_VER >= 1300) // MSVC 7.0 is the first version to support friend templates + template friend class PriorityQueue; + protected: +#else + public: +#endif + int mPriorityQueuePosition; // -1=not in queue +}; + + // this class provides a priority queue that is capable of reprioritizing/removing entries. The + // compiler will ensure that objects stored in this class are derived from PriorityQueueMember. + // We don't really need this to be a template class, since we could just treat everything as a + // PriorityQueueMember, but then the application would lose some type checking. We don't use references + // in the api as most priority queue templates do as we can't support non pointer types anyways. +template class PriorityQueue +{ + public: + PriorityQueue(int queueSize); + ~PriorityQueue(); + + T* Top(); // returns NULL if queue is empty + T* TopRemove(); // returns NULL if queue is empty + T* TopRemove(P priority); // removes item from queue if it has a lower priority value, otherwise return NULL + T* Add(T* entry, P priority); // reprioritizes if already in queue, returns entry always + T* Remove(T* entry); // returns entry always (even if it was not in the queue) + P *GetPriority(T* entry); // returns NULL if entry is not in the queue + int QueueUsed(); // returns how many entries are in the queue + protected: + struct QueueEntry + { + T* entry; + P priority; + }; + + QueueEntry* mQueue; + int mQueueSize; + int mQueueEnd; + + void Refloat(T* entry); +}; + + + + ////////////////////////////////////////////////////////////////////////// + // PriorityQueueMember implementation + ////////////////////////////////////////////////////////////////////////// +inline PriorityQueueMember::PriorityQueueMember() +{ + mPriorityQueuePosition = -1; +} + + + ////////////////////////////////////////////////////////////////////////// + // PriorityQueue implementation + ////////////////////////////////////////////////////////////////////////// +template PriorityQueue::PriorityQueue(int queueSize) +{ + mQueueEnd = 0; + mQueueSize = queueSize; + mQueue = new QueueEntry[mQueueSize]; + memset(mQueue, 0, sizeof(mQueue)); +} + +template PriorityQueue::~PriorityQueue() +{ + delete[] mQueue; +} + +template T* PriorityQueue::Top() +{ + if (mQueueEnd == 0) + return(NULL); + return(mQueue[0].entry); +} + +template T* PriorityQueue::TopRemove() +{ + if (mQueueEnd == 0) + return(NULL); + T* top = mQueue[0].entry; + Remove(top); + return(top); +} + +template T* PriorityQueue::TopRemove(P priority) +{ + if (mQueueEnd > 0 && mQueue[0].priority <= priority) + return(Remove(mQueue[0].entry)); + return(NULL); +} + +template P* PriorityQueue::GetPriority(T* entry) +{ + if (entry->mPriorityQueuePosition >= 0) + return(&mQueue[entry->mPriorityQueuePosition].priority); + return(NULL); +} + +template T* PriorityQueue::Add(T* entry, P priority) +{ + if (entry->mPriorityQueuePosition == -1) + { + // not in queue, so add it to the bottom + if (mQueueEnd >= mQueueSize) + return(NULL); + mQueue[mQueueEnd].entry = entry; + mQueue[mQueueEnd].priority = priority; + mQueue[mQueueEnd].entry->mPriorityQueuePosition = mQueueEnd; + mQueueEnd++; + } + else + { + // see if priority has actually changed, if not, just return, otherwise change priority and fall through to refloat it + if (mQueue[entry->mPriorityQueuePosition].priority == priority) + return(entry); + mQueue[entry->mPriorityQueuePosition].priority = priority; + } + + Refloat(entry); + return(entry); +} + +template T* PriorityQueue::Remove(T* entry) +{ + if (entry->mPriorityQueuePosition == -1) + return(entry); + + // move end entry into place of one being removed + mQueueEnd--; + int spot = entry->mPriorityQueuePosition; + if (spot != mQueueEnd) // don't remove last item in queue (bottom of tree), so no need to copy bottom one up and refloat it (we would be refloating our own removed entry) + { + mQueue[spot] = mQueue[mQueueEnd]; + mQueue[spot].entry->mPriorityQueuePosition = spot; + Refloat(mQueue[spot].entry); + } + entry->mPriorityQueuePosition = -1; + return(entry); +} + +template void PriorityQueue::Refloat(T* entry) +{ + // float upward + int spot = entry->mPriorityQueuePosition; + bool tryDown = true; + while (spot > 0 && mQueue[spot].priority < mQueue[(spot - 1) / 2].priority) + { + int newSpot = (spot - 1) / 2; + QueueEntry hold = mQueue[spot]; + mQueue[spot] = mQueue[newSpot]; + mQueue[newSpot] = hold; + mQueue[spot].entry->mPriorityQueuePosition = spot; + mQueue[newSpot].entry->mPriorityQueuePosition = newSpot; + spot = newSpot; + tryDown = false; + } + + if (tryDown) + { + // if we didn't manage to float up at all, then we need to try floating down + for (;;) + { + // pick smallest child + int downSpot1 = (spot * 2) + 1; + if (downSpot1 >= mQueueEnd) + break; + int downSpot2 = (spot * 2) + 2; + + if (downSpot2 >= mQueueEnd || mQueue[downSpot1].priority < mQueue[downSpot2].priority) + { + if (mQueue[downSpot1].priority < mQueue[spot].priority) + { + QueueEntry hold = mQueue[spot]; + mQueue[spot] = mQueue[downSpot1]; + mQueue[downSpot1] = hold; + mQueue[spot].entry->mPriorityQueuePosition = spot; + mQueue[downSpot1].entry->mPriorityQueuePosition = downSpot1; + spot = downSpot1; + } + else + break; + } + else + { + if (mQueue[downSpot2].priority < mQueue[spot].priority) + { + QueueEntry hold = mQueue[spot]; + mQueue[spot] = mQueue[downSpot2]; + mQueue[downSpot2] = hold; + mQueue[spot].entry->mPriorityQueuePosition = spot; + mQueue[downSpot2].entry->mPriorityQueuePosition = downSpot2; + spot = downSpot2; + } + else + break; + } + } + } +} + +template int PriorityQueue::QueueUsed() +{ + return(mQueueEnd); +} + +#endif diff --git a/external/3rd/library/udplibrary/udpclient.cpp b/external/3rd/library/udplibrary/udpclient.cpp new file mode 100644 index 00000000..9debac0e --- /dev/null +++ b/external/3rd/library/udplibrary/udpclient.cpp @@ -0,0 +1,245 @@ +#include +#include +#include +#include +#include +#ifdef WIN32 +#include +#endif +#include +#include +#include +#include +#include + +#include + +#ifdef WIN32 +#ifdef _DEBUG + #include +#endif +#endif + +#include "UdpLibrary.hpp" + + +class MyConnectionHandler : public UdpConnectionHandler +{ + public: + virtual void OnRoutePacket(UdpConnection *con, const uchar *data, int dataLen); +}; + + +int main(int argc, char **argv) +{ +#ifdef _DEBUG + _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF); +#endif + + printf("UdpLibrary Test Client, press SHIFT-F12 to terminate.\n"); + printf(" /IP:xxx.xxx.xxx.xxx\n"); + printf(" /PORT:xxx\n"); + + char connectIp[256]; + strcpy(connectIp, "127.0.0.1"); + int connectPort = 9950; + for (int i = 0; i < argc; i++) + { + if (memicmp(argv[i], "/IP:", 4) == 0) + strcpy(connectIp, argv[i] + 4); + if (memicmp(argv[i], "/PORT:", 6) == 0) + connectPort = atoi(argv[i] + 6); + } + + ////////////////////////////////////////////////// + // initialize everything + ////////////////////////////////////////////////// + srand(clock()); + UdpManager::Params params; + params.clockSyncDelay = 45000; + params.crcBytes = 2; + params.hashTableSize = 10; + params.incomingBufferSize = 32 * 1024; + params.keepAliveDelay = 8000; + params.portAliveDelay = 5000; + params.maxConnections = 3; + params.maxDataHoldSize = 400; + params.maxDataHoldTime = 60; + params.maxRawPacketSize = 496; + params.outgoingBufferSize = 32 * 1024; + params.packetHistoryMax = 1000; + params.port = 0; + params.pooledPacketMax = 50; + params.pooledPacketSize = 512; + +#if 0 + params.simulateIncomingByteRate = 0; + params.simulateIncomingLossPercent = 10; + params.simulateOutgoingByteRate = 0; + params.simulateOutgoingLossPercent = 10; + params.simulateDestinationOverloadLevel = 0; + params.simulateOutgoingOverloadLevel = 0; +#endif + + params.reliable[0].maxInstandingPackets = 500; + params.reliable[0].maxOutstandingBytes = 200000; + params.reliable[0].maxOutstandingPackets = 500; + params.reliable[0].outOfOrder = false; + params.reliable[0].trickleRate = 0; + params.reliable[0].trickleSize = 0; + params.reliable[0].processOnSend = true; + params.reliable[1].maxInstandingPackets = 20; + params.reliable[1].maxOutstandingBytes = 5000; + params.reliable[1].maxOutstandingPackets = 30; + params.reliable[1].outOfOrder = false; + params.reliable[1].trickleRate = 200; + params.reliable[1].trickleSize = 200; + params.reliable[2] = params.reliable[0]; + params.reliable[2].outOfOrder = true; + params.reliable[3] = params.reliable[1]; + params.reliable[3].outOfOrder = true; + UdpManager *myUdpManager = new UdpManager(¶ms); + + + // establish connection + MyConnectionHandler myConnectionHandler; + printf("Connecting to: %s,%d.", connectIp, connectPort); + UdpConnection *myConnection = myUdpManager->EstablishConnection(connectIp, connectPort); + myConnection->SetHandler(&myConnectionHandler); + assert(myConnection != NULL); + int count = 0; + while (myConnection->GetStatus() == UdpConnection::cStatusNegotiating) + { + myUdpManager->GiveTime(); + Sleep(50); + if ((count++ % 20) == 0) + printf("."); + } + printf("\n"); + + + + ////////////////////////////////////////////////// + // master loop (sending data) + ////////////////////////////////////////////////// + count = 0; + for (;;) + { + // check for shutdown keys + if (kbhit()) + { + int k = getch(); + if ((k == 0 || k == 0xe0) && (getch() == 0x88)) // hit shift-F12 to exit gracefully + break; + if (k == 32) + { + // hit space bar to see statistics + UdpConnectionStatistics stats; + myConnection->GetStats(&stats); + char hold[256]; + printf("%s,%d AVE=%d HIGH=%d LOW=%d MSTR=%d,%d CRC=%I64d ORD=%I64d %I64d<<%I64d %I64d>>%I64d\n", myConnection->GetDestinationIp().GetAddress(hold), myConnection->GetDestinationPort() + , stats.averagePingTime, stats.highPingTime, stats.lowPingTime, stats.masterPingTime, stats.masterPingAge, stats.crcRejectedPackets, stats.orderRejectedPackets, stats.syncOurReceived, stats.syncTheirSent + , stats.syncOurSent, stats.syncTheirReceived); + } + + if (k == 'd') + { + myConnection->Disconnect(5000); // disconnect when channel is clear + printf("Disconnecting... (giving connection time to flush)\n"); + } + } + + // see if server terminated out connection, if so, break out and exit app + if (myConnection->GetStatus() == UdpConnection::cStatusDisconnected) + { + printf("Connection broken.\n"); + break; + } + + +#if 1 + // randomly send a medium sized packet on a random channel + if (rand() % 200 == 0) + { + char buf[8000]; + for (int i = 1; i < sizeof(buf); i++) + { + buf[i] = (char)(i % 100); + } + + int pc = rand() % 5; + for (i = 0; i < pc; i++) + { + buf[0] = (char)(rand() % 50); + int len = (rand() % 1800 + 2); + printf("OUTLEN=%d \n", len); + myConnection->Send(cUdpChannelReliable1, buf, len); + } + } +#elif 0 + // send a very large packet one after the other (don't send next packet til the previous one has made it) + if (myConnection->TotalPendingBytes() == 0) + { + // send another packet + SimpleLogicalPacket *lp = new SimpleLogicalPacket(NULL, 30000000); + int dlen = lp->GetDataLen(); + char *ptr = (char *)lp->GetDataPtr(); + + ptr[0] = (char)(rand() % 50); + for (int i = 1; i < dlen; i++) + { + ptr[i] = (char)(i % 100); + } + + printf("OUT LEN=%d \n", dlen); + myConnection->Send(cUdpChannelReliable1, lp); + lp->Release(); + } +#else + // prepare a random sized group of medium packets and then send the group, don't prepare next group until previous one has made it + UdpConnection::ChannelStatus cs; + myConnection->GetChannelStatus(cUdpChannelReliable1, &cs); + if (cs.queuedBytes == 0) + { + int count = 0; + GroupLogicalPacket *glp = new GroupLogicalPacket(); + int di = rand() % 20 + 1; + for (int i = 0; i < di; i++) + { + GroupLogicalPacket *sub = new GroupLogicalPacket(); + for (int j = 0; j < di; j++) + { + char buf[8000]; + *(ushort *)buf = count; + int len = (rand() % 1000) + 10; + *(ushort *)(buf + len - 2) = count; + sub->AddPacket(buf, len); + count++; + } + glp->AddPacket(sub); + sub->Release(); + } + myConnection->Send(cUdpChannelReliable1, glp); + glp->Release(); + printf("Group of %d packets sent\n", count); + } +#endif + + myUdpManager->GiveTime(); + Sleep(10); + } + + ////////////////////////////////////////////////// + // terminate everything + ////////////////////////////////////////////////// + myConnection->Release(); + myUdpManager->Release(); + return(0); +} + + +void MyConnectionHandler::OnRoutePacket(UdpConnection * /*con*/, const uchar *data, int dataLen) +{ + printf("IN=%d/%d LEN=%d \n", *(ushort *)data, *(ushort *)(data + dataLen - 2), dataLen); +} + diff --git a/external/3rd/library/udplibrary/udplibrary.dsp b/external/3rd/library/udplibrary/udplibrary.dsp new file mode 100644 index 00000000..8aecdb9c --- /dev/null +++ b/external/3rd/library/udplibrary/udplibrary.dsp @@ -0,0 +1,152 @@ +# Microsoft Developer Studio Project File - Name="udplibrary" - Package Owner=<4> +# Microsoft Developer Studio Generated Build File, Format Version 6.00 +# ** DO NOT EDIT ** + +# TARGTYPE "Win32 (x86) Static Library" 0x0104 + +CFG=udplibrary - Win32 Debug +!MESSAGE This is not a valid makefile. To build this project using NMAKE, +!MESSAGE use the Export Makefile command and run +!MESSAGE +!MESSAGE NMAKE /f "udplibrary.mak". +!MESSAGE +!MESSAGE You can specify a configuration when running NMAKE +!MESSAGE by defining the macro CFG on the command line. For example: +!MESSAGE +!MESSAGE NMAKE /f "udplibrary.mak" CFG="udplibrary - Win32 Debug" +!MESSAGE +!MESSAGE Possible choices for configuration are: +!MESSAGE +!MESSAGE "udplibrary - Win32 Release" (based on "Win32 (x86) Static Library") +!MESSAGE "udplibrary - Win32 Optimized" (based on "Win32 (x86) Static Library") +!MESSAGE "udplibrary - Win32 Debug" (based on "Win32 (x86) Static Library") +!MESSAGE + +# Begin Project +# PROP AllowPerConfigDependencies 0 +# PROP Scc_ProjName "udplibrary" +# PROP Scc_LocalPath "." +CPP=cl.exe +RSC=rc.exe + +!IF "$(CFG)" == "udplibrary - Win32 Release" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 0 +# PROP BASE Output_Dir "Release" +# PROP BASE Intermediate_Dir "Release" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 0 +# PROP Output_Dir "..\..\..\..\compile\win32\udplibrary\Release" +# PROP Intermediate_Dir "..\..\..\..\compile\win32\udplibrary\Release" +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c +# ADD CPP /nologo /MT /W4 /WX /GX /Zi /O2 /I "..\stlport453\stlport" /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /FD /c +# SUBTRACT CPP /YX +# ADD BASE RSC /l 0x409 /d "NDEBUG" +# ADD RSC /l 0x409 /d "NDEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LIB32=link.exe -lib +# ADD BASE LIB32 /nologo +# ADD LIB32 /nologo + +!ELSEIF "$(CFG)" == "udplibrary - Win32 Optimized" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 1 +# PROP BASE Output_Dir "Optimized" +# PROP BASE Intermediate_Dir "Optimized" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 1 +# PROP Output_Dir "..\..\..\..\compile\win32\udplibrary\Optimized" +# PROP Intermediate_Dir "..\..\..\..\compile\win32\udplibrary\Optimized" +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /YX /FD /GZ /c +# ADD CPP /nologo /MTd /W4 /WX /Gm /GX /Zi /Od /I "..\stlport453\stlport" /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /FD /GZ /c +# SUBTRACT CPP /YX +# ADD BASE RSC /l 0x409 /d "_DEBUG" +# ADD RSC /l 0x409 /d "_DEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LIB32=link.exe -lib +# ADD BASE LIB32 /nologo +# ADD LIB32 /nologo + +!ELSEIF "$(CFG)" == "udplibrary - Win32 Debug" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 1 +# PROP BASE Output_Dir "Debug" +# PROP BASE Intermediate_Dir "Debug" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 1 +# PROP Output_Dir "..\..\..\..\compile\win32\udplibrary\Debug" +# PROP Intermediate_Dir "..\..\..\..\compile\win32\udplibrary\Debug" +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /YX /FD /GZ /c +# ADD CPP /nologo /MTd /W4 /WX /Gm /GX /Zi /Od /I "..\stlport453\stlport" /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /FD /GZ /c +# SUBTRACT CPP /YX +# ADD BASE RSC /l 0x409 /d "_DEBUG" +# ADD RSC /l 0x409 /d "_DEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LIB32=link.exe -lib +# ADD BASE LIB32 /nologo +# ADD LIB32 /nologo + +!ENDIF + +# Begin Target + +# Name "udplibrary - Win32 Release" +# Name "udplibrary - Win32 Optimized" +# Name "udplibrary - Win32 Debug" +# Begin Group "Source Files" + +# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" +# Begin Source File + +SOURCE=.\UdpLibrary.cpp +# End Source File +# End Group +# Begin Group "Header Files" + +# PROP Default_Filter "h;hpp;hxx;hm;inl" +# Begin Source File + +SOURCE=.\hashtable.hpp +# End Source File +# Begin Source File + +SOURCE=.\PointerDeque.hpp +# End Source File +# Begin Source File + +SOURCE=.\priority.hpp +# End Source File +# Begin Source File + +SOURCE=.\UdpHandler.h +# End Source File +# Begin Source File + +SOURCE=.\UdpHandler.hpp +# End Source File +# Begin Source File + +SOURCE=.\UdpLibrary.h +# End Source File +# Begin Source File + +SOURCE=.\UdpLibrary.hpp +# End Source File +# End Group +# End Target +# End Project diff --git a/external/3rd/library/udplibrary/udpserver.cpp b/external/3rd/library/udplibrary/udpserver.cpp new file mode 100644 index 00000000..c523a05b --- /dev/null +++ b/external/3rd/library/udplibrary/udpserver.cpp @@ -0,0 +1,290 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#ifdef _DEBUG + #include +#endif + +#include "UdpLibrary.hpp" + +class Player : public UdpConnectionHandler +{ + public: + Player(UdpConnection *con); + virtual ~Player(); + virtual void OnRoutePacket(UdpConnection *con, const uchar *data, int dataLen); + public: + UdpConnection *mConnection; +}; + +class PlayerManager : public UdpManagerHandler +{ + public: + PlayerManager(); + ~PlayerManager(); + + void AddPlayer(Player *player); + void GiveTime(); + void DumpStats(); + void SendPacketToAll(UdpChannel channel, void *data, int dataLen); + + virtual void OnConnectRequest(UdpConnection *con); + + protected: + Player *mPlayers[5000]; + int mPlayersCount; + UdpMisc::ClockStamp mStartPacketTime; + int mLastIncomingLargeSoFar; +}; + + + + +void MyCallbackRoutePacket(void *mainPassThrough, UdpConnection *con, const uchar *data, int dataLen); +void MyCallbackConnectRequest(void *mainPassThrough, UdpConnection *con); + +UdpManager *myUdpManager; + +int main(int argc, char **argv) +{ + printf("UdpLibrary Test Server, press SHIFT-F12 to terminate.\n"); + printf(" /PORT:xxx\n"); + + int listenPort = 9950; + for (int i = 0; i < argc; i++) + { + if (memicmp(argv[i], "/PORT:", 6) == 0) + listenPort = atoi(argv[i] + 6); + } + +#ifdef _DEBUG + _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF); +#endif + + ////////////////////////////////////////////////// + // initialize everything + ////////////////////////////////////////////////// + srand(clock()); + + PlayerManager *myPlayerManager = new PlayerManager(); + + UdpManager::Params params; + params.handler = myPlayerManager; + params.crcBytes = 2; + params.encryptMethod[0] = UdpManager::cEncryptMethodXorBuffer; + params.hashTableSize = 1000; + params.incomingBufferSize = 256 * 1024; + params.outgoingBufferSize = 256 * 1024; + params.clockSyncDelay = 0; + params.keepAliveDelay = 0; + params.maxConnections = 4000; + params.maxDataHoldSize = 400; + params.maxDataHoldTime = 60; + params.maxRawPacketSize = 256; + params.packetHistoryMax = 1000; + params.port = listenPort; + params.pooledPacketMax = 5000; + params.pooledPacketSize = 512; + +#if 0 + params.simulateIncomingByteRate = 2000; + params.simulateIncomingLossPercent = 0; + params.simulateOutgoingByteRate = 2000; + params.simulateOutgoingLossPercent = 0; + params.simulateDestinationOverloadLevel = 8000; + params.simulateOutgoingOverloadLevel = 8000; +#endif + params.reliable[0].maxInstandingPackets = 500; + params.reliable[0].maxOutstandingBytes = 200000; + params.reliable[0].maxOutstandingPackets = 500; + params.reliable[0].outOfOrder = false; + params.reliable[0].trickleRate = 0; + params.reliable[0].trickleSize = 0; + params.reliable[1].maxInstandingPackets = 30; + params.reliable[1].maxOutstandingBytes = 200000; + params.reliable[1].maxOutstandingPackets = 30; + params.reliable[1].outOfOrder = false; + params.reliable[1].trickleRate = 200; + params.reliable[1].trickleSize = 200; + params.reliable[2] = params.reliable[0]; + params.reliable[2].outOfOrder = true; + params.reliable[3] = params.reliable[1]; + params.reliable[3].outOfOrder = true; + myUdpManager = new UdpManager(¶ms); + + ////////////////////////////////////////////////// + // master loop (processing incoming data/connections) + ////////////////////////////////////////////////// + printf("Listening on port %d\n", listenPort); + int count = 0; + for (;;) + { + // check for shutdown keys + if (kbhit()) + { + int k = getch(); + if ((k == 0 || k == 0xe0) && (getch() == 0x88)) + break; + if (k == 32) + myPlayerManager->DumpStats(); + } + + if ((rand() % 300) == 0) + { + count++; + char buf[2000]; + *(ushort *)buf = (ushort)count; + int len = (rand() % 1000) + 10; + *(ushort *)(buf + len - 2) = (ushort)count; + myPlayerManager->SendPacketToAll(cUdpChannelReliable1, buf, len); + printf("OUT=%d/%d LEN=%d \n", *(ushort *)buf, *(ushort *)(buf + len - 2), len); + } + + myUdpManager->GiveTime(); + myPlayerManager->GiveTime(); + Sleep(10); + } + + ////////////////////////////////////////////////// + // terminate everything + ////////////////////////////////////////////////// + delete myPlayerManager; + myUdpManager->Release(); + return(0); +} + + + //////////////////////////////////////// + // Player implementation + //////////////////////////////////////// +Player::Player(UdpConnection *con) +{ + mConnection = con; + mConnection->AddRef(); + mConnection->SetHandler(this); + + char hold[256]; + printf("CONNECTION %s,%d \n", mConnection->GetDestinationIp().GetAddress(hold), mConnection->GetDestinationPort()); +} + +Player::~Player() +{ + char hold[256]; + printf("TERMINATE %s,%d \n", mConnection->GetDestinationIp().GetAddress(hold), mConnection->GetDestinationPort()); + mConnection->SetHandler(NULL); + mConnection->Disconnect(); + mConnection->Release(); +} + +void Player::OnRoutePacket(UdpConnection * /*con*/, const uchar *data, int dataLen) +{ + for (int i = 1; i < dataLen; i++) + { + bool match = (data[i] == (i % 100)); + assert(match); + } + + char hold[256]; + printf("FROM=%s,%d LEN=%d \n", mConnection->GetDestinationIp().GetAddress(hold), mConnection->GetDestinationPort(), dataLen); + +#if 0 + // reflect exact packet back to client + mConnection->Send(cUdpChannelReliable1, data, dataLen); +#endif +} + + + + + //////////////////////////////////////// + // PlayerManager implementation + //////////////////////////////////////// +PlayerManager::PlayerManager() +{ + mPlayersCount = 0; + mStartPacketTime = 0; + mLastIncomingLargeSoFar = 90000000; +} + +PlayerManager::~PlayerManager() +{ + for (int i = mPlayersCount - 1; i >= 0; i--) + delete mPlayers[i]; +} + +void PlayerManager::OnConnectRequest(UdpConnection *con) +{ + AddPlayer(new Player(con)); +} + +void PlayerManager::AddPlayer(Player *player) +{ + mPlayers[mPlayersCount++] = player; +} + +void PlayerManager::GiveTime() +{ + // see if the player object is no longer connected + for (int i = 0; i < mPlayersCount; i++) + { + if (mPlayers[i]->mConnection->GetStatus() == UdpConnection::cStatusDisconnected || mPlayers[i]->mConnection->LastReceive() > 2000000) + { + Player *p = mPlayers[i]; + mPlayersCount--; + memmove(&mPlayers[i], &mPlayers[i + 1], (mPlayersCount - i) * sizeof(Player *)); + i--; + delete p; + } + } + + if (mPlayersCount > 0) + { + UdpConnection::ChannelStatus cs; + mPlayers[0]->mConnection->GetChannelStatus(cUdpChannelReliable1, &cs); + UdpManagerStatistics ms; + myUdpManager->GetStats(&ms); + if (cs.incomingLargeSoFar < mLastIncomingLargeSoFar) + mStartPacketTime = UdpMisc::Clock(); + + mLastIncomingLargeSoFar = cs.incomingLargeSoFar; + int e = UdpMisc::ClockElapsed(mStartPacketTime); + if (e > 0) + fprintf(stderr, "%d of %d (%d bps)(%d) \r", cs.incomingLargeSoFar, cs.incomingLargeTotal, (int)(((__int64)cs.incomingLargeSoFar * 1000) / (__int64)e), ms.packetsReceived); + } +} + +void PlayerManager::SendPacketToAll(UdpChannel channel, void *data, int dataLen) +{ + LogicalPacket *lp = myUdpManager->CreatePacket(data, dataLen); + for (int i = 0; i < mPlayersCount; i++) + { + mPlayers[i]->mConnection->Send(channel, lp); + } + lp->Release(); +} + +void PlayerManager::DumpStats() +{ + for (int i = 0; i < mPlayersCount; i++) + { + UdpConnectionStatistics stats; + mPlayers[i]->mConnection->GetStats(&stats); + + char hold[256]; + printf("%s,%d AVE=%d HIGH=%d LOW=%d MSTR=%d,%d CRC=%I64d ORD=%I64d %I64d<<%I64d %I64d>>%I64d\n", mPlayers[i]->mConnection->GetDestinationIp().GetAddress(hold), mPlayers[i]->mConnection->GetDestinationPort() + , stats.averagePingTime, stats.highPingTime, stats.lowPingTime, stats.masterPingTime, stats.masterPingAge, stats.crcRejectedPackets, stats.orderRejectedPackets, stats.syncOurReceived, stats.syncTheirSent + , stats.syncOurSent, stats.syncTheirReceived); + } +} + diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index 28d749c5..ca4f992b 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -1,2 +1,3 @@ +add_subdirectory(3rd) add_subdirectory(ours)