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/progress.hpp>
|
||||||
#include <stdex/ring.hpp>
|
#include <stdex/ring.hpp>
|
||||||
#include <stdex/sgml.hpp>
|
#include <stdex/sgml.hpp>
|
||||||
|
#include <stdex/socket.hpp>
|
||||||
#include <stdex/stream.hpp>
|
#include <stdex/stream.hpp>
|
||||||
#include <stdex/string.hpp>
|
#include <stdex/string.hpp>
|
||||||
#include <stdex/sys_info.hpp>
|
#include <stdex/sys_info.hpp>
|
||||||
|
@ -213,10 +213,3 @@ size_t _countof(T (&arr)[N])
|
|||||||
#define lockf64 lockf
|
#define lockf64 lockf
|
||||||
#define ftruncate64 ftruncate
|
#define ftruncate64 ftruncate
|
||||||
#endif
|
#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 "locale.hpp"
|
||||||
#include "math.hpp"
|
#include "math.hpp"
|
||||||
#include "ring.hpp"
|
#include "ring.hpp"
|
||||||
|
#include "socket.hpp"
|
||||||
#include "string.hpp"
|
#include "string.hpp"
|
||||||
#include "unicode.hpp"
|
#include "unicode.hpp"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@ -19,11 +20,9 @@
|
|||||||
#include "windows.h"
|
#include "windows.h"
|
||||||
#include <asptlb.h>
|
#include <asptlb.h>
|
||||||
#include <objidl.h>
|
#include <objidl.h>
|
||||||
#include <WinSock2.h>
|
|
||||||
#else
|
#else
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#endif
|
#endif
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
@ -2347,7 +2346,7 @@ namespace stdex
|
|||||||
class socket : public basic
|
class socket : public basic
|
||||||
{
|
{
|
||||||
public:
|
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),
|
basic(state),
|
||||||
m_h(h)
|
m_h(h)
|
||||||
{}
|
{}
|
||||||
@ -2359,16 +2358,16 @@ namespace stdex
|
|||||||
public:
|
public:
|
||||||
socket(_Inout_ socket&& other) noexcept : m_h(other.m_h)
|
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
|
socket& operator =(_Inout_ socket&& other) noexcept
|
||||||
{
|
{
|
||||||
if (this != std::addressof(other)) {
|
if (this != std::addressof(other)) {
|
||||||
if (m_h != INVALID_SOCKET)
|
if (m_h != invalid_socket)
|
||||||
::closesocket(m_h);
|
closesocket(m_h);
|
||||||
m_h = other.m_h;
|
m_h = other.m_h;
|
||||||
other.m_h = INVALID_SOCKET;
|
other.m_h = invalid_socket;
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -2383,25 +2382,25 @@ namespace stdex
|
|||||||
socket(_In_ int af, _In_ int type, _In_ int protocol)
|
socket(_In_ int af, _In_ int type, _In_ int protocol)
|
||||||
{
|
{
|
||||||
m_h = ::socket(af, type, protocol);
|
m_h = ::socket(af, type, protocol);
|
||||||
if (m_h == INVALID_SOCKET) _Unlikely_
|
if (m_h == invalid_socket) _Unlikely_
|
||||||
m_state = state_t::fail;
|
m_state = state_t::fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~socket()
|
virtual ~socket()
|
||||||
{
|
{
|
||||||
if (m_h != INVALID_SOCKET)
|
if (m_h != invalid_socket)
|
||||||
::closesocket(m_h);
|
closesocket(m_h);
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Returns true if socket handle is valid
|
/// 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
|
/// 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(
|
virtual _Success_(return != 0 || length == 0) size_t read(
|
||||||
_Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
|
_Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
|
||||||
@ -2449,15 +2448,15 @@ namespace stdex
|
|||||||
|
|
||||||
virtual void close()
|
virtual void close()
|
||||||
{
|
{
|
||||||
if (m_h != INVALID_SOCKET) {
|
if (m_h != invalid_socket) {
|
||||||
::closesocket(m_h);
|
closesocket(m_h);
|
||||||
m_h = INVALID_SOCKET;
|
m_h = invalid_socket;
|
||||||
}
|
}
|
||||||
m_state = state_t::ok;
|
m_state = state_t::ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
SOCKET m_h;
|
socket_t m_h;
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
Loading…
x
Reference in New Issue
Block a user