SockAddress.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // Copyright @ 2015 Hangzhou Topzen Ltd.
  3. // Author: Tang (tang@hztopzen.com) @ 2015-07
  4. //
  5. #ifndef __SOCKADDRESS_H
  6. #define __SOCKADDRESS_H
  7. #include "IPAddress.h"
  8. #if defined(WIN32) || defined(WINDOWS)
  9. typedef SOCKET TZ_SOCKET;
  10. typedef int TZ_SOCKLEN;
  11. #elif defined(LINUX)
  12. typedef int TZ_SOCKET;
  13. typedef socklen_t TZ_SOCKLEN;
  14. #endif
  15. #ifndef INVALID_SOCKET
  16. #define INVALID_SOCKET (-1)
  17. #endif
  18. namespace tzc {
  19. class SockAddressImp {
  20. public:
  21. virtual IPAddress Host() const = 0;
  22. virtual TZ_Uint16 Port() const = 0;
  23. virtual TZ_SOCKLEN Size() const = 0;
  24. virtual TZ_Uint32 Family() const = 0;
  25. virtual int SinFamily() const = 0;
  26. virtual const struct sockaddr * Addr() const = 0;
  27. virtual ~SockAddressImp()
  28. {
  29. tzc::UninitOSNetwork();
  30. }
  31. protected:
  32. SockAddressImp()
  33. {
  34. tzc::InitOSNetwork();
  35. }
  36. private:
  37. SockAddressImp(const SockAddressImp &);
  38. SockAddressImp & operator = (const SockAddressImp &);
  39. };
  40. class IPv4SockAddressImp : public SockAddressImp {
  41. public:
  42. IPv4SockAddressImp();
  43. IPv4SockAddressImp(const struct sockaddr_in * addr);
  44. IPv4SockAddressImp(const void * addr, TZ_Uint16 port);
  45. virtual IPAddress Host() const;
  46. virtual TZ_Uint16 Port() const;
  47. virtual TZ_SOCKLEN Size() const;
  48. virtual TZ_Uint32 Family() const;
  49. virtual int SinFamily() const;
  50. virtual const struct sockaddr * Addr() const;
  51. private:
  52. struct sockaddr_in m_addr;
  53. };
  54. class IPv6SockAddressImp : public SockAddressImp {
  55. public:
  56. IPv6SockAddressImp(const struct sockaddr_in6 * addr);
  57. IPv6SockAddressImp(const void * addr, TZ_Uint16 port);
  58. IPv6SockAddressImp(const void * addr, TZ_Uint16 port, TZ_Uint32 scope);
  59. virtual IPAddress Host() const;
  60. virtual TZ_Uint16 Port() const;
  61. virtual TZ_SOCKLEN Size() const;
  62. virtual TZ_Uint32 Family() const;
  63. virtual int SinFamily() const;
  64. virtual const struct sockaddr * Addr() const;
  65. private:
  66. struct sockaddr_in6 m_addr;
  67. };
  68. #if defined(LINUX)
  69. class UnixSockAddressImp : public SockAddressImp {
  70. public:
  71. UnixSockAddressImp();
  72. UnixSockAddressImp(const struct sockaddr_un * addr);
  73. UnixSockAddressImp(const std::string & addr);
  74. virtual IPAddress Host() const;
  75. virtual TZ_Uint16 Port() const;
  76. virtual TZ_SOCKLEN Size() const;
  77. virtual TZ_Uint32 Family() const;
  78. virtual int SinFamily() const;
  79. virtual const struct sockaddr * Addr() const;
  80. private:
  81. struct sockaddr_un m_addr;
  82. };
  83. #endif
  84. class DECLDLL SockAddress {
  85. public:
  86. enum {
  87. MAX_ADDRESS_SIZE = sizeof(struct sockaddr_storage)
  88. };
  89. public:
  90. // default, IPv4, INADDR_ANY + random port
  91. SockAddress();
  92. // IPv4, INADDR_ANY
  93. SockAddress(TZ_Uint16 port);
  94. SockAddress(const IPAddress & host, TZ_Uint16 port);
  95. SockAddress(const std::string & host, TZ_Uint16 port);
  96. SockAddress(const SockAddress & addr);
  97. SockAddress(const struct sockaddr * addr, TZ_SOCKLEN size);
  98. #if defined(LINUX)
  99. // just only for unix socket address
  100. SockAddress(const std::string & unixPath);
  101. #endif
  102. ~SockAddress();
  103. SockAddress & operator = (const SockAddress & addr);
  104. void Swap(SockAddress & addr);
  105. IPAddress Host() const;
  106. TZ_Uint16 Port() const;
  107. TZ_SOCKLEN Size() const;
  108. TZ_Uint32 Family() const;
  109. int SinFamily() const;
  110. const struct sockaddr * Addr() const;
  111. std::string ToString() const;
  112. bool operator == (const SockAddress & addr) const;
  113. bool operator != (const SockAddress & addr) const;
  114. bool operator < (const SockAddress & addr) const;
  115. bool operator <= (const SockAddress & addr) const;
  116. bool operator > (const SockAddress & addr) const;
  117. bool operator >= (const SockAddress & addr) const;
  118. private:
  119. SockAddressImp * m_imp;
  120. };
  121. //
  122. // inlines
  123. //
  124. inline TZ_Uint32 SockAddress::Family() const
  125. {
  126. return m_imp->Family();
  127. }
  128. inline int SockAddress::SinFamily() const
  129. {
  130. return m_imp->SinFamily();
  131. }
  132. inline IPAddress SockAddress::Host() const
  133. {
  134. return m_imp->Host();
  135. }
  136. inline TZ_Uint16 SockAddress::Port() const
  137. {
  138. return m_imp->Port();
  139. }
  140. inline TZ_SOCKLEN SockAddress::Size() const
  141. {
  142. return m_imp->Size();
  143. }
  144. inline const struct sockaddr * SockAddress::Addr() const
  145. {
  146. return m_imp->Addr();
  147. }
  148. inline bool SockAddress::operator == (const SockAddress & addr) const
  149. {
  150. return this->Host() == addr.Host() && this->Port() == addr.Port();
  151. }
  152. inline bool SockAddress::operator != (const SockAddress & addr) const
  153. {
  154. return this->Host() != addr.Host() || this->Port() != addr.Port();
  155. }
  156. }; // namespace tzc
  157. #endif /* ----- #ifndef __SOCKADDRESS_H ----- */