Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup the g_PortManager shutdown process #17750

Merged
merged 1 commit into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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