From 072aaf4117de63da7501410a4e30aef24b6fb1fa Mon Sep 17 00:00:00 2001 From: halx99 Date: Fri, 5 Nov 2021 15:42:16 +0800 Subject: [PATCH] Improve some platform features implementation --- thirdparty/yasio/compiler/feature_test.hpp | 10 +++++----- thirdparty/yasio/detail/thread_name.hpp | 17 +++++++++++++---- thirdparty/yasio/xxsocket.cpp | 8 +++----- thirdparty/yasio/xxsocket.hpp | 2 +- thirdparty/yasio/yasio.cpp | 2 +- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/thirdparty/yasio/compiler/feature_test.hpp b/thirdparty/yasio/compiler/feature_test.hpp index 0c59e70480..7f601a60ec 100644 --- a/thirdparty/yasio/compiler/feature_test.hpp +++ b/thirdparty/yasio/compiler/feature_test.hpp @@ -104,21 +104,21 @@ SOFTWARE. # define YASIO__UDP_KROUTE 1 #endif -// Tests whether current OS is BSD-like system +// Tests whether current OS is BSD-like system for process common BSD socket behaviors #if !defined(_WIN32) && !defined(__linux__) # include # if defined(BSD) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__) || defined(__DragonFly__) -# define YASIO__OS_BSD 1 +# define YASIO__OS_BSD_LIKE 1 # else -# define YASIO__OS_BSD 0 +# define YASIO__OS_BSD_LIKE 0 # endif #else -# define YASIO__OS_BSD 0 +# define YASIO__OS_BSD_LIKE 0 #endif // Test whether sockaddr has member 'sa_len' // see also: https://github.com/freebsd/freebsd-src/blob/main/sys/sys/socket.h#L329 -#if YASIO__OS_BSD +#if YASIO__OS_BSD_LIKE # define YASIO__HAS_SA_LEN 1 #else # define YASIO__HAS_SA_LEN 0 diff --git a/thirdparty/yasio/detail/thread_name.hpp b/thirdparty/yasio/detail/thread_name.hpp index a378abcadb..8bc5bc7ed1 100644 --- a/thirdparty/yasio/detail/thread_name.hpp +++ b/thirdparty/yasio/detail/thread_name.hpp @@ -34,6 +34,9 @@ SOFTWARE. # include #else # include +# if defined(__FreeBSD__) || defined(__OpenBSD__) +# include // For pthread_getthreadid_np() / pthread_set_name_np() +# endif #endif #if defined(_WIN32) @@ -62,12 +65,18 @@ static void yasio__set_thread_name(const char* threadName) {} # endif } +#elif defined(__linux__) +# if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__) +# define yasio__set_thread_name(name) pthread_setname_np(pthread_self(), name) +# else +# define yasio__set_thread_name(name) +# endif #elif defined(__APPLE__) # define yasio__set_thread_name(name) pthread_setname_np(name) -#elif defined(__linux__) && (defined(__GLIBC__) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 12)))) -// These functions first appeared in glibc in version 2.12. -// see: http://man7.org/linux/man-pages/man3/pthread_setname_np.3.html -# define yasio__set_thread_name(name) pthread_setname_np(pthread_self(), name) +#elif defined(__FreeBSD__) || defined(__OpenBSD__) +# define yasio__set_thread_name(name) pthread_set_name_np(pthread_self(), name) +#elif defined(__NetBSD__) +# define yasio__set_thread_name(name) pthread_setname_np(pthread_self(), "%s", (void*)name); #else # define yasio__set_thread_name(name) #endif diff --git a/thirdparty/yasio/xxsocket.cpp b/thirdparty/yasio/xxsocket.cpp index b925165d8c..3ca695fc2a 100644 --- a/thirdparty/yasio/xxsocket.cpp +++ b/thirdparty/yasio/xxsocket.cpp @@ -325,7 +325,7 @@ void xxsocket::traverse_local_address(std::function h { errmsg = xxsocket::gai_strerror(iret); } -#else // __APPLE__ or linux with +#else // unix like systems with struct ifaddrs *ifaddr, *ifa; /* The value of ifa->ifa_name: @@ -608,7 +608,7 @@ int xxsocket::disconnect(socket_native_type s) return 0; if ((error = xxsocket::get_last_errno()) == EINTR) continue; -# if YASIO__OS_BSD +# if YASIO__OS_BSD_LIKE /* * From kernel source code of FreeBSD,NetBSD,OpenBSD,etc. * The udp socket will be success disconnected by kernel function: `sodisconnect(upic_socket.c)`, then in the kernel, will continue try to @@ -894,14 +894,12 @@ unsigned int xxsocket::tcp_rtt(socket_native_type s) if (status == 0) return info.RttUs; # endif -#elif defined(__linux__) +#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) struct tcp_info info; - int length = sizeof(struct tcp_info); if (0 == xxsocket::get_optval(s, IPPROTO_TCP, TCP_INFO, info)) return info.tcpi_rtt; #elif defined(__APPLE__) struct tcp_connection_info info; - int length = sizeof(struct tcp_connection_info); /* info.tcpi_srtt: average RTT in ms info.tcpi_rttcur: most recent RTT in ms diff --git a/thirdparty/yasio/xxsocket.hpp b/thirdparty/yasio/xxsocket.hpp index ad8c406895..c4042909bb 100644 --- a/thirdparty/yasio/xxsocket.hpp +++ b/thirdparty/yasio/xxsocket.hpp @@ -1003,7 +1003,7 @@ public: ** @params: ** non ** - ** @returns: [0] succeed, otherwise, a value of SOCKET_ERROR is returned. + ** @returns: > 0 succeed, 0 failed. */ YASIO__DECL unsigned int tcp_rtt() const; YASIO__DECL static unsigned int tcp_rtt(socket_native_type s); diff --git a/thirdparty/yasio/yasio.cpp b/thirdparty/yasio/yasio.cpp index 08c3bad67f..c1e5f4d173 100644 --- a/thirdparty/yasio/yasio.cpp +++ b/thirdparty/yasio/yasio.cpp @@ -1776,7 +1776,7 @@ void io_service::handle_connect_succeed(transport_handle_t transport) register_descriptor(connection->native_handle(), YEM_POLLIN); if (yasio__testbits(ctx->properties_, YCM_TCP)) { -#if defined(__APPLE__) || defined(__linux__) +#if defined(SO_NOSIGPIPE) connection->set_optval(SOL_SOCKET, SO_NOSIGPIPE, (int)1); #endif // apply tcp keepalive options