2020-11-16 14:47:43 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
2021-01-14 15:30:18 +08:00
|
|
|
// A multi-platform support c++11 library with focus on asynchronous socket I/O for any
|
|
|
|
// client application.
|
2020-11-16 14:47:43 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2023-01-01 17:36:38 +08:00
|
|
|
Copyright (c) 2012-2023 HALX99
|
2020-11-16 14:47:43 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
#ifndef YASIO__FEATURE_TEST_HPP
|
|
|
|
#define YASIO__FEATURE_TEST_HPP
|
|
|
|
|
2022-10-13 22:00:06 +08:00
|
|
|
// Includes msvc basic system headers for compiler feature tests
|
2020-11-16 14:47:43 +08:00
|
|
|
// About preprocessor '_MSC_VER', please see:
|
|
|
|
// https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2019
|
2022-10-13 22:00:06 +08:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER > 1900
|
|
|
|
# include <vcruntime.h>
|
|
|
|
# include <sdkddkver.h>
|
|
|
|
#endif
|
|
|
|
|
2023-04-17 00:28:05 +08:00
|
|
|
#if defined(__linux__)
|
|
|
|
# include <linux/version.h>
|
|
|
|
#endif
|
|
|
|
|
2023-01-04 23:20:07 +08:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1900
|
|
|
|
# define snprintf sprintf_s
|
|
|
|
#endif
|
|
|
|
|
2022-10-13 22:00:06 +08:00
|
|
|
// Tests whether compiler has(fully) c++11 support and keywords workaround for msvc
|
|
|
|
#if !defined(_MSC_VER) || _MSC_VER >= 1900
|
|
|
|
# define YASIO__HAS_CXX11 1
|
|
|
|
# define YASIO__NS_INLINE inline
|
|
|
|
# define YASIO__CONSTEXPR constexpr
|
|
|
|
# define YASIO__NOEXCEPT noexcept
|
2020-11-16 14:47:43 +08:00
|
|
|
#else
|
2022-10-13 22:00:06 +08:00
|
|
|
# define YASIO__HAS_CXX11 0
|
|
|
|
# define YASIO__NS_INLINE
|
|
|
|
# define YASIO__CONSTEXPR const
|
|
|
|
# define YASIO__NOEXCEPT throw()
|
2020-11-16 14:47:43 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Tests whether compiler has c++14 support
|
2023-01-04 23:20:07 +08:00
|
|
|
#if (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_MSC_VER) && _MSC_VER >= 1900 && (defined(_MSVC_LANG) && (_MSVC_LANG >= 201402L)))
|
2020-11-16 14:47:43 +08:00
|
|
|
# ifndef YASIO_HAS_CXX14
|
|
|
|
# define YASIO__HAS_CXX14 1
|
|
|
|
# endif // C++14 features macro
|
|
|
|
#endif // C++14 features check
|
|
|
|
#if !defined(YASIO__HAS_CXX14)
|
|
|
|
# define YASIO__HAS_CXX14 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Tests whether compiler has c++17 support
|
2022-10-13 22:00:06 +08:00
|
|
|
#if (defined(__cplusplus) && __cplusplus >= 201703L) || \
|
2023-01-04 23:20:07 +08:00
|
|
|
(defined(_MSC_VER) && _MSC_VER > 1900 && ((defined(_HAS_CXX17) && _HAS_CXX17 == 1) || (defined(_MSVC_LANG) && (_MSVC_LANG > 201402L))))
|
2020-11-16 14:47:43 +08:00
|
|
|
# ifndef YASIO_HAS_CXX17
|
|
|
|
# define YASIO__HAS_CXX17 1
|
|
|
|
# endif // C++17 features macro
|
|
|
|
#endif // C++17 features check
|
|
|
|
#if !defined(YASIO__HAS_CXX17)
|
|
|
|
# define YASIO__HAS_CXX17 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Tests whether compiler has c++20 support
|
2022-10-13 22:00:06 +08:00
|
|
|
#if (defined(__cplusplus) && __cplusplus > 201703L) || \
|
2023-01-04 23:20:07 +08:00
|
|
|
(defined(_MSC_VER) && _MSC_VER > 1900 && ((defined(_HAS_CXX20) && _HAS_CXX20 == 1) || (defined(_MSVC_LANG) && (_MSVC_LANG > 201703L))))
|
2020-11-16 14:47:43 +08:00
|
|
|
# ifndef YASIO__HAS_CXX20
|
|
|
|
# define YASIO__HAS_CXX20 1
|
|
|
|
# endif // C++20 features macro
|
|
|
|
#endif // C++20 features check
|
|
|
|
#if !defined(YASIO__HAS_CXX20)
|
|
|
|
# define YASIO__HAS_CXX20 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Unix domain socket feature test
|
|
|
|
#if !defined(_WIN32) || defined(NTDDI_WIN10_RS5)
|
|
|
|
# define YASIO__HAS_UDS 1
|
|
|
|
#else
|
|
|
|
# define YASIO__HAS_UDS 0
|
|
|
|
#endif
|
|
|
|
|
2023-04-22 20:27:21 +08:00
|
|
|
// Tests whether current OS support route client io event in kernel for udp server
|
|
|
|
#if defined(_WIN32)
|
|
|
|
# define YASIO__UDP_KROUTE 0
|
|
|
|
#else
|
|
|
|
# define YASIO__UDP_KROUTE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Tests whether current OS is BSD-like system for process common BSD socket behaviors
|
|
|
|
#if !defined(_WIN32) && !defined(__linux__)
|
|
|
|
# include <sys/param.h>
|
|
|
|
# if defined(BSD) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__) || defined(__DragonFly__)
|
|
|
|
# define YASIO__OS_BSD_LIKE 1
|
|
|
|
# else
|
|
|
|
# define YASIO__OS_BSD_LIKE 0
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define YASIO__OS_BSD_LIKE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/// Tests multiplex io model of current OS
|
|
|
|
|
|
|
|
// ppoll
|
2023-04-17 00:28:05 +08:00
|
|
|
#if defined(__linux__) && !defined(__ANDROID__) || (defined(__ANDROID_API__) && __ANDROID_API__ >= 21)
|
|
|
|
# define YASIO__HAS_PPOLL 1
|
|
|
|
#else
|
|
|
|
# define YASIO__HAS_PPOLL 0
|
|
|
|
#endif
|
|
|
|
|
2023-04-22 20:27:21 +08:00
|
|
|
// iocp
|
|
|
|
#if defined(_WIN32)
|
|
|
|
# define YASIO__HAS_IOCP 1
|
|
|
|
#else
|
|
|
|
# define YASIO__HAS_IOCP 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// epoll
|
2023-04-17 00:28:05 +08:00
|
|
|
#if defined(__linux__)
|
|
|
|
# define YASIO__HAS_EPOLL 1
|
2023-04-22 20:27:21 +08:00
|
|
|
# if !defined(__ANDROID__) && (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0))
|
2023-04-17 00:28:05 +08:00
|
|
|
# define YASIO__HAS_EPOLL_PWAIT2 1
|
|
|
|
# else
|
|
|
|
# define YASIO__HAS_EPOLL_PWAIT2 0
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define YASIO__HAS_EPOLL 0
|
|
|
|
# define YASIO__HAS_EPOLL_PWAIT2 0
|
|
|
|
#endif
|
|
|
|
|
2023-04-22 20:27:21 +08:00
|
|
|
// kequeue
|
|
|
|
#if YASIO__OS_BSD_LIKE
|
|
|
|
# define YASIO__HAS_KQUEUE 1
|
2021-10-29 17:54:49 +08:00
|
|
|
#else
|
2023-04-22 20:27:21 +08:00
|
|
|
# define YASIO__HAS_KQUEUE 0
|
2021-10-29 17:54:49 +08:00
|
|
|
#endif
|
|
|
|
|
2023-04-22 20:27:21 +08:00
|
|
|
// evport
|
|
|
|
#if defined(__sun)
|
|
|
|
# define YASIO__HAS_EVPORT 1
|
2021-11-04 16:06:18 +08:00
|
|
|
#else
|
2023-04-22 20:27:21 +08:00
|
|
|
# define YASIO__HAS_EVPORT 0
|
2021-11-04 16:06:18 +08:00
|
|
|
#endif
|
|
|
|
|
2020-11-16 14:47:43 +08:00
|
|
|
// Test whether sockaddr has member 'sa_len'
|
2021-10-26 13:26:36 +08:00
|
|
|
// see also: https://github.com/freebsd/freebsd-src/blob/main/sys/sys/socket.h#L329
|
2021-11-05 15:42:16 +08:00
|
|
|
#if YASIO__OS_BSD_LIKE
|
2021-11-04 17:09:51 +08:00
|
|
|
# define YASIO__HAS_SA_LEN 1
|
2020-11-16 14:47:43 +08:00
|
|
|
#else
|
2021-11-04 17:09:51 +08:00
|
|
|
# define YASIO__HAS_SA_LEN 0
|
2020-11-16 14:47:43 +08:00
|
|
|
#endif
|
|
|
|
|
2021-01-14 15:30:18 +08:00
|
|
|
#if !defined(_WIN32) || defined(NTDDI_VISTA)
|
|
|
|
# define YASIO__HAS_NTOP 1
|
|
|
|
#else
|
|
|
|
# define YASIO__HAS_NTOP 0
|
|
|
|
#endif
|
|
|
|
|
2020-11-16 14:47:43 +08:00
|
|
|
// 64bits Sense Macros
|
2023-01-04 23:20:07 +08:00
|
|
|
#if defined(_M_X64) || defined(_WIN64) || defined(__LP64__) || defined(_LP64) || defined(__x86_64) || defined(__arm64__) || defined(__aarch64__)
|
2020-11-16 14:47:43 +08:00
|
|
|
# define YASIO__64BITS 1
|
|
|
|
#else
|
|
|
|
# define YASIO__64BITS 0
|
|
|
|
#endif
|
|
|
|
|
2020-12-08 19:10:58 +08:00
|
|
|
// Try detect compiler exceptions
|
|
|
|
#if !defined(__cpp_exceptions)
|
|
|
|
# define YASIO__NO_EXCEPTIONS 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(YASIO__NO_EXCEPTIONS)
|
|
|
|
# define YASIO__THROW(x, retval) throw(x)
|
|
|
|
# define YASIO__THROW0(x) throw(x)
|
|
|
|
#else
|
|
|
|
# define YASIO__THROW(x, retval) return (retval)
|
|
|
|
# define YASIO__THROW0(x) return
|
|
|
|
#endif
|
|
|
|
|
2022-10-13 22:00:06 +08:00
|
|
|
#include <system_error>
|
2023-01-04 23:20:07 +08:00
|
|
|
#define yasio__throw_error(ec, what) YASIO__THROW0(std::system_error(std::error_code{(int)ec, std::system_category()}, what))
|
2022-10-13 22:00:06 +08:00
|
|
|
#define yasio__throw_error0(ec) yasio__throw_error(ec, "")
|
|
|
|
|
2020-11-16 14:47:43 +08:00
|
|
|
// Compatibility with non-clang compilers...
|
|
|
|
#ifndef __has_attribute
|
|
|
|
# define __has_attribute(x) 0
|
|
|
|
#endif
|
|
|
|
#ifndef __has_builtin
|
|
|
|
# define __has_builtin(x) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helps the compiler's optimizer predicting branches
|
|
|
|
*/
|
|
|
|
#if __has_builtin(__builtin_expect)
|
|
|
|
# ifdef __cplusplus
|
|
|
|
# define yasio__likely(exp) (__builtin_expect(!!(exp), true))
|
|
|
|
# define yasio__unlikely(exp) (__builtin_expect(!!(exp), false))
|
|
|
|
# else
|
|
|
|
# define yasio__likely(exp) (__builtin_expect(!!(exp), 1))
|
|
|
|
# define yasio__unlikely(exp) (__builtin_expect(!!(exp), 0))
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define yasio__likely(exp) (!!(exp))
|
|
|
|
# define yasio__unlikely(exp) (!!(exp))
|
|
|
|
#endif
|
|
|
|
|
2021-11-04 16:06:18 +08:00
|
|
|
#ifdef __GNUC__
|
|
|
|
# define YASIO__UNUSED __attribute__((unused))
|
|
|
|
#else
|
|
|
|
# define YASIO__UNUSED
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define YASIO__UNUSED_PARAM(param) (void)param
|
|
|
|
|
2020-11-16 14:47:43 +08:00
|
|
|
#define YASIO__STD ::std::
|
|
|
|
|
2021-05-18 00:25:11 +08:00
|
|
|
#if YASIO__HAS_CXX14
|
|
|
|
namespace cxx14
|
|
|
|
{
|
|
|
|
using namespace std;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2020-11-16 14:47:43 +08:00
|
|
|
#if YASIO__HAS_CXX17
|
|
|
|
namespace cxx17
|
|
|
|
{
|
|
|
|
using namespace std;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|