Skip to content

Commit

Permalink
Replace s_conns lists with single pointers
Browse files Browse the repository at this point in the history
The code in the Connect functions already ensured that the lists never
contain multiple connections, so there's no need to use lists at all.
  • Loading branch information
dgelessus committed Sep 17, 2023
1 parent 6193fd9 commit 5b2cc2e
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 65 deletions.
43 changes: 26 additions & 17 deletions Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglAuth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ struct CliAuConn : hsRefCnt {
void Send (const uintptr_t fields[], unsigned count);

std::recursive_mutex critsect;
LINK(CliAuConn) link;
AsyncSocket sock;
NetCli * cli;
ST::string name;
Expand Down Expand Up @@ -1249,7 +1248,7 @@ enum {

static bool s_running;
static std::recursive_mutex s_critsect;
static LISTDECL(CliAuConn, link) s_conns;
static CliAuConn* s_conn = nullptr;
static CliAuConn * s_active;
static ST::string s_accountName;
static ShaDigest s_accountNamePassHash;
Expand Down Expand Up @@ -1327,8 +1326,7 @@ static CliAuConn * GetConnIncRef (const char tag[]) {
}

//============================================================================
static void UnlinkAndAbandonConn_CS (CliAuConn * conn) {
s_conns.Unlink(conn);
static void AbandonConn(CliAuConn* conn) {
conn->abandoned = true;

conn->StopAutoReconnect();
Expand Down Expand Up @@ -1425,7 +1423,9 @@ static void NotifyConnSocketConnectFailed (CliAuConn * conn) {
{
hsLockGuard(s_critsect);
conn->cancelId = nullptr;
s_conns.Unlink(conn);
if (s_conn == conn) {
s_conn = nullptr;
}

if (conn == s_active)
s_active = nullptr;
Expand All @@ -1444,7 +1444,9 @@ static void NotifyConnSocketDisconnect (CliAuConn * conn) {
{
hsLockGuard(s_critsect);
conn->cancelId = nullptr;
s_conns.Unlink(conn);
if (s_conn == conn) {
s_conn = nullptr;
}

if (conn == s_active)
s_active = nullptr;
Expand Down Expand Up @@ -1525,13 +1527,13 @@ static void Connect (

{
hsLockGuard(s_critsect);
while (CliAuConn * oldConn = s_conns.Head()) {
if (oldConn != conn)
UnlinkAndAbandonConn_CS(oldConn);
else
s_conns.Unlink(oldConn);
if (CliAuConn* oldConn = s_conn) {
s_conn = nullptr;
if (oldConn != conn) {
AbandonConn(oldConn);
}
}
s_conns.Link(conn);
s_conn = conn;
}

Cli2Auth_Connect connect;
Expand Down Expand Up @@ -1643,7 +1645,10 @@ void CliAuConn::TimerReconnect () {

if (!s_running) {
hsLockGuard(s_critsect);
UnlinkAndAbandonConn_CS(this);
if (s_conn == this) {
s_conn = nullptr;
}
AbandonConn(this);
}
else {
Ref("Connecting");
Expand Down Expand Up @@ -4671,8 +4676,10 @@ void AuthDestroy (bool wait) {

{
hsLockGuard(s_critsect);
while (CliAuConn * conn = s_conns.Head())
UnlinkAndAbandonConn_CS(conn);
if (CliAuConn* conn = s_conn) {
s_conn = nullptr;
AbandonConn(conn);
}
s_active = nullptr;
}

Expand Down Expand Up @@ -4769,8 +4776,10 @@ void NetCliAuthAutoReconnectEnable (bool enable) {
void NetCliAuthDisconnect () {

hsLockGuard(s_critsect);
while (CliAuConn * conn = s_conns.Head())
UnlinkAndAbandonConn_CS(conn);
if (CliAuConn* conn = s_conn) {
s_conn = nullptr;
AbandonConn(conn);
}
s_active = nullptr;
}

Expand Down
43 changes: 26 additions & 17 deletions Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ namespace Ngl { namespace File {
***/

struct CliFileConn : hsRefCnt {
LINK(CliFileConn) link;
hsReaderWriterLock sockLock; // to protect the socket pointer so we don't nuke it while using it
AsyncSocket sock;
ST::string name;
Expand Down Expand Up @@ -234,7 +233,7 @@ enum {

static bool s_running;
static std::recursive_mutex s_critsect;
static LISTDECL(CliFileConn, link) s_conns;
static CliFileConn* s_conn = nullptr;
static CliFileConn * s_active;
static std::atomic<long> s_perf[kNumPerf];
static unsigned s_connectBuildId;
Expand Down Expand Up @@ -275,8 +274,7 @@ static CliFileConn * GetConnIncRef (const char tag[]) {
}

//============================================================================
static void UnlinkAndAbandonConn_CS (CliFileConn * conn) {
s_conns.Unlink(conn);
static void AbandonConn(CliFileConn* conn) {
conn->abandoned = true;

if (conn->AutoReconnectEnabled())
Expand Down Expand Up @@ -325,7 +323,9 @@ static void NotifyConnSocketConnectFailed (CliFileConn * conn) {
{
hsLockGuard(s_critsect);
conn->cancelId = nullptr;
s_conns.Unlink(conn);
if (s_conn == conn) {
s_conn = nullptr;
}

if (conn == s_active)
s_active = nullptr;
Expand Down Expand Up @@ -355,7 +355,9 @@ static void NotifyConnSocketDisconnect (CliFileConn * conn) {
{
hsLockGuard(s_critsect);
conn->cancelId = nullptr;
s_conns.Unlink(conn);
if (s_conn == conn) {
s_conn = nullptr;
}

if (conn == s_active)
s_active = nullptr;
Expand Down Expand Up @@ -493,13 +495,13 @@ static void Connect (CliFileConn * conn) {

{
hsLockGuard(s_critsect);
while (CliFileConn * oldConn = s_conns.Head()) {
if (oldConn != conn)
UnlinkAndAbandonConn_CS(oldConn);
else
s_conns.Unlink(oldConn);
if (CliFileConn* oldConn = s_conn) {
s_conn = nullptr;
if (oldConn != conn) {
AbandonConn(oldConn);
}
}
s_conns.Link(conn);
s_conn = conn;
}

Cli2File_Connect connect;
Expand Down Expand Up @@ -587,7 +589,10 @@ void CliFileConn::TimerReconnect () {

if (!s_running) {
hsLockGuard(s_critsect);
UnlinkAndAbandonConn_CS(this);
if (s_conn == this) {
s_conn = nullptr;
AbandonConn(this);
}
}
else {
Ref("Connecting");
Expand Down Expand Up @@ -1303,8 +1308,10 @@ void FileDestroy (bool wait) {

{
hsLockGuard(s_critsect);
while (CliFileConn * conn = s_conns.Head())
UnlinkAndAbandonConn_CS(conn);
if (CliFileConn* conn = s_conn) {
s_conn = nullptr;
AbandonConn(conn);
}
s_active = nullptr;
}

Expand Down Expand Up @@ -1379,8 +1386,10 @@ bool NetCliFileQueryConnected () {
//============================================================================
void NetCliFileDisconnect () {
hsLockGuard(s_critsect);
while (CliFileConn * conn = s_conns.Head())
UnlinkAndAbandonConn_CS(conn);
if (CliFileConn* conn = s_conn) {
s_conn = nullptr;
AbandonConn(conn);
}
s_active = nullptr;
}

Expand Down
35 changes: 21 additions & 14 deletions Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ namespace Ngl { namespace Game {
***/

struct CliGmConn : hsRefCnt {
LINK(CliGmConn) link;

std::recursive_mutex critsect;
AsyncSocket sock;
AsyncCancelId cancelId;
Expand Down Expand Up @@ -157,7 +155,7 @@ enum {

static bool s_running;
static std::recursive_mutex s_critsect;
static LISTDECL(CliGmConn, link) s_conns;
static CliGmConn* s_conn = nullptr;
static CliGmConn * s_active;
static FNetCliGameRecvBufferHandler s_bufHandler;
static FNetCliGameRecvGameMgrMsgHandler s_gameMgrMsgHandler;
Expand Down Expand Up @@ -194,8 +192,7 @@ static CliGmConn * GetConnIncRef (const char tag[]) {
}

//============================================================================
static void UnlinkAndAbandonConn_CS (CliGmConn * conn) {
s_conns.Unlink(conn);
static void AbandonConn(CliGmConn* conn) {
conn->abandoned = true;
if (conn->cancelId) {
AsyncSocketConnectCancel(conn->cancelId);
Expand Down Expand Up @@ -244,7 +241,9 @@ static void NotifyConnSocketConnectFailed (CliGmConn * conn) {
{
hsLockGuard(s_critsect);
conn->cancelId = nullptr;
s_conns.Unlink(conn);
if (s_conn == conn) {
s_conn = nullptr;
}

notify
= s_running
Expand All @@ -270,7 +269,9 @@ static void NotifyConnSocketDisconnect (CliGmConn * conn) {
bool notify;
{
hsLockGuard(s_critsect);
s_conns.Unlink(conn);
if (s_conn == conn) {
s_conn = nullptr;
}

notify
= s_running
Expand Down Expand Up @@ -377,9 +378,11 @@ static void Connect (

{
hsLockGuard(s_critsect);
while (CliGmConn * conn = s_conns.Head())
UnlinkAndAbandonConn_CS(conn);
s_conns.Link(conn);
if (CliGmConn* oldConn = s_conn) {
s_conn = nullptr;
AbandonConn(oldConn);
}
s_conn = conn;
}

Cli2Game_Connect connect;
Expand Down Expand Up @@ -722,8 +725,10 @@ void GameDestroy (bool wait) {

{
hsLockGuard(s_critsect);
while (CliGmConn * conn = s_conns.Head())
UnlinkAndAbandonConn_CS(conn);
if (CliGmConn* conn = s_conn) {
s_conn = nullptr;
AbandonConn(conn);
}
s_active = nullptr;
}

Expand Down Expand Up @@ -783,8 +788,10 @@ void NetCliGameStartConnect (
//============================================================================
void NetCliGameDisconnect () {
hsLockGuard(s_critsect);
while (CliGmConn * conn = s_conns.Head())
UnlinkAndAbandonConn_CS(conn);
if (CliGmConn* conn = s_conn) {
s_conn = nullptr;
AbandonConn(conn);
}
s_active = nullptr;
}

Expand Down
Loading

0 comments on commit 5b2cc2e

Please sign in to comment.