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

Refactor PortMappingManager libplum implementation, add fallback miniupnpc implementation #4023

Merged
merged 8 commits into from
Aug 10, 2024
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@
[submodule "3rdparty/libplum"]
path = 3rdparty/libplum
url = https://github.com/paullouisageneau/libplum.git
[submodule "lib/netplay/3rdparty/miniupnp"]
path = lib/netplay/3rdparty/miniupnp
url = https://github.com/miniupnp/miniupnp.git
1 change: 1 addition & 0 deletions lib/netplay/3rdparty/miniupnp
Submodule miniupnp added at 814e85
29 changes: 29 additions & 0 deletions lib/netplay/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,41 @@ file(GLOB SRC "*.cpp")
find_package (Threads REQUIRED)
find_package (ZLIB REQUIRED)

# Attempt to find Miniupnpc (minimum supported API version = 9)
# NOTE: This is not available on every platform / distro
find_package(Miniupnpc 9)
if(MINIUPNPC_FOUND)
set(WZ_USE_IMPORTED_MINIUPNPC ON)
else()
message(STATUS "Using in-tree Miniupnpc")
set(WZ_USE_IMPORTED_MINIUPNPC OFF)
SET(UPNPC_BUILD_STATIC ON CACHE BOOL "miniupnpc - Build static library" FORCE)
SET(UPNPC_BUILD_SHARED OFF CACHE BOOL "miniupnpc - Build shared library" FORCE)
SET(UPNPC_BUILD_TESTS OFF CACHE BOOL "miniupnpc - Build tests" FORCE)
SET(UPNPC_BUILD_SAMPLE OFF CACHE BOOL "miniupnpc - Build samples" FORCE)
SET(UPNPC_NO_INSTALL TRUE CACHE BOOL "miniupnpc - Disable installation" FORCE)
add_subdirectory(3rdparty/miniupnp/miniupnpc)
set_property(TARGET libminiupnpc-static PROPERTY FOLDER "3rdparty")
if(CMAKE_SYSTEM_NAME MATCHES "Windows" AND (MINGW OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND NOT MSVC)))
target_compile_options(miniupnpc-private INTERFACE -Wno-macro-redefined)
endif()
endif()

add_library(netplay STATIC ${HEADERS} ${SRC} "${_netplay_config_output_file}")
add_dependencies(netplay autorevision_netcodeversion)
set_property(TARGET netplay PROPERTY FOLDER "lib")
include(WZTargetConfiguration)
WZ_TARGET_CONFIGURATION(netplay)
target_link_libraries(netplay PRIVATE framework re2::re2 nlohmann_json plum-static Threads::Threads ZLIB::ZLIB)

if(WZ_USE_IMPORTED_MINIUPNPC)
target_link_libraries(netplay PRIVATE imported-miniupnpc)
else()
# Link with in-tree miniupnpc
target_link_libraries(netplay PRIVATE libminiupnpc-static)
target_include_directories(netplay PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../../3rdparty/miniupnp")
endif()

if(MSVC)
# C4267: 'conversion': conversion from 'type1' to 'type2', possible loss of data // FIXME!!
target_compile_options(netplay PRIVATE "/wd4267")
Expand Down
9 changes: 8 additions & 1 deletion lib/netplay/netplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,13 @@ void NETaddRedirects()
if (!ipv4MappingRequest)
{
debug(LOG_NET, "Failed to create port mapping!");
// Workaround: Delay console message until next main loop iteration, to ensure it gets displayed in the chat box
wzAsyncExecOnMainThread([](){
std::string msg = _("Failed to create port mapping");
msg += "\n";
msg += astringf(_("Manually configure your router/firewall to open port %d!"), NETgetGameserverPort());
addConsoleMessage(msg.c_str(), DEFAULT_JUSTIFY, NOTIFY_MESSAGE);
});
return;
}
debug(LOG_NET, "Port mapping creation is in progress...");
Expand All @@ -1469,7 +1476,7 @@ void NETaddRedirects()
std::string msg;
if (status == PortMappingDiscoveryStatus::TIMEOUT)
{
msg = astringf(_("Failed to create port mapping (timeout after %d seconds)"), PortMappingManager::DISCOVERY_TIMEOUT_SECONDS);
msg = _("Failed to create port mapping (timeout)");
}
else
{
Expand Down
Loading
Loading