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 20, 2024
1 parent 267512a commit f95e85e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
5 changes: 4 additions & 1 deletion Upnplib/include/upnplib/global.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#ifndef UPNPLIB_GLOBAL_HPP
#define UPNPLIB_GLOBAL_HPP
// 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-20
/*!
* \file
* \brief Global used constants and variables.
*/

// Due to the global nature of this header file additional #include statements
// should be taken with great care. They are included in nearly all other
// compile units.
#include <upnplib/global.ipp>

namespace upnplib {
Expand Down
5 changes: 4 additions & 1 deletion Upnplib/include/upnplib/global.ipp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2022+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
// Redistribution only with this Copyright remark. Last modified: 2024-06-04
// Redistribution only with this Copyright remark. Last modified: 2024-06-20

// There is no include guard '#ifndef ...' because this file shouln't be
// included more than two times as given.
Expand All @@ -9,6 +9,9 @@
* \brief Global used constants, variables, functions and macros.
*/

// Due to the global nature of this header file additional #include statements
// should be taken with great care. They are included in nearly all other
// compile units.
#include <upnplib/visibility.hpp>
/// \cond
#include <string>
Expand Down
49 changes: 36 additions & 13 deletions Upnplib/src/global.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
// Copyright (C) 2022+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
// Redistribution only with this Copyright remark. Last modified: 2024-06-19
// Redistribution only with this Copyright remark. Last modified: 2024-06-20
/*!
* \file
* \brief Global used flag, class and emulated system function.
* \brief Global used flags, classes and emulated system functions.
*
* At least one of the global used constants or variables here is used nearly
* by all compile units. This ensures that this unit is always linked to the
* library no matter what options are selected. So I use it also for an
* authomatic initialization of the library with no need to call an init
* function by the user.
*/

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

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

namespace upnplib {

SUPPRESS_MSVC_WARN_4273_NEXT_LINE
// SUPPRESS_MSVC_WARN_4273_NEXT_LINE // don't do that
UPNPLIB_API bool g_dbug{false};

/*!
Expand All @@ -47,14 +52,26 @@ UPNPLIB_API bool g_dbug{false};
class CWSAStartup {
public:
CWSAStartup() {
TRACE2(this, " Construct CWSAStartup")
// TRACE2(this, " Construct CWSAStartup")
// Due to MSVC_WARN_4273, I will not use TRACE() with this global
// linkage
#ifdef UPNPLIB_WITH_TRACE
std::cout << "TRACE[Upnplib/src/global.cpp:" << __LINE__ << "] " << this
<< " Construct CWSAStartup"
<< "\n";
#endif
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");
if (rc != 0) {
// Prepare output string will not split its output on '<<' by output
// from other threads.
std::string msg{"UPnPlib [" + std::string(__FUNCTION__) +
"] CRITICAL MSG1003: Failed to initialize Windows "
"sockets, WSAStartup() returns (" +
std::to_string(rc) + ") \"" +
std::system_category().message(rc) + "\"\n"};
std::cerr << msg;
}
}

// No copy constructor
Expand All @@ -63,7 +80,14 @@ class CWSAStartup {
CWSAStartup& operator=(CWSAStartup) = delete;

virtual ~CWSAStartup() {
TRACE2(this, " Destruct CWSAStartup")
// TRACE2(this, " Destruct CWSAStartup")
// Due to MSVC_WARN_4273, I will not use TRACE() with this global
// linkage
#ifdef UPNPLIB_WITH_TRACE
std::cout << "TRACE[Upnplib/src/global.cpp:" << __LINE__ << "] " << this
<< " Destruct CWSAStartup"
<< "\n";
#endif
::WSACleanup();
}
};
Expand All @@ -72,7 +96,6 @@ class CWSAStartup {
const CWSAStartup init_winsock;
#endif // _MSC_VER


} // namespace upnplib

/// \endcond

0 comments on commit f95e85e

Please sign in to comment.