diff --git a/UnitTests/pch.hpp b/UnitTests/pch.hpp index 56c123dcb..7e9a3ad0b 100644 --- a/UnitTests/pch.hpp +++ b/UnitTests/pch.hpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include diff --git a/include/stdex/compat.hpp b/include/stdex/compat.hpp index 919f94150..a0dc65d66 100644 --- a/include/stdex/compat.hpp +++ b/include/stdex/compat.hpp @@ -213,10 +213,3 @@ size_t _countof(T (&arr)[N]) #define lockf64 lockf #define ftruncate64 ftruncate #endif - -#ifndef _WIN32 -typedef int SOCKET; -#define INVALID_SOCKET ((SOCKET)-1) -#define SOCKET_ERROR -1 -#define closesocket close -#endif diff --git a/include/stdex/socket.hpp b/include/stdex/socket.hpp new file mode 100644 index 000000000..199551db9 --- /dev/null +++ b/include/stdex/socket.hpp @@ -0,0 +1,28 @@ +/* + SPDX-License-Identifier: MIT + Copyright © 2023 Amebis +*/ + +#pragma once + +#include "compat.hpp" +#if defined(_WIN32) +#include "windows.h" +#include +#else +#include +#include +#endif + +namespace stdex +{ +#ifdef _WIN32 + using socket_t = SOCKET; + constexpr socket_t invalid_socket = INVALID_SOCKET; + inline int closesocket(_In_ socket_t socket) { return ::closesocket(socket); } +#else + using socket_t = int; + constexpr socket_t invalid_socket = ((socket_t)-1); + inline int closesocket(_In_ socket_t socket) { return ::close(socket); } +#endif +} \ No newline at end of file diff --git a/include/stdex/stream.hpp b/include/stdex/stream.hpp index ebdd38359..48dd1c4b1 100644 --- a/include/stdex/stream.hpp +++ b/include/stdex/stream.hpp @@ -11,6 +11,7 @@ #include "locale.hpp" #include "math.hpp" #include "ring.hpp" +#include "socket.hpp" #include "string.hpp" #include "unicode.hpp" #include @@ -19,11 +20,9 @@ #include "windows.h" #include #include -#include #else #include #include -#include #include #endif #include @@ -2347,7 +2346,7 @@ namespace stdex class socket : public basic { public: - socket(_In_opt_ SOCKET h = INVALID_SOCKET, _In_ state_t state = state_t::ok) : + socket(_In_opt_ socket_t h = invalid_socket, _In_ state_t state = state_t::ok) : basic(state), m_h(h) {} @@ -2359,16 +2358,16 @@ namespace stdex public: socket(_Inout_ socket&& other) noexcept : m_h(other.m_h) { - other.m_h = INVALID_SOCKET; + other.m_h = invalid_socket; } socket& operator =(_Inout_ socket&& other) noexcept { if (this != std::addressof(other)) { - if (m_h != INVALID_SOCKET) - ::closesocket(m_h); + if (m_h != invalid_socket) + closesocket(m_h); m_h = other.m_h; - other.m_h = INVALID_SOCKET; + other.m_h = invalid_socket; } return *this; } @@ -2383,25 +2382,25 @@ namespace stdex socket(_In_ int af, _In_ int type, _In_ int protocol) { m_h = ::socket(af, type, protocol); - if (m_h == INVALID_SOCKET) _Unlikely_ + if (m_h == invalid_socket) _Unlikely_ m_state = state_t::fail; } virtual ~socket() { - if (m_h != INVALID_SOCKET) - ::closesocket(m_h); + if (m_h != invalid_socket) + closesocket(m_h); } /// /// Returns true if socket handle is valid /// - inline operator bool() const noexcept { return m_h != INVALID_SOCKET; } + inline operator bool() const noexcept { return m_h != invalid_socket; } /// /// Returns socket handle /// - inline SOCKET get() const noexcept { return m_h; } + inline socket_t get() const noexcept { return m_h; } virtual _Success_(return != 0 || length == 0) size_t read( _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length) @@ -2449,15 +2448,15 @@ namespace stdex virtual void close() { - if (m_h != INVALID_SOCKET) { - ::closesocket(m_h); - m_h = INVALID_SOCKET; + if (m_h != invalid_socket) { + closesocket(m_h); + m_h = invalid_socket; } m_state = state_t::ok; } protected: - SOCKET m_h; + socket_t m_h; }; #ifdef _WIN32