From 8694d16149db1402f89edf7fbf0c4142013aa3ac Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 25 Aug 2016 03:15:37 +0200 Subject: [PATCH] Import better-enums --- .gitmodules | 3 +++ CMakeLists.txt | 1 + lib/better-enums | 1 + src/main.cpp | 21 +++++++++++++-------- 4 files changed, 18 insertions(+), 8 deletions(-) create mode 160000 lib/better-enums diff --git a/.gitmodules b/.gitmodules index b9fa25b..85252af 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "lib/miniupnp"] path = lib/miniupnp url = https://github.com/miniupnp/miniupnp.git +[submodule "lib/better-enums"] + path = lib/better-enums + url = https://github.com/aantron/better-enums diff --git a/CMakeLists.txt b/CMakeLists.txt index df9508b..ea24a74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,7 @@ add_executable(${PROJECT_NAME} target_include_directories(${PROJECT_NAME} PRIVATE lib/miniupnp/miniupnpc + PRIVATE lib/better-enums ) target_link_libraries(${PROJECT_NAME} diff --git a/lib/better-enums b/lib/better-enums new file mode 160000 index 0000000..37d8f98 --- /dev/null +++ b/lib/better-enums @@ -0,0 +1 @@ +Subproject commit 37d8f987ca939af846a2d29a8f8198f9bf3ac4b7 diff --git a/src/main.cpp b/src/main.cpp index 199c2bd..a5b0d6d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include "miniupnpc.h" #include "upnpcommands.h" #include "upnperrors.h" +#include "enum.h" #include #include #include @@ -14,13 +15,13 @@ # define KUU_VERBOSE #endif -namespace { - enum Protocol { - ProtocolUDP, - ProtocolTCP, - ProtocolOther - }; +BETTER_ENUM (Protocol, uint8_t, + UDP, + TCP, + Other +); +namespace { class UPNPUrlsResource { public: enum IGDReply { @@ -56,7 +57,8 @@ namespace { { std::array externaladdr; const auto r = UPNP_GetExternalIPAddress(m_urls.controlURL, m_data.first.servicetype, externaladdr.data()); - m_externaladdr = externaladdr.data(); + if (UPNPCOMMAND_SUCCESS != r) + m_externaladdr = externaladdr.data(); } } @@ -82,6 +84,8 @@ namespace { }; struct Redirection { + Redirection() : protocol(Protocol::Other) {} + std::string internal_client; std::string remote_host; std::string desc; @@ -162,7 +166,8 @@ namespace { if (not r) { Redirection redir; - redir.protocol = (std::string(protocol) == "TCP" ? ProtocolTCP : (std::string(protocol) == "UDP" ? ProtocolUDP : ProtocolOther)); + better_enums::optional proto = Protocol::_from_string_nothrow(protocol); + redir.protocol = (proto ? *proto : +Protocol::Other); redir.internal_client = intClient; redir.remote_host = rHost; redir.desc = desc;