Skip to content

Commit

Permalink
Cleanup the g_PortManager shutdown process
Browse files Browse the repository at this point in the history
Following up comment by @anr2me in #17744, to be merged before that one.
  • Loading branch information
hrydgard committed Jul 20, 2023
1 parent 52d30c0 commit b54d5ab
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
10 changes: 4 additions & 6 deletions Core/Util/PortManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ PortManager::PortManager():
m_InitState(UPNP_INITSTATE_NONE),
m_LocalPort(UPNP_LOCAL_PORT_ANY),
m_leaseDuration("43200") {
// Since WSAStartup can be used multiple times it should be safe to do this right?
net::Init();
}

PortManager::~PortManager() {
// FIXME: On Windows it seems using any UPnP functions in this destructor that gets triggered when exiting PPSSPP will resulting to UPNPCOMMAND_HTTP_ERROR due to early WSACleanup (miniupnpc was getting WSANOTINITIALISED internally)
}

void PortManager::Shutdown() {
Clear();
Restore();
Terminate();
net::Shutdown();
}

void PortManager::Terminate() {
Expand Down Expand Up @@ -502,9 +502,7 @@ int upnpService(const unsigned int timeout)

// Cleaning up regardless of g_Config.bEnableUPnP to prevent lingering open ports on the router
if (g_PortManager.GetInitState() == UPNP_INITSTATE_DONE) {
g_PortManager.Clear();
g_PortManager.Restore();
g_PortManager.Terminate();
g_PortManager.Shutdown();
}

// Should we ingore any leftover UPnP requests? instead of processing it on the next game start
Expand Down
15 changes: 9 additions & 6 deletions Core/Util/PortManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ class PortManager {
// timeout: milliseconds to wait for a router to respond (default = 2000 ms)
bool Initialize(const unsigned int timeout = 2000);

// Uninitialize/Reset the state
void Terminate();

// Get UPnP Initialization status
int GetInitState();

Expand All @@ -95,16 +92,22 @@ class PortManager {
// Remove a port mapping (external port)
bool Remove(const char* protocol, unsigned short port);

// Call on exit. Does a full shutdown.
void Shutdown();

private:
// Retrieves port lists mapped by PPSSPP for current LAN IP & other's applications
bool RefreshPortList();

// Removes any lingering mapped ports created by PPSSPP (including from previous crashes)
bool Clear();

// Restore ports mapped by others that were taken by PPSSPP, better used after Clear()
bool Restore();

// Retrieves port lists mapped by PPSSPP for current LAN IP & other's applications
bool RefreshPortList();
// Uninitialize/Reset the state
void Terminate();

protected:
struct UPNPUrls* urls = NULL;
struct IGDdatas* datas = NULL;

Expand Down
3 changes: 3 additions & 0 deletions UI/NativeApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
#include "Core/HLE/proAdhoc.h"
#include "Core/HW/MemoryStick.h"
#include "Core/Util/GameManager.h"
#include "Core/Util/PortManager.h"
#include "Core/Util/AudioFormat.h"
#include "Core/WebServer.h"
#include "Core/TiltEventProcessor.h"
Expand Down Expand Up @@ -1409,6 +1410,8 @@ void NativeShutdown() {
System_ExitApp();
#endif

g_PortManager.Shutdown();

net::Shutdown();

g_Discord.Shutdown();
Expand Down

0 comments on commit b54d5ab

Please sign in to comment.