Improve some platform features implementation

This commit is contained in:
halx99 2021-11-05 15:42:16 +08:00
parent bf68e35b08
commit 072aaf4117
5 changed files with 23 additions and 16 deletions

View File

@ -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 <sys/param.h>
# 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

View File

@ -34,6 +34,9 @@ SOFTWARE.
# include <Windows.h>
#else
# include <thread>
# if defined(__FreeBSD__) || defined(__OpenBSD__)
# include <pthread_np.h> // 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

View File

@ -325,7 +325,7 @@ void xxsocket::traverse_local_address(std::function<bool(const ip::endpoint&)> h
{
errmsg = xxsocket::gai_strerror(iret);
}
#else // __APPLE__ or linux with <ifaddrs.h>
#else // unix like systems with <ifaddrs.h>
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

View File

@ -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);

View File

@ -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