Skip to content

Commit

Permalink
Make UPNP optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
klei1984 committed Jan 21, 2024
1 parent 0317272 commit b6b2522
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
22 changes: 19 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ project(max LANGUAGES C CXX)
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)

option(MAX_BUILD_TESTS "Build unit tests by default" ON)
option(MAX_ENABLE_UPNP "Use miniupnpc library" ON)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
Expand All @@ -27,13 +28,25 @@ set_target_properties(${PROJECT_NAME} PROPERTIES
COMPILE_FLAGS "${MAX_CFLAGS} -DGAME_VERSION_STRING=\"\\\"v${GAME_VERSION_STRING}\\\"\" -DGAME_VERSION_MAJOR=\"${GAME_VERSION_MAJOR}\" -DGAME_VERSION_MINOR=\"${GAME_VERSION_MINOR}\" -DGAME_VERSION_PATCH=\"${GAME_VERSION_PATCH}\" -DSDL_ASSERT_LEVEL=2"
)

if(MAX_ENABLE_UPNP)
target_compile_definitions(${PROJECT_NAME} PUBLIC MAX_ENABLE_UPNP=1)
endif()

if(NOT BUILD_SHARED_LIBS)
set(${PROJECT_NAME}_deps SDL2::SDL2main Iconv::Iconv Freetype::Freetype Miniaudio::Miniaudio Enet::Enet miniupnpc::miniupnpc SDL2::SDL2-static)
set(${PROJECT_NAME}_deps SDL2::SDL2main Iconv::Iconv Freetype::Freetype Miniaudio::Miniaudio Enet::Enet SDL2::SDL2-static)

if(MAX_ENABLE_UPNP)
list(APPEND ${PROJECT_NAME}_deps miniupnpc::miniupnpc)
endif()

target_link_options(${PROJECT_NAME} PUBLIC -static -static-libgcc -static-libstdc++)
target_link_libraries(${PROJECT_NAME} PRIVATE ${${PROJECT_NAME}_deps})
else()
set(${PROJECT_NAME}_deps SDL2::SDL2main Iconv::Iconv Freetype::Freetype Miniaudio::Miniaudio Enet::Enet miniupnpc::miniupnpc SDL2::SDL2)
set(${PROJECT_NAME}_deps SDL2::SDL2main Iconv::Iconv Freetype::Freetype Miniaudio::Miniaudio Enet::Enet SDL2::SDL2)

if(MAX_ENABLE_UPNP)
list(APPEND ${PROJECT_NAME}_deps miniupnpc::miniupnpc)
endif()

target_link_libraries(${PROJECT_NAME} PRIVATE ${${PROJECT_NAME}_deps})
endif()
Expand All @@ -46,7 +59,10 @@ include(miniaudio)

set(ENET_RELEASE ON)
include(enet)
include(miniupnp)

if(MAX_ENABLE_UPNP)
include(miniupnp)
endif()

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_OBJCOPY} ARGS --only-keep-debug $<TARGET_FILE:${PROJECT_NAME}> ${PROJECT_NAME}.debug
Expand Down
20 changes: 19 additions & 1 deletion src/transport_udp_default.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
#include <SDL.h>
#include <SDL_thread.h>
#include <enet/enet.h>

#if defined(MAX_ENABLE_UPNP)
#include <miniupnpc.h>
#include <upnpcommands.h>
#endif

#include <utility>

Expand Down Expand Up @@ -58,7 +61,10 @@ static constexpr uint32_t TransportUdpDefault_MaximumPeers = 32;
static constexpr uint32_t TransportUdpDefault_Channels = TRANSPORT_CHANNEL_COUNT;

static constexpr uint32_t TransportUdpDefault_UpnpDeviceResponseTimeout = 3000;

#if defined(MAX_ENABLE_UPNP)
static_assert(MINIUPNPC_API_VERSION == 17, "API changes of MINIUPNP library shall be reviewed.");
#endif

enum {
TRANSPORT_IGDSTATUS_NOIGD,
Expand Down Expand Up @@ -116,10 +122,13 @@ static inline void TransportUdpDefault_ProcessTpPacket(struct TransportUdpDefaul
static inline void TransportUdpDefault_ProcessApplPacket(struct TransportUdpDefault_Context* const context,
ENetPeer* const peer, ENetPacket* const enet_packet);
static inline void TransportUdpDefault_TransmitApplPackets(struct TransportUdpDefault_Context* const context);

#if defined(MAX_ENABLE_UPNP)
static void TransportUdpDefault_UpnpInit(struct TransportUdpDefault_Context* const context) noexcept;
static void TransportUdpDefault_UpnpDeinit(struct TransportUdpDefault_Context* const context) noexcept;
static bool TransportUdpDefault_UpnpAddPortMapping(struct UpnpDevice& device, ENetAddress& host_address) noexcept;
static bool TransportUdpDefault_UpnpRemovePortMapping(struct UpnpDevice& device, ENetAddress& host_address) noexcept;
#endif

TransportUdpDefault::~TransportUdpDefault() {
Deinit();
Expand Down Expand Up @@ -494,6 +503,7 @@ void TransportUdpDefault_TransmitApplPackets(struct TransportUdpDefault_Context*
}
}

#if defined(MAX_ENABLE_UPNP)
void TransportUdpDefault_UpnpInit(struct TransportUdpDefault_Context* const context) noexcept {
struct UPNPDev* device_list{nullptr};
int discovery_result{UPNPDISCOVER_SUCCESS};
Expand Down Expand Up @@ -598,6 +608,7 @@ bool TransportUdpDefault_UpnpRemovePortMapping(struct UpnpDevice& device, ENetAd

return result;
}
#endif

int TransportUdpDefault_ServerFunction(void* data) noexcept {
auto context = reinterpret_cast<struct TransportUdpDefault_Context*>(data);
Expand All @@ -608,8 +619,9 @@ int TransportUdpDefault_ServerFunction(void* data) noexcept {
enet_host_create(&context->ServerAddress, TransportUdpDefault_MaximumPeers, TransportUdpDefault_Channels, 0, 0);

if (context->Host) {
#if defined(MAX_ENABLE_UPNP)
TransportUdpDefault_UpnpInit(context);

#endif
context->NetState = TRANSPORT_NETSTATE_CONNECTED;

for (;;) {
Expand Down Expand Up @@ -645,7 +657,9 @@ int TransportUdpDefault_ServerFunction(void* data) noexcept {
}
}

#if defined(MAX_ENABLE_UPNP)
TransportUdpDefault_UpnpDeinit(context);
#endif

enet_host_destroy(context->Host);

Expand All @@ -665,7 +679,9 @@ int TransportUdpDefault_ClientFunction(void* data) noexcept {
context->Host = enet_host_create(nullptr, TransportUdpDefault_MaximumPeers, TransportUdpDefault_Channels, 0, 0);

if (context->Host) {
#if defined(MAX_ENABLE_UPNP)
TransportUdpDefault_UpnpInit(context);
#endif

ENetPeer* server_peer =
enet_host_connect(context->Host, &context->ServerAddress, TransportUdpDefault_Channels, 0);
Expand Down Expand Up @@ -720,7 +736,9 @@ int TransportUdpDefault_ClientFunction(void* data) noexcept {
}
}

#if defined(MAX_ENABLE_UPNP)
TransportUdpDefault_UpnpDeinit(context);
#endif

enet_host_destroy(context->Host);

Expand Down

0 comments on commit b6b2522

Please sign in to comment.