diff options
author | uzhas <uzhas@yandex-team.ru> | 2022-02-10 16:47:06 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:06 +0300 |
commit | 2002caab389ddffd0e7db4d8c4db835fa4c99f83 (patch) | |
tree | 9d3448d5ba97edddf88aa69bcb138e88286e8176 /library/cpp/messagebus/network.cpp | |
parent | 17d51d51855dc3ed663d0e31fa75d3ff5ab80702 (diff) | |
download | ydb-2002caab389ddffd0e7db4d8c4db835fa4c99f83.tar.gz |
Restoring authorship annotation for <uzhas@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/messagebus/network.cpp')
-rw-r--r-- | library/cpp/messagebus/network.cpp | 88 |
1 files changed, 44 insertions, 44 deletions
diff --git a/library/cpp/messagebus/network.cpp b/library/cpp/messagebus/network.cpp index 304bedae5a..1e2a89d605 100644 --- a/library/cpp/messagebus/network.cpp +++ b/library/cpp/messagebus/network.cpp @@ -14,15 +14,15 @@ namespace { Y_VERIFY(af == AF_INET || af == AF_INET6, "wrong af"); SOCKET fd = ::socket(af, SOCK_STREAM, 0); - if (fd == INVALID_SOCKET) { - ythrow TSystemError() << "failed to create a socket"; - } + if (fd == INVALID_SOCKET) { + ythrow TSystemError() << "failed to create a socket"; + } int one = 1; int r1 = SetSockOpt(fd, SOL_SOCKET, SO_REUSEADDR, one); - if (r1 < 0) { - ythrow TSystemError() << "failed to setsockopt SO_REUSEADDR"; - } + if (r1 < 0) { + ythrow TSystemError() << "failed to setsockopt SO_REUSEADDR"; + } #ifdef SO_REUSEPORT if (reusePort) { @@ -54,72 +54,72 @@ namespace { int r2 = ::bind(fd, sa, len); if (r2 < 0) { - ythrow TSystemError() << "failed to bind on port " << port; + ythrow TSystemError() << "failed to bind on port " << port; } int rsn = ::getsockname(fd, addr->MutableAddr(), addr->LenPtr()); - if (rsn < 0) { - ythrow TSystemError() << "failed to getsockname"; - } + if (rsn < 0) { + ythrow TSystemError() << "failed to getsockname"; + } int r3 = ::listen(fd, 50); - if (r3 < 0) { - ythrow TSystemError() << "listen failed"; - } + if (r3 < 0) { + ythrow TSystemError() << "listen failed"; + } 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 { + try { return {BindOnPortProto(port, af, reusePort)}; - } catch (const TSystemError&) { - return {}; - } - } - - std::pair<unsigned, TVector<TBindResult>> AggregateBindResults(TBindResult&& r1, TBindResult&& r2) { - Y_VERIFY(r1.Addr.GetPort() == r2.Addr.GetPort(), "internal"); - std::pair<unsigned, TVector<TBindResult>> r; - r.second.reserve(2); - - r.first = r1.Addr.GetPort(); - r.second.emplace_back(std::move(r1)); - r.second.emplace_back(std::move(r2)); - return r; - } + } catch (const TSystemError&) { + return {}; + } + } + + std::pair<unsigned, TVector<TBindResult>> AggregateBindResults(TBindResult&& r1, TBindResult&& r2) { + Y_VERIFY(r1.Addr.GetPort() == r2.Addr.GetPort(), "internal"); + std::pair<unsigned, TVector<TBindResult>> r; + r.second.reserve(2); + + r.first = r1.Addr.GetPort(); + r.second.emplace_back(std::move(r1)); + 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); + r.second.reserve(2); if (port != 0) { return AggregateBindResults(BindOnPortProto(port, AF_INET, reusePort), BindOnPortProto(port, AF_INET6, reusePort)); - } - - // use nothrow versions in cycle - for (int i = 0; i < 1000; ++i) { + } + + // use nothrow versions in cycle + for (int i = 0; i < 1000; ++i) { TMaybe<TBindResult> in4 = TryBindOnPortProto(0, AF_INET, reusePort); - if (!in4) { - continue; + if (!in4) { + continue; } - + TMaybe<TBindResult> in6 = TryBindOnPortProto(in4->Addr.GetPort(), AF_INET6, reusePort); - if (!in6) { - continue; + if (!in6) { + continue; } - - return AggregateBindResults(std::move(*in4), std::move(*in6)); + + 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)); + return AggregateBindResults(std::move(in4), std::move(in6)); } void NBus::NPrivate::SetSockOptTcpCork(SOCKET s, bool value) { |