Browse Source

Clean up pnSimpleNet.

Removes Pch.h.
Removes a pnUtList.
Removes a PnUtHash (and all the weird warnings from it).
Darryl Pogue 13 years ago
parent
commit
270e05738f
  1. 1
      Sources/Plasma/NucleusLib/pnSimpleNet/CMakeLists.txt
  2. 54
      Sources/Plasma/NucleusLib/pnSimpleNet/Pch.h
  3. 156
      Sources/Plasma/NucleusLib/pnSimpleNet/pnSimpleNet.cpp

1
Sources/Plasma/NucleusLib/pnSimpleNet/CMakeLists.txt

@ -2,7 +2,6 @@ include_directories("../../CoreLib")
include_directories("../../NucleusLib")
set(pnSimpleNet_HEADERS
Pch.h
pnSimpleNet.h
)

54
Sources/Plasma/NucleusLib/pnSimpleNet/Pch.h

@ -1,54 +0,0 @@
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
/*****************************************************************************
*
* $/Plasma20/Sources/Plasma/NucleusLib/pnSimpleNet/Pch.h
*
***/
#ifdef PLASMA20_SOURCES_PLASMA_NUCLEUSLIB_PNSIMPLENET_PCH_H
#error "Header $/Plasma20/Sources/Plasma/NucleusLib/pnSimpleNet/Pch.h included more than once"
#endif
#define PLASMA20_SOURCES_PLASMA_NUCLEUSLIB_PNSIMPLENET_PCH_H
#include "pnSimpleNet.h"

156
Sources/Plasma/NucleusLib/pnSimpleNet/pnSimpleNet.cpp

@ -45,9 +45,11 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
*
***/
#include "Pch.h"
#pragma hdrstop
#include "pnSimpleNet.h"
#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 {
LINK(SimpleNetConn) link;
AsyncSocket sock;
AsyncCancelId cancelId;
unsigned channelId;
uint32_t channelId;
bool abandoned;
struct ConnectParam * connectParam;
SimpleNet_MsgHeader * oversizeMsg;
ARRAY(uint8_t) oversizeBuffer;
~SimpleNetConn () {
ASSERT(!link.IsLinked());
}
};
struct SimpleNetChannel : AtomicRef, THashKeyVal<unsigned> {
HASHLINK(SimpleNetChannel) link;
struct SimpleNetChannel : AtomicRef {
FSimpleNetOnMsg onMsg;
FSimpleNetOnError onError;
uint32_t channelId;
std::list<SimpleNetConn*> conns;
LISTDECL(SimpleNetConn, link) conns;
SimpleNetChannel (unsigned channel)
: THashKeyVal<unsigned>(channel)
{ }
SimpleNetChannel (uint32_t channel) : channelId(channel) { }
~SimpleNetChannel () {
ASSERT(!link.IsLinked());
ASSERT(!conns.Head());
ASSERT(!conns.size());
}
};
@ -107,15 +99,10 @@ struct ConnectParam {
***/
static bool s_running;
static CCritSect s_critsect;
static hsMutex s_critsect;
static FSimpleNetQueryAccept s_queryAccept;
static void * s_queryAcceptParam;
static HASHTABLEDECL(
SimpleNetChannel,
THashKeyVal<unsigned>,
link
) s_channels;
static std::map<uint32_t, SimpleNetChannel*> s_channels;
/*****************************************************************************
@ -142,11 +129,14 @@ static void NotifyConnSocketConnect (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->param,
@ -165,15 +155,18 @@ static void NotifyConnSocketConnectFailed (SimpleNetConn * conn) {
static void NotifyConnSocketDisconnect (SimpleNetConn * conn) {
bool abandoned;
SimpleNetChannel * channel;
s_critsect.Enter();
SimpleNetChannel* channel = nil;
s_critsect.Lock();
{
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();
conn->link.Unlink();
channel->conns.remove(conn);
}
s_critsect.Leave();
}
s_critsect.Unlock();
if (channel && !abandoned) {
channel->onError(conn, kNetErrDisconnected);
@ -186,13 +179,16 @@ static void NotifyConnSocketDisconnect (SimpleNetConn * conn) {
//============================================================================
static bool NotifyConnSocketRead (SimpleNetConn * conn, AsyncNotifySocketRead * read) {
SimpleNetChannel * channel;
s_critsect.Enter();
SimpleNetChannel* channel = nil;
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();
}
s_critsect.Leave();
}
s_critsect.Unlock();
if (!channel)
return false;
@ -286,19 +282,22 @@ static bool AsyncNotifySocketProc (
const SimpleNet_ConnData & connect = *(const SimpleNet_ConnData *) listen->buffer;
listen->bytesProcessed += sizeof(connect);
SimpleNetChannel * channel;
s_critsect.Enter();
SimpleNetChannel* channel = nil;
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();
}
s_critsect.Leave();
}
s_critsect.Unlock();
if (!channel)
break;
conn = NEWZERO(SimpleNetConn);
conn->channelId = channel->GetValue();
conn->channelId = channel->channelId;
conn->IncRef("Lifetime");
conn->IncRef("Connected");
conn->sock = sock;
@ -306,7 +305,7 @@ static bool AsyncNotifySocketProc (
bool accepted = s_queryAccept(
s_queryAcceptParam,
channel->GetValue(),
channel->channelId,
conn,
listen->remoteAddr
);
@ -315,11 +314,11 @@ static bool AsyncNotifySocketProc (
SimpleNetDisconnect(conn);
}
else {
s_critsect.Enter();
s_critsect.Lock();
{
channel->conns.Link(conn);
channel->conns.push_back(conn);
}
s_critsect.Leave();
s_critsect.Unlock();
}
channel->DecRef();
@ -331,13 +330,13 @@ static bool AsyncNotifySocketProc (
*userState = conn;
bool abandoned;
s_critsect.Enter();
s_critsect.Lock();
{
conn->sock = sock;
conn->cancelId = 0;
abandoned = conn->abandoned;
}
s_critsect.Leave();
s_critsect.Unlock();
if (abandoned)
AsyncSocketDisconnect(sock, true);
@ -360,6 +359,9 @@ static bool AsyncNotifySocketProc (
conn = (SimpleNetConn *) *userState;
result = NotifyConnSocketRead(conn, (AsyncNotifySocketRead *) notify);
break;
default:
break;
}
return result;
@ -369,14 +371,14 @@ static bool AsyncNotifySocketProc (
static void Connect (const NetAddress & addr, ConnectParam * cp) {
SimpleNetConn * conn = NEWZERO(SimpleNetConn);
conn->channelId = cp->channel->GetValue();
conn->channelId = cp->channel->channelId;
conn->connectParam = cp;
conn->IncRef("Lifetime");
conn->IncRef("Connecting");
s_critsect.Enter();
s_critsect.Lock();
{
cp->channel->conns.Link(conn);
cp->channel->conns.push_back(conn);
SimpleNet_Connect connect;
connect.hdr.connType = kConnTypeSimpleNet;
@ -385,7 +387,7 @@ static void Connect (const NetAddress & addr, ConnectParam * cp) {
connect.hdr.buildType = BUILD_TYPE_LIVE;
connect.hdr.branchId = BranchId();
connect.hdr.productId = ProductId();
connect.data.channelId = cp->channel->GetValue();
connect.data.channelId = cp->channel->channelId;
AsyncSocketConnect(
&conn->cancelId,
@ -399,7 +401,7 @@ static void Connect (const NetAddress & addr, ConnectParam * cp) {
conn = nil;
cp = nil;
}
s_critsect.Leave();
s_critsect.Unlock();
delete conn;
delete cp;
@ -447,7 +449,7 @@ void SimpleNetShutdown () {
s_running = false;
ASSERT(!s_channels.Head());
ASSERT(!s_channels.size());
AsyncSocketUnregisterNotifyProc(
kConnTypeSimpleNet,
@ -514,21 +516,21 @@ void SimpleNetCreateChannel (
SimpleNetChannel * channel = NEWZERO(SimpleNetChannel)(channelId);
channel->IncRef();
s_critsect.Enter();
s_critsect.Lock();
{
#ifdef HS_DEBUGGING
{
SimpleNetChannel * existing = s_channels.Find(channelId);
ASSERT(!existing);
std::map<uint32_t, SimpleNetChannel*>::iterator it = s_channels.find(channelId);
ASSERT(it == s_channels.end());
}
#endif
channel->onMsg = onMsg;
channel->onError = onError;
s_channels.Add(channel);
s_channels[channelId] = channel;
channel->IncRef();
}
s_critsect.Leave();
s_critsect.Unlock();
channel->DecRef();
}
@ -539,17 +541,23 @@ void SimpleNetDestroyChannel (unsigned channelId) {
ASSERT(s_running);
SimpleNetChannel * channel;
s_critsect.Enter();
s_critsect.Lock();
{
if (nil != (channel = s_channels.Find(channelId))) {
s_channels.Unlink(channel);
while (SimpleNetConn * conn = channel->conns.Head()) {
std::map<uint32_t, SimpleNetChannel*>::iterator it;
if ((it = s_channels.find(channelId)) != s_channels.end()) {
channel = it->second;
while (channel->conns.size()) {
SimpleNetConn* conn = channel->conns.front();
SimpleNetDisconnect(conn);
channel->conns.Unlink(conn);
channel->conns.pop_front();
}
s_channels.erase(it);
}
}
s_critsect.Leave();
s_critsect.Unlock();
if (channel)
channel->DecRef();
@ -568,13 +576,17 @@ void SimpleNetStartConnecting (
ConnectParam * cp = new ConnectParam;
cp->callback = onConnect;
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();
}
s_critsect.Leave();
}
s_critsect.Unlock();
ASSERT(cp->channel);
@ -609,7 +621,7 @@ void SimpleNetDisconnect (
ASSERT(s_running);
ASSERT(conn);
s_critsect.Enter();
s_critsect.Lock();
{
conn->abandoned = true;
if (conn->sock) {
@ -621,7 +633,7 @@ void SimpleNetDisconnect (
conn->cancelId = nil;
}
}
s_critsect.Leave();
s_critsect.Unlock();
conn->DecRef("Lifetime");
}
@ -636,10 +648,10 @@ void SimpleNetSend (
ASSERT(msg->messageBytes != (uint32_t)-1);
ASSERT(conn);
s_critsect.Enter();
s_critsect.Lock();
{
if (conn->sock)
AsyncSocketSend(conn->sock, msg, msg->messageBytes);
}
s_critsect.Leave();
s_critsect.Unlock();
}

Loading…
Cancel
Save