Socket.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // Copyright @ 2015 Hangzhou Topzen Ltd.
  3. // Author: Tang (tang@hztopzen.com) @ 2015-07
  4. //
  5. #ifndef __SOCKET_H
  6. #define __SOCKET_H
  7. #include "Types.h"
  8. #include "RefCounter.h"
  9. #include "SockAddress.h"
  10. #if defined(WIN32) || defined(WINDOWS)
  11. #include <MSTcpIP.h>
  12. #elif defined(LINUX)
  13. #include <netinet/tcp.h>
  14. #include <sys/poll.h>
  15. #endif
  16. namespace tzc {
  17. class DECLDLL Socket {
  18. public:
  19. enum __SOCK_TYPE {
  20. STYPE_UDP,
  21. STYPE_TCP,
  22. STYPE_TCPS,
  23. STYPE_UNIX_UDP,
  24. STYPE_UNIX_TCP,
  25. STYPE_UNIX_TCPS,
  26. STYPE_UNKNOWN
  27. };
  28. typedef std::vector<Socket *> SockList;
  29. static TZ_INT LastError();
  30. // @timeout in microsecond
  31. static TZ_INT SelectList(SockList & readList,
  32. SockList & writeList,
  33. SockList & errorList,
  34. TZ_Uint32 timeout);
  35. public:
  36. Socket(TZ_Uint32 type = STYPE_UNKNOWN);
  37. Socket(const Socket & sock);
  38. virtual ~Socket();
  39. virtual TZ_INT Bind(const SockAddress & addr) = 0;
  40. virtual TZ_INT Connect(const SockAddress & raddr) = 0;
  41. virtual TZ_INT RecvBytes(TZ_BYTE * buffer, TZ_Uint32 length) = 0;
  42. virtual TZ_INT SendBytes(const TZ_BYTE * buffer, TZ_Uint32 length) = 0;
  43. // the socket type must be UDP or UNIX_UDP
  44. virtual TZ_INT SendBytes(const TZ_BYTE * buffer, TZ_Uint32 length,
  45. const SockAddress & raddr) = 0;
  46. // the following two functions always should be
  47. // called after the first SendBytes()
  48. virtual SockAddress LocalAddress() const;
  49. virtual SockAddress PeerAddress() const;
  50. // operator reloaded interfaces
  51. public:
  52. Socket & operator = (const Socket& socket);
  53. bool operator == (const Socket& socket) const;
  54. bool operator != (const Socket& socket) const;
  55. bool operator < (const Socket& socket) const;
  56. bool operator <= (const Socket& socket) const;
  57. bool operator > (const Socket& socket) const;
  58. bool operator >= (const Socket& socket) const;
  59. // configuration interfaces
  60. public:
  61. // return number of available bytes in the read buffer
  62. // -1 indicates failure, the error number can be fetched by LastError()
  63. TZ_INT Available();
  64. // return 0 on success, otherwise return -1
  65. TZ_INT SetBlocking(TZ_BOOL flag);
  66. TZ_BOOL GetBlocking() const;
  67. // return 0 on success, otherwise return -1
  68. TZ_INT SetSendBuffSize(TZ_Uint32 size);
  69. TZ_Uint32 GetSendBuffSize();
  70. // return 0 on success, otherwise return -1
  71. TZ_INT SetRecvBuffSize(TZ_Uint32 size);
  72. TZ_Uint32 GetRecvBuffSize();
  73. // return 0 on success, otherwise return -1
  74. TZ_INT SetReuseAddress(TZ_BOOL flag);
  75. TZ_BOOL GetReuseAddress();
  76. // return 0 on success, otherwise return -1
  77. TZ_INT SetReusePort(TZ_BOOL flag);
  78. TZ_BOOL GetReusePort();
  79. // data interfaces
  80. public:
  81. // all the Select functions are in following rules
  82. //
  83. // @timeout in microsecond,
  84. // return value : 0 for timeout, -1 for failure, 1 for success
  85. //
  86. // it indicates the select will block indefinitely
  87. // if @timeout is equal to INVALID_VALUE32
  88. // select for all
  89. TZ_INT Select(TZ_Uint32 timeout, TZ_BOOL & rdSet,
  90. TZ_BOOL & wrSet, TZ_BOOL & errSet);
  91. // select for read only
  92. TZ_INT SelectForRead(TZ_Uint32 timeout);
  93. // select for write only
  94. TZ_INT SelectForWrite(TZ_Uint32 timeout);
  95. // select for error only
  96. TZ_INT SelectForError(TZ_Uint32 timeout);
  97. void Close();
  98. public:
  99. TZ_BOOL IsValid() const;
  100. TZ_SOCKET Descriptor() const;
  101. TZ_Uint32 Type() const;
  102. TZ_Uint32 State() const;
  103. #ifdef LINUX
  104. const struct pollfd & Pollfd() const;
  105. #endif
  106. protected:
  107. int setOption(int level, int option, int value);
  108. int setOption(int level, int option, unsigned value);
  109. int setOption(int level, int option, unsigned char value);
  110. int setOption(int level, int option, struct ip_mreq value);
  111. int setOption(int level, int option, struct in_addr value);
  112. int setRawOption(int level, int option,
  113. const void * value, TZ_SOCKLEN length);
  114. int getOption(int level, int option, int & value);
  115. int getOption(int level, int option, unsigned & value);
  116. int getOption(int level, int option, unsigned char & value);
  117. int getOption(int level, int option, struct ip_mreq & value);
  118. int getOption(int level, int option, struct in_addr & value);
  119. int getRawOption(int level, int option,
  120. void * value, TZ_SOCKLEN & length);
  121. void setState(TZ_Uint32 state);
  122. #ifdef LINUX
  123. void initfd();
  124. #endif
  125. protected:
  126. TZ_SOCKET m_sock;
  127. TZ_Uint32 m_type;
  128. TZ_BOOL m_blocking;
  129. TZ_BOOL m_connected;
  130. #ifdef LINUX
  131. struct pollfd m_pfd;
  132. #endif
  133. private:
  134. RefCounter * m_refcnt;
  135. };
  136. //
  137. // inlines
  138. //
  139. inline bool Socket::operator == (const Socket& socket) const
  140. {
  141. return (this->m_sock == socket.m_sock);
  142. }
  143. inline bool Socket::operator != (const Socket& socket) const
  144. {
  145. return !(*this == socket);
  146. }
  147. inline bool Socket::operator < (const Socket& socket) const
  148. {
  149. return (this->m_sock < socket.m_sock);
  150. }
  151. inline bool Socket::operator <= (const Socket& socket) const
  152. {
  153. return (*this == socket || *this < socket);
  154. }
  155. inline bool Socket::operator > (const Socket& socket) const
  156. {
  157. return !(*this <= socket);
  158. }
  159. inline bool Socket::operator >= (const Socket& socket) const
  160. {
  161. return !(*this < socket);
  162. }
  163. inline TZ_BOOL Socket::GetBlocking() const
  164. {
  165. return m_blocking;
  166. }
  167. inline TZ_BOOL Socket::IsValid() const
  168. {
  169. return (m_sock != INVALID_SOCKET);
  170. }
  171. inline TZ_SOCKET Socket::Descriptor() const
  172. {
  173. return m_sock;
  174. }
  175. inline TZ_Uint32 Socket::Type() const
  176. {
  177. return m_type;
  178. }
  179. #ifdef LINUX
  180. inline const struct pollfd & Socket::Pollfd() const
  181. {
  182. return m_pfd;
  183. }
  184. #endif
  185. }; // namespace tzc
  186. #endif /* ----- #ifndef __SOCKET_H ----- */