From 5b2cc2e81c1e08ec219f4f2ea05a71cba9617025 Mon Sep 17 00:00:00 2001 From: dgelessus Date: Sun, 20 Aug 2023 16:52:13 +0200 Subject: [PATCH] Replace s_conns lists with single pointers 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. --- .../plNetGameLib/Private/plNglAuth.cpp | 43 +++++++++++-------- .../plNetGameLib/Private/plNglFile.cpp | 43 +++++++++++-------- .../plNetGameLib/Private/plNglGame.cpp | 35 +++++++++------ .../plNetGameLib/Private/plNglGateKeeper.cpp | 43 +++++++++++-------- 4 files changed, 99 insertions(+), 65 deletions(-) diff --git a/Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglAuth.cpp b/Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglAuth.cpp index 251badda09..bbab8994cb 100644 --- a/Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglAuth.cpp +++ b/Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglAuth.cpp @@ -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; @@ -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; @@ -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(); @@ -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; @@ -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; @@ -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; @@ -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"); @@ -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; } @@ -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; } diff --git a/Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglFile.cpp b/Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglFile.cpp index bedeabd6dc..dff93c44a2 100644 --- a/Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglFile.cpp +++ b/Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglFile.cpp @@ -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; @@ -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 s_perf[kNumPerf]; static unsigned s_connectBuildId; @@ -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()) @@ -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; @@ -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; @@ -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; @@ -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"); @@ -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; } @@ -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; } diff --git a/Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglGame.cpp b/Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglGame.cpp index 8b6d709394..e3a6e8c69f 100644 --- a/Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglGame.cpp +++ b/Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglGame.cpp @@ -55,8 +55,6 @@ namespace Ngl { namespace Game { ***/ struct CliGmConn : hsRefCnt { - LINK(CliGmConn) link; - std::recursive_mutex critsect; AsyncSocket sock; AsyncCancelId cancelId; @@ -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; @@ -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); @@ -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 @@ -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 @@ -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; @@ -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; } @@ -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; } diff --git a/Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglGateKeeper.cpp b/Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglGateKeeper.cpp index 9485306cf9..642ad50954 100644 --- a/Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglGateKeeper.cpp +++ b/Sources/Plasma/PubUtilLib/plNetGameLib/Private/plNglGateKeeper.cpp @@ -85,7 +85,6 @@ struct CliGkConn : hsRefCnt { void Send (const uintptr_t fields[], unsigned count); std::recursive_mutex critsect; - LINK(CliGkConn) link; AsyncSocket sock; NetCli * cli; ST::string name; @@ -184,7 +183,7 @@ enum { static bool s_running; static std::recursive_mutex s_critsect; -static LISTDECL(CliGkConn, link) s_conns; +static CliGkConn* s_conn = nullptr; static CliGkConn * s_active; static std::atomic s_perf[kNumPerf]; @@ -220,8 +219,7 @@ static CliGkConn * GetConnIncRef (const char tag[]) { } //============================================================================ -static void UnlinkAndAbandonConn_CS (CliGkConn * conn) { - s_conns.Unlink(conn); +static void AbandonConn(CliGkConn* conn) { conn->abandoned = true; conn->StopAutoReconnect(); @@ -312,7 +310,9 @@ static void NotifyConnSocketConnectFailed (CliGkConn * 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; @@ -331,7 +331,9 @@ static void NotifyConnSocketDisconnect (CliGkConn * 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; @@ -412,13 +414,13 @@ static void Connect ( { hsLockGuard(s_critsect); - while (CliGkConn * oldConn = s_conns.Head()) { - if (oldConn != conn) - UnlinkAndAbandonConn_CS(oldConn); - else - s_conns.Unlink(oldConn); + if (CliGkConn* oldConn = s_conn) { + s_conn = nullptr; + if (oldConn != conn) { + AbandonConn(oldConn); + } } - s_conns.Link(conn); + s_conn = conn; } Cli2GateKeeper_Connect connect; @@ -523,7 +525,10 @@ void CliGkConn::TimerReconnect () { if (!s_running) { hsLockGuard(s_critsect); - UnlinkAndAbandonConn_CS(this); + if (s_conn == this) { + s_conn = nullptr; + } + AbandonConn(this); } else { Ref("Connecting"); @@ -926,8 +931,10 @@ void GateKeeperDestroy (bool wait) { { hsLockGuard(s_critsect); - while (CliGkConn * conn = s_conns.Head()) - UnlinkAndAbandonConn_CS(conn); + if (CliGkConn* conn = s_conn) { + s_conn = nullptr; + AbandonConn(conn); + } s_active = nullptr; } @@ -994,8 +1001,10 @@ void NetCliGateKeeperStartConnect ( //============================================================================ void NetCliGateKeeperDisconnect () { hsLockGuard(s_critsect); - while (CliGkConn * conn = s_conns.Head()) - UnlinkAndAbandonConn_CS(conn); + if (CliGkConn* conn = s_conn) { + s_conn = nullptr; + AbandonConn(conn); + } s_active = nullptr; }