UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
global.cpp
Go to the documentation of this file.
1// Copyright (C) 2022+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
2// Redistribution only with this Copyright remark. Last modified: 2025-05-04
14#include <UPnPsdk/global.ipp>
15#include <UPnPsdk/port_sock.hpp>
17#include <iostream>
18#include <string>
19
20// strndup() is a GNU extension.
21#ifndef HAVE_STRNDUP
22char* strndup(const char* __string, size_t __n) {
23 size_t strsize = strnlen(__string, __n);
24 char* newstr = (char*)malloc(strsize + 1);
25 if (newstr == NULL)
26 return NULL;
27
28 strncpy(newstr, __string, strsize);
29 newstr[strsize] = 0;
30
31 return newstr;
32}
33#endif
35
36
37namespace UPnPsdk {
38namespace {
39
55#if defined(_MSC_VER) || defined(DOXYGEN_RUN)
57 public:
58 CWSAStartup() {
59 // TRACE2(this, " Construct CWSAStartup")
60 // Due to MSVC_WARN_4273, I will not use TRACE2() with this global
61 // linkage
62#ifdef UPnPsdk_WITH_TRACE
63 std::cout << "TRACE[UPnPsdk/src/global.cpp:" << __LINE__ << "] " << this
64 << " Construct CWSAStartup\n";
65#endif
66 ::WSADATA wsaData;
67 int rc = ::WSAStartup(MAKEWORD(2, 2), &wsaData);
68 if (rc != 0) {
69 std::string err_str = "UPnPsdk [" + std::string(__FUNCTION__) +
70 "] FATAL MSG1003: Failed to initialize "
71 "Windows Sockets, WSAStartup() returns (" +
72 std::to_string(rc) + ") \"" +
73 std::system_category().message(rc) + "\"\n";
74 std::cerr << err_str;
75 exit(EXIT_FAILURE);
76 }
77 /* Confirm that the WinSock DLL supports 2.2. Note that if the DLL
78 * supports versions greater than 2.2 in addition to 2.2, it will still
79 * return 2.2 in wVersion since that is the version we requested. */
80 if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
81 /* Tell the user that we could not find a usable WinSock DLL. */
82 ::WSACleanup();
83 std::string err_str =
84 "UPnPsdk [" + std::string(__FUNCTION__) +
85 "] FATAL MSG1131: Windows Sockets DLL must "
86 "support version 2.2 but it has " +
87 std::to_string(HIBYTE(wsaData.wVersion)) + "." +
88 std::to_string(LOBYTE(wsaData.wVersion)) + "\n";
89 std::cerr << err_str;
90 exit(EXIT_FAILURE);
91 }
92 }
93
95 // No copy constructor
96 CWSAStartup(const CWSAStartup&) = delete;
97 // No copy assignment operator
98 CWSAStartup& operator=(CWSAStartup) = delete;
100
101 virtual ~CWSAStartup() {
102 // TRACE2(this, " Destruct CWSAStartup")
103 // Due to MSVC_WARN_4273, I will not use TRACE2() with this global
104 // linkage
105#ifdef UPnPsdk_WITH_TRACE
106 // clang-format off
107 std::cout << "TRACE[UPnPsdk/src/global.cpp:" << __LINE__ << "] " << this
108 << " Destruct CWSAStartup"
109 << "\n";
110 // clang-format on
111#endif
112 ::WSACleanup();
113 }
114};
115
120#endif // _MSC_VER
121
122} // anonymous namespace
123
124
125// SUPPRESS_MSVC_WARN_4273_NEXT_LINE // don't do that
126UPnPsdk_API bool g_dbug{false};
127
128} // namespace UPnPsdk
Initialize and cleanup Microsoft Windows Sockets.
Definition global.cpp:56
Global used constants, variables, functions and macros.
static size_t strnlen(const char *s, size_t n)
strnlen() is a GNU extension and here provided for portability.
const CWSAStartup init_winsock
This object initialize and cleanup the Microsoft Windows Socket subsystem.
Definition global.cpp:119
Reengineered Object Oriented UPnP+ program code.
UPnPsdk_EXTERN bool g_dbug
Switch to enable verbose (debug) output.
Definition global.hpp:26
Specifications to be portable with sockets between different platforms.