Some code cleanup in the sockets module.

This commit is contained in:
2026-03-01 06:03:05 -05:00
parent 08d9ab2823
commit f75dff4cbc
3 changed files with 37 additions and 73 deletions

View File

@@ -149,3 +149,4 @@ public:
UFUNCTION(BlueprintImplementableEvent, BlueprintCallable, Category = "Luprex|Miscellaneous") UFUNCTION(BlueprintImplementableEvent, BlueprintCallable, Category = "Luprex|Miscellaneous")
void ReadLuaConfiguration(UlxLuaValues *Config); void ReadLuaConfiguration(UlxLuaValues *Config);
}; };

View File

@@ -3,6 +3,7 @@
#include "LuprexGameModeBase.h" #include "LuprexGameModeBase.h"
#include "lpx-enginewrapper.hpp" #include "lpx-enginewrapper.hpp"
#include "lpx-drvutil.hpp" #include "lpx-drvutil.hpp"
#include <system_error>
#include "Sockets.h" #include "Sockets.h"
#include "SocketTypes.h" #include "SocketTypes.h"
#include "SocketSubsystem.h" #include "SocketSubsystem.h"
@@ -96,15 +97,15 @@ class FlxSocketsI;
// //
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
class FLpxListener class FlxListener
{ {
public: public:
FlxSocketsI *LSI; FlxSocketsI *LSI;
int BoundPort; int BoundPort;
FSocket* Socket; FSocket* Socket;
FLpxListener(FlxSocketsI *lsi, int bp, FSocket* sock); FlxListener(FlxSocketsI *lsi, int bp, FSocket* sock);
~FLpxListener(); ~FlxListener();
void AcceptConnection(); void AcceptConnection();
}; };
@@ -122,7 +123,7 @@ enum EChanState {
CHAN_SSL_READWRITE, CHAN_SSL_READWRITE,
}; };
class FLpxChannel class FlxChannel
{ {
public: public:
FlxSocketsI* LSI; FlxSocketsI* LSI;
@@ -175,9 +176,9 @@ public:
// buffer, that's inexplicable and therefore serious. // buffer, that's inexplicable and therefore serious.
void CloseChannelIfSSLErrorIsSerious(int retval); void CloseChannelIfSSLErrorIsSerious(int retval);
FLpxChannel(FlxSocketsI *lsi, FSocket* sock, int chid, SSL_CTX* ctx, EChanState state); FlxChannel(FlxSocketsI *lsi, FSocket* sock, int chid, SSL_CTX* ctx, EChanState state);
FLpxChannel() : FLpxChannel(nullptr, nullptr, 0, nullptr, CHAN_INACTIVE) {} FlxChannel() : FlxChannel(nullptr, nullptr, 0, nullptr, CHAN_INACTIVE) {}
~FLpxChannel() { } ~FlxChannel() { }
}; };
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
@@ -194,13 +195,13 @@ public:
// This pointer is NULL except when inside // This pointer is NULL except when inside
// one of the methods that accepts a LockedWrapper. // one of the methods that accepts a LockedWrapper.
EngineWrapper* Luprex; EngineWrapper* Luprex = nullptr;
// A general-purpose character buffer. // A general-purpose character buffer.
char ChBuf[DRV_SHORTSTRING_SIZE]; char ChBuf[DRV_SHORTSTRING_SIZE];
TArray<FLpxChannel> Channels; TArray<FlxChannel> Channels;
TArray<FLpxListener> Listeners; TArray<FlxListener> Listeners;
// Pointer to the socket subsystem. // Pointer to the socket subsystem.
ISocketSubsystem* Subsys; 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) 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); ERR_print_errors(b);
char* data; char* data;
int ndata = BIO_get_mem_data(b, &data); int ndata = BIO_get_mem_data(b, &data);
std::string result(' ', ndata); std::string result(ndata, ' ');
memcpy(&result[0], data, ndata); memcpy(&result[0], data, ndata);
BIO_free(b); BIO_free(b);
return result; return result;
@@ -400,11 +371,11 @@ static std::string SSLErrorString() {
return rc; return rc;
} }
else { else {
return strerror_str(ERR_GET_REASON(code)); return std::system_category().message(ERR_GET_REASON(code));
} }
} }
else if (terrno != 0) { else if (terrno != 0) {
return strerror_str(terrno); return std::system_category().message(terrno);
} }
else { else {
return ""; return "";
@@ -428,7 +399,9 @@ static SSL_CTX* SSLNewContext(int verify, const SSL_METHOD *method, BIO *tracebi
#ifdef __linux__ #ifdef __linux__
static std::string SSLLoadCertificateAuthorities(SSL_CTX* ctx) { 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 ""; return "";
} }
#else #else
@@ -538,7 +511,7 @@ static void BIODiscard(BIO* b, int nbytes, char* chbuf) {
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
#pragma optimize("", off) #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; LSI = lsi;
ChannelID = chid; ChannelID = chid;
@@ -557,7 +530,7 @@ FLpxChannel::FLpxChannel(FlxSocketsI* lsi, FSocket* sock, int chid, SSL_CTX* ctx
State = st; State = st;
} }
void FLpxChannel::Close(std::string_view err) { void FlxChannel::Close(std::string_view err) {
// Close and release the SSL channel. // Close and release the SSL channel.
// This frees the BIO objects as well. // This frees the BIO objects as well.
if (SSLState != nullptr) { if (SSLState != nullptr) {
@@ -595,7 +568,7 @@ void FLpxChannel::Close(std::string_view err) {
} }
#pragma optimize("", off) #pragma optimize("", off)
void FLpxChannel::TransferSocketToRecvBIO() { void FlxChannel::TransferSocketToRecvBIO() {
if ((State == CHAN_INACTIVE) || RecvFail) { if ((State == CHAN_INACTIVE) || RecvFail) {
return; return;
} }
@@ -614,7 +587,7 @@ void FLpxChannel::TransferSocketToRecvBIO() {
} }
} }
void FLpxChannel::TransferSendBIOToSocket() { void FlxChannel::TransferSendBIOToSocket() {
if ((State == CHAN_INACTIVE) || SendFail) { if ((State == CHAN_INACTIVE) || SendFail) {
return; 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); int error = SSL_get_error(SSLState, retval);
// Should never have write errors, because we're // Should never have write errors, because we're
@@ -660,7 +633,7 @@ void FLpxChannel::CloseChannelIfSSLErrorIsSerious(int retval) {
Close(errstr); Close(errstr);
} }
void FLpxChannel::AdvanceConnecting() void FlxChannel::AdvanceConnecting()
{ {
int retval = SSL_connect(SSLState); int retval = SSL_connect(SSLState);
if (retval == 1) if (retval == 1)
@@ -674,7 +647,7 @@ void FLpxChannel::AdvanceConnecting()
} }
#pragma optimize("", off) #pragma optimize("", off)
void FLpxChannel::AdvanceAccepting() void FlxChannel::AdvanceAccepting()
{ {
int retval = SSL_accept(SSLState); int retval = SSL_accept(SSLState);
if (retval == 1) if (retval == 1)
@@ -688,7 +661,7 @@ void FLpxChannel::AdvanceAccepting()
LSI->LogTrace(); LSI->LogTrace();
} }
void FLpxChannel::AdvanceReadWrite() void FlxChannel::AdvanceReadWrite()
{ {
// Read as much as we can, which of course will be limited // Read as much as we can, which of course will be limited
// by the fact that the recv_bio contains finite data. // by the fact that the recv_bio contains finite data.
@@ -746,7 +719,7 @@ void FLpxChannel::AdvanceReadWrite()
} }
#pragma optimize("", off) #pragma optimize("", off)
void FLpxChannel::Advance() void FlxChannel::Advance()
{ {
check(State != CHAN_INACTIVE); 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; LSI = lsi;
BoundPort = bp; BoundPort = bp;
Socket = sock; Socket = sock;
} }
FLpxListener::~FLpxListener() FlxListener::~FlxListener()
{ {
if (Socket != nullptr) if (Socket != nullptr)
{ {
@@ -817,7 +790,7 @@ FLpxListener::~FLpxListener()
} }
#pragma optimize("", off) #pragma optimize("", off)
void FLpxListener::AcceptConnection() void FlxListener::AcceptConnection()
{ {
FSocket* csocket = Socket->Accept(TEXT("Incoming Connection")); FSocket* csocket = Socket->Accept(TEXT("Incoming Connection"));
if (csocket == nullptr) if (csocket == nullptr)
@@ -847,7 +820,7 @@ void FlxSocketsI::SetError(const std::string& s)
FlxSocketsI::FlxSocketsI(FlxLockedWrapper &w) FlxSocketsI::FlxSocketsI(FlxLockedWrapper &w)
{ {
// We retain this pointer only so long as we have the wrapper lock. // We retain this pointer only so long as we have the wrapper lock.
Luprex = w.Get(); TGuardValue<EngineWrapper*> GuardLuprex(Luprex, w.Get());
// This function is nonreentrant. It's not clear whether // This function is nonreentrant. It's not clear whether
// this is needed - it may be initialized elsewhere in unreal. // this is needed - it may be initialized elsewhere in unreal.
@@ -884,18 +857,15 @@ FlxSocketsI::FlxSocketsI(FlxLockedWrapper &w)
names = names + " " + name; names = names + " " + name;
} }
HandleListenPorts(); HandleListenPorts();
// We're losing the wrapper lock, so set the pointer to nullptr.
Luprex = nullptr;
} }
void FlxSocketsI::ForceCloseEverything(FlxLockedWrapper& w) void FlxSocketsI::ForceCloseEverything(FlxLockedWrapper& w)
{ {
// We retain this pointer only so long as we have the wrapper lock. // We retain this pointer only so long as we have the wrapper lock.
Luprex = w.Get(); TGuardValue<EngineWrapper*> GuardLuprex(Luprex, w.Get());
// Close all channels // Close all channels
for (FLpxChannel& chan : Channels) { for (FlxChannel& chan : Channels) {
chan.Close("Force Close Everything"); chan.Close("Force Close Everything");
} }
@@ -904,9 +874,6 @@ void FlxSocketsI::ForceCloseEverything(FlxLockedWrapper& w)
// All channels should be gone now. // All channels should be gone now.
check(Channels.IsEmpty()); check(Channels.IsEmpty());
// We're losing the wrapper lock, so set the pointer to nullptr.
Luprex = nullptr;
} }
FlxSocketsI::~FlxSocketsI() FlxSocketsI::~FlxSocketsI()
@@ -944,7 +911,7 @@ void FlxSocketsI::LogTrace()
bool FlxSocketsI::ListeningOnPort(int p) bool FlxSocketsI::ListeningOnPort(int p)
{ {
for (const FLpxListener& l : Listeners) for (const FlxListener& l : Listeners)
{ {
if (l.BoundPort == p) return true; if (l.BoundPort == p) return true;
} }
@@ -1038,13 +1005,13 @@ void FlxSocketsI::RemoveInactiveChannels()
void FlxSocketsI::HandleSocketInputOutput() void FlxSocketsI::HandleSocketInputOutput()
{ {
for (FLpxListener& listener : Listeners) for (FlxListener& listener : Listeners)
{ {
listener.AcceptConnection(); listener.AcceptConnection();
} }
// Peek output buffers and determine channel release flags. // Peek output buffers and determine channel release flags.
for (FLpxChannel& chan : Channels) for (FlxChannel& chan : Channels)
{ {
chan.Advance(); chan.Advance();
} }
@@ -1056,13 +1023,10 @@ void FlxSocketsI::HandleSocketInputOutput()
void FlxSocketsI::Update(FlxLockedWrapper &w) void FlxSocketsI::Update(FlxLockedWrapper &w)
{ {
// We retain this pointer only so long as we have the wrapper lock. // We retain this pointer only so long as we have the wrapper lock.
Luprex = w.Get(); TGuardValue<EngineWrapper*> GuardLuprex(Luprex, w.Get());
HandleNewOutgoingSockets(); HandleNewOutgoingSockets();
HandleSocketInputOutput(); HandleSocketInputOutput();
// We're losing the wrapper lock, so set the pointer to nullptr.
Luprex = nullptr;
} }
FlxSockets* FlxSockets::Create(FlxLockedWrapper &w) FlxSockets* FlxSockets::Create(FlxLockedWrapper &w)

View File

@@ -24,7 +24,6 @@
// implements all the behavior. // implements all the behavior.
// //
class FlxSockets;
struct EngineWrapper; struct EngineWrapper;
class FlxSockets class FlxSockets