mirror of
https://bitbucket.org/seefoe/dockerized-swg-src.git
synced 2026-10-20 09:02:54 -04:00
Restructured src folder to be more clean
This commit is contained in:
+82
@@ -0,0 +1,82 @@
|
||||
#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())
|
||||
// For the encrypt-functions, the destination buffer will be (sourceLen + userSuppliedEncryptExpansionBytes1) long,
|
||||
// do not overflow it, the debug version has an assert check that ensures that you do not.
|
||||
// For the decrypt-functions, the destination buffer is guaranteed to be large enough to hold what the original
|
||||
// data used to be before it was encrypted.
|
||||
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
|
||||
|
||||
+4380
File diff suppressed because it is too large
Load Diff
+2104
File diff suppressed because it is too large
Load Diff
+394
@@ -0,0 +1,394 @@
|
||||
//This code is not used by anything intentionally
|
||||
//The Connection class was getting mistakenly linked in
|
||||
//instead of the SWG Connection class that is used for
|
||||
//all of our connections. Removing the whole thing
|
||||
//to prevent the issue from coming up again.
|
||||
#if 0
|
||||
|
||||
#ifdef WIN32
|
||||
#pragma warning (disable: 4786)
|
||||
#endif
|
||||
|
||||
#include "Base/timer.h"
|
||||
#include "UdpListener.h"
|
||||
#include "Base/profile.h"
|
||||
|
||||
|
||||
#ifdef WIN32
|
||||
#define snprintf _snprintf
|
||||
#endif
|
||||
|
||||
|
||||
namespace UdpLibrary
|
||||
{
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
Listener::QueueNode::QueueNode() :
|
||||
connection(0),
|
||||
request(0)
|
||||
{
|
||||
}
|
||||
|
||||
Listener::QueueNode::QueueNode(Connection * con, RequestBase * req) :
|
||||
connection(con),
|
||||
request(req)
|
||||
{
|
||||
}
|
||||
|
||||
Listener::Listener() :
|
||||
mParams(),
|
||||
mUdpManager(0),
|
||||
mConnections(),
|
||||
mConnectionCount(0),
|
||||
mClosedConnections(),
|
||||
mQueuedRequests(),
|
||||
mActiveRequests(),
|
||||
mActiveCount(0),
|
||||
mActiveMax(0)
|
||||
{
|
||||
//printf("ctor 0x%x Listener\n",this);
|
||||
}
|
||||
|
||||
Listener::~Listener()
|
||||
{
|
||||
std::set<Connection *>::iterator iterator;
|
||||
for (iterator = mConnections.begin(); iterator != mConnections.end(); iterator++)
|
||||
{
|
||||
Connection * connection = *iterator;
|
||||
delete connection;
|
||||
}
|
||||
mConnections.clear();
|
||||
|
||||
if (mUdpManager != 0)
|
||||
{
|
||||
mUdpManager->Release();
|
||||
mUdpManager = 0;
|
||||
}
|
||||
|
||||
//printf("dtor 0x%x Listener\n",this);
|
||||
}
|
||||
|
||||
void Listener::RequestSleep(RequestBase * request)
|
||||
{
|
||||
mSleepingRequests.insert(request);
|
||||
}
|
||||
|
||||
void Listener::RequestWake(RequestBase * request)
|
||||
{
|
||||
if (mSleepingRequests.erase(request))
|
||||
{
|
||||
QueueNode node(request->mConnection, request);
|
||||
mActiveRequests.push_front(node);
|
||||
}
|
||||
}
|
||||
|
||||
void Listener::OnConnectRequest(UdpConnection * connection)
|
||||
{
|
||||
if (IsActive())
|
||||
{
|
||||
Connection * connectionObject = new Connection(*this, connection);
|
||||
|
||||
mConnections.insert(connectionObject);
|
||||
mConnectionCount++;
|
||||
|
||||
OnConnectionOpened(connectionObject);
|
||||
}
|
||||
}
|
||||
|
||||
void Listener::QueueRequest(Connection * connection, RequestBase * request)
|
||||
{
|
||||
connection->NotifyQueuedRequest(request);
|
||||
mQueuedRequests.push_back(QueueNode(connection,request));
|
||||
}
|
||||
|
||||
bool Listener::IsIdle() const
|
||||
{
|
||||
return (!IsActive() && !mUdpManager && mQueuedRequests.empty() && !mActiveCount);
|
||||
}
|
||||
|
||||
unsigned Listener::Process()
|
||||
{
|
||||
Profile profile("Listener::Process");
|
||||
////////////////////////////////////////
|
||||
// handle inactive state (with UdpManager)
|
||||
if (!IsActive() && mUdpManager)
|
||||
{
|
||||
// check all connections to see if they are idle
|
||||
std::set<Connection *>::iterator iterator;
|
||||
for (iterator = mConnections.begin(); iterator != mConnections.end(); iterator++)
|
||||
{
|
||||
Connection * connection = *iterator;
|
||||
if (connection->IsConnected())
|
||||
connection->Disconnect();
|
||||
}
|
||||
// close the UdpManager if all the connections are closed
|
||||
if (!mConnectionCount)
|
||||
{
|
||||
mUdpManager->Release();
|
||||
mUdpManager = 0;
|
||||
OnShutdown();
|
||||
}
|
||||
}
|
||||
////////////////////////////////////////
|
||||
// handle active state (without UdpManager)
|
||||
else if (IsActive() && !mUdpManager)
|
||||
{
|
||||
mParams = GetConnectionParams();
|
||||
mParams.handler = this;
|
||||
mActiveMax = GetActiveRequestMax();
|
||||
mUdpManager = new UdpManager(&mParams);
|
||||
OnStartup();
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
// process the UdpManager
|
||||
if (mUdpManager)
|
||||
{
|
||||
Profile subProfile("UdpManager::GiveTime()");
|
||||
mUdpManager->GiveTime();
|
||||
}
|
||||
|
||||
// check all closed connections to see if they are idle
|
||||
std::list<Connection *>::iterator closedIterator = mClosedConnections.begin();
|
||||
while (closedIterator != mClosedConnections.end())
|
||||
{
|
||||
Profile profile("Listener::Process (cleanup connection)");
|
||||
std::list<Connection *>::iterator current = closedIterator++;
|
||||
Connection * connection = *current;
|
||||
if (!connection->GetActiveRequests() &&
|
||||
!connection->GetQueuedRequests())
|
||||
{
|
||||
mClosedConnections.erase(current);
|
||||
mConnections.erase(connection);
|
||||
mConnectionCount--;
|
||||
OnConnectionDestroyed(connection);
|
||||
delete connection;
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
// process request queue
|
||||
while (!mQueuedRequests.empty() && (!mActiveMax || mActiveCount < mActiveMax))
|
||||
{
|
||||
Profile profile("Listener::Process (activate queued request)");
|
||||
QueueNode & node = mQueuedRequests.front();
|
||||
if (!IsActive())
|
||||
{
|
||||
// If not active, discard queued request
|
||||
node.connection->NotifyDiscardRequest(node.request);
|
||||
DestroyRequest(node.request);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Move request to active list
|
||||
node.connection->NotifyBeginRequest(node.request);
|
||||
mActiveRequests.push_back(node);
|
||||
mActiveCount++;
|
||||
}
|
||||
mQueuedRequests.pop_front();
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
// Process active requests
|
||||
unsigned requestsProcessed = 0;
|
||||
std::list<QueueNode>::iterator iterator = mActiveRequests.begin();
|
||||
while (iterator != mActiveRequests.end())
|
||||
{
|
||||
Profile profile("Listener::Process (process request)");
|
||||
std::list<QueueNode>::iterator current = iterator++;
|
||||
RequestBase * request = current->request;
|
||||
Connection * connection = current->connection;
|
||||
|
||||
if (request->Process())
|
||||
{
|
||||
connection->NotifyEndRequest(request);
|
||||
DestroyRequest(request);
|
||||
mActiveRequests.erase(current);
|
||||
mActiveCount--;
|
||||
}
|
||||
else if (mSleepingRequests.find(request) != mSleepingRequests.end())
|
||||
{
|
||||
mActiveRequests.erase(current);
|
||||
}
|
||||
requestsProcessed++;
|
||||
}
|
||||
return requestsProcessed;
|
||||
}
|
||||
|
||||
void Listener::GetStats(UdpManagerStatistics & statsStruct)
|
||||
{
|
||||
if (mUdpManager)
|
||||
mUdpManager->GetStats(&statsStruct);
|
||||
}
|
||||
|
||||
void Listener::ResetStats()
|
||||
{
|
||||
if (mUdpManager)
|
||||
mUdpManager->ResetStats();
|
||||
}
|
||||
|
||||
void Listener::OnStartup()
|
||||
{
|
||||
}
|
||||
|
||||
void Listener::OnShutdown()
|
||||
{
|
||||
}
|
||||
|
||||
void Listener::OnConnectionOpened(Connection * connection)
|
||||
{
|
||||
}
|
||||
|
||||
void Listener::OnConnectionClosed(Connection * connection, const char * reason)
|
||||
{
|
||||
}
|
||||
|
||||
void Listener::OnConnectionDestroyed(Connection * connection)
|
||||
{
|
||||
}
|
||||
|
||||
void Listener::OnCrcReject(Connection *connection, const unsigned char * buffer, unsigned size)
|
||||
{
|
||||
}
|
||||
|
||||
void Listener::OnPacketCorrupt(Connection *con, const unsigned char *data, int dataLen, UdpCorruptionReason reason)
|
||||
{
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
Connection::Connection(Listener & listener, UdpConnection * connection) :
|
||||
mListener(listener),
|
||||
mConnection(connection),
|
||||
mHost(),
|
||||
mHostIp(0),
|
||||
mDisconnectReason(0),
|
||||
mQueuedRequests(0),
|
||||
mActiveRequests(0)
|
||||
{
|
||||
mConnection->AddRef();
|
||||
mConnection->SetHandler(this);
|
||||
|
||||
char buffer[256];
|
||||
char addr[32];
|
||||
mHostIp = mConnection->GetDestinationIp().GetAddress();
|
||||
mConnection->GetDestinationIp().GetAddress(addr);
|
||||
snprintf(buffer, sizeof(buffer), "%s:%u", addr, mConnection->GetDestinationPort());
|
||||
mHost = buffer;
|
||||
|
||||
//printf("ctor 0x%x connection\n",this);
|
||||
}
|
||||
|
||||
Connection::~Connection()
|
||||
{
|
||||
Disconnect();
|
||||
|
||||
//printf("dtor 0x%x connection\n",this);
|
||||
}
|
||||
|
||||
unsigned Connection::Send(const unsigned char * data, unsigned dataLen)
|
||||
{
|
||||
if (mConnection)
|
||||
{
|
||||
mConnection->Send(cUdpChannelReliable1, data, dataLen);
|
||||
return dataLen;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Connection::Disconnect()
|
||||
{
|
||||
if (mConnection)
|
||||
OnTerminated(mConnection);
|
||||
}
|
||||
|
||||
bool Connection::IsConnected() const
|
||||
{
|
||||
return mConnection != 0;
|
||||
}
|
||||
|
||||
const std::string & Connection::GetHost() const
|
||||
{
|
||||
return mHost;
|
||||
}
|
||||
|
||||
const unsigned Connection::GetHostIP() const
|
||||
{
|
||||
return mHostIp;
|
||||
}
|
||||
|
||||
unsigned Connection::GetQueuedRequests() const
|
||||
{
|
||||
return mQueuedRequests;
|
||||
}
|
||||
|
||||
unsigned Connection::GetActiveRequests() const
|
||||
{
|
||||
return mActiveRequests;
|
||||
}
|
||||
|
||||
void Connection::OnTerminated(UdpConnection *con)
|
||||
{
|
||||
mConnection->SetHandler(0);
|
||||
mConnection->Disconnect();
|
||||
UdpConnection::DisconnectReason disconnectReason = mConnection->GetDisconnectReason();
|
||||
mConnection->Release();
|
||||
mConnection = 0;
|
||||
mListener.mClosedConnections.push_back(this);
|
||||
mListener.OnConnectionClosed(this, UdpConnection::DisconnectReasonText(disconnectReason));
|
||||
}
|
||||
|
||||
void Connection::OnRoutePacket(UdpConnection *, const unsigned char * data, int dataLen)
|
||||
{
|
||||
mListener.OnReceive(this, data, dataLen);
|
||||
}
|
||||
|
||||
void Connection::OnCrcReject(UdpConnection *, const unsigned char * data, int dataLen)
|
||||
{
|
||||
mListener.OnCrcReject(this, data, dataLen);
|
||||
}
|
||||
|
||||
void Connection::OnPacketCorrupt(UdpConnection *, const uchar *data, int dataLen, UdpCorruptionReason reason)
|
||||
{
|
||||
mListener.OnPacketCorrupt(this, data, dataLen, reason);
|
||||
}
|
||||
|
||||
void Connection::NotifyQueuedRequest(RequestBase * request)
|
||||
{
|
||||
mQueuedRequests++;
|
||||
}
|
||||
|
||||
void Connection::NotifyDiscardRequest(RequestBase * request)
|
||||
{
|
||||
mQueuedRequests--;
|
||||
}
|
||||
|
||||
void Connection::NotifyBeginRequest(RequestBase * request)
|
||||
{
|
||||
mQueuedRequests--;
|
||||
mActiveRequests++;
|
||||
}
|
||||
|
||||
void Connection::NotifyEndRequest(RequestBase * request)
|
||||
{
|
||||
mActiveRequests--;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
RequestBase::RequestBase(Connection * connection) :
|
||||
mConnection(connection),
|
||||
mProcessState(0)
|
||||
{
|
||||
}
|
||||
|
||||
RequestBase::~RequestBase()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
//This code is not used by anything intentionally
|
||||
//The Connection class was getting mistakenly linked in
|
||||
//instead of the SWG Connection class that is used for
|
||||
//all of our connections. Removing the whole thing
|
||||
//to prevent the issue from coming up again.
|
||||
#if 0
|
||||
|
||||
#ifndef COMMON_SERVER__LISTENER_H
|
||||
#define COMMON_SERVER__LISTENER_H
|
||||
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <set>
|
||||
#include "UdpLibrary.hpp"
|
||||
|
||||
namespace UdpLibrary
|
||||
{
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class RequestBase;
|
||||
class Connection;
|
||||
class Listener : public UdpManagerHandler
|
||||
{
|
||||
friend class Connection;
|
||||
struct QueueNode
|
||||
{
|
||||
QueueNode();
|
||||
QueueNode(Connection * connection, RequestBase * request);
|
||||
bool operator<(const QueueNode & rhs) { return request < rhs.request; }
|
||||
|
||||
Connection * connection;
|
||||
RequestBase * request;
|
||||
};
|
||||
|
||||
public:
|
||||
Listener();
|
||||
virtual ~Listener();
|
||||
|
||||
void QueueRequest(Connection * connection, RequestBase * request);
|
||||
bool IsIdle() const;
|
||||
unsigned Process();
|
||||
void GetStats(UdpManagerStatistics & statsStruct);
|
||||
void ResetStats();
|
||||
|
||||
void RequestSleep(RequestBase * request);
|
||||
void RequestWake(RequestBase * request);
|
||||
|
||||
virtual bool IsActive() const = 0;
|
||||
virtual unsigned GetActiveRequestMax() = 0;
|
||||
virtual UdpManager::Params GetConnectionParams() = 0;
|
||||
|
||||
virtual void OnStartup();
|
||||
virtual void OnShutdown();
|
||||
virtual void OnConnectionOpened(Connection * connection);
|
||||
virtual void OnConnectionClosed(Connection * connection, const char * reason);
|
||||
virtual void OnConnectionDestroyed(Connection * connection);
|
||||
virtual void OnReceive(Connection * connection, const unsigned char * data, unsigned dataLen) = 0;
|
||||
virtual void OnCrcReject(Connection *connection, const unsigned char * data, unsigned dataLen);
|
||||
virtual void OnPacketCorrupt(Connection *con, const unsigned char *data, int dataLen, UdpCorruptionReason reason);
|
||||
virtual void DestroyRequest(RequestBase * request) = 0;
|
||||
|
||||
virtual void OnConnectRequest(UdpConnection *con);
|
||||
|
||||
protected:
|
||||
UdpManager::Params mParams;
|
||||
UdpManager * mUdpManager;
|
||||
|
||||
std::set<Connection *> mConnections;
|
||||
unsigned mConnectionCount;
|
||||
std::list<Connection *> mClosedConnections;
|
||||
|
||||
std::list<QueueNode> mQueuedRequests;
|
||||
std::list<QueueNode> mActiveRequests;
|
||||
std::set<RequestBase *> mSleepingRequests;
|
||||
unsigned mActiveCount;
|
||||
unsigned mActiveMax;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
class Connection : public UdpConnectionHandler
|
||||
{
|
||||
friend class Listener;
|
||||
public:
|
||||
Connection(Listener & listener, UdpConnection * connection);
|
||||
virtual ~Connection();
|
||||
|
||||
unsigned Send(const uchar * data, unsigned dataLen);
|
||||
void Disconnect();
|
||||
bool IsConnected() const;
|
||||
|
||||
const std::string & GetHost() const;
|
||||
const unsigned GetHostIP() const;
|
||||
unsigned GetQueuedRequests() const;
|
||||
unsigned GetActiveRequests() const;
|
||||
|
||||
virtual void OnTerminated(UdpConnection *con);
|
||||
virtual void OnRoutePacket(UdpConnection *con, const uchar *data, int dataLen);
|
||||
virtual void OnCrcReject(UdpConnection *con, const uchar *data, int dataLen);
|
||||
virtual void OnPacketCorrupt(UdpConnection *con, const uchar *data, int dataLen, UdpCorruptionReason reason);
|
||||
|
||||
protected:
|
||||
void NotifyQueuedRequest(RequestBase * request);
|
||||
void NotifyDiscardRequest(RequestBase * request);
|
||||
void NotifyBeginRequest(RequestBase * request);
|
||||
void NotifyEndRequest(RequestBase * request);
|
||||
|
||||
protected:
|
||||
Listener & mListener;
|
||||
UdpConnection * mConnection;
|
||||
std::string mHost;
|
||||
unsigned mHostIp;
|
||||
const char * mDisconnectReason;
|
||||
unsigned mQueuedRequests;
|
||||
unsigned mActiveRequests;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
class RequestBase
|
||||
{
|
||||
friend class Listener;
|
||||
public:
|
||||
RequestBase(Connection * connection);
|
||||
virtual ~RequestBase();
|
||||
|
||||
virtual bool Process() = 0;
|
||||
|
||||
protected:
|
||||
Connection * mConnection;
|
||||
unsigned mProcessState;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user