Refactor to use LockedWrapper guard

This commit is contained in:
2023-09-03 02:01:32 -04:00
parent 4073c88d08
commit e237c28382
7 changed files with 166 additions and 64 deletions

View File

@@ -172,7 +172,7 @@ public:
FLpxChannel(FLpxSocketsI *lsi, FSocket* sock, int chid, SSL_CTX* ctx, EChanState state);
FLpxChannel() : FLpxChannel(nullptr, nullptr, 0, nullptr, CHAN_INACTIVE) {}
~FLpxChannel() { Close(""); }
~FLpxChannel() { }
};
/////////////////////////////////////////////////////////////////
@@ -187,8 +187,8 @@ public:
// Fatal error status.
std::string FatalError;
// We don't own the wrapper, we just have a pointer to it.
// We require a guarantee that it outlives us.
// This pointer is NULL except when inside
// one of the methods that accepts a LockedWrapper.
EngineWrapper* Luprex;
// A general-purpose character buffer.
@@ -206,7 +206,7 @@ public:
SSL_CTX* ClientSecureCTX;
SSL_CTX* ClientInsecureCTX;
FLpxSocketsI(EngineWrapper* w);
FLpxSocketsI(FLockedWrapper &w);
virtual ~FLpxSocketsI() override;
// Copy the trace to the DPrint output.
@@ -226,8 +226,11 @@ public:
void HandleNewOutgoingSockets();
void HandleSocketInputOutput();
// Force Close Everything.
virtual void ForceCloseEverything(FLockedWrapper& w);
// Main update routine.
virtual void Update() override;
virtual void Update(FLockedWrapper &w) override;
};
/////////////////////////////////////////////////////////////////
@@ -807,8 +810,11 @@ void FLpxSocketsI::SetError(const std::string& s)
}
FLpxSocketsI::FLpxSocketsI(EngineWrapper *w)
FLpxSocketsI::FLpxSocketsI(FLockedWrapper &w)
{
// We retain this pointer only so long as we have the wrapper lock.
Luprex = w.Get();
// This function is nonreentrant. It's not clear whether
// this is needed - it may be initialized elsewhere in unreal.
// It is also not clear that it's safe to do this in the
@@ -816,7 +822,6 @@ FLpxSocketsI::FLpxSocketsI(EngineWrapper *w)
// thread).
SSL_library_init();
Luprex = w;
ServerCTX = nullptr;
ClientSecureCTX = nullptr;
ClientInsecureCTX = nullptr;
@@ -845,27 +850,52 @@ FLpxSocketsI::FLpxSocketsI(EngineWrapper *w)
names = names + " " + name;
}
HandleListenPorts();
// We're losing the wrapper lock, so set the pointer to nullptr.
Luprex = nullptr;
}
void FLpxSocketsI::ForceCloseEverything(FLockedWrapper& w)
{
// We retain this pointer only so long as we have the wrapper lock.
Luprex = w.Get();
// Close all channels
for (FLpxChannel& chan : Channels) {
chan.Close("Force Close Everything");
}
// Delete any channels released by the above.
RemoveInactiveChannels();
// All channels should be gone now.
check(Channels.IsEmpty());
// We're losing the wrapper lock, so set the pointer to nullptr.
Luprex = nullptr;
}
FLpxSocketsI::~FLpxSocketsI()
{
checkf(Channels.IsEmpty(), TEXT("Must call ForceCloseEverything before destructor"));
if (ServerCTX != nullptr)
{
SSL_CTX_free(ServerCTX);
ServerCTX = nullptr;
}
if (ClientSecureCTX != nullptr)
{
SSL_CTX_free(ClientSecureCTX);
ClientSecureCTX = nullptr;
}
if (ClientInsecureCTX != nullptr)
{
SSL_CTX_free(ClientInsecureCTX);
ClientInsecureCTX = nullptr;
}
// Cleanup
//for (ChanInfo& chan : chans_) {
// close_channel(chan, "");
//}
// TODO: Be more thorough.
}
void FLpxSocketsI::DPrintTrace()
@@ -988,13 +1018,19 @@ void FLpxSocketsI::HandleSocketInputOutput()
RemoveInactiveChannels();
}
void FLpxSocketsI::Update()
void FLpxSocketsI::Update(FLockedWrapper &w)
{
// We retain this pointer only so long as we have the wrapper lock.
Luprex = w.Get();
HandleNewOutgoingSockets();
HandleSocketInputOutput();
// We're losing the wrapper lock, so set the pointer to nullptr.
Luprex = nullptr;
}
FLpxSockets* FLpxSockets::Create(EngineWrapper* w)
FLpxSockets* FLpxSockets::Create(FLockedWrapper &w)
{
return new FLpxSocketsI(w);
}