WinStd
Windows Win32 API using Standard C++
Loading...
Searching...
No Matches
WinSock2.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2023 Amebis
4 Copyright © 2016 GÉANT
5*/
6
8
9#pragma once
10
11#include "Common.h"
12#include <WinSock2.h>
13#include <ws2def.h>
14#include <WS2tcpip.h>
15
16namespace winstd
17{
20
25 {
26 public:
33 ws2_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<int>(num, msg)
34 {
35 }
36
43 ws2_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) : num_runtime_error<int>(num, msg)
44 {
45 }
46
52 ws2_runtime_error(_In_ const std::string& msg) : num_runtime_error<int>(WSAGetLastError(), msg)
53 {
54 }
55
61 ws2_runtime_error(_In_opt_z_ const char *msg = nullptr) : num_runtime_error<int>(WSAGetLastError(), msg)
62 {
63 }
64
70 tstring msg(_In_opt_ DWORD dwLanguageId = 0) const
71 {
72 tstring str;
73 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, m_num, dwLanguageId, str, NULL)) {
74 // Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
75 str.erase(str.find_last_not_of(_T(" \t\n\r\f\v")) + 1);
76 } else
77 sprintf(str, m_num >= 0x10000 ? _T("Error 0x%X") : _T("Error %u"), m_num);
78 return str;
79 }
80 };
81
83
86
87#if (NTDDI_VERSION >= NTDDI_WINXPSP2) || (_WIN32_WINNT >= 0x0502)
88
94 class addrinfo : public handle<PADDRINFOA, NULL>
95 {
97
98 public:
104 virtual ~addrinfo()
105 {
106 if (m_h != invalid)
108 }
109
110 protected:
116 void free_internal() noexcept override
117 {
118 FreeAddrInfoA(m_h);
119 }
120 };
121
127 class waddrinfo : public handle<PADDRINFOW, NULL>
128 {
130
131 public:
137 virtual ~waddrinfo()
138 {
139 if (m_h != invalid)
141 }
142
143 protected:
149 void free_internal() noexcept override
150 {
151 FreeAddrInfoW(m_h);
152 }
153 };
154
158#ifdef _UNICODE
159 typedef waddrinfo taddrinfo;
160#else
162#endif
163
164#endif
165
167}
168
171
172#pragma warning(push)
173#pragma warning(disable: 4505) // Don't warn on unused code
174
176static INT GetAddrInfoA(
177 _In_opt_ PCSTR pNodeName,
178 _In_opt_ PCSTR pServiceName,
179 _In_opt_ const ADDRINFOA *pHints,
180 _Inout_ winstd::addrinfo &result)
181{
182 PADDRINFOA h;
183 INT iResult = GetAddrInfoA(pNodeName, pServiceName, pHints, &h);
184 if (iResult == 0)
185 result.attach(h);
186 return iResult;
187}
188
194static INT GetAddrInfoW(
195 _In_opt_ PCWSTR pNodeName,
196 _In_opt_ PCWSTR pServiceName,
197 _In_opt_ const ADDRINFOW *pHints,
198 _Inout_ winstd::waddrinfo &result)
199{
200 PADDRINFOW h;
201 INT iResult = GetAddrInfoW(pNodeName, pServiceName, pHints, &h);
202 if (iResult == 0)
203 result.attach(h);
204 return iResult;
205}
206
207#pragma warning(pop)
208
ADDRINFOA wrapper class.
Definition WinSock2.h:95
void free_internal() noexcept override
Frees address information.
Definition WinSock2.h:116
virtual ~addrinfo()
Frees address information.
Definition WinSock2.h:104
Base abstract template class to support generic object handle keeping.
Definition Common.h:635
handle_type m_h
Object handle.
Definition Common.h:889
Numerical runtime error.
Definition Common.h:1029
int error_type
Error number type.
Definition Common.h:1031
error_type m_num
Numeric error code.
Definition Common.h:1067
ADDRINFOW wrapper class.
Definition WinSock2.h:128
virtual ~waddrinfo()
Frees address information.
Definition WinSock2.h:137
void free_internal() noexcept override
Frees address information.
Definition WinSock2.h:149
WinSock2 runtime error.
Definition WinSock2.h:25
ws2_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition WinSock2.h:43
ws2_runtime_error(const char *msg=nullptr)
Constructs an exception using WSAGetLastError()
Definition WinSock2.h:61
ws2_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition WinSock2.h:33
ws2_runtime_error(const std::string &msg)
Constructs an exception using WSAGetLastError()
Definition WinSock2.h:52
tstring msg(DWORD dwLanguageId=0) const
Returns a user-readable Windows error message.
Definition WinSock2.h:70
addrinfo taddrinfo
Multi-byte / Wide-character ADDRINFO wrapper class (according to _UNICODE)
Definition WinSock2.h:161
static INT GetAddrInfoW(PCWSTR pNodeName, PCWSTR pServiceName, const ADDRINFOW *pHints, winstd::waddrinfo &result)
Provides protocol-independent translation from a host name to an address.
Definition WinSock2.h:194
static INT GetAddrInfoA(PCSTR pNodeName, PCSTR pServiceName, const ADDRINFOA *pHints, winstd::addrinfo &result)
Provides protocol-independent translation from a host name to an address.
Definition WinSock2.h:176
std::string tstring
Multi-byte / Wide-character string (according to _UNICODE)
Definition Common.h:338
static DWORD FormatMessage(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, std::basic_string< char, _Traits, _Ax > &str, va_list *Arguments)
Formats a message string.
Definition Common.h:299
static int sprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format,...)
Formats string using printf().
Definition Common.h:284
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition Common.h:163
static const PADDRINFOA invalid
Invalid handle value.
Definition Common.h:645