|
|
@ -46,6 +46,8 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com |
|
|
|
***/ |
|
|
|
***/ |
|
|
|
|
|
|
|
|
|
|
|
#include "Pch.h" |
|
|
|
#include "Pch.h" |
|
|
|
|
|
|
|
#include "hsThread.h" |
|
|
|
|
|
|
|
#include <list> |
|
|
|
#pragma hdrstop |
|
|
|
#pragma hdrstop |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -60,34 +62,33 @@ namespace pnNetCli { |
|
|
|
struct ChannelCrit { |
|
|
|
struct ChannelCrit { |
|
|
|
~ChannelCrit (); |
|
|
|
~ChannelCrit (); |
|
|
|
ChannelCrit (); |
|
|
|
ChannelCrit (); |
|
|
|
inline void Enter () { m_critsect.Enter(); } |
|
|
|
inline void Enter () { m_critsect.Lock(); } |
|
|
|
inline void Leave () { m_critsect.Leave(); } |
|
|
|
inline void Leave () { m_critsect.Unlock(); } |
|
|
|
inline void EnterSafe () { if (m_init) m_critsect.Enter(); } |
|
|
|
inline void EnterSafe () { if (m_init) m_critsect.Lock(); } |
|
|
|
inline void LeaveSafe () { if (m_init) m_critsect.Leave(); } |
|
|
|
inline void LeaveSafe () { if (m_init) m_critsect.Unlock(); } |
|
|
|
|
|
|
|
|
|
|
|
private: |
|
|
|
private: |
|
|
|
bool m_init; |
|
|
|
bool m_init; |
|
|
|
CCritSect m_critsect; |
|
|
|
hsMutex m_critsect; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
struct NetMsgChannel : AtomicRef { |
|
|
|
struct NetMsgChannel : AtomicRef { |
|
|
|
LINK(NetMsgChannel) m_link; |
|
|
|
uint32_t m_protocol; |
|
|
|
unsigned m_protocol; |
|
|
|
|
|
|
|
bool m_server; |
|
|
|
bool m_server; |
|
|
|
|
|
|
|
|
|
|
|
// Message definitions
|
|
|
|
// Message definitions
|
|
|
|
unsigned m_largestRecv; |
|
|
|
uint32_t m_largestRecv; |
|
|
|
ARRAY(NetMsgInitSend) m_sendMsgs; |
|
|
|
ARRAY(NetMsgInitSend) m_sendMsgs; |
|
|
|
ARRAY(NetMsgInitRecv) m_recvMsgs; |
|
|
|
ARRAY(NetMsgInitRecv) m_recvMsgs; |
|
|
|
|
|
|
|
|
|
|
|
// Diffie-Hellman constants
|
|
|
|
// Diffie-Hellman constants
|
|
|
|
unsigned m_dh_g; |
|
|
|
uint32_t m_dh_g; |
|
|
|
BigNum m_dh_xa; // client: dh_x server: dh_a
|
|
|
|
BigNum m_dh_xa; // client: dh_x server: dh_a
|
|
|
|
BigNum m_dh_n; |
|
|
|
BigNum m_dh_n; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
static ChannelCrit s_channelCrit; |
|
|
|
static ChannelCrit s_channelCrit; |
|
|
|
static LIST(NetMsgChannel) * s_channels; |
|
|
|
static std::list<NetMsgChannel*>* s_channels; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
/****************************************************************************
|
|
|
@ -105,8 +106,9 @@ ChannelCrit::ChannelCrit () { |
|
|
|
ChannelCrit::~ChannelCrit () { |
|
|
|
ChannelCrit::~ChannelCrit () { |
|
|
|
EnterSafe(); |
|
|
|
EnterSafe(); |
|
|
|
if (s_channels) { |
|
|
|
if (s_channels) { |
|
|
|
while (NetMsgChannel * const channel = s_channels->Head()) { |
|
|
|
while (s_channels->size()) { |
|
|
|
s_channels->Unlink(channel); |
|
|
|
NetMsgChannel* const channel = s_channels->front(); |
|
|
|
|
|
|
|
s_channels->remove(channel); |
|
|
|
channel->DecRef("ChannelLink"); |
|
|
|
channel->DecRef("ChannelLink"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -239,30 +241,29 @@ static void AddRecvMsgs_CS ( |
|
|
|
// copy the message handler
|
|
|
|
// copy the message handler
|
|
|
|
*dst = *src; |
|
|
|
*dst = *src; |
|
|
|
|
|
|
|
|
|
|
|
const unsigned bytes = ValidateMsg(dst->msg); |
|
|
|
const uint32_t bytes = ValidateMsg(dst->msg); |
|
|
|
channel->m_largestRecv = max(channel->m_largestRecv, bytes); |
|
|
|
channel->m_largestRecv = max(channel->m_largestRecv, bytes); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//===========================================================================
|
|
|
|
static NetMsgChannel * FindChannel_CS (unsigned protocol, bool server) { |
|
|
|
static NetMsgChannel* FindChannel_CS (uint32_t protocol, bool server) { |
|
|
|
if (!s_channels) |
|
|
|
if (!s_channels) |
|
|
|
return nil; |
|
|
|
return nil; |
|
|
|
|
|
|
|
|
|
|
|
NetMsgChannel * channel = s_channels->Head(); |
|
|
|
std::list<NetMsgChannel*>::iterator it = s_channels->begin(); |
|
|
|
for (; channel; channel = s_channels->Next(channel)) { |
|
|
|
for (; it != s_channels->end(); ++it) { |
|
|
|
if ((channel->m_protocol == protocol) && (channel->m_server == server)) |
|
|
|
if (((*it)->m_protocol == protocol) && ((*it)->m_server == server)) |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return channel; |
|
|
|
return *it; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//===========================================================================
|
|
|
|
static NetMsgChannel * FindOrCreateChannel_CS (unsigned protocol, bool server) { |
|
|
|
static NetMsgChannel* FindOrCreateChannel_CS (uint32_t protocol, bool server) { |
|
|
|
if (!s_channels) { |
|
|
|
if (!s_channels) { |
|
|
|
s_channels = new LIST(NetMsgChannel); |
|
|
|
s_channels = new std::list<NetMsgChannel*>(); |
|
|
|
s_channels->SetLinkOffset(offsetof(NetMsgChannel, m_link)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// find or create protocol
|
|
|
|
// find or create protocol
|
|
|
@ -273,7 +274,7 @@ static NetMsgChannel * FindOrCreateChannel_CS (unsigned protocol, bool server) { |
|
|
|
channel->m_server = server; |
|
|
|
channel->m_server = server; |
|
|
|
channel->m_largestRecv = 0; |
|
|
|
channel->m_largestRecv = 0; |
|
|
|
|
|
|
|
|
|
|
|
s_channels->Link(channel); |
|
|
|
s_channels->push_back(channel); |
|
|
|
channel->IncRef("ChannelLink"); |
|
|
|
channel->IncRef("ChannelLink"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -291,7 +292,7 @@ static NetMsgChannel * FindOrCreateChannel_CS (unsigned protocol, bool server) { |
|
|
|
NetMsgChannel * NetMsgChannelLock ( |
|
|
|
NetMsgChannel * NetMsgChannelLock ( |
|
|
|
unsigned protocol, |
|
|
|
unsigned protocol, |
|
|
|
bool server, |
|
|
|
bool server, |
|
|
|
unsigned * largestRecv |
|
|
|
uint32_t * largestRecv |
|
|
|
) { |
|
|
|
) { |
|
|
|
NetMsgChannel * channel; |
|
|
|
NetMsgChannel * channel; |
|
|
|
s_channelCrit.Enter(); |
|
|
|
s_channelCrit.Enter(); |
|
|
@ -353,7 +354,7 @@ const NetMsgInitSend * NetMsgChannelFindSendMessage ( |
|
|
|
//============================================================================
|
|
|
|
//============================================================================
|
|
|
|
void NetMsgChannelGetDhConstants ( |
|
|
|
void NetMsgChannelGetDhConstants ( |
|
|
|
const NetMsgChannel * channel, |
|
|
|
const NetMsgChannel * channel, |
|
|
|
unsigned * dh_g, |
|
|
|
uint32_t * dh_g, |
|
|
|
const BigNum ** dh_xa, |
|
|
|
const BigNum ** dh_xa, |
|
|
|
const BigNum ** dh_n |
|
|
|
const BigNum ** dh_n |
|
|
|
) { |
|
|
|
) { |
|
|
@ -374,13 +375,13 @@ void NetMsgChannelGetDhConstants ( |
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//===========================================================================
|
|
|
|
void NetMsgProtocolRegister ( |
|
|
|
void NetMsgProtocolRegister ( |
|
|
|
unsigned protocol, |
|
|
|
uint32_t protocol, |
|
|
|
bool server, |
|
|
|
bool server, |
|
|
|
const NetMsgInitSend sendMsgs[], |
|
|
|
const NetMsgInitSend sendMsgs[], |
|
|
|
unsigned sendMsgCount, |
|
|
|
uint32_t sendMsgCount, |
|
|
|
const NetMsgInitRecv recvMsgs[], |
|
|
|
const NetMsgInitRecv recvMsgs[], |
|
|
|
unsigned recvMsgCount, |
|
|
|
uint32_t recvMsgCount, |
|
|
|
unsigned dh_g, |
|
|
|
uint32_t dh_g, |
|
|
|
const BigNum & dh_xa, // client: dh_x server: dh_a
|
|
|
|
const BigNum & dh_xa, // client: dh_x server: dh_a
|
|
|
|
const BigNum & dh_n |
|
|
|
const BigNum & dh_n |
|
|
|
) { |
|
|
|
) { |
|
|
@ -406,10 +407,10 @@ void NetMsgProtocolRegister ( |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//===========================================================================
|
|
|
|
void NetMsgProtocolDestroy (unsigned protocol, bool server) { |
|
|
|
void NetMsgProtocolDestroy (uint32_t protocol, bool server) { |
|
|
|
s_channelCrit.EnterSafe(); |
|
|
|
s_channelCrit.EnterSafe(); |
|
|
|
if (NetMsgChannel* channel = FindChannel_CS(protocol, server)) { |
|
|
|
if (NetMsgChannel* channel = FindChannel_CS(protocol, server)) { |
|
|
|
s_channels->Unlink(channel); |
|
|
|
s_channels->remove(channel); |
|
|
|
channel->DecRef("ChannelLink"); |
|
|
|
channel->DecRef("ChannelLink"); |
|
|
|
} |
|
|
|
} |
|
|
|
s_channelCrit.LeaveSafe(); |
|
|
|
s_channelCrit.LeaveSafe(); |
|
|
|