Skip to content

Commit

Permalink
Convert plNglTrans lists from pnUtList to std::list
Browse files Browse the repository at this point in the history
  • Loading branch information
dgelessus committed Sep 17, 2023
1 parent 5b2cc2e commit caee7c6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
1 change: 0 additions & 1 deletion Sources/Plasma/PubUtilLib/plNetGameLib/Intern.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ enum ENetTransState {
};

struct NetTrans : hsRefCnt {
LINK(NetTrans) m_link;
ENetTransState m_state;
ENetError m_result;
unsigned m_transId;
Expand Down
64 changes: 38 additions & 26 deletions Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglTrans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ You can contact Cyan Worlds, Inc. by email [email protected]

#include "../Pch.h"

#include <list>

namespace Ngl {
/*****************************************************************************
Expand All @@ -64,7 +65,7 @@ static const unsigned kDefaultTimeoutMs = 5 * 60 * 1000;

static bool s_running;
static std::recursive_mutex s_critsect;
static LISTDECL(NetTrans, m_link) s_transactions;
static std::list<NetTrans*> s_transactions;
static std::atomic<long> s_perf[kNumPerf];
static unsigned s_timeoutMs = kDefaultTimeoutMs;

Expand All @@ -78,11 +79,12 @@ static unsigned s_timeoutMs = kDefaultTimeoutMs;
//============================================================================
static NetTrans * FindTransIncRef_CS (unsigned transId, const char tag[]) {
// There shouldn't be more than a few transactions; just do a linear scan
for (NetTrans * trans = s_transactions.Head(); trans; trans = s_transactions.Next(trans))
for (NetTrans* trans : s_transactions) {
if (trans->m_transId == transId) {
trans->Ref(tag);
return trans;
}
}

return nullptr;
}
Expand Down Expand Up @@ -128,7 +130,15 @@ NetTrans::NetTrans (ENetProtocol protocol, ETransType transType)

//============================================================================
NetTrans::~NetTrans () {
ASSERT(!m_link.IsLinked());
#if defined(HS_DEBUGGING)
{
hsLockGuard(s_critsect);
hsAssert(
std::find(s_transactions.begin(), s_transactions.end(), this) == s_transactions.end(),
"Destroying a transaction that's still in progress!"
);
}
#endif
--s_perfTransCount[m_transType];
--s_perf[kPerfCurrTransactions];
// DebugMsg("%s@%p destroyed", s_transTypes[m_transType], this);
Expand Down Expand Up @@ -193,7 +203,7 @@ void NetTransSend (NetTrans * trans) {
static unsigned s_transId;
while (!trans->m_transId)
trans->m_transId = ++s_transId;
s_transactions.Link(trans, kListTail);
s_transactions.push_back(trans);
if (!s_running)
CancelTrans_CS(trans, kNetErrRemoteShutdown);
}
Expand All @@ -220,8 +230,7 @@ bool NetTransRecv (unsigned transId, const uint8_t msg[], unsigned bytes) {
//============================================================================
void NetTransCancel (unsigned transId, ENetError error) {
hsLockGuard(s_critsect);
NetTrans * trans = s_transactions.Head();
for (; trans; trans = trans->m_link.Next()) {
for (NetTrans* trans : s_transactions) {
if (trans->m_transId == transId) {
CancelTrans_CS(trans, error);
break;
Expand All @@ -232,8 +241,7 @@ void NetTransCancel (unsigned transId, ENetError error) {
//============================================================================
void NetTransCancelByProtocol (ENetProtocol protocol, ENetError error) {
hsLockGuard(s_critsect);
NetTrans * trans = s_transactions.Head();
for (; trans; trans = trans->m_link.Next()) {
for (NetTrans* trans : s_transactions) {
if (trans->m_protocol == protocol)
CancelTrans_CS(trans, error);
}
Expand All @@ -242,8 +250,7 @@ void NetTransCancelByProtocol (ENetProtocol protocol, ENetError error) {
//============================================================================
void NetTransCancelByConnId (unsigned connId, ENetError error) {
hsLockGuard(s_critsect);
NetTrans * trans = s_transactions.Head();
for (; trans; trans = trans->m_link.Next()) {
for (NetTrans* trans : s_transactions) {
if (trans->m_connId == connId)
CancelTrans_CS(trans, error);
}
Expand All @@ -252,31 +259,36 @@ void NetTransCancelByConnId (unsigned connId, ENetError error) {
//============================================================================
void NetTransCancelAll (ENetError error) {
hsLockGuard(s_critsect);
NetTrans * trans = s_transactions.Head();
for (; trans; trans = trans->m_link.Next())
for (NetTrans* trans : s_transactions) {
CancelTrans_CS(trans, error);
}
}

//============================================================================
void NetTransUpdate () {
LISTDECL(NetTrans, m_link) completed;
LISTDECL(NetTrans, m_link) parentCompleted;
std::list<NetTrans*> completed;
std::list<NetTrans*> parentCompleted;

{
hsLockGuard(s_critsect);

NetTrans * next, * trans = s_transactions.Head();
for (; trans; trans = next) {
next = s_transactions.Next(trans);
for (auto it = s_transactions.begin(); it != s_transactions.end();) {
NetTrans* trans = *it;
// Increment a copy of the iterator here already,
// because the original iterator may be invalidated by an erase call below.
auto next = it;
next++;

bool done = false;
while (!done) {
switch (trans->m_state) {
case kTransStateComplete:
if (trans->m_hasSubTrans)
parentCompleted.Link(trans);
else
completed.Link(trans);
s_transactions.erase(it);
if (trans->m_hasSubTrans) {
parentCompleted.push_back(trans);
} else {
completed.push_back(trans);
}
done = true;
break;

Expand Down Expand Up @@ -316,18 +328,18 @@ void NetTransUpdate () {
DEFAULT_FATAL(trans->m_state);
}
}

it = next;
}
}

// Post completed transactions
while (NetTrans * trans = completed.Head()) {
completed.Unlink(trans);
// Post completed transactions
for (NetTrans* trans : completed) {
trans->Post();
trans->UnRef("Lifetime");
}
// Post completed parent transactions
while (NetTrans * trans = parentCompleted.Head()) {
parentCompleted.Unlink(trans);
for (NetTrans* trans : parentCompleted) {
trans->Post();
trans->UnRef("Lifetime");
}
Expand Down

0 comments on commit caee7c6

Please sign in to comment.