|
|
@ -45,9 +45,11 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com |
|
|
|
*
|
|
|
|
*
|
|
|
|
***/ |
|
|
|
***/ |
|
|
|
|
|
|
|
|
|
|
|
#include "Pch.h" |
|
|
|
#include "pnSimpleNet.h" |
|
|
|
#pragma hdrstop |
|
|
|
#include "hsThread.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <list> |
|
|
|
|
|
|
|
#include <map> |
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
/*****************************************************************************
|
|
|
|
* |
|
|
|
* |
|
|
@ -56,35 +58,25 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com |
|
|
|
***/ |
|
|
|
***/ |
|
|
|
|
|
|
|
|
|
|
|
struct SimpleNetConn : AtomicRef { |
|
|
|
struct SimpleNetConn : AtomicRef { |
|
|
|
LINK(SimpleNetConn) link; |
|
|
|
|
|
|
|
AsyncSocket sock; |
|
|
|
AsyncSocket sock; |
|
|
|
AsyncCancelId cancelId; |
|
|
|
AsyncCancelId cancelId; |
|
|
|
unsigned channelId; |
|
|
|
uint32_t channelId; |
|
|
|
bool abandoned; |
|
|
|
bool abandoned; |
|
|
|
struct ConnectParam * connectParam; |
|
|
|
struct ConnectParam * connectParam; |
|
|
|
|
|
|
|
|
|
|
|
SimpleNet_MsgHeader * oversizeMsg; |
|
|
|
SimpleNet_MsgHeader * oversizeMsg; |
|
|
|
ARRAY(uint8_t) oversizeBuffer; |
|
|
|
ARRAY(uint8_t) oversizeBuffer; |
|
|
|
|
|
|
|
|
|
|
|
~SimpleNetConn () { |
|
|
|
|
|
|
|
ASSERT(!link.IsLinked()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
struct SimpleNetChannel : AtomicRef, THashKeyVal<unsigned> { |
|
|
|
struct SimpleNetChannel : AtomicRef { |
|
|
|
HASHLINK(SimpleNetChannel) link; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FSimpleNetOnMsg onMsg; |
|
|
|
FSimpleNetOnMsg onMsg; |
|
|
|
FSimpleNetOnError onError; |
|
|
|
FSimpleNetOnError onError; |
|
|
|
|
|
|
|
uint32_t channelId; |
|
|
|
LISTDECL(SimpleNetConn, link) conns; |
|
|
|
std::list<SimpleNetConn*> conns; |
|
|
|
|
|
|
|
|
|
|
|
SimpleNetChannel (unsigned channel) |
|
|
|
SimpleNetChannel (uint32_t channel) : channelId(channel) { } |
|
|
|
: THashKeyVal<unsigned>(channel) |
|
|
|
|
|
|
|
{ } |
|
|
|
|
|
|
|
~SimpleNetChannel () { |
|
|
|
~SimpleNetChannel () { |
|
|
|
ASSERT(!link.IsLinked()); |
|
|
|
ASSERT(!conns.size()); |
|
|
|
ASSERT(!conns.Head()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -92,7 +84,7 @@ struct ConnectParam { |
|
|
|
SimpleNetChannel * channel; |
|
|
|
SimpleNetChannel * channel; |
|
|
|
FSimpleNetOnConnect callback; |
|
|
|
FSimpleNetOnConnect callback; |
|
|
|
void * param; |
|
|
|
void * param; |
|
|
|
|
|
|
|
|
|
|
|
~ConnectParam () { |
|
|
|
~ConnectParam () { |
|
|
|
if (channel) |
|
|
|
if (channel) |
|
|
|
channel->DecRef(); |
|
|
|
channel->DecRef(); |
|
|
@ -107,15 +99,10 @@ struct ConnectParam { |
|
|
|
***/ |
|
|
|
***/ |
|
|
|
|
|
|
|
|
|
|
|
static bool s_running; |
|
|
|
static bool s_running; |
|
|
|
static CCritSect s_critsect; |
|
|
|
static hsMutex s_critsect; |
|
|
|
static FSimpleNetQueryAccept s_queryAccept; |
|
|
|
static FSimpleNetQueryAccept s_queryAccept; |
|
|
|
static void * s_queryAcceptParam; |
|
|
|
static void * s_queryAcceptParam; |
|
|
|
|
|
|
|
static std::map<uint32_t, SimpleNetChannel*> s_channels; |
|
|
|
static HASHTABLEDECL( |
|
|
|
|
|
|
|
SimpleNetChannel, |
|
|
|
|
|
|
|
THashKeyVal<unsigned>, |
|
|
|
|
|
|
|
link |
|
|
|
|
|
|
|
) s_channels; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
/*****************************************************************************
|
|
|
@ -128,13 +115,13 @@ static HASHTABLEDECL( |
|
|
|
static void NotifyConnSocketConnect (SimpleNetConn * conn) { |
|
|
|
static void NotifyConnSocketConnect (SimpleNetConn * conn) { |
|
|
|
|
|
|
|
|
|
|
|
conn->TransferRef("Connecting", "Connected"); |
|
|
|
conn->TransferRef("Connecting", "Connected"); |
|
|
|
|
|
|
|
|
|
|
|
conn->connectParam->callback( |
|
|
|
conn->connectParam->callback( |
|
|
|
conn->connectParam->param, |
|
|
|
conn->connectParam->param, |
|
|
|
conn, |
|
|
|
conn, |
|
|
|
kNetSuccess |
|
|
|
kNetSuccess |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
delete conn->connectParam; |
|
|
|
delete conn->connectParam; |
|
|
|
conn->connectParam = nil; |
|
|
|
conn->connectParam = nil; |
|
|
|
} |
|
|
|
} |
|
|
@ -142,18 +129,21 @@ static void NotifyConnSocketConnect (SimpleNetConn * conn) { |
|
|
|
//============================================================================
|
|
|
|
//============================================================================
|
|
|
|
static void NotifyConnSocketConnectFailed (SimpleNetConn * conn) { |
|
|
|
static void NotifyConnSocketConnectFailed (SimpleNetConn * conn) { |
|
|
|
|
|
|
|
|
|
|
|
s_critsect.Enter(); |
|
|
|
s_critsect.Lock(); |
|
|
|
{ |
|
|
|
{ |
|
|
|
conn->link.Unlink(); |
|
|
|
std::map<uint32_t, SimpleNetChannel*>::iterator it; |
|
|
|
|
|
|
|
if ((it = s_channels.find(conn->channelId)) != s_channels.end()) { |
|
|
|
|
|
|
|
it->second->conns.remove(conn); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
s_critsect.Leave(); |
|
|
|
s_critsect.Unlock(); |
|
|
|
|
|
|
|
|
|
|
|
conn->connectParam->callback( |
|
|
|
conn->connectParam->callback( |
|
|
|
conn->connectParam->param, |
|
|
|
conn->connectParam->param, |
|
|
|
nil, |
|
|
|
nil, |
|
|
|
kNetErrConnectFailed |
|
|
|
kNetErrConnectFailed |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
delete conn->connectParam; |
|
|
|
delete conn->connectParam; |
|
|
|
conn->connectParam = nil; |
|
|
|
conn->connectParam = nil; |
|
|
|
|
|
|
|
|
|
|
@ -165,38 +155,44 @@ static void NotifyConnSocketConnectFailed (SimpleNetConn * conn) { |
|
|
|
static void NotifyConnSocketDisconnect (SimpleNetConn * conn) { |
|
|
|
static void NotifyConnSocketDisconnect (SimpleNetConn * conn) { |
|
|
|
|
|
|
|
|
|
|
|
bool abandoned; |
|
|
|
bool abandoned; |
|
|
|
SimpleNetChannel * channel; |
|
|
|
SimpleNetChannel* channel = nil; |
|
|
|
s_critsect.Enter(); |
|
|
|
s_critsect.Lock(); |
|
|
|
{ |
|
|
|
{ |
|
|
|
abandoned = conn->abandoned; |
|
|
|
abandoned = conn->abandoned; |
|
|
|
if (nil != (channel = s_channels.Find(conn->channelId))) |
|
|
|
std::map<uint32_t, SimpleNetChannel*>::iterator it; |
|
|
|
|
|
|
|
if ((it = s_channels.find(conn->channelId)) != s_channels.end()) { |
|
|
|
|
|
|
|
channel = it->second; |
|
|
|
channel->IncRef(); |
|
|
|
channel->IncRef(); |
|
|
|
conn->link.Unlink(); |
|
|
|
channel->conns.remove(conn); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
s_critsect.Leave(); |
|
|
|
s_critsect.Unlock(); |
|
|
|
|
|
|
|
|
|
|
|
if (channel && !abandoned) { |
|
|
|
if (channel && !abandoned) { |
|
|
|
channel->onError(conn, kNetErrDisconnected); |
|
|
|
channel->onError(conn, kNetErrDisconnected); |
|
|
|
channel->DecRef(); |
|
|
|
channel->DecRef(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
conn->DecRef("Connected"); |
|
|
|
conn->DecRef("Connected"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
//============================================================================
|
|
|
|
static bool NotifyConnSocketRead (SimpleNetConn * conn, AsyncNotifySocketRead * read) { |
|
|
|
static bool NotifyConnSocketRead (SimpleNetConn * conn, AsyncNotifySocketRead * read) { |
|
|
|
|
|
|
|
|
|
|
|
SimpleNetChannel * channel; |
|
|
|
SimpleNetChannel* channel = nil; |
|
|
|
s_critsect.Enter(); |
|
|
|
s_critsect.Lock(); |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (nil != (channel = s_channels.Find(conn->channelId))) |
|
|
|
std::map<uint32_t, SimpleNetChannel*>::iterator it; |
|
|
|
|
|
|
|
if ((it = s_channels.find(conn->channelId)) != s_channels.end()) { |
|
|
|
|
|
|
|
channel = it->second; |
|
|
|
channel->IncRef(); |
|
|
|
channel->IncRef(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
s_critsect.Leave(); |
|
|
|
s_critsect.Unlock(); |
|
|
|
|
|
|
|
|
|
|
|
if (!channel) |
|
|
|
if (!channel) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
bool result = true; |
|
|
|
bool result = true; |
|
|
|
|
|
|
|
|
|
|
|
const uint8_t * curr = read->buffer; |
|
|
|
const uint8_t * curr = read->buffer; |
|
|
@ -208,19 +204,19 @@ static bool NotifyConnSocketRead (SimpleNetConn * conn, AsyncNotifySocketRead * |
|
|
|
unsigned spaceLeft = conn->oversizeMsg->messageBytes - conn->oversizeBuffer.Count(); |
|
|
|
unsigned spaceLeft = conn->oversizeMsg->messageBytes - conn->oversizeBuffer.Count(); |
|
|
|
unsigned copyBytes = min(spaceLeft, term - curr); |
|
|
|
unsigned copyBytes = min(spaceLeft, term - curr); |
|
|
|
conn->oversizeBuffer.Add(curr, copyBytes); |
|
|
|
conn->oversizeBuffer.Add(curr, copyBytes); |
|
|
|
|
|
|
|
|
|
|
|
curr += copyBytes; |
|
|
|
curr += copyBytes; |
|
|
|
|
|
|
|
|
|
|
|
// Wait until we have received the entire message
|
|
|
|
// Wait until we have received the entire message
|
|
|
|
if (copyBytes != spaceLeft) |
|
|
|
if (copyBytes != spaceLeft) |
|
|
|
break; |
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
// Dispatch oversize msg
|
|
|
|
// Dispatch oversize msg
|
|
|
|
if (!channel->onMsg(conn, conn->oversizeMsg)) { |
|
|
|
if (!channel->onMsg(conn, conn->oversizeMsg)) { |
|
|
|
result = false; |
|
|
|
result = false; |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
conn->oversizeBuffer.SetCount(0); |
|
|
|
conn->oversizeBuffer.SetCount(0); |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
@ -231,23 +227,23 @@ static bool NotifyConnSocketRead (SimpleNetConn * conn, AsyncNotifySocketRead * |
|
|
|
|
|
|
|
|
|
|
|
SimpleNet_MsgHeader * msg = (SimpleNet_MsgHeader *) read->buffer; |
|
|
|
SimpleNet_MsgHeader * msg = (SimpleNet_MsgHeader *) read->buffer; |
|
|
|
|
|
|
|
|
|
|
|
// Sanity check message size
|
|
|
|
// Sanity check message size
|
|
|
|
if (msg->messageBytes < sizeof(*msg)) { |
|
|
|
if (msg->messageBytes < sizeof(*msg)) { |
|
|
|
result = false; |
|
|
|
result = false; |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Handle oversized messages
|
|
|
|
// Handle oversized messages
|
|
|
|
if (msg->messageBytes > kAsyncSocketBufferSize) { |
|
|
|
if (msg->messageBytes > kAsyncSocketBufferSize) { |
|
|
|
|
|
|
|
|
|
|
|
conn->oversizeBuffer.SetCount(msg->messageBytes); |
|
|
|
conn->oversizeBuffer.SetCount(msg->messageBytes); |
|
|
|
conn->oversizeMsg = (SimpleNet_MsgHeader *) conn->oversizeBuffer.Ptr(); |
|
|
|
conn->oversizeMsg = (SimpleNet_MsgHeader *) conn->oversizeBuffer.Ptr(); |
|
|
|
*conn->oversizeMsg = *msg;
|
|
|
|
*conn->oversizeMsg = *msg; |
|
|
|
|
|
|
|
|
|
|
|
curr += sizeof(*msg); |
|
|
|
curr += sizeof(*msg); |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Wait until we have received the entire message
|
|
|
|
// Wait until we have received the entire message
|
|
|
|
const uint8_t * msgTerm = (const uint8_t *) curr + msg->messageBytes; |
|
|
|
const uint8_t * msgTerm = (const uint8_t *) curr + msg->messageBytes; |
|
|
|
if (msgTerm > term) |
|
|
|
if (msgTerm > term) |
|
|
@ -263,7 +259,7 @@ static bool NotifyConnSocketRead (SimpleNetConn * conn, AsyncNotifySocketRead * |
|
|
|
|
|
|
|
|
|
|
|
// Return count of bytes we processed
|
|
|
|
// Return count of bytes we processed
|
|
|
|
read->bytesProcessed = curr - read->buffer; |
|
|
|
read->bytesProcessed = curr - read->buffer; |
|
|
|
|
|
|
|
|
|
|
|
channel->DecRef(); |
|
|
|
channel->DecRef(); |
|
|
|
return result; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
@ -286,59 +282,62 @@ static bool AsyncNotifySocketProc ( |
|
|
|
const SimpleNet_ConnData & connect = *(const SimpleNet_ConnData *) listen->buffer; |
|
|
|
const SimpleNet_ConnData & connect = *(const SimpleNet_ConnData *) listen->buffer; |
|
|
|
listen->bytesProcessed += sizeof(connect); |
|
|
|
listen->bytesProcessed += sizeof(connect); |
|
|
|
|
|
|
|
|
|
|
|
SimpleNetChannel * channel; |
|
|
|
SimpleNetChannel* channel = nil; |
|
|
|
s_critsect.Enter(); |
|
|
|
s_critsect.Lock(); |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (nil != (channel = s_channels.Find(connect.channelId))) |
|
|
|
std::map<uint32_t, SimpleNetChannel*>::iterator it; |
|
|
|
|
|
|
|
if ((it = s_channels.find(conn->channelId)) != s_channels.end()) { |
|
|
|
|
|
|
|
channel = it->second; |
|
|
|
channel->IncRef(); |
|
|
|
channel->IncRef(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
s_critsect.Leave(); |
|
|
|
s_critsect.Unlock(); |
|
|
|
|
|
|
|
|
|
|
|
if (!channel) |
|
|
|
if (!channel) |
|
|
|
break; |
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
conn = NEWZERO(SimpleNetConn); |
|
|
|
conn = NEWZERO(SimpleNetConn); |
|
|
|
conn->channelId = channel->GetValue(); |
|
|
|
conn->channelId = channel->channelId; |
|
|
|
conn->IncRef("Lifetime"); |
|
|
|
conn->IncRef("Lifetime"); |
|
|
|
conn->IncRef("Connected"); |
|
|
|
conn->IncRef("Connected"); |
|
|
|
conn->sock = sock; |
|
|
|
conn->sock = sock; |
|
|
|
*userState = conn; |
|
|
|
*userState = conn; |
|
|
|
|
|
|
|
|
|
|
|
bool accepted = s_queryAccept( |
|
|
|
bool accepted = s_queryAccept( |
|
|
|
s_queryAcceptParam, |
|
|
|
s_queryAcceptParam, |
|
|
|
channel->GetValue(), |
|
|
|
channel->channelId, |
|
|
|
conn, |
|
|
|
conn, |
|
|
|
listen->remoteAddr |
|
|
|
listen->remoteAddr |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if (!accepted) { |
|
|
|
if (!accepted) { |
|
|
|
SimpleNetDisconnect(conn); |
|
|
|
SimpleNetDisconnect(conn); |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
s_critsect.Enter(); |
|
|
|
s_critsect.Lock(); |
|
|
|
{ |
|
|
|
{ |
|
|
|
channel->conns.Link(conn); |
|
|
|
channel->conns.push_back(conn); |
|
|
|
} |
|
|
|
} |
|
|
|
s_critsect.Leave(); |
|
|
|
s_critsect.Unlock(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
channel->DecRef(); |
|
|
|
channel->DecRef(); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case kNotifySocketConnectSuccess: { |
|
|
|
case kNotifySocketConnectSuccess: { |
|
|
|
conn = (SimpleNetConn *) notify->param; |
|
|
|
conn = (SimpleNetConn *) notify->param; |
|
|
|
*userState = conn; |
|
|
|
*userState = conn; |
|
|
|
bool abandoned; |
|
|
|
bool abandoned; |
|
|
|
|
|
|
|
|
|
|
|
s_critsect.Enter(); |
|
|
|
s_critsect.Lock(); |
|
|
|
{ |
|
|
|
{ |
|
|
|
conn->sock = sock; |
|
|
|
conn->sock = sock; |
|
|
|
conn->cancelId = 0; |
|
|
|
conn->cancelId = 0; |
|
|
|
abandoned = conn->abandoned; |
|
|
|
abandoned = conn->abandoned; |
|
|
|
} |
|
|
|
} |
|
|
|
s_critsect.Leave(); |
|
|
|
s_critsect.Unlock(); |
|
|
|
|
|
|
|
|
|
|
|
if (abandoned) |
|
|
|
if (abandoned) |
|
|
|
AsyncSocketDisconnect(sock, true); |
|
|
|
AsyncSocketDisconnect(sock, true); |
|
|
|
else |
|
|
|
else |
|
|
@ -349,17 +348,20 @@ static bool AsyncNotifySocketProc ( |
|
|
|
case kNotifySocketConnectFailed: |
|
|
|
case kNotifySocketConnectFailed: |
|
|
|
conn = (SimpleNetConn *) notify->param; |
|
|
|
conn = (SimpleNetConn *) notify->param; |
|
|
|
NotifyConnSocketConnectFailed(conn); |
|
|
|
NotifyConnSocketConnectFailed(conn); |
|
|
|
break; |
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case kNotifySocketDisconnect: |
|
|
|
case kNotifySocketDisconnect: |
|
|
|
conn = (SimpleNetConn *) *userState; |
|
|
|
conn = (SimpleNetConn *) *userState; |
|
|
|
NotifyConnSocketDisconnect(conn); |
|
|
|
NotifyConnSocketDisconnect(conn); |
|
|
|
break; |
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case kNotifySocketRead: |
|
|
|
case kNotifySocketRead: |
|
|
|
conn = (SimpleNetConn *) *userState; |
|
|
|
conn = (SimpleNetConn *) *userState; |
|
|
|
result = NotifyConnSocketRead(conn, (AsyncNotifySocketRead *) notify); |
|
|
|
result = NotifyConnSocketRead(conn, (AsyncNotifySocketRead *) notify); |
|
|
|
break; |
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
return result; |
|
|
@ -369,15 +371,15 @@ static bool AsyncNotifySocketProc ( |
|
|
|
static void Connect (const NetAddress & addr, ConnectParam * cp) { |
|
|
|
static void Connect (const NetAddress & addr, ConnectParam * cp) { |
|
|
|
|
|
|
|
|
|
|
|
SimpleNetConn * conn = NEWZERO(SimpleNetConn); |
|
|
|
SimpleNetConn * conn = NEWZERO(SimpleNetConn); |
|
|
|
conn->channelId = cp->channel->GetValue(); |
|
|
|
conn->channelId = cp->channel->channelId; |
|
|
|
conn->connectParam = cp; |
|
|
|
conn->connectParam = cp; |
|
|
|
conn->IncRef("Lifetime"); |
|
|
|
conn->IncRef("Lifetime"); |
|
|
|
conn->IncRef("Connecting"); |
|
|
|
conn->IncRef("Connecting"); |
|
|
|
|
|
|
|
|
|
|
|
s_critsect.Enter(); |
|
|
|
s_critsect.Lock(); |
|
|
|
{ |
|
|
|
{ |
|
|
|
cp->channel->conns.Link(conn); |
|
|
|
cp->channel->conns.push_back(conn); |
|
|
|
|
|
|
|
|
|
|
|
SimpleNet_Connect connect; |
|
|
|
SimpleNet_Connect connect; |
|
|
|
connect.hdr.connType = kConnTypeSimpleNet; |
|
|
|
connect.hdr.connType = kConnTypeSimpleNet; |
|
|
|
connect.hdr.hdrBytes = sizeof(connect.hdr); |
|
|
|
connect.hdr.hdrBytes = sizeof(connect.hdr); |
|
|
@ -385,8 +387,8 @@ static void Connect (const NetAddress & addr, ConnectParam * cp) { |
|
|
|
connect.hdr.buildType = BUILD_TYPE_LIVE; |
|
|
|
connect.hdr.buildType = BUILD_TYPE_LIVE; |
|
|
|
connect.hdr.branchId = BranchId(); |
|
|
|
connect.hdr.branchId = BranchId(); |
|
|
|
connect.hdr.productId = ProductId(); |
|
|
|
connect.hdr.productId = ProductId(); |
|
|
|
connect.data.channelId = cp->channel->GetValue(); |
|
|
|
connect.data.channelId = cp->channel->channelId; |
|
|
|
|
|
|
|
|
|
|
|
AsyncSocketConnect( |
|
|
|
AsyncSocketConnect( |
|
|
|
&conn->cancelId, |
|
|
|
&conn->cancelId, |
|
|
|
addr, |
|
|
|
addr, |
|
|
@ -395,12 +397,12 @@ static void Connect (const NetAddress & addr, ConnectParam * cp) { |
|
|
|
&connect, |
|
|
|
&connect, |
|
|
|
sizeof(connect) |
|
|
|
sizeof(connect) |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
conn = nil;
|
|
|
|
conn = nil; |
|
|
|
cp = nil; |
|
|
|
cp = nil; |
|
|
|
} |
|
|
|
} |
|
|
|
s_critsect.Leave(); |
|
|
|
s_critsect.Unlock(); |
|
|
|
|
|
|
|
|
|
|
|
delete conn; |
|
|
|
delete conn; |
|
|
|
delete cp; |
|
|
|
delete cp; |
|
|
|
} |
|
|
|
} |
|
|
@ -447,8 +449,8 @@ void SimpleNetShutdown () { |
|
|
|
|
|
|
|
|
|
|
|
s_running = false; |
|
|
|
s_running = false; |
|
|
|
|
|
|
|
|
|
|
|
ASSERT(!s_channels.Head()); |
|
|
|
ASSERT(!s_channels.size()); |
|
|
|
|
|
|
|
|
|
|
|
AsyncSocketUnregisterNotifyProc( |
|
|
|
AsyncSocketUnregisterNotifyProc( |
|
|
|
kConnTypeSimpleNet, |
|
|
|
kConnTypeSimpleNet, |
|
|
|
AsyncNotifySocketProc |
|
|
|
AsyncNotifySocketProc |
|
|
@ -460,7 +462,7 @@ void SimpleNetConnIncRef (SimpleNetConn * conn) { |
|
|
|
|
|
|
|
|
|
|
|
ASSERT(s_running); |
|
|
|
ASSERT(s_running); |
|
|
|
ASSERT(conn); |
|
|
|
ASSERT(conn); |
|
|
|
|
|
|
|
|
|
|
|
conn->IncRef(); |
|
|
|
conn->IncRef(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -469,7 +471,7 @@ void SimpleNetConnDecRef (SimpleNetConn * conn) { |
|
|
|
|
|
|
|
|
|
|
|
ASSERT(s_running); |
|
|
|
ASSERT(s_running); |
|
|
|
ASSERT(conn); |
|
|
|
ASSERT(conn); |
|
|
|
|
|
|
|
|
|
|
|
conn->DecRef(); |
|
|
|
conn->DecRef(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -481,10 +483,10 @@ bool SimpleNetStartListening ( |
|
|
|
ASSERT(s_running); |
|
|
|
ASSERT(s_running); |
|
|
|
ASSERT(queryAccept); |
|
|
|
ASSERT(queryAccept); |
|
|
|
ASSERT(!s_queryAccept); |
|
|
|
ASSERT(!s_queryAccept); |
|
|
|
|
|
|
|
|
|
|
|
s_queryAccept = queryAccept; |
|
|
|
s_queryAccept = queryAccept; |
|
|
|
s_queryAcceptParam = param; |
|
|
|
s_queryAcceptParam = param; |
|
|
|
|
|
|
|
|
|
|
|
NetAddress addr; |
|
|
|
NetAddress addr; |
|
|
|
NetAddressFromNode(0, kNetDefaultSimpleNetPort, &addr); |
|
|
|
NetAddressFromNode(0, kNetDefaultSimpleNetPort, &addr); |
|
|
|
return (0 != AsyncSocketStartListening(addr, nil)); |
|
|
|
return (0 != AsyncSocketStartListening(addr, nil)); |
|
|
@ -514,22 +516,22 @@ void SimpleNetCreateChannel ( |
|
|
|
SimpleNetChannel * channel = NEWZERO(SimpleNetChannel)(channelId); |
|
|
|
SimpleNetChannel * channel = NEWZERO(SimpleNetChannel)(channelId); |
|
|
|
channel->IncRef(); |
|
|
|
channel->IncRef(); |
|
|
|
|
|
|
|
|
|
|
|
s_critsect.Enter(); |
|
|
|
s_critsect.Lock(); |
|
|
|
{ |
|
|
|
{ |
|
|
|
#ifdef HS_DEBUGGING |
|
|
|
#ifdef HS_DEBUGGING |
|
|
|
{ |
|
|
|
{ |
|
|
|
SimpleNetChannel * existing = s_channels.Find(channelId); |
|
|
|
std::map<uint32_t, SimpleNetChannel*>::iterator it = s_channels.find(channelId); |
|
|
|
ASSERT(!existing); |
|
|
|
ASSERT(it == s_channels.end()); |
|
|
|
} |
|
|
|
} |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
channel->onMsg = onMsg; |
|
|
|
channel->onMsg = onMsg; |
|
|
|
channel->onError = onError; |
|
|
|
channel->onError = onError; |
|
|
|
s_channels.Add(channel); |
|
|
|
s_channels[channelId] = channel; |
|
|
|
channel->IncRef(); |
|
|
|
channel->IncRef(); |
|
|
|
} |
|
|
|
} |
|
|
|
s_critsect.Leave(); |
|
|
|
s_critsect.Unlock(); |
|
|
|
|
|
|
|
|
|
|
|
channel->DecRef(); |
|
|
|
channel->DecRef(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -539,20 +541,26 @@ void SimpleNetDestroyChannel (unsigned channelId) { |
|
|
|
ASSERT(s_running); |
|
|
|
ASSERT(s_running); |
|
|
|
|
|
|
|
|
|
|
|
SimpleNetChannel * channel; |
|
|
|
SimpleNetChannel * channel; |
|
|
|
s_critsect.Enter(); |
|
|
|
s_critsect.Lock(); |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (nil != (channel = s_channels.Find(channelId))) { |
|
|
|
std::map<uint32_t, SimpleNetChannel*>::iterator it; |
|
|
|
s_channels.Unlink(channel); |
|
|
|
if ((it = s_channels.find(channelId)) != s_channels.end()) { |
|
|
|
while (SimpleNetConn * conn = channel->conns.Head()) { |
|
|
|
channel = it->second; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (channel->conns.size()) { |
|
|
|
|
|
|
|
SimpleNetConn* conn = channel->conns.front(); |
|
|
|
SimpleNetDisconnect(conn); |
|
|
|
SimpleNetDisconnect(conn); |
|
|
|
channel->conns.Unlink(conn); |
|
|
|
|
|
|
|
|
|
|
|
channel->conns.pop_front(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s_channels.erase(it); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
s_critsect.Leave(); |
|
|
|
s_critsect.Unlock(); |
|
|
|
|
|
|
|
|
|
|
|
if (channel) |
|
|
|
if (channel) |
|
|
|
channel->DecRef();
|
|
|
|
channel->DecRef(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
//============================================================================
|
|
|
@ -564,18 +572,22 @@ void SimpleNetStartConnecting ( |
|
|
|
) { |
|
|
|
) { |
|
|
|
ASSERT(s_running); |
|
|
|
ASSERT(s_running); |
|
|
|
ASSERT(onConnect); |
|
|
|
ASSERT(onConnect); |
|
|
|
|
|
|
|
|
|
|
|
ConnectParam * cp = new ConnectParam; |
|
|
|
ConnectParam * cp = new ConnectParam; |
|
|
|
cp->callback = onConnect; |
|
|
|
cp->callback = onConnect; |
|
|
|
cp->param = param; |
|
|
|
cp->param = param; |
|
|
|
|
|
|
|
cp->channel = nil; |
|
|
|
s_critsect.Enter(); |
|
|
|
|
|
|
|
|
|
|
|
s_critsect.Lock(); |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (nil != (cp->channel = s_channels.Find(channelId))) |
|
|
|
std::map<uint32_t, SimpleNetChannel*>::iterator it; |
|
|
|
|
|
|
|
if ((it = s_channels.find(channelId)) != s_channels.end()) { |
|
|
|
|
|
|
|
cp->channel = it->second; |
|
|
|
cp->channel->IncRef(); |
|
|
|
cp->channel->IncRef(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
s_critsect.Leave(); |
|
|
|
s_critsect.Unlock(); |
|
|
|
|
|
|
|
|
|
|
|
ASSERT(cp->channel); |
|
|
|
ASSERT(cp->channel); |
|
|
|
|
|
|
|
|
|
|
|
// Do we need to lookup the address?
|
|
|
|
// Do we need to lookup the address?
|
|
|
@ -608,8 +620,8 @@ void SimpleNetDisconnect ( |
|
|
|
) { |
|
|
|
) { |
|
|
|
ASSERT(s_running); |
|
|
|
ASSERT(s_running); |
|
|
|
ASSERT(conn); |
|
|
|
ASSERT(conn); |
|
|
|
|
|
|
|
|
|
|
|
s_critsect.Enter(); |
|
|
|
s_critsect.Lock(); |
|
|
|
{ |
|
|
|
{ |
|
|
|
conn->abandoned = true; |
|
|
|
conn->abandoned = true; |
|
|
|
if (conn->sock) { |
|
|
|
if (conn->sock) { |
|
|
@ -621,7 +633,7 @@ void SimpleNetDisconnect ( |
|
|
|
conn->cancelId = nil; |
|
|
|
conn->cancelId = nil; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
s_critsect.Leave(); |
|
|
|
s_critsect.Unlock(); |
|
|
|
|
|
|
|
|
|
|
|
conn->DecRef("Lifetime"); |
|
|
|
conn->DecRef("Lifetime"); |
|
|
|
} |
|
|
|
} |
|
|
@ -635,11 +647,11 @@ void SimpleNetSend ( |
|
|
|
ASSERT(msg); |
|
|
|
ASSERT(msg); |
|
|
|
ASSERT(msg->messageBytes != (uint32_t)-1); |
|
|
|
ASSERT(msg->messageBytes != (uint32_t)-1); |
|
|
|
ASSERT(conn); |
|
|
|
ASSERT(conn); |
|
|
|
|
|
|
|
|
|
|
|
s_critsect.Enter(); |
|
|
|
s_critsect.Lock(); |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (conn->sock) |
|
|
|
if (conn->sock) |
|
|
|
AsyncSocketSend(conn->sock, msg, msg->messageBytes); |
|
|
|
AsyncSocketSend(conn->sock, msg, msg->messageBytes); |
|
|
|
} |
|
|
|
} |
|
|
|
s_critsect.Leave(); |
|
|
|
s_critsect.Unlock(); |
|
|
|
} |
|
|
|
} |
|
|
|