socket: add own cross-platform declaration
Like with stdex::locale_t, this frees us from using Windowsizms on other platforms. Sockets came from Unix to Windows and not the other way around in the first place. Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
parent
2c45749f78
commit
3dfd62ec2f
@ -19,6 +19,7 @@
|
||||
#include <stdex/progress.hpp>
|
||||
#include <stdex/ring.hpp>
|
||||
#include <stdex/sgml.hpp>
|
||||
#include <stdex/socket.hpp>
|
||||
#include <stdex/stream.hpp>
|
||||
#include <stdex/string.hpp>
|
||||
#include <stdex/sys_info.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
|
||||
|
28
include/stdex/socket.hpp
Normal file
28
include/stdex/socket.hpp
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
SPDX-License-Identifier: MIT
|
||||
Copyright © 2023 Amebis
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "compat.hpp"
|
||||
#if defined(_WIN32)
|
||||
#include "windows.h"
|
||||
#include <WinSock2.h>
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#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
|
||||
}
|
@ -11,6 +11,7 @@
|
||||
#include "locale.hpp"
|
||||
#include "math.hpp"
|
||||
#include "ring.hpp"
|
||||
#include "socket.hpp"
|
||||
#include "string.hpp"
|
||||
#include "unicode.hpp"
|
||||
#include <stdint.h>
|
||||
@ -19,11 +20,9 @@
|
||||
#include "windows.h"
|
||||
#include <asptlb.h>
|
||||
#include <objidl.h>
|
||||
#include <WinSock2.h>
|
||||
#else
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#endif
|
||||
#include <chrono>
|
||||
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user