endpoint.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #pragma once
  2. #include "address.h"
  3. #include <util/str_stl.h>
  4. // some equivalent boost::asio::ip::endpoint (easy for using pair ip:port)
  5. class TEndpoint {
  6. public:
  7. using TAddrRef = NAddr::IRemoteAddrRef;
  8. TEndpoint(const TAddrRef& addr);
  9. TEndpoint();
  10. inline const TAddrRef& Addr() const noexcept {
  11. return Addr_;
  12. }
  13. inline const sockaddr* SockAddr() const {
  14. return Addr_->Addr();
  15. }
  16. inline socklen_t SockAddrLen() const {
  17. return Addr_->Len();
  18. }
  19. inline bool IsIpV4() const {
  20. return Addr_->Addr()->sa_family == AF_INET;
  21. }
  22. inline bool IsIpV6() const {
  23. return Addr_->Addr()->sa_family == AF_INET6;
  24. }
  25. inline bool IsUnix() const {
  26. return Addr_->Addr()->sa_family == AF_UNIX;
  27. }
  28. inline TString IpToString() const {
  29. return NAddr::PrintHost(*Addr_);
  30. }
  31. void SetPort(ui16 port);
  32. ui16 Port() const noexcept;
  33. size_t Hash() const;
  34. private:
  35. TAddrRef Addr_;
  36. };
  37. template <>
  38. struct THash<TEndpoint> {
  39. inline size_t operator()(const TEndpoint& ep) const {
  40. return ep.Hash();
  41. }
  42. };
  43. inline bool operator==(const TEndpoint& l, const TEndpoint& r) {
  44. try {
  45. return NAddr::IsSame(*l.Addr(), *r.Addr()) && l.Port() == r.Port();
  46. } catch (...) {
  47. return false;
  48. }
  49. }