Skip to content

Commit

Permalink
re-en: Initialize Winsock with library load
Browse files Browse the repository at this point in the history
  • Loading branch information
ingo-h committed Jun 19, 2024
1 parent 4293b62 commit 267512a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 54 deletions.
43 changes: 0 additions & 43 deletions Upnplib/include/upnplib/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,49 +94,6 @@

namespace upnplib {

/*!
* \brief Initialize and cleanup Microsoft Windows Sockets
* <!-- ================================================ -->
* \ingroup upnplib-socket
*
* Winsock needs to be initialized before using it and it needs to be freed. I
* do that with a class, following the RAII paradigm. Multiple initialization
* doesn't matter. This is managed by the operating system with a counter. It
* ensures that winsock is initialzed only one time and freed with the last
* free call. This is only done on Microsoft Windows.
*/
#ifdef _MSC_VER
class CWSAStartup {
public:
CWSAStartup() {
TRACE2(this, " Construct CWSAStartup")
WSADATA wsaData;
int rc = ::WSAStartup(MAKEWORD(2, 2), &wsaData);
if (rc != 0)
throw std::runtime_error(UPNPLIB_LOGEXCEPT +
"MSG1003: Failed to initialize Windows "
"sockets: WSAStartup() returns " +
std::to_string(rc) + "\n");
}

// No copy constructor
CWSAStartup(const CWSAStartup&) = delete;
// No copy assignment operator
CWSAStartup& operator=(CWSAStartup) = delete;

virtual ~CWSAStartup() {
TRACE2(this, " Destruct CWSAStartup")
::WSACleanup();
}

void load() const { TRACE2(this, " Executing CWSAStartup::load()") }
};

// This initialize and cleanup the Microsoft Windows Socket subsystem
extern const CWSAStartup init_winsock;
#endif // _MSC_VER


/*!
* \brief Get information from a raw network socket file descriptor
* <!-- ========================================================= -->
Expand Down
50 changes: 48 additions & 2 deletions Upnplib/src/global.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// Copyright (C) 2022+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
// Redistribution only with this Copyright remark. Last modified: 2024-03-14
// Redistribution only with this Copyright remark. Last modified: 2024-06-19
/*!
* \file
* \brief Global used flag and emulated system function.
* \brief Global used flag, class and emulated system function.
*/

#include <upnplib/global.ipp>
#include <upnplib/port.hpp>
#include <upnplib/port_sock.hpp>
#include <upnplib/synclog.hpp>
/// \cond
#include <stdexcept>

// strndup() is a GNU extension.
#ifndef HAVE_STRNDUP
Expand All @@ -25,8 +29,50 @@ char* strndup(const char* __string, size_t __n) {

namespace upnplib {

SUPPRESS_MSVC_WARN_4273_NEXT_LINE
UPNPLIB_API bool g_dbug{false};

/*!
* \brief Initialize and cleanup Microsoft Windows Sockets
* <!-- ================================================ -->
* \ingroup upnplib-socket
*
* Winsock needs to be initialized before using it and it needs to be freed. I
* do that with a class, following the RAII paradigm. Multiple initialization
* doesn't matter. This is managed by the operating system with a counter. It
* ensures that winsock is initialzed only one time and freed with the last
* free call. This is only done on Microsoft Windows.
*/
#ifdef _MSC_VER
class CWSAStartup {
public:
CWSAStartup() {
TRACE2(this, " Construct CWSAStartup")
WSADATA wsaData;
int rc = ::WSAStartup(MAKEWORD(2, 2), &wsaData);
if (rc != 0)
throw std::runtime_error(UPNPLIB_LOGEXCEPT +
"MSG1003: Failed to initialize Windows "
"sockets: WSAStartup() returns " +
std::to_string(rc) + "\n");
}

// No copy constructor
CWSAStartup(const CWSAStartup&) = delete;
// No copy assignment operator
CWSAStartup& operator=(CWSAStartup) = delete;

virtual ~CWSAStartup() {
TRACE2(this, " Destruct CWSAStartup")
::WSACleanup();
}
};

// This initialize and cleanup the Microsoft Windows Socket subsystem
const CWSAStartup init_winsock;
#endif // _MSC_VER


} // namespace upnplib

/// \endcond
5 changes: 0 additions & 5 deletions Upnplib/src/net/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@

namespace upnplib {

#ifdef _MSC_VER
// This initialize and cleanup the Microsoft Windows Socket subsystem
const CWSAStartup init_winsock;
#endif

namespace {

// Free helper functions
Expand Down
2 changes: 0 additions & 2 deletions Utest/upnplib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ add_test(NAME ctest_upnptools-ust COMMAND test_upnptools-ust --gtest_shuffle
# =======
add_executable(test_netaddr-ust
test_netaddr.cpp
${UPNPLIB_SOURCE_DIR}/src/net/socket.cpp
)
target_link_libraries(test_netaddr-ust
PRIVATE upnplib_static
Expand Down Expand Up @@ -70,7 +69,6 @@ add_test(NAME ctest_sockaddr-ust COMMAND test_sockaddr-ust --gtest_shuffle
# =========
add_executable(test_addrinfo-ust
test_addrinfo.cpp
${UPNPLIB_SOURCE_DIR}/src/net/socket.cpp
)
target_include_directories(test_addrinfo-ust
PRIVATE ${UPNPLIB_PROJECT_SOURCE_DIR}
Expand Down
4 changes: 2 additions & 2 deletions Utest/upnplib/test_netaddr.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2024+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
// Redistribution only with this Copyright remark. Last modified: 2024-06-18
// Redistribution only with this Copyright remark. Last modified: 2024-06-19

#include <upnplib/netaddr.hpp>

Expand Down Expand Up @@ -27,7 +27,7 @@ TEST(NetaddrTestSuite, netaddr_successful) {
captureObj.start();

// Test Unit output stream
std::cout << napObj << "\n";
std::cout << std::endl << napObj << std::endl;
EXPECT_THAT(captureObj.str(), EndsWith("[2001:db8::1]:61234\n"));

// Test Unit default copy constructor
Expand Down

0 comments on commit 267512a

Please sign in to comment.