From f75dff4cbcb6d978c3a16b11c7311058cd77f9da Mon Sep 17 00:00:00 2001 From: jyelon Date: Sun, 1 Mar 2026 06:03:05 -0500 Subject: [PATCH] Some code cleanup in the sockets module. --- Source/Integration/Common.h | 1 + Source/Integration/LuprexSockets.cpp | 108 +++++++++------------------ Source/Integration/LuprexSockets.h | 1 - 3 files changed, 37 insertions(+), 73 deletions(-) diff --git a/Source/Integration/Common.h b/Source/Integration/Common.h index 96b61abb..0daa9d70 100644 --- a/Source/Integration/Common.h +++ b/Source/Integration/Common.h @@ -149,3 +149,4 @@ public: UFUNCTION(BlueprintImplementableEvent, BlueprintCallable, Category = "Luprex|Miscellaneous") void ReadLuaConfiguration(UlxLuaValues *Config); }; + diff --git a/Source/Integration/LuprexSockets.cpp b/Source/Integration/LuprexSockets.cpp index 1e4b4a6f..779dee5c 100644 --- a/Source/Integration/LuprexSockets.cpp +++ b/Source/Integration/LuprexSockets.cpp @@ -3,6 +3,7 @@ #include "LuprexGameModeBase.h" #include "lpx-enginewrapper.hpp" #include "lpx-drvutil.hpp" +#include #include "Sockets.h" #include "SocketTypes.h" #include "SocketSubsystem.h" @@ -96,15 +97,15 @@ class FlxSocketsI; // ///////////////////////////////////////////////////////////////// -class FLpxListener +class FlxListener { public: FlxSocketsI *LSI; int BoundPort; FSocket* Socket; - FLpxListener(FlxSocketsI *lsi, int bp, FSocket* sock); - ~FLpxListener(); + FlxListener(FlxSocketsI *lsi, int bp, FSocket* sock); + ~FlxListener(); void AcceptConnection(); }; @@ -122,7 +123,7 @@ enum EChanState { CHAN_SSL_READWRITE, }; -class FLpxChannel +class FlxChannel { public: FlxSocketsI* LSI; @@ -175,9 +176,9 @@ public: // buffer, that's inexplicable and therefore serious. void CloseChannelIfSSLErrorIsSerious(int retval); - FLpxChannel(FlxSocketsI *lsi, FSocket* sock, int chid, SSL_CTX* ctx, EChanState state); - FLpxChannel() : FLpxChannel(nullptr, nullptr, 0, nullptr, CHAN_INACTIVE) {} - ~FLpxChannel() { } + FlxChannel(FlxSocketsI *lsi, FSocket* sock, int chid, SSL_CTX* ctx, EChanState state); + FlxChannel() : FlxChannel(nullptr, nullptr, 0, nullptr, CHAN_INACTIVE) {} + ~FlxChannel() { } }; ///////////////////////////////////////////////////////////////// @@ -194,13 +195,13 @@ public: // This pointer is NULL except when inside // one of the methods that accepts a LockedWrapper. - EngineWrapper* Luprex; + EngineWrapper* Luprex = nullptr; // A general-purpose character buffer. char ChBuf[DRV_SHORTSTRING_SIZE]; - TArray Channels; - TArray Listeners; + TArray Channels; + TArray Listeners; // Pointer to the socket subsystem. ISocketSubsystem* Subsys; @@ -244,36 +245,6 @@ public: // ///////////////////////////////////////////////////////////////// -#ifdef __linux__ -inline static void strerror_helper(int status, int errnum, char errbuf[256]) { - if (status != 0) { - snprintf(errbuf, 256, "unknown errno %d", errnum); - } -} - -inline static void strerror_helper(const char *result, int errnum, char errbuf[256]) { - if (result != errbuf) { - snprintf(errbuf, 256, "%s", result); - } -} - -static std::string strerror_str(int errnum) { - char buf[256]; - auto rval = strerror_r(errnum, buf, 256); - strerror_helper(rval, errnum, buf); - return buf; -} -#else -static std::string strerror_str(int errnum) { - char buf[256]; - int status = strerror_s(buf, 256, errnum); - if (status != 0) - { - snprintf(buf, 256, "unknown errno %d", errnum); - } - return buf; -} -#endif static FSocket* OpenConnection(ISocketSubsystem *subsys, const std::string& host, const std::string& port, std::string& err) @@ -375,7 +346,7 @@ static std::string SSLFullErrorString() { ERR_print_errors(b); char* data; int ndata = BIO_get_mem_data(b, &data); - std::string result(' ', ndata); + std::string result(ndata, ' '); memcpy(&result[0], data, ndata); BIO_free(b); return result; @@ -400,11 +371,11 @@ static std::string SSLErrorString() { return rc; } else { - return strerror_str(ERR_GET_REASON(code)); + return std::system_category().message(ERR_GET_REASON(code)); } } else if (terrno != 0) { - return strerror_str(terrno); + return std::system_category().message(terrno); } else { return ""; @@ -428,7 +399,9 @@ static SSL_CTX* SSLNewContext(int verify, const SSL_METHOD *method, BIO *tracebi #ifdef __linux__ static std::string SSLLoadCertificateAuthorities(SSL_CTX* ctx) { - check(SSL_CTX_set_default_verify_paths(ctx) == 1); + if (SSL_CTX_set_default_verify_paths(ctx) != 1) { + return "Could not load default certificate authority paths."; + } return ""; } #else @@ -538,7 +511,7 @@ static void BIODiscard(BIO* b, int nbytes, char* chbuf) { ///////////////////////////////////////////////////////////////// #pragma optimize("", off) -FLpxChannel::FLpxChannel(FlxSocketsI* lsi, FSocket* sock, int chid, SSL_CTX* ctx, EChanState st) +FlxChannel::FlxChannel(FlxSocketsI* lsi, FSocket* sock, int chid, SSL_CTX* ctx, EChanState st) { LSI = lsi; ChannelID = chid; @@ -557,7 +530,7 @@ FLpxChannel::FLpxChannel(FlxSocketsI* lsi, FSocket* sock, int chid, SSL_CTX* ctx State = st; } -void FLpxChannel::Close(std::string_view err) { +void FlxChannel::Close(std::string_view err) { // Close and release the SSL channel. // This frees the BIO objects as well. if (SSLState != nullptr) { @@ -595,7 +568,7 @@ void FLpxChannel::Close(std::string_view err) { } #pragma optimize("", off) -void FLpxChannel::TransferSocketToRecvBIO() { +void FlxChannel::TransferSocketToRecvBIO() { if ((State == CHAN_INACTIVE) || RecvFail) { return; } @@ -614,7 +587,7 @@ void FLpxChannel::TransferSocketToRecvBIO() { } } -void FLpxChannel::TransferSendBIOToSocket() { +void FlxChannel::TransferSendBIOToSocket() { if ((State == CHAN_INACTIVE) || SendFail) { return; } @@ -638,7 +611,7 @@ void FLpxChannel::TransferSendBIOToSocket() { } } -void FLpxChannel::CloseChannelIfSSLErrorIsSerious(int retval) { +void FlxChannel::CloseChannelIfSSLErrorIsSerious(int retval) { int error = SSL_get_error(SSLState, retval); // Should never have write errors, because we're @@ -660,7 +633,7 @@ void FLpxChannel::CloseChannelIfSSLErrorIsSerious(int retval) { Close(errstr); } -void FLpxChannel::AdvanceConnecting() +void FlxChannel::AdvanceConnecting() { int retval = SSL_connect(SSLState); if (retval == 1) @@ -674,7 +647,7 @@ void FLpxChannel::AdvanceConnecting() } #pragma optimize("", off) -void FLpxChannel::AdvanceAccepting() +void FlxChannel::AdvanceAccepting() { int retval = SSL_accept(SSLState); if (retval == 1) @@ -688,7 +661,7 @@ void FLpxChannel::AdvanceAccepting() LSI->LogTrace(); } -void FLpxChannel::AdvanceReadWrite() +void FlxChannel::AdvanceReadWrite() { // Read as much as we can, which of course will be limited // by the fact that the recv_bio contains finite data. @@ -746,7 +719,7 @@ void FLpxChannel::AdvanceReadWrite() } #pragma optimize("", off) -void FLpxChannel::Advance() +void FlxChannel::Advance() { check(State != CHAN_INACTIVE); @@ -799,14 +772,14 @@ void FLpxChannel::Advance() // ///////////////////////////////////////////////////////////////// -FLpxListener::FLpxListener(FlxSocketsI *lsi, int bp, FSocket *sock) +FlxListener::FlxListener(FlxSocketsI *lsi, int bp, FSocket *sock) { LSI = lsi; BoundPort = bp; Socket = sock; } -FLpxListener::~FLpxListener() +FlxListener::~FlxListener() { if (Socket != nullptr) { @@ -817,7 +790,7 @@ FLpxListener::~FLpxListener() } #pragma optimize("", off) -void FLpxListener::AcceptConnection() +void FlxListener::AcceptConnection() { FSocket* csocket = Socket->Accept(TEXT("Incoming Connection")); if (csocket == nullptr) @@ -847,7 +820,7 @@ void FlxSocketsI::SetError(const std::string& s) FlxSocketsI::FlxSocketsI(FlxLockedWrapper &w) { // We retain this pointer only so long as we have the wrapper lock. - Luprex = w.Get(); + TGuardValue GuardLuprex(Luprex, w.Get()); // This function is nonreentrant. It's not clear whether // this is needed - it may be initialized elsewhere in unreal. @@ -884,18 +857,15 @@ FlxSocketsI::FlxSocketsI(FlxLockedWrapper &w) names = names + " " + name; } HandleListenPorts(); - - // We're losing the wrapper lock, so set the pointer to nullptr. - Luprex = nullptr; } void FlxSocketsI::ForceCloseEverything(FlxLockedWrapper& w) { // We retain this pointer only so long as we have the wrapper lock. - Luprex = w.Get(); + TGuardValue GuardLuprex(Luprex, w.Get()); // Close all channels - for (FLpxChannel& chan : Channels) { + for (FlxChannel& chan : Channels) { chan.Close("Force Close Everything"); } @@ -904,9 +874,6 @@ void FlxSocketsI::ForceCloseEverything(FlxLockedWrapper& w) // All channels should be gone now. check(Channels.IsEmpty()); - - // We're losing the wrapper lock, so set the pointer to nullptr. - Luprex = nullptr; } FlxSocketsI::~FlxSocketsI() @@ -944,7 +911,7 @@ void FlxSocketsI::LogTrace() bool FlxSocketsI::ListeningOnPort(int p) { - for (const FLpxListener& l : Listeners) + for (const FlxListener& l : Listeners) { if (l.BoundPort == p) return true; } @@ -1038,13 +1005,13 @@ void FlxSocketsI::RemoveInactiveChannels() void FlxSocketsI::HandleSocketInputOutput() { - for (FLpxListener& listener : Listeners) + for (FlxListener& listener : Listeners) { listener.AcceptConnection(); } // Peek output buffers and determine channel release flags. - for (FLpxChannel& chan : Channels) + for (FlxChannel& chan : Channels) { chan.Advance(); } @@ -1056,13 +1023,10 @@ void FlxSocketsI::HandleSocketInputOutput() void FlxSocketsI::Update(FlxLockedWrapper &w) { // We retain this pointer only so long as we have the wrapper lock. - Luprex = w.Get(); + TGuardValue GuardLuprex(Luprex, w.Get()); HandleNewOutgoingSockets(); HandleSocketInputOutput(); - - // We're losing the wrapper lock, so set the pointer to nullptr. - Luprex = nullptr; } FlxSockets* FlxSockets::Create(FlxLockedWrapper &w) diff --git a/Source/Integration/LuprexSockets.h b/Source/Integration/LuprexSockets.h index aa55dc1c..33543ef9 100644 --- a/Source/Integration/LuprexSockets.h +++ b/Source/Integration/LuprexSockets.h @@ -24,7 +24,6 @@ // implements all the behavior. // -class FlxSockets; struct EngineWrapper; class FlxSockets