# WinStd: include/WinStd/WinSock2.h Source File

Loading...
Searching...
No Matches
WinSock2.h
/*
SPDX-License-Identifier: MIT
Copyright © 1991-2024 Amebis
Copyright © 2016 GÉANT
*/
#pragma once
#include "Common.h"
#include <WinSock2.h>
#include <ws2def.h>
#include <WS2tcpip.h>
namespace winstd
{
class ws2_runtime_error : public num_runtime_error<int>
{
public:
ws2_runtime_error(_In_ error_type num) : num_runtime_error<int>(num, message(num))
{}
ws2_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<int>(num, msg + ": " + message(num))
{}
ws2_runtime_error(_In_ error_type num, _In_z_ const char *msg) : num_runtime_error<int>(num, std::string(msg) + ": " + message(num))
{}
ws2_runtime_error() : num_runtime_error<int>(WSAGetLastError(), message(WSAGetLastError()))
{}
ws2_runtime_error(_In_ const std::string& msg) : num_runtime_error<int>(WSAGetLastError(), msg + ": " + message(WSAGetLastError()))
{}
ws2_runtime_error(_In_z_ const char *msg) : num_runtime_error<int>(WSAGetLastError(), std::string(msg) + ": " + message(WSAGetLastError()))
{}
protected:
static std::string message(_In_ error_type num, _In_opt_ DWORD dwLanguageId = 0)
{
std::wstring wstr;
if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, num, dwLanguageId, wstr, NULL)) {
// Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
wstr.erase(wstr.find_last_not_of(L" \t\n\r\f\v") + 1);
}
else
sprintf(wstr, num >= 0x10000 ? L"Error 0x%X" : L"Error %u", num);
std::string str;
WideCharToMultiByte(CP_UTF8, 0, wstr, str, NULL, NULL);
return str;
}
};
#if (NTDDI_VERSION >= NTDDI_WINXPSP2) || (_WIN32_WINNT >= 0x0502)
class addrinfo : public handle<PADDRINFOA, NULL>
{
WINSTD_HANDLE_IMPL(addrinfo, PADDRINFOA, NULL)
public:
virtual ~addrinfo()
{
if (m_h != invalid)
free_internal();
}
protected:
void free_internal() noexcept override
{
FreeAddrInfoA(m_h);
}
};
class waddrinfo : public handle<PADDRINFOW, NULL>
{
WINSTD_HANDLE_IMPL(waddrinfo, PADDRINFOW, NULL)
public:
virtual ~waddrinfo()
{
if (m_h != invalid)
free_internal();
}
protected:
void free_internal() noexcept override
{
FreeAddrInfoW(m_h);
}
};
#ifdef _UNICODE
typedef waddrinfo taddrinfo;
#else
typedef addrinfo taddrinfo;
#endif
#endif
}
#pragma warning(push)
#pragma warning(disable: 4505) // Don't warn on unused code
static INT GetAddrInfoA(
_In_opt_ PCSTR pNodeName,
_In_opt_ PCSTR pServiceName,
_In_opt_ const ADDRINFOA *pHints,
_Inout_ winstd::addrinfo &result)
{
PADDRINFOA h;
INT iResult = GetAddrInfoA(pNodeName, pServiceName, pHints, &h);
if (iResult == 0)
result.attach(h);
return iResult;
}
static INT GetAddrInfoW(
_In_opt_ PCWSTR pNodeName,
_In_opt_ PCWSTR pServiceName,
_In_opt_ const ADDRINFOW *pHints,
_Inout_ winstd::waddrinfo &result)
{
PADDRINFOW h;
INT iResult = GetAddrInfoW(pNodeName, pServiceName, pHints, &h);
if (iResult == 0)
result.attach(h);
return iResult;
}
#pragma warning(pop)
winstd::addrinfo
ADDRINFOA wrapper class.
Definition WinSock2.h:109
winstd::addrinfo::free_internal
void free_internal() noexcept override
Frees address information.
Definition WinSock2.h:130
winstd::addrinfo::~addrinfo
virtual ~addrinfo()
Frees address information.
Definition WinSock2.h:118
winstd::handle
Base abstract template class to support generic object handle keeping.
Definition Common.h:874
winstd::handle< PADDRINFOA, NULL >::m_h
handle_type m_h
Definition Common.h:1126
winstd::num_runtime_error
Numerical runtime error.
Definition Common.h:1331
winstd::num_runtime_error< int >::error_type
int error_type
Definition Common.h:1333
winstd::waddrinfo
ADDRINFOW wrapper class.
Definition WinSock2.h:142
winstd::waddrinfo::~waddrinfo
virtual ~waddrinfo()
Frees address information.
Definition WinSock2.h:151
winstd::waddrinfo::free_internal
void free_internal() noexcept override
Frees address information.
Definition WinSock2.h:163
winstd::ws2_runtime_error
WinSock2 runtime error.
Definition WinSock2.h:25
winstd::ws2_runtime_error::ws2_runtime_error
ws2_runtime_error(error_type num)
Constructs an exception.
Definition WinSock2.h:32
winstd::ws2_runtime_error::ws2_runtime_error
ws2_runtime_error()
Constructs an exception using WSAGetLastError()
Definition WinSock2.h:56
winstd::ws2_runtime_error::ws2_runtime_error
ws2_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition WinSock2.h:41
winstd::ws2_runtime_error::ws2_runtime_error
ws2_runtime_error(error_type num, const char *msg)
Constructs an exception.
Definition WinSock2.h:50
winstd::ws2_runtime_error::ws2_runtime_error
ws2_runtime_error(const std::string &msg)
Constructs an exception using WSAGetLastError()
Definition WinSock2.h:64
winstd::ws2_runtime_error::ws2_runtime_error
ws2_runtime_error(const char *msg)
Constructs an exception using WSAGetLastError()
Definition WinSock2.h:72
winstd::ws2_runtime_error::message
static std::string message(error_type num, DWORD dwLanguageId=0)
Returns a user-readable Windows error message.
Definition WinSock2.h:81
winstd::taddrinfo
addrinfo taddrinfo
Multi-byte / Wide-character ADDRINFO wrapper class (according to _UNICODE)
Definition WinSock2.h:175
GetAddrInfoW
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:208
GetAddrInfoA
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:190
FormatMessageW
static DWORD FormatMessageW(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, std::basic_string< wchar_t, _Traits, _Ax > &str, va_list *Arguments)
Formats a message string.
Definition Common.h:705
WideCharToMultiByte
static int WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, std::basic_string< char, _Traits, _Ax > &sMultiByteStr, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar) noexcept
Maps a UTF-16 (wide character) string to a std::string. The new character string is not necessarily f...
Definition Common.h:348
sprintf
static int sprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format,...)
Formats string using printf().
Definition Common.h:333
WINSTD_HANDLE_IMPL
#define WINSTD_HANDLE_IMPL(C, T, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition Common.h:164
winstd::handle< PADDRINFOA, NULL >::invalid
static const PADDRINFOA invalid
Definition Common.h:884