PageRenderTime 53ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/poco-1.3.6p2/Net/include/Poco/Net/Socket.h

https://bitbucket.org/rosselm/projects
C Header | 610 lines | 268 code | 161 blank | 181 comment | 6 complexity | c4759d552326ba86dfcac78e450506fc MD5 | raw file
Possible License(s): GPL-2.0, MIT, GPL-3.0, BSD-3-Clause, LGPL-3.0
  1. //
  2. // Socket.h
  3. //
  4. // $Id: //poco/1.3/Net/include/Poco/Net/Socket.h#4 $
  5. //
  6. // Library: Net
  7. // Package: Sockets
  8. // Module: Socket
  9. //
  10. // Definition of the Socket class.
  11. //
  12. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // Permission is hereby granted, free of charge, to any person or organization
  16. // obtaining a copy of the software and accompanying documentation covered by
  17. // this license (the "Software") to use, reproduce, display, distribute,
  18. // execute, and transmit the Software, and to prepare derivative works of the
  19. // Software, and to permit third-parties to whom the Software is furnished to
  20. // do so, all subject to the following:
  21. //
  22. // The copyright notices in the Software and this entire statement, including
  23. // the above license grant, this restriction and the following disclaimer,
  24. // must be included in all copies of the Software, in whole or in part, and
  25. // all derivative works of the Software, unless such copies or derivative
  26. // works are solely in the form of machine-executable object code generated by
  27. // a source language processor.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  32. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  33. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  34. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. //
  37. #ifndef Net_Socket_INCLUDED
  38. #define Net_Socket_INCLUDED
  39. #include "Poco/Net/Net.h"
  40. #include "Poco/Net/SocketImpl.h"
  41. #include <vector>
  42. namespace Poco {
  43. namespace Net {
  44. class Net_API Socket
  45. /// Socket is the common base class for
  46. /// StreamSocket, ServerSocket, DatagramSocket and other
  47. /// socket classes.
  48. ///
  49. /// It provides operations common to all socket types.
  50. {
  51. public:
  52. enum SelectMode
  53. /// The mode argument to poll() and select().
  54. {
  55. SELECT_READ = 1,
  56. SELECT_WRITE = 2,
  57. SELECT_ERROR = 4
  58. };
  59. typedef std::vector<Socket> SocketList;
  60. Socket();
  61. /// Creates an uninitialized socket.
  62. Socket(const Socket& socket);
  63. /// Copy constructor.
  64. ///
  65. /// Attaches the SocketImpl from the other socket and
  66. /// increments the reference count of the SocketImpl.
  67. Socket& operator = (const Socket& socket);
  68. /// Assignment operator.
  69. ///
  70. /// Releases the socket's SocketImpl and
  71. /// attaches the SocketImpl from the other socket and
  72. /// increments the reference count of the SocketImpl.
  73. virtual ~Socket();
  74. /// Destroys the Socket and releases the
  75. /// SocketImpl.
  76. bool operator == (const Socket& socket) const;
  77. /// Returns true if both sockets share the same
  78. /// SocketImpl, false otherwise.
  79. bool operator != (const Socket& socket) const;
  80. /// Returns false if both sockets share the same
  81. /// SocketImpl, true otherwise.
  82. bool operator < (const Socket& socket) const;
  83. /// Compares the SocketImpl pointers.
  84. bool operator <= (const Socket& socket) const;
  85. /// Compares the SocketImpl pointers.
  86. bool operator > (const Socket& socket) const;
  87. /// Compares the SocketImpl pointers.
  88. bool operator >= (const Socket& socket) const;
  89. /// Compares the SocketImpl pointers.
  90. void close();
  91. /// Closes the socket.
  92. static int select(SocketList& readList, SocketList& writeList, SocketList& exceptList, const Poco::Timespan& timeout);
  93. /// Determines the status of one or more sockets,
  94. /// using a call to select().
  95. ///
  96. /// ReadList contains the list of sockets which should be
  97. /// checked for readability.
  98. ///
  99. /// WriteList contains the list of sockets which should be
  100. /// checked for writeability.
  101. ///
  102. /// ExceptList contains a list of sockets which should be
  103. /// checked for a pending error.
  104. ///
  105. /// Returns the number of sockets ready.
  106. ///
  107. /// After return,
  108. /// * readList contains those sockets ready for reading,
  109. /// * writeList contains those sockets ready for writing,
  110. /// * exceptList contains those sockets with a pending error.
  111. ///
  112. /// If the total number of sockets passed in readList, writeList and
  113. /// exceptList is zero, select() will return immediately and the
  114. /// return value will be 0.
  115. ///
  116. /// If one of the sockets passed to select() is closed while
  117. /// select() runs, select will return immediately. However,
  118. /// the closed socket will not be included in any list.
  119. /// In this case, the return value may be greater than the sum
  120. /// of all sockets in all list.
  121. bool poll(const Poco::Timespan& timeout, int mode) const;
  122. /// Determines the status of the socket, using a
  123. /// call to select().
  124. ///
  125. /// The mode argument is constructed by combining the values
  126. /// of the SelectMode enumeration.
  127. ///
  128. /// Returns true if the next operation corresponding to
  129. /// mode will not block, false otherwise.
  130. int available() const;
  131. /// Returns the number of bytes available that can be read
  132. /// without causing the socket to block.
  133. void setSendBufferSize(int size);
  134. /// Sets the size of the send buffer.
  135. int getSendBufferSize() const;
  136. /// Returns the size of the send buffer.
  137. ///
  138. /// The returned value may be different than the
  139. /// value previously set with setSendBufferSize(),
  140. /// as the system is free to adjust the value.
  141. void setReceiveBufferSize(int size);
  142. /// Sets the size of the receive buffer.
  143. int getReceiveBufferSize() const;
  144. /// Returns the size of the receive buffer.
  145. ///
  146. /// The returned value may be different than the
  147. /// value previously set with setReceiveBufferSize(),
  148. /// as the system is free to adjust the value.
  149. void setSendTimeout(const Poco::Timespan& timeout);
  150. /// Sets the send timeout for the socket.
  151. Poco::Timespan getSendTimeout() const;
  152. /// Returns the send timeout for the socket.
  153. ///
  154. /// The returned timeout may be different than the
  155. /// timeout previously set with setSendTimeout(),
  156. /// as the system is free to adjust the value.
  157. void setReceiveTimeout(const Poco::Timespan& timeout);
  158. /// Sets the send timeout for the socket.
  159. ///
  160. /// On systems that do not support SO_RCVTIMEO, a
  161. /// workaround using poll() is provided.
  162. Poco::Timespan getReceiveTimeout() const;
  163. /// Returns the receive timeout for the socket.
  164. ///
  165. /// The returned timeout may be different than the
  166. /// timeout previously set with getReceiveTimeout(),
  167. /// as the system is free to adjust the value.
  168. void setOption(int level, int option, int value);
  169. /// Sets the socket option specified by level and option
  170. /// to the given integer value.
  171. void setOption(int level, int option, unsigned value);
  172. /// Sets the socket option specified by level and option
  173. /// to the given integer value.
  174. void setOption(int level, int option, unsigned char value);
  175. /// Sets the socket option specified by level and option
  176. /// to the given integer value.
  177. void setOption(int level, int option, const Poco::Timespan& value);
  178. /// Sets the socket option specified by level and option
  179. /// to the given time value.
  180. void setOption(int level, int option, const IPAddress& value);
  181. /// Sets the socket option specified by level and option
  182. /// to the given time value.
  183. void getOption(int level, int option, int& value) const;
  184. /// Returns the value of the socket option
  185. /// specified by level and option.
  186. void getOption(int level, int option, unsigned& value) const;
  187. /// Returns the value of the socket option
  188. /// specified by level and option.
  189. void getOption(int level, int option, unsigned char& value) const;
  190. /// Returns the value of the socket option
  191. /// specified by level and option.
  192. void getOption(int level, int option, Poco::Timespan& value) const;
  193. /// Returns the value of the socket option
  194. /// specified by level and option.
  195. void getOption(int level, int option, IPAddress& value) const;
  196. /// Returns the value of the socket option
  197. /// specified by level and option.
  198. void setLinger(bool on, int seconds);
  199. /// Sets the value of the SO_LINGER socket option.
  200. void getLinger(bool& on, int& seconds) const;
  201. /// Returns the value of the SO_LINGER socket option.
  202. void setNoDelay(bool flag);
  203. /// Sets the value of the TCP_NODELAY socket option.
  204. bool getNoDelay() const;
  205. /// Returns the value of the TCP_NODELAY socket option.
  206. void setKeepAlive(bool flag);
  207. /// Sets the value of the SO_KEEPALIVE socket option.
  208. bool getKeepAlive() const;
  209. /// Returns the value of the SO_KEEPALIVE socket option.
  210. void setReuseAddress(bool flag);
  211. /// Sets the value of the SO_REUSEADDR socket option.
  212. bool getReuseAddress() const;
  213. /// Returns the value of the SO_REUSEADDR socket option.
  214. void setReusePort(bool flag);
  215. /// Sets the value of the SO_REUSEPORT socket option.
  216. /// Does nothing if the socket implementation does not
  217. /// support SO_REUSEPORT.
  218. bool getReusePort() const;
  219. /// Returns the value of the SO_REUSEPORT socket option.
  220. ///
  221. /// Returns false if the socket implementation does not
  222. /// support SO_REUSEPORT.
  223. void setOOBInline(bool flag);
  224. /// Sets the value of the SO_OOBINLINE socket option.
  225. bool getOOBInline() const;
  226. /// Returns the value of the SO_OOBINLINE socket option.
  227. void setBlocking(bool flag);
  228. /// Sets the socket in blocking mode if flag is true,
  229. /// disables blocking mode if flag is false.
  230. bool getBlocking() const;
  231. /// Returns the blocking mode of the socket.
  232. /// This method will only work if the blocking modes of
  233. /// the socket are changed via the setBlocking method!
  234. SocketAddress address() const;
  235. /// Returns the IP address and port number of the socket.
  236. SocketAddress peerAddress() const;
  237. /// Returns the IP address and port number of the peer socket.
  238. SocketImpl* impl() const;
  239. /// Returns the SocketImpl for this socket.
  240. static bool supportsIPv4();
  241. /// Returns true if the system supports IPv4.
  242. static bool supportsIPv6();
  243. /// Returns true if the system supports IPv6.
  244. protected:
  245. Socket(SocketImpl* pImpl);
  246. /// Creates the Socket and attaches the given SocketImpl.
  247. /// The socket takes owership of the SocketImpl.
  248. poco_socket_t sockfd() const;
  249. /// Returns the socket descriptor for this socket.
  250. private:
  251. SocketImpl* _pImpl;
  252. };
  253. //
  254. // inlines
  255. //
  256. inline bool Socket::operator == (const Socket& socket) const
  257. {
  258. return _pImpl == socket._pImpl;
  259. }
  260. inline bool Socket::operator != (const Socket& socket) const
  261. {
  262. return _pImpl != socket._pImpl;
  263. }
  264. inline bool Socket::operator < (const Socket& socket) const
  265. {
  266. return _pImpl < socket._pImpl;
  267. }
  268. inline bool Socket::operator <= (const Socket& socket) const
  269. {
  270. return _pImpl <= socket._pImpl;
  271. }
  272. inline bool Socket::operator > (const Socket& socket) const
  273. {
  274. return _pImpl > socket._pImpl;
  275. }
  276. inline bool Socket::operator >= (const Socket& socket) const
  277. {
  278. return _pImpl >= socket._pImpl;
  279. }
  280. inline void Socket::close()
  281. {
  282. _pImpl->close();
  283. }
  284. inline bool Socket::poll(const Poco::Timespan& timeout, int mode) const
  285. {
  286. return _pImpl->poll(timeout, mode);
  287. }
  288. inline int Socket::available() const
  289. {
  290. return _pImpl->available();
  291. }
  292. inline void Socket::setSendBufferSize(int size)
  293. {
  294. _pImpl->setSendBufferSize(size);
  295. }
  296. inline int Socket::getSendBufferSize() const
  297. {
  298. return _pImpl->getSendBufferSize();
  299. }
  300. inline void Socket::setReceiveBufferSize(int size)
  301. {
  302. _pImpl->setReceiveBufferSize(size);
  303. }
  304. inline int Socket::getReceiveBufferSize() const
  305. {
  306. return _pImpl->getReceiveBufferSize();
  307. }
  308. inline void Socket::setSendTimeout(const Poco::Timespan& timeout)
  309. {
  310. _pImpl->setSendTimeout(timeout);
  311. }
  312. inline Poco::Timespan Socket::getSendTimeout() const
  313. {
  314. return _pImpl->getSendTimeout();
  315. }
  316. inline void Socket::setReceiveTimeout(const Poco::Timespan& timeout)
  317. {
  318. _pImpl->setReceiveTimeout(timeout);
  319. }
  320. inline Poco::Timespan Socket::getReceiveTimeout() const
  321. {
  322. return _pImpl->getReceiveTimeout();
  323. }
  324. inline void Socket::setOption(int level, int option, int value)
  325. {
  326. _pImpl->setOption(level, option, value);
  327. }
  328. inline void Socket::setOption(int level, int option, unsigned value)
  329. {
  330. _pImpl->setOption(level, option, value);
  331. }
  332. inline void Socket::setOption(int level, int option, unsigned char value)
  333. {
  334. _pImpl->setOption(level, option, value);
  335. }
  336. inline void Socket::setOption(int level, int option, const Poco::Timespan& value)
  337. {
  338. _pImpl->setOption(level, option, value);
  339. }
  340. inline void Socket::setOption(int level, int option, const IPAddress& value)
  341. {
  342. _pImpl->setOption(level, option, value);
  343. }
  344. inline void Socket::getOption(int level, int option, int& value) const
  345. {
  346. _pImpl->getOption(level, option, value);
  347. }
  348. inline void Socket::getOption(int level, int option, unsigned& value) const
  349. {
  350. _pImpl->getOption(level, option, value);
  351. }
  352. inline void Socket::getOption(int level, int option, unsigned char& value) const
  353. {
  354. _pImpl->getOption(level, option, value);
  355. }
  356. inline void Socket::getOption(int level, int option, Poco::Timespan& value) const
  357. {
  358. _pImpl->getOption(level, option, value);
  359. }
  360. inline void Socket::getOption(int level, int option, IPAddress& value) const
  361. {
  362. _pImpl->getOption(level, option, value);
  363. }
  364. inline void Socket::setLinger(bool on, int seconds)
  365. {
  366. _pImpl->setLinger(on, seconds);
  367. }
  368. inline void Socket::getLinger(bool& on, int& seconds) const
  369. {
  370. _pImpl->getLinger(on, seconds);
  371. }
  372. inline void Socket::setNoDelay(bool flag)
  373. {
  374. _pImpl->setNoDelay(flag);
  375. }
  376. inline bool Socket::getNoDelay() const
  377. {
  378. return _pImpl->getNoDelay();
  379. }
  380. inline void Socket::setKeepAlive(bool flag)
  381. {
  382. _pImpl->setKeepAlive(flag);
  383. }
  384. inline bool Socket::getKeepAlive() const
  385. {
  386. return _pImpl->getKeepAlive();
  387. }
  388. inline void Socket::setReuseAddress(bool flag)
  389. {
  390. _pImpl->setReuseAddress(flag);
  391. }
  392. inline bool Socket::getReuseAddress() const
  393. {
  394. return _pImpl->getReuseAddress();
  395. }
  396. inline void Socket::setReusePort(bool flag)
  397. {
  398. _pImpl->setReusePort(flag);
  399. }
  400. inline bool Socket::getReusePort() const
  401. {
  402. return _pImpl->getReusePort();
  403. }
  404. inline void Socket::setOOBInline(bool flag)
  405. {
  406. _pImpl->setOOBInline(flag);
  407. }
  408. inline bool Socket::getOOBInline() const
  409. {
  410. return _pImpl->getOOBInline();
  411. }
  412. inline void Socket::setBlocking(bool flag)
  413. {
  414. _pImpl->setBlocking(flag);
  415. }
  416. inline bool Socket::getBlocking() const
  417. {
  418. return _pImpl->getBlocking();
  419. }
  420. inline SocketImpl* Socket::impl() const
  421. {
  422. return _pImpl;
  423. }
  424. inline poco_socket_t Socket::sockfd() const
  425. {
  426. return _pImpl->sockfd();
  427. }
  428. inline SocketAddress Socket::address() const
  429. {
  430. return _pImpl->address();
  431. }
  432. inline SocketAddress Socket::peerAddress() const
  433. {
  434. return _pImpl->peerAddress();
  435. }
  436. inline bool Socket::supportsIPv4()
  437. {
  438. return true;
  439. }
  440. inline bool Socket::supportsIPv6()
  441. {
  442. #if defined(POCO_HAVE_IPv6)
  443. return true;
  444. #else
  445. return false;
  446. #endif
  447. }
  448. } } // namespace Poco::Net
  449. #endif // Net_Socket_INCLUDED