mirror of
https://bitbucket.org/theswgsource/src-1.2.git
synced 2026-08-01 02:15:54 -04:00
Added ChatServer and soePlatform
This commit is contained in:
+256
@@ -0,0 +1,256 @@
|
||||
#include "GenericApiCore.h"
|
||||
#include "GenericConnection.h"
|
||||
#include "GenericMessage.h"
|
||||
|
||||
|
||||
#ifdef USE_SERIALIZE_LIB
|
||||
#include <Base/serialize.h>
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
#ifdef EXTERNAL_DISTRO
|
||||
namespace NAMESPACE
|
||||
{
|
||||
|
||||
#endif
|
||||
namespace GenericAPI
|
||||
{
|
||||
|
||||
|
||||
GenericAPICore::GenericAPICore(const char *host,
|
||||
short port,
|
||||
unsigned reqTimeout,
|
||||
unsigned reconnectTimeout,
|
||||
unsigned noDataTimeoutSecs,
|
||||
unsigned noAckTimeoutSecs,
|
||||
unsigned incomingBufSizeInKB,
|
||||
unsigned outgoingBufSizeInKB,
|
||||
unsigned keepAlive,
|
||||
unsigned maxRecvMessageSizeInKB)
|
||||
: m_currTrack(0),
|
||||
m_reconnectTimeout(0),
|
||||
m_outCount(0),
|
||||
m_pendingCount(0),
|
||||
m_requestTimeout(reqTimeout),
|
||||
m_suspended(false),
|
||||
m_nextConnectionIndex(0)
|
||||
{
|
||||
m_serverConnections.push_back(new GenericConnection(host, port, this, reconnectTimeout, noDataTimeoutSecs, noAckTimeoutSecs, incomingBufSizeInKB, outgoingBufSizeInKB, keepAlive, maxRecvMessageSizeInKB));
|
||||
}
|
||||
|
||||
GenericAPICore::GenericAPICore(const char *hosts[],
|
||||
const short port[],
|
||||
unsigned arraySize,
|
||||
unsigned reqTimeout,
|
||||
unsigned reconnectTimeout,
|
||||
unsigned noDataTimeoutSecs,
|
||||
unsigned noAckTimeoutSecs,
|
||||
unsigned incomingBufSizeInKB,
|
||||
unsigned outgoingBufSizeInKB,
|
||||
unsigned keepAlive,
|
||||
unsigned maxRecvMessageSizeInKB)
|
||||
: m_currTrack(0),
|
||||
m_reconnectTimeout(0),
|
||||
m_outCount(0),
|
||||
m_pendingCount(0),
|
||||
m_requestTimeout(reqTimeout),
|
||||
m_suspended(false),
|
||||
m_nextConnectionIndex(0)
|
||||
{
|
||||
for (unsigned i=0; i<arraySize; i++)
|
||||
{
|
||||
m_serverConnections.push_back(new GenericConnection(hosts[i], port[i], this, reconnectTimeout, noDataTimeoutSecs, noAckTimeoutSecs, incomingBufSizeInKB, outgoingBufSizeInKB, keepAlive, maxRecvMessageSizeInKB));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
GenericAPICore::~GenericAPICore()
|
||||
{
|
||||
for (std::vector<GenericConnection *>::iterator conIter = m_serverConnections.begin(); conIter != m_serverConnections.end(); conIter++)
|
||||
{
|
||||
GenericConnection *con = *conIter;
|
||||
delete con;
|
||||
}
|
||||
m_serverConnections.clear();
|
||||
|
||||
for(map<unsigned, GenericResponse *>::iterator iter = m_pending.begin(); iter != m_pending.end(); ++iter)
|
||||
{
|
||||
delete (*iter).second;
|
||||
}
|
||||
|
||||
m_pending.empty();
|
||||
|
||||
while(m_outCount > 0)
|
||||
{
|
||||
delete m_outboundQueue.front().second;
|
||||
delete m_outboundQueue.front().first;
|
||||
m_outboundQueue.pop();
|
||||
--m_outCount;
|
||||
}
|
||||
}
|
||||
|
||||
void GenericAPICore::changeHostPort(unsigned connectionIndex, const char *host, short port)
|
||||
{
|
||||
if (connectionIndex <= m_serverConnections.size() - 1)
|
||||
{
|
||||
m_serverConnections[connectionIndex]->changeHostPort(host, port);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned GenericAPICore::submitRequest(GenericRequest *req, GenericResponse *res)
|
||||
{
|
||||
++m_outCount;
|
||||
if(m_currTrack == 0)
|
||||
{
|
||||
m_currTrack++;
|
||||
}
|
||||
req->setTrack(m_currTrack);
|
||||
res->setTrack(m_currTrack);
|
||||
time_t timeout = time(NULL) + m_requestTimeout;
|
||||
|
||||
req->setTimeout(timeout);
|
||||
res->setTimeout(timeout);
|
||||
|
||||
m_outboundQueue.push(pair<GenericRequest *, GenericResponse *>(req, res));
|
||||
return(m_currTrack++);
|
||||
}
|
||||
|
||||
unsigned GenericAPICore::submitRequest(GenericRequest *req, GenericResponse *res, unsigned reqTimeout)
|
||||
{
|
||||
++m_outCount;
|
||||
if(m_currTrack == 0)
|
||||
{
|
||||
m_currTrack++;
|
||||
}
|
||||
req->setTrack(m_currTrack);
|
||||
res->setTrack(m_currTrack);
|
||||
time_t timeout = time(NULL) + reqTimeout;
|
||||
|
||||
req->setTimeout(timeout);
|
||||
res->setTimeout(timeout);
|
||||
|
||||
m_outboundQueue.push(pair<GenericRequest *, GenericResponse *>(req, res));
|
||||
return(m_currTrack++);
|
||||
}
|
||||
|
||||
void GenericAPICore::process()
|
||||
{
|
||||
GenericRequest *req;
|
||||
GenericResponse *res;
|
||||
|
||||
// Process timeout on pending requests - regardless of whether processing is suspended or not
|
||||
while((m_outCount > 0) && ((req = m_outboundQueue.front().first)->getTimeout() <= time(NULL)))
|
||||
{
|
||||
--m_outCount;
|
||||
res = m_outboundQueue.front().second;
|
||||
m_outboundQueue.pop();
|
||||
|
||||
responseCallback(res);
|
||||
delete res;
|
||||
delete req;
|
||||
}
|
||||
|
||||
// Process timeout on pending responses
|
||||
while((m_pendingCount > 0) && ((res = (*m_pending.begin()).second)->getTimeout() <= time(NULL)))
|
||||
{
|
||||
--m_pendingCount;
|
||||
m_pending.erase(m_pending.begin());
|
||||
responseCallback(res);
|
||||
delete res;
|
||||
}
|
||||
|
||||
if (!m_suspended)
|
||||
{
|
||||
while(m_outCount > 0)
|
||||
{
|
||||
GenericConnection *con = getNextActiveConnection();
|
||||
if (con != NULL)
|
||||
{
|
||||
pair<GenericRequest *, GenericResponse *> out_pair = m_outboundQueue.front();
|
||||
|
||||
req = out_pair.first;
|
||||
res = out_pair.second;
|
||||
if (!req->isInputValid())
|
||||
{
|
||||
res->setResult(req->getInputError());
|
||||
--m_outCount;
|
||||
m_outboundQueue.pop();
|
||||
|
||||
responseCallback(res);
|
||||
delete res;
|
||||
delete req;
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef USE_SERIALIZE_LIB
|
||||
const unsigned char *msgBuf = NULL;
|
||||
unsigned msgSize = 0;
|
||||
msgBuf = req->pack(msgSize);
|
||||
con->Send(msgBuf, msgSize);
|
||||
#else
|
||||
Base::ByteStream msg;
|
||||
req->pack(msg);
|
||||
con->Send(msg);
|
||||
#endif
|
||||
m_pending.insert(pair<unsigned, GenericResponse *>(res->getTrack(), res));
|
||||
--m_outCount;
|
||||
++m_pendingCount;
|
||||
m_outboundQueue.pop();
|
||||
delete req;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//no active connections
|
||||
break; //from while loop
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
for (std::vector<GenericConnection *>::iterator conIter = m_serverConnections.begin(); conIter != m_serverConnections.end(); conIter++)
|
||||
{
|
||||
GenericConnection *con = *conIter;
|
||||
con->process();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
GenericConnection *GenericAPICore::getNextActiveConnection()
|
||||
{
|
||||
unsigned startIndex = m_nextConnectionIndex;
|
||||
unsigned maxIndex = m_serverConnections.size() - 1;
|
||||
|
||||
GenericConnection *con = NULL;
|
||||
|
||||
//loop until we find an active connection, or until we get back
|
||||
// to where we started
|
||||
do
|
||||
{
|
||||
if (m_serverConnections[m_nextConnectionIndex]->isConnected())
|
||||
{
|
||||
con = m_serverConnections[m_nextConnectionIndex];
|
||||
if (m_nextConnectionIndex == maxIndex)
|
||||
m_nextConnectionIndex = 0;
|
||||
else
|
||||
m_nextConnectionIndex++;
|
||||
|
||||
}
|
||||
else if (++m_nextConnectionIndex > maxIndex)
|
||||
{
|
||||
//went past end of vector, start back at 0
|
||||
m_nextConnectionIndex = 0;
|
||||
}
|
||||
}while (con == NULL && m_nextConnectionIndex != startIndex);
|
||||
|
||||
return con;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
#ifdef EXTERNAL_DISTRO
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,102 @@
|
||||
#if !defined (GENERICAPICORE_H_)
|
||||
#define GENERICAPICORE_H_
|
||||
|
||||
#pragma warning (disable: 4786)
|
||||
|
||||
#include <map>
|
||||
#include <queue>
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#include <Base/Archive.h>
|
||||
|
||||
#ifdef EXTERNAL_DISTRO
|
||||
namespace NAMESPACE
|
||||
{
|
||||
|
||||
#endif
|
||||
namespace GenericAPI
|
||||
{
|
||||
|
||||
class GenericRequest;
|
||||
class GenericResponse;
|
||||
class GenericConnection;
|
||||
class GenericAPI;
|
||||
|
||||
class GenericAPICore
|
||||
{
|
||||
public:
|
||||
friend class GenericConnection;
|
||||
friend class GenericAPI;
|
||||
|
||||
GenericAPICore::GenericAPICore(const char *host,
|
||||
short port,
|
||||
unsigned reqTimeout,
|
||||
unsigned reconnectTimeout,
|
||||
unsigned noDataTimeoutSecs = 5,
|
||||
unsigned noAckTimeoutSecs = 5,
|
||||
unsigned incomingBufSizeInKB = 32,
|
||||
unsigned outgoingBufSizeInKB = 32,
|
||||
unsigned keepAlive = 1,
|
||||
unsigned maxRecvMessageSizeInKB = 0);
|
||||
|
||||
/**
|
||||
* NOTE: arraySize must be actual size of host and port arrays.
|
||||
* ALSO: cannot specify a 0 array size.
|
||||
*/
|
||||
GenericAPICore(const char *hosts[],
|
||||
const short port[],
|
||||
unsigned arraySize,
|
||||
unsigned reqTimeout,
|
||||
unsigned reconnectTimeout,
|
||||
unsigned noDataTimeoutSecs = 5,
|
||||
unsigned noAckTimeoutSecs = 5,
|
||||
unsigned incomingBufSizeInKB = 32,
|
||||
unsigned outgoingBufSizeInKB = 32,
|
||||
unsigned keepAlive = 1,
|
||||
unsigned maxRecvMessageSizeInKB = 0);
|
||||
|
||||
virtual ~GenericAPICore();
|
||||
|
||||
void process();
|
||||
#ifdef USE_SERIALIZE_LIB
|
||||
virtual void responseCallback(short type, const unsigned char *data, unsigned dataLen) = 0;
|
||||
#else
|
||||
virtual void responseCallback(short type, Base::ByteStream::ReadIterator &iter) = 0;
|
||||
#endif
|
||||
virtual void responseCallback(GenericResponse *R) = 0;
|
||||
|
||||
virtual void OnDisconnect(const char *host, short port) = 0;
|
||||
virtual void OnConnect(const char *host, short port) = 0;
|
||||
|
||||
void suspendProcessing() { m_suspended = true; }
|
||||
void resumeProcessing() { m_suspended = false; }
|
||||
|
||||
// change the host and port for a connection object, will take effect
|
||||
// on connection's next CON_DISCONNECT state.
|
||||
void changeHostPort(unsigned connectionIndex, const char *host, short port);
|
||||
|
||||
unsigned submitRequest(GenericRequest *req, GenericResponse *res);
|
||||
unsigned submitRequest(GenericRequest *req, GenericResponse *res, unsigned reqTimeout);
|
||||
|
||||
protected:
|
||||
std::vector<GenericConnection *> m_serverConnections;
|
||||
std::map<unsigned, GenericResponse *> m_pending;
|
||||
std::queue<std::pair<GenericRequest *, GenericResponse *> > m_outboundQueue;
|
||||
unsigned m_currTrack;
|
||||
time_t m_reconnectTimeout;
|
||||
unsigned m_outCount;
|
||||
unsigned m_pendingCount;
|
||||
unsigned m_requestTimeout;
|
||||
private:
|
||||
bool m_suspended;
|
||||
unsigned m_nextConnectionIndex;
|
||||
|
||||
GenericConnection *getNextActiveConnection();
|
||||
};
|
||||
|
||||
};
|
||||
#ifdef EXTERNAL_DISTRO
|
||||
};
|
||||
#endif
|
||||
#endif
|
||||
+298
@@ -0,0 +1,298 @@
|
||||
#include "GenericAPI/GenericApiCore.h"
|
||||
#include "GenericAPI/GenericConnection.h"
|
||||
#include "GenericAPI/GenericMessage.h"
|
||||
|
||||
|
||||
using namespace UdpLibrary;
|
||||
|
||||
#ifdef USE_SERIALIZE_LIB
|
||||
#include <Base/serialize.h>
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef EXTERNAL_DISTRO
|
||||
namespace NAMESPACE
|
||||
{
|
||||
|
||||
|
||||
#endif
|
||||
namespace GenericAPI
|
||||
{
|
||||
|
||||
|
||||
using namespace std;
|
||||
using namespace Base;
|
||||
|
||||
unsigned GenericConnection::ms_crcBytes = 0;
|
||||
|
||||
GenericConnection::GenericConnection(const char *host, short port, GenericAPICore *apiCore, unsigned reconnectTimeout, unsigned noDataTimeoutSecs, unsigned noAckTimeoutSecs, unsigned incomingBufSizeInKB, unsigned outgoingBufSizeInKB, unsigned keepAlive, unsigned maxRecvMessageSizeInKB, unsigned holdTime)
|
||||
: m_bConnected(false),
|
||||
m_apiCore(apiCore),
|
||||
m_con(NULL),
|
||||
m_host(host),
|
||||
m_nextHost(host),
|
||||
m_port(port),
|
||||
m_nextPort(port),
|
||||
m_lastTrack(123455), //random choice != 1
|
||||
m_conState(CON_DISCONNECT),
|
||||
m_reconnectTimeout(reconnectTimeout)
|
||||
{
|
||||
#ifdef USE_TCP_LIBRARY
|
||||
TcpManager::TcpParams params;
|
||||
|
||||
params.incomingBufferSize = incomingBufSizeInKB * 1024;
|
||||
params.outgoingBufferSize = outgoingBufSizeInKB * 1024;
|
||||
params.maxConnections = 1;
|
||||
params.port = 0;
|
||||
params.maxRecvMessageSize = maxRecvMessageSizeInKB*1024;
|
||||
params.keepAliveDelay = keepAlive * 1000;
|
||||
params.noDataTimeout = noDataTimeoutSecs * 1000;
|
||||
//params.oldestUnacknowledgedTimeout = noAckTimeoutSecs * 1000;
|
||||
|
||||
m_manager = new TcpManager(params);
|
||||
#else //default to UDP_LIBRARY
|
||||
UdpManager::Params params;
|
||||
|
||||
params.keepAliveDelay = keepAlive * 1000;
|
||||
params.maxDataHoldTime = holdTime;
|
||||
params.incomingBufferSize = incomingBufSizeInKB * 1024;
|
||||
params.outgoingBufferSize = outgoingBufSizeInKB * 1024;
|
||||
params.maxConnections = 1;
|
||||
params.port = 0;
|
||||
params.noDataTimeout = noDataTimeoutSecs * 1000;
|
||||
params.oldestUnacknowledgedTimeout = noAckTimeoutSecs * 1000;
|
||||
params.crcBytes = ms_crcBytes;
|
||||
|
||||
m_manager = new UdpManager(¶ms);
|
||||
#endif //USE_TCP_LIBRARY
|
||||
|
||||
}
|
||||
|
||||
GenericConnection::~GenericConnection()
|
||||
{
|
||||
if(m_con)
|
||||
{
|
||||
m_con->SetHandler(NULL);
|
||||
m_con->Disconnect();//don't worry about onterminated being called, we've set it's handler to null, so it wont
|
||||
m_con->Release();
|
||||
}
|
||||
|
||||
m_manager->Release();
|
||||
}
|
||||
|
||||
void GenericConnection::changeHostPort(const char *host, short port)
|
||||
{
|
||||
if (host &&
|
||||
strcmp(host, "") != 0)
|
||||
{
|
||||
m_nextHost = host;
|
||||
m_nextPort = port;
|
||||
}
|
||||
}
|
||||
|
||||
void GenericConnection::disconnect()
|
||||
{
|
||||
if (m_con)
|
||||
{
|
||||
m_con->Disconnect();
|
||||
//no need to release, since callback to onTerminated releases it, and callback is allways made m_con->Release();
|
||||
m_con = NULL;
|
||||
}
|
||||
m_conState = CON_DISCONNECT;
|
||||
m_bConnected = false;
|
||||
}
|
||||
|
||||
#ifdef USE_TCP_LIBRARY
|
||||
void GenericConnection::OnTerminated(TcpConnection *con)
|
||||
#else //default to UDP_LIBRARY
|
||||
void GenericConnection::OnTerminated(UdpConnection *con)
|
||||
#endif
|
||||
{
|
||||
m_apiCore->OnDisconnect(m_host.c_str(), m_port);
|
||||
if(m_con)
|
||||
{
|
||||
m_con->Release();
|
||||
m_con = NULL;
|
||||
}
|
||||
m_conState = CON_DISCONNECT;
|
||||
m_bConnected = false;
|
||||
}
|
||||
|
||||
#ifdef USE_TCP_LIBRARY
|
||||
void GenericConnection::OnRoutePacket(TcpConnection *con, const unsigned char *data, int dataLen)
|
||||
#else //default to UDP_LIBRARY
|
||||
void GenericConnection::OnRoutePacket(UdpConnection *con, const unsigned char *data, int dataLen)
|
||||
#endif
|
||||
{
|
||||
short type;
|
||||
unsigned track;
|
||||
|
||||
|
||||
#ifdef USE_SERIALIZE_LIB
|
||||
unsigned bytes = 0;
|
||||
unsigned fieldLen = soe::Read(data, dataLen, type);
|
||||
if (fieldLen == 0)
|
||||
return;//invalid message
|
||||
bytes += fieldLen;
|
||||
|
||||
fieldLen = soe::Read(data+bytes, dataLen-bytes, track);
|
||||
if (fieldLen == 0)
|
||||
return;//invalid message
|
||||
bytes += fieldLen;
|
||||
#else
|
||||
ByteStream msg(data, dataLen);
|
||||
ByteStream::ReadIterator iter = msg.begin();
|
||||
|
||||
get(iter, type);
|
||||
get(iter, track);
|
||||
#endif
|
||||
GenericResponse *res = NULL;
|
||||
|
||||
// the following if block is a temporary fix that prevents
|
||||
// a crash with a game team in which they occasionally find
|
||||
// themselves receiving a dupe track in consecutive calls to
|
||||
// OnRoutePacket (which then leads to a callback being called
|
||||
// twice and data being invalid on the second call -> crash!).
|
||||
if (track != 0 &&
|
||||
track == m_lastTrack)
|
||||
{
|
||||
printf("!!! ERROR !!! Got a duplicate track ID %u\n", track);
|
||||
return;
|
||||
}
|
||||
m_lastTrack = track;
|
||||
|
||||
// end temporary fix.
|
||||
|
||||
if(track == 0)
|
||||
{
|
||||
#ifdef USE_SERIALIZE_LIB
|
||||
m_apiCore->responseCallback(type, data+bytes, dataLen-bytes);
|
||||
#else
|
||||
m_apiCore->responseCallback(type, iter);
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
map<unsigned, GenericResponse *>::iterator mapIter = m_apiCore->m_pending.find(track);
|
||||
|
||||
if(mapIter != m_apiCore->m_pending.end())
|
||||
{
|
||||
res = (*mapIter).second;
|
||||
#ifdef USE_SERIALIZE_LIB
|
||||
res->unpack(data, dataLen);
|
||||
#else
|
||||
iter = msg.begin();
|
||||
res->unpack(iter);
|
||||
#endif
|
||||
m_apiCore->m_pending.erase(mapIter);
|
||||
m_apiCore->m_pendingCount--;
|
||||
m_apiCore->responseCallback(res);
|
||||
delete res;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GenericConnection::process(bool giveTime)
|
||||
{
|
||||
switch(m_conState)
|
||||
{
|
||||
case CON_DISCONNECT:
|
||||
// if host/port was changed, it takes effect here
|
||||
m_host = m_nextHost;
|
||||
m_port = m_nextPort;
|
||||
|
||||
// create connection object, attempting to connect and
|
||||
// checking for connection in next state, CON_NEGOTIATE
|
||||
m_con = m_manager->EstablishConnection(m_host.c_str(), m_port);
|
||||
if(m_con)
|
||||
{
|
||||
m_con->SetHandler(this);
|
||||
m_conState = CON_NEGOTIATE;
|
||||
m_conTimeout = time(NULL) + m_reconnectTimeout;
|
||||
}
|
||||
break;
|
||||
case CON_NEGOTIATE:
|
||||
// check for connection
|
||||
|
||||
#ifdef USE_TCP_LIBRARY
|
||||
if(m_con->GetStatus() == TcpConnection::StatusConnected)
|
||||
#else //default to UDP_LIBRARY
|
||||
if(m_con->GetStatus() == UdpConnection::cStatusConnected)
|
||||
#endif
|
||||
{
|
||||
// we're connected
|
||||
m_conState = CON_CONNECT;
|
||||
m_apiCore->OnConnect(m_host.c_str(), m_port);
|
||||
m_bConnected = true;
|
||||
}
|
||||
else if(time(NULL) > m_conTimeout)
|
||||
{
|
||||
// we did not connect
|
||||
m_con->Disconnect();
|
||||
//no need to release, since callback to onTerminated releases it, and callback is allways made m_con->Release();
|
||||
m_con = NULL;
|
||||
m_conState = CON_DISCONNECT;
|
||||
m_bConnected = false;
|
||||
}
|
||||
break;
|
||||
case CON_CONNECT:
|
||||
// do nothing
|
||||
break;
|
||||
default:
|
||||
// this should not occur, but we revert to CON_DISCONNECT if it does
|
||||
m_conState = CON_DISCONNECT;
|
||||
m_bConnected = false;
|
||||
if (m_con)
|
||||
{
|
||||
m_con->Disconnect();
|
||||
//no need to release, since callback to onTerminated releases it, and callback is allways made m_con->Release();
|
||||
m_con = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (giveTime)
|
||||
{
|
||||
m_manager->GiveTime();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#ifdef USE_SERIALIZE_LIB
|
||||
void GenericConnection::Send(const unsigned char *data, int dataLen)
|
||||
{
|
||||
#ifdef USE_TCP_LIBRARY
|
||||
if(m_con && m_con->GetStatus() == TcpConnection::StatusConnected)
|
||||
{
|
||||
m_con->Send((const char *)data, dataLen);
|
||||
}
|
||||
#else//USE_TCP_LIBRARY
|
||||
if(m_con && m_con->GetStatus() == UdpConnection::cStatusConnected)
|
||||
{
|
||||
m_con->Send(cUdpChannelReliable1, data, dataLen);
|
||||
}
|
||||
#endif//USE_TCP_LIBRARY
|
||||
}
|
||||
#else //USE_SERIALIZE_LIB
|
||||
void GenericConnection::Send(Base::ByteStream &msg)
|
||||
{
|
||||
#ifdef USE_TCP_LIBRARY
|
||||
if(m_con && m_con->GetStatus() == TcpConnection::StatusConnected)
|
||||
{
|
||||
m_con->Send((const char *)msg.getBuffer(), msg.getSize());
|
||||
}
|
||||
#else //USE_TCP_LIBRARY
|
||||
if(m_con && m_con->GetStatus() == UdpConnection::cStatusConnected)
|
||||
{
|
||||
m_con->Send(cUdpChannelReliable1, msg.getBuffer(), msg.getSize());
|
||||
}
|
||||
#endif//USE_TCP_LIBRARY
|
||||
}
|
||||
#endif //USE_SERIALIZE_LIB
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
#ifdef EXTERNAL_DISTRO
|
||||
};
|
||||
#endif
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
#if !defined (GENERICCONNECTION_H_)
|
||||
#define GENERICCONNECTION_H_
|
||||
|
||||
#include <Base/Archive.h>
|
||||
|
||||
#ifdef USE_TCP_LIBRARY
|
||||
#include <TcpLibrary/TcpManager.h>
|
||||
#include <TcpLibrary/TcpConnection.h>
|
||||
#include <TcpLibrary/TcpHandlers.h>
|
||||
#else //default to UDP_LIBRARY
|
||||
#include <UdpLibrary/UdpLibrary.h>
|
||||
#include <UdpLibrary/UdpHandler.h>
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef EXTERNAL_DISTRO
|
||||
namespace NAMESPACE
|
||||
{
|
||||
|
||||
#endif
|
||||
namespace GenericAPI
|
||||
{
|
||||
|
||||
enum eConState
|
||||
{
|
||||
CON_DISCONNECT,
|
||||
CON_NEGOTIATE,
|
||||
CON_CONNECT
|
||||
};
|
||||
|
||||
#ifdef USE_TCP_LIBRARY
|
||||
class GenericConnection : public TcpConnectionHandler
|
||||
#else //default to UDP_LIBRARY
|
||||
class GenericConnection : public UdpLibrary::UdpConnectionHandler
|
||||
#endif
|
||||
{
|
||||
public:
|
||||
GenericConnection(const char *host,
|
||||
short port,
|
||||
GenericAPICore *apiCore,
|
||||
unsigned reconnectTimeout,
|
||||
unsigned noDataTimeoutSecs = 5,
|
||||
unsigned noAckTimeoutSecs = 5,
|
||||
unsigned incomingBufSizeInKB = 32,
|
||||
unsigned outgoingBufSizeInKB = 32,
|
||||
unsigned keepAlive = 1,
|
||||
unsigned maxRecvMessageSizeInKB = 0,
|
||||
unsigned holdTime = 0);
|
||||
|
||||
virtual ~GenericConnection();
|
||||
|
||||
#ifdef USE_TCP_LIBRARY
|
||||
virtual void OnRoutePacket(TcpConnection *con, const unsigned char *data, int dataLen);
|
||||
virtual void OnTerminated(TcpConnection *con);
|
||||
#else //default to UDP_LIBRARY
|
||||
virtual void OnRoutePacket(UdpLibrary::UdpConnection *con, const unsigned char *data, int dataLen);
|
||||
virtual void OnTerminated(UdpLibrary::UdpConnection *con);
|
||||
#endif
|
||||
#ifdef USE_SERIALIZE_LIB
|
||||
void Send(const unsigned char *data, int dataLen);
|
||||
#else
|
||||
void Send(Base::ByteStream &msg);
|
||||
#endif
|
||||
|
||||
|
||||
void changeHostPort(const char *host, short port);
|
||||
bool isConnected() { return m_bConnected; }
|
||||
void disconnect();
|
||||
void process(bool giveTime = true);
|
||||
|
||||
static unsigned ms_crcBytes;
|
||||
private:
|
||||
bool m_bConnected;
|
||||
GenericAPICore *m_apiCore;
|
||||
#ifdef USE_TCP_LIBRARY
|
||||
TcpManager *m_manager;
|
||||
TcpConnection *m_con;
|
||||
#else //default to UDP_LIBRARY
|
||||
UdpLibrary::UdpManager *m_manager;
|
||||
UdpLibrary::UdpConnection *m_con;
|
||||
#endif
|
||||
std::string m_host;
|
||||
std::string m_nextHost;
|
||||
short m_port;
|
||||
short m_nextPort;
|
||||
|
||||
unsigned m_lastTrack;
|
||||
|
||||
eConState m_conState;
|
||||
time_t m_conTimeout;
|
||||
unsigned m_reconnectTimeout;
|
||||
};
|
||||
|
||||
};
|
||||
#ifdef EXTERNAL_DISTRO
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,32 @@
|
||||
#include "GenericMessage.h"
|
||||
|
||||
#ifdef EXTERNAL_DISTRO
|
||||
namespace NAMESPACE
|
||||
{
|
||||
|
||||
#endif
|
||||
namespace GenericAPI
|
||||
{
|
||||
|
||||
GenericMessage::GenericMessage(short type)
|
||||
: m_type(type)
|
||||
{
|
||||
}
|
||||
|
||||
GenericRequest::GenericRequest(short type)
|
||||
: GenericMessage(type)
|
||||
{
|
||||
}
|
||||
|
||||
GenericResponse::GenericResponse(short type, unsigned result, void *user)
|
||||
: GenericMessage(type),
|
||||
m_result(result),
|
||||
m_user(user)
|
||||
{
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#ifdef EXTERNAL_DISTRO
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,84 @@
|
||||
#if !defined (GENERICMESSAGE_H_)
|
||||
#define GENERICMESSAGE_H_
|
||||
|
||||
#include <time.h>
|
||||
#include <Base/Archive.h>
|
||||
|
||||
#ifdef EXTERNAL_DISTRO
|
||||
namespace NAMESPACE
|
||||
{
|
||||
|
||||
#endif
|
||||
namespace GenericAPI
|
||||
{
|
||||
|
||||
// Basic request message, implements a type, and a timeout mechanism
|
||||
class GenericMessage
|
||||
{
|
||||
public:
|
||||
GenericMessage(short type);
|
||||
virtual ~GenericMessage() {};
|
||||
|
||||
short getType() const { return m_type; }
|
||||
protected:
|
||||
short m_type;
|
||||
};
|
||||
|
||||
class GenericRequest : public GenericMessage
|
||||
{
|
||||
public:
|
||||
GenericRequest(short type);
|
||||
virtual ~GenericRequest() {};
|
||||
#ifdef USE_SERIALIZE_LIB
|
||||
virtual const unsigned char *pack(unsigned &msgSize) = 0;
|
||||
#else
|
||||
virtual void pack(Base::ByteStream &msg) = 0;
|
||||
#endif
|
||||
|
||||
const time_t &getTimeout() const { return m_timeout; }
|
||||
void setTimeout(time_t t) { m_timeout = t; }
|
||||
void setTrack(unsigned t) { m_track = t; }
|
||||
unsigned getTrack() const { return m_track; }
|
||||
|
||||
virtual bool isInputValid() const { return true; }
|
||||
virtual unsigned getInputError() const { return 0; }
|
||||
|
||||
protected:
|
||||
unsigned m_track;
|
||||
time_t m_timeout;
|
||||
};
|
||||
|
||||
// Basic response message from server. In the case that this response to a request
|
||||
// submitted from this API, the response would have been generated at request submission
|
||||
// time, and the timeout value filled in appropriatly
|
||||
class GenericResponse : public GenericMessage
|
||||
{
|
||||
public:
|
||||
GenericResponse(short type, unsigned result, void *user);
|
||||
virtual ~GenericResponse() {};
|
||||
#ifdef USE_SERIALIZE_LIB
|
||||
virtual void unpack(const unsigned char *data, unsigned dataLen) = 0;
|
||||
#else
|
||||
virtual void unpack(Base::ByteStream::ReadIterator &iter) = 0;
|
||||
#endif
|
||||
|
||||
time_t getTimeout() const { return m_timeout; }
|
||||
void setTimeout(time_t t) { m_timeout = t; }
|
||||
void setTrack(unsigned t) { m_track = t; }
|
||||
unsigned getTrack() const { return m_track; }
|
||||
unsigned getResult() const { return m_result; }
|
||||
void setResult(unsigned res) { m_result = res; }
|
||||
void *getUser() const { return m_user; }
|
||||
protected:
|
||||
unsigned m_track;
|
||||
unsigned m_result;
|
||||
void *m_user;
|
||||
time_t m_timeout;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#ifdef EXTERNAL_DISTRO
|
||||
};
|
||||
#endif
|
||||
#endif
|
||||
Reference in New Issue
Block a user