diff options
author | nga <nga@yandex-team.ru> | 2022-02-10 16:48:09 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:48:09 +0300 |
commit | 1f553f46fb4f3c5eec631352cdd900a0709016af (patch) | |
tree | a231fba2c03b440becaea6c86a2702d0bfb0336e /library/cpp/messagebus/network.cpp | |
parent | c4de7efdedc25b49cbea74bd589eecb61b55b60a (diff) | |
download | ydb-1f553f46fb4f3c5eec631352cdd900a0709016af.tar.gz |
Restoring authorship annotation for <nga@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/messagebus/network.cpp')
-rw-r--r-- | library/cpp/messagebus/network.cpp | 142 |
1 files changed, 71 insertions, 71 deletions
diff --git a/library/cpp/messagebus/network.cpp b/library/cpp/messagebus/network.cpp index 304bedae5a..5a0ab5163d 100644 --- a/library/cpp/messagebus/network.cpp +++ b/library/cpp/messagebus/network.cpp @@ -3,27 +3,27 @@ #include <util/generic/maybe.h> #include <util/generic/ptr.h> #include <util/network/init.h> -#include <util/network/socket.h> +#include <util/network/socket.h> #include <util/system/platform.h> - -using namespace NBus; -using namespace NBus::NPrivate; - -namespace { + +using namespace NBus; +using namespace NBus::NPrivate; + +namespace { TBindResult BindOnPortProto(int port, int af, bool reusePort) { Y_VERIFY(af == AF_INET || af == AF_INET6, "wrong af"); - - SOCKET fd = ::socket(af, SOCK_STREAM, 0); + + SOCKET fd = ::socket(af, SOCK_STREAM, 0); if (fd == INVALID_SOCKET) { ythrow TSystemError() << "failed to create a socket"; } - - int one = 1; + + int one = 1; int r1 = SetSockOpt(fd, SOL_SOCKET, SO_REUSEADDR, one); if (r1 < 0) { ythrow TSystemError() << "failed to setsockopt SO_REUSEADDR"; } - + #ifdef SO_REUSEPORT if (reusePort) { int r = SetSockOpt(fd, SOL_SOCKET, SO_REUSEPORT, one); @@ -35,43 +35,43 @@ namespace { Y_UNUSED(reusePort); #endif - THolder<TOpaqueAddr> addr(new TOpaqueAddr); - sockaddr* sa = addr->MutableAddr(); - sa->sa_family = af; - socklen_t len; - if (af == AF_INET) { - len = sizeof(sockaddr_in); + THolder<TOpaqueAddr> addr(new TOpaqueAddr); + sockaddr* sa = addr->MutableAddr(); + sa->sa_family = af; + socklen_t len; + if (af == AF_INET) { + len = sizeof(sockaddr_in); ((sockaddr_in*)sa)->sin_port = HostToInet((ui16)port); ((sockaddr_in*)sa)->sin_addr.s_addr = INADDR_ANY; - } else { - len = sizeof(sockaddr_in6); + } else { + len = sizeof(sockaddr_in6); ((sockaddr_in6*)sa)->sin6_port = HostToInet((ui16)port); - } - - if (af == AF_INET6) { - FixIPv6ListenSocket(fd); - } - - int r2 = ::bind(fd, sa, len); - if (r2 < 0) { + } + + if (af == AF_INET6) { + FixIPv6ListenSocket(fd); + } + + int r2 = ::bind(fd, sa, len); + if (r2 < 0) { ythrow TSystemError() << "failed to bind on port " << port; - } - - int rsn = ::getsockname(fd, addr->MutableAddr(), addr->LenPtr()); + } + + int rsn = ::getsockname(fd, addr->MutableAddr(), addr->LenPtr()); if (rsn < 0) { ythrow TSystemError() << "failed to getsockname"; } - - int r3 = ::listen(fd, 50); + + int r3 = ::listen(fd, 50); if (r3 < 0) { ythrow TSystemError() << "listen failed"; } - - TBindResult r; - r.Socket.Reset(new TSocketHolder(fd)); - r.Addr = TNetAddr(addr.Release()); - return r; - } + + TBindResult r; + r.Socket.Reset(new TSocketHolder(fd)); + r.Addr = TNetAddr(addr.Release()); + return r; + } TMaybe<TBindResult> TryBindOnPortProto(int port, int af, bool reusePort) { try { @@ -91,13 +91,13 @@ namespace { r.second.emplace_back(std::move(r2)); return r; } -} - +} + std::pair<unsigned, TVector<TBindResult>> NBus::BindOnPort(int port, bool reusePort) { std::pair<unsigned, TVector<TBindResult>> r; r.second.reserve(2); - - if (port != 0) { + + if (port != 0) { return AggregateBindResults(BindOnPortProto(port, AF_INET, reusePort), BindOnPortProto(port, AF_INET6, reusePort)); } @@ -107,50 +107,50 @@ std::pair<unsigned, TVector<TBindResult>> NBus::BindOnPort(int port, bool reuseP TMaybe<TBindResult> in4 = TryBindOnPortProto(0, AF_INET, reusePort); if (!in4) { continue; - } + } TMaybe<TBindResult> in6 = TryBindOnPortProto(in4->Addr.GetPort(), AF_INET6, reusePort); if (!in6) { continue; - } + } return AggregateBindResults(std::move(*in4), std::move(*in6)); - } + } TBindResult in4 = BindOnPortProto(0, AF_INET, reusePort); TBindResult in6 = BindOnPortProto(in4.Addr.GetPort(), AF_INET6, reusePort); return AggregateBindResults(std::move(in4), std::move(in6)); -} - -void NBus::NPrivate::SetSockOptTcpCork(SOCKET s, bool value) { -#ifdef _linux_ +} + +void NBus::NPrivate::SetSockOptTcpCork(SOCKET s, bool value) { +#ifdef _linux_ CheckedSetSockOpt(s, IPPROTO_TCP, TCP_CORK, (int)value, "TCP_CORK"); #else Y_UNUSED(s); Y_UNUSED(value); -#endif -} - +#endif +} + ssize_t NBus::NPrivate::SocketSend(SOCKET s, TArrayRef<const char> data) { - int flags = 0; -#if defined(_linux_) || defined(_freebsd_) - flags |= MSG_NOSIGNAL; -#endif - ssize_t r = ::send(s, data.data(), data.size(), flags); - if (r < 0) { + int flags = 0; +#if defined(_linux_) || defined(_freebsd_) + flags |= MSG_NOSIGNAL; +#endif + ssize_t r = ::send(s, data.data(), data.size(), flags); + if (r < 0) { Y_VERIFY(LastSystemError() != EBADF, "bad fd"); - } - return r; -} - + } + return r; +} + ssize_t NBus::NPrivate::SocketRecv(SOCKET s, TArrayRef<char> buffer) { - int flags = 0; -#if defined(_linux_) || defined(_freebsd_) - flags |= MSG_NOSIGNAL; -#endif - ssize_t r = ::recv(s, buffer.data(), buffer.size(), flags); - if (r < 0) { + int flags = 0; +#if defined(_linux_) || defined(_freebsd_) + flags |= MSG_NOSIGNAL; +#endif + ssize_t r = ::recv(s, buffer.data(), buffer.size(), flags); + if (r < 0) { Y_VERIFY(LastSystemError() != EBADF, "bad fd"); - } - return r; -} + } + return r; +} |