diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/network/nonblock.cpp | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/network/nonblock.cpp')
-rw-r--r-- | util/network/nonblock.cpp | 196 |
1 files changed, 98 insertions, 98 deletions
diff --git a/util/network/nonblock.cpp b/util/network/nonblock.cpp index e515c27cc5..ad00becbce 100644 --- a/util/network/nonblock.cpp +++ b/util/network/nonblock.cpp @@ -1,104 +1,104 @@ -#include "nonblock.h" - -#include <util/system/platform.h> - -#include <util/generic/singleton.h> - -#if defined(_unix_) - #include <dlfcn.h> -#endif - -#if defined(_linux_) - #if !defined(SOCK_NONBLOCK) - #define SOCK_NONBLOCK 04000 - #endif -#endif - -namespace { - struct TFeatureCheck { - inline TFeatureCheck() +#include "nonblock.h" + +#include <util/system/platform.h> + +#include <util/generic/singleton.h> + +#if defined(_unix_) + #include <dlfcn.h> +#endif + +#if defined(_linux_) + #if !defined(SOCK_NONBLOCK) + #define SOCK_NONBLOCK 04000 + #endif +#endif + +namespace { + struct TFeatureCheck { + inline TFeatureCheck() : Accept4(nullptr) - , HaveSockNonBlock(false) - { -#if defined(_unix_) && defined(SOCK_NONBLOCK) - { + , HaveSockNonBlock(false) + { +#if defined(_unix_) && defined(SOCK_NONBLOCK) + { Accept4 = reinterpret_cast<TAccept4>(dlsym(RTLD_DEFAULT, "accept4")); - - #if defined(_musl_) - //musl always statically linked - if (!Accept4) { - Accept4 = accept4; - } - #endif - - if (Accept4) { + + #if defined(_musl_) + //musl always statically linked + if (!Accept4) { + Accept4 = accept4; + } + #endif + + if (Accept4) { Accept4(-1, nullptr, nullptr, SOCK_NONBLOCK); - - if (errno == ENOSYS) { + + if (errno == ENOSYS) { Accept4 = nullptr; - } - } - } -#endif - -#if defined(SOCK_NONBLOCK) - { - TSocketHolder tmp(socket(PF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)); - - HaveSockNonBlock = !tmp.Closed(); - } -#endif - } - - inline SOCKET FastAccept(SOCKET s, struct sockaddr* addr, socklen_t* addrlen) const { -#if defined(SOCK_NONBLOCK) - if (Accept4) { - return Accept4(s, addr, addrlen, SOCK_NONBLOCK); - } -#endif - - const SOCKET ret = accept(s, addr, addrlen); - -#if !defined(_freebsd_) - //freebsd inherit O_NONBLOCK flag - if (ret != INVALID_SOCKET) { - SetNonBlock(ret); - } -#endif - - return ret; - } - - inline SOCKET FastSocket(int domain, int type, int protocol) const { -#if defined(SOCK_NONBLOCK) - if (HaveSockNonBlock) { - return socket(domain, type | SOCK_NONBLOCK, protocol); - } -#endif - - const SOCKET ret = socket(domain, type, protocol); - - if (ret != INVALID_SOCKET) { - SetNonBlock(ret); - } - - return ret; - } - + } + } + } +#endif + +#if defined(SOCK_NONBLOCK) + { + TSocketHolder tmp(socket(PF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)); + + HaveSockNonBlock = !tmp.Closed(); + } +#endif + } + + inline SOCKET FastAccept(SOCKET s, struct sockaddr* addr, socklen_t* addrlen) const { +#if defined(SOCK_NONBLOCK) + if (Accept4) { + return Accept4(s, addr, addrlen, SOCK_NONBLOCK); + } +#endif + + const SOCKET ret = accept(s, addr, addrlen); + +#if !defined(_freebsd_) + //freebsd inherit O_NONBLOCK flag + if (ret != INVALID_SOCKET) { + SetNonBlock(ret); + } +#endif + + return ret; + } + + inline SOCKET FastSocket(int domain, int type, int protocol) const { +#if defined(SOCK_NONBLOCK) + if (HaveSockNonBlock) { + return socket(domain, type | SOCK_NONBLOCK, protocol); + } +#endif + + const SOCKET ret = socket(domain, type, protocol); + + if (ret != INVALID_SOCKET) { + SetNonBlock(ret); + } + + return ret; + } + static inline const TFeatureCheck* Instance() noexcept { - return Singleton<TFeatureCheck>(); - } - + return Singleton<TFeatureCheck>(); + } + using TAccept4 = int (*)(int sockfd, struct sockaddr* addr, socklen_t* addrlen, int flags); - TAccept4 Accept4; - bool HaveSockNonBlock; - }; -} - -SOCKET Accept4(SOCKET s, struct sockaddr* addr, socklen_t* addrlen) { - return TFeatureCheck::Instance()->FastAccept(s, addr, addrlen); -} - -SOCKET Socket4(int domain, int type, int protocol) { - return TFeatureCheck::Instance()->FastSocket(domain, type, protocol); -} + TAccept4 Accept4; + bool HaveSockNonBlock; + }; +} + +SOCKET Accept4(SOCKET s, struct sockaddr* addr, socklen_t* addrlen) { + return TFeatureCheck::Instance()->FastAccept(s, addr, addrlen); +} + +SOCKET Socket4(int domain, int type, int protocol) { + return TFeatureCheck::Instance()->FastSocket(domain, type, protocol); +} |