utils.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "utils.h"
  2. #include <util/generic/utility.h>
  3. #include <util/stream/output.h>
  4. #include <util/stream/str.h>
  5. #include <util/system/error.h>
  6. #if defined(_unix_)
  7. #include <pthread.h>
  8. #endif
  9. #if defined(_win_)
  10. #include <windows.h>
  11. #endif
  12. using namespace NNeh;
  13. size_t NNeh::RealStackSize(size_t len) noexcept {
  14. #if defined(NDEBUG) && !defined(_san_enabled_)
  15. return len;
  16. #else
  17. return Max<size_t>(len, 64000);
  18. #endif
  19. }
  20. TString NNeh::PrintHostByRfc(const NAddr::IRemoteAddr& addr) {
  21. TStringStream ss;
  22. if (addr.Addr()->sa_family == AF_INET) {
  23. NAddr::PrintHost(ss, addr);
  24. } else if (addr.Addr()->sa_family == AF_INET6) {
  25. ss << '[';
  26. NAddr::PrintHost(ss, addr);
  27. ss << ']';
  28. }
  29. return ss.Str();
  30. }
  31. NAddr::IRemoteAddrPtr NNeh::GetPeerAddr(SOCKET s) {
  32. TAutoPtr<NAddr::TOpaqueAddr> addr(new NAddr::TOpaqueAddr());
  33. if (getpeername(s, addr->MutableAddr(), addr->LenPtr()) < 0) {
  34. ythrow TSystemError() << "getpeername() failed";
  35. }
  36. return NAddr::IRemoteAddrPtr(addr.Release());
  37. }