PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/of/of_v0.9.3_vs_release/libs/poco/include/Poco/Net/SocketImpl.h

https://gitlab.com/cocoroac/walkingartists
C Header | 469 lines | 130 code | 98 blank | 241 comment | 1 complexity | 0eeee372793dbbe382a53d5fac628c65 MD5 | raw file
  1. //
  2. // SocketImpl.h
  3. //
  4. // $Id: //poco/1.4/Net/include/Poco/Net/SocketImpl.h#4 $
  5. //
  6. // Library: Net
  7. // Package: Sockets
  8. // Module: SocketImpl
  9. //
  10. // Definition of the SocketImpl class.
  11. //
  12. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Net_SocketImpl_INCLUDED
  18. #define Net_SocketImpl_INCLUDED
  19. #include "Poco/Net/Net.h"
  20. #include "Poco/Net/SocketDefs.h"
  21. #include "Poco/Net/SocketAddress.h"
  22. #include "Poco/RefCountedObject.h"
  23. #include "Poco/Timespan.h"
  24. namespace Poco {
  25. namespace Net {
  26. class Net_API SocketImpl: public Poco::RefCountedObject
  27. /// This class encapsulates the Berkeley sockets API.
  28. ///
  29. /// Subclasses implement specific socket types like
  30. /// stream or datagram sockets.
  31. ///
  32. /// You should not create any instances of this class.
  33. {
  34. public:
  35. enum SelectMode
  36. {
  37. SELECT_READ = 1,
  38. SELECT_WRITE = 2,
  39. SELECT_ERROR = 4
  40. };
  41. virtual SocketImpl* acceptConnection(SocketAddress& clientAddr);
  42. /// Get the next completed connection from the
  43. /// socket's completed connection queue.
  44. ///
  45. /// If the queue is empty, waits until a connection
  46. /// request completes.
  47. ///
  48. /// Returns a new TCP socket for the connection
  49. /// with the client.
  50. ///
  51. /// The client socket's address is returned in clientAddr.
  52. virtual void connect(const SocketAddress& address);
  53. /// Initializes the socket and establishes a connection to
  54. /// the TCP server at the given address.
  55. ///
  56. /// Can also be used for UDP sockets. In this case, no
  57. /// connection is established. Instead, incoming and outgoing
  58. /// packets are restricted to the specified address.
  59. virtual void connect(const SocketAddress& address, const Poco::Timespan& timeout);
  60. /// Initializes the socket, sets the socket timeout and
  61. /// establishes a connection to the TCP server at the given address.
  62. virtual void connectNB(const SocketAddress& address);
  63. /// Initializes the socket and establishes a connection to
  64. /// the TCP server at the given address. Prior to opening the
  65. /// connection the socket is set to nonblocking mode.
  66. virtual void bind(const SocketAddress& address, bool reuseAddress = false);
  67. /// Bind a local address to the socket.
  68. ///
  69. /// This is usually only done when establishing a server
  70. /// socket. TCP clients should not bind a socket to a
  71. /// specific address.
  72. ///
  73. /// If reuseAddress is true, sets the SO_REUSEADDR
  74. /// socket option.
  75. virtual void bind6(const SocketAddress& address, bool reuseAddress = false, bool ipV6Only = false);
  76. /// Bind a local IPv6 address to the socket.
  77. ///
  78. /// This is usually only done when establishing a server
  79. /// socket. TCP clients should not bind a socket to a
  80. /// specific address.
  81. ///
  82. /// If reuseAddress is true, sets the SO_REUSEADDR
  83. /// socket option.
  84. ///
  85. /// The given address must be an IPv6 address. The
  86. /// IPPROTO_IPV6/IPV6_V6ONLY option is set on the socket
  87. /// according to the ipV6Only parameter.
  88. ///
  89. /// If the library has not been built with IPv6 support,
  90. /// a Poco::NotImplementedException will be thrown.
  91. virtual void listen(int backlog = 64);
  92. /// Puts the socket into listening state.
  93. ///
  94. /// The socket becomes a passive socket that
  95. /// can accept incoming connection requests.
  96. ///
  97. /// The backlog argument specifies the maximum
  98. /// number of connections that can be queued
  99. /// for this socket.
  100. virtual void close();
  101. /// Close the socket.
  102. virtual void shutdownReceive();
  103. /// Shuts down the receiving part of the socket connection.
  104. virtual void shutdownSend();
  105. /// Shuts down the sending part of the socket connection.
  106. virtual void shutdown();
  107. /// Shuts down both the receiving and the sending part
  108. /// of the socket connection.
  109. virtual int sendBytes(const void* buffer, int length, int flags = 0);
  110. /// Sends the contents of the given buffer through
  111. /// the socket.
  112. ///
  113. /// Returns the number of bytes sent, which may be
  114. /// less than the number of bytes specified.
  115. ///
  116. /// Certain socket implementations may also return a negative
  117. /// value denoting a certain condition.
  118. virtual int receiveBytes(void* buffer, int length, int flags = 0);
  119. /// Receives data from the socket and stores it
  120. /// in buffer. Up to length bytes are received.
  121. ///
  122. /// Returns the number of bytes received.
  123. ///
  124. /// Certain socket implementations may also return a negative
  125. /// value denoting a certain condition.
  126. virtual int sendTo(const void* buffer, int length, const SocketAddress& address, int flags = 0);
  127. /// Sends the contents of the given buffer through
  128. /// the socket to the given address.
  129. ///
  130. /// Returns the number of bytes sent, which may be
  131. /// less than the number of bytes specified.
  132. virtual int receiveFrom(void* buffer, int length, SocketAddress& address, int flags = 0);
  133. /// Receives data from the socket and stores it
  134. /// in buffer. Up to length bytes are received.
  135. /// Stores the address of the sender in address.
  136. ///
  137. /// Returns the number of bytes received.
  138. virtual void sendUrgent(unsigned char data);
  139. /// Sends one byte of urgent data through
  140. /// the socket.
  141. ///
  142. /// The data is sent with the MSG_OOB flag.
  143. ///
  144. /// The preferred way for a socket to receive urgent data
  145. /// is by enabling the SO_OOBINLINE option.
  146. virtual int available();
  147. /// Returns the number of bytes available that can be read
  148. /// without causing the socket to block.
  149. virtual bool poll(const Poco::Timespan& timeout, int mode);
  150. /// Determines the status of the socket, using a
  151. /// call to select().
  152. ///
  153. /// The mode argument is constructed by combining the values
  154. /// of the SelectMode enumeration.
  155. ///
  156. /// Returns true if the next operation corresponding to
  157. /// mode will not block, false otherwise.
  158. virtual void setSendBufferSize(int size);
  159. /// Sets the size of the send buffer.
  160. virtual int getSendBufferSize();
  161. /// Returns the size of the send buffer.
  162. ///
  163. /// The returned value may be different than the
  164. /// value previously set with setSendBufferSize(),
  165. /// as the system is free to adjust the value.
  166. virtual void setReceiveBufferSize(int size);
  167. /// Sets the size of the receive buffer.
  168. virtual int getReceiveBufferSize();
  169. /// Returns the size of the receive buffer.
  170. ///
  171. /// The returned value may be different than the
  172. /// value previously set with setReceiveBufferSize(),
  173. /// as the system is free to adjust the value.
  174. virtual void setSendTimeout(const Poco::Timespan& timeout);
  175. /// Sets the send timeout for the socket.
  176. virtual Poco::Timespan getSendTimeout();
  177. /// Returns the send timeout for the socket.
  178. ///
  179. /// The returned timeout may be different than the
  180. /// timeout previously set with setSendTimeout(),
  181. /// as the system is free to adjust the value.
  182. virtual void setReceiveTimeout(const Poco::Timespan& timeout);
  183. /// Sets the send timeout for the socket.
  184. ///
  185. /// On systems that do not support SO_RCVTIMEO, a
  186. /// workaround using poll() is provided.
  187. virtual Poco::Timespan getReceiveTimeout();
  188. /// Returns the receive timeout for the socket.
  189. ///
  190. /// The returned timeout may be different than the
  191. /// timeout previously set with setReceiveTimeout(),
  192. /// as the system is free to adjust the value.
  193. virtual SocketAddress address();
  194. /// Returns the IP address and port number of the socket.
  195. virtual SocketAddress peerAddress();
  196. /// Returns the IP address and port number of the peer socket.
  197. void setOption(int level, int option, int value);
  198. /// Sets the socket option specified by level and option
  199. /// to the given integer value.
  200. void setOption(int level, int option, unsigned value);
  201. /// Sets the socket option specified by level and option
  202. /// to the given integer value.
  203. void setOption(int level, int option, unsigned char value);
  204. /// Sets the socket option specified by level and option
  205. /// to the given integer value.
  206. void setOption(int level, int option, const Poco::Timespan& value);
  207. /// Sets the socket option specified by level and option
  208. /// to the given time value.
  209. void setOption(int level, int option, const IPAddress& value);
  210. /// Sets the socket option specified by level and option
  211. /// to the given time value.
  212. virtual void setRawOption(int level, int option, const void* value, poco_socklen_t length);
  213. /// Sets the socket option specified by level and option
  214. /// to the given time value.
  215. void getOption(int level, int option, int& value);
  216. /// Returns the value of the socket option
  217. /// specified by level and option.
  218. void getOption(int level, int option, unsigned& value);
  219. /// Returns the value of the socket option
  220. /// specified by level and option.
  221. void getOption(int level, int option, unsigned char& value);
  222. /// Returns the value of the socket option
  223. /// specified by level and option.
  224. void getOption(int level, int option, Poco::Timespan& value);
  225. /// Returns the value of the socket option
  226. /// specified by level and option.
  227. void getOption(int level, int option, IPAddress& value);
  228. /// Returns the value of the socket option
  229. /// specified by level and option.
  230. virtual void getRawOption(int level, int option, void* value, poco_socklen_t& length);
  231. /// Returns the value of the socket option
  232. /// specified by level and option.
  233. void setLinger(bool on, int seconds);
  234. /// Sets the value of the SO_LINGER socket option.
  235. void getLinger(bool& on, int& seconds);
  236. /// Returns the value of the SO_LINGER socket option.
  237. void setNoDelay(bool flag);
  238. /// Sets the value of the TCP_NODELAY socket option.
  239. bool getNoDelay();
  240. /// Returns the value of the TCP_NODELAY socket option.
  241. void setKeepAlive(bool flag);
  242. /// Sets the value of the SO_KEEPALIVE socket option.
  243. bool getKeepAlive();
  244. /// Returns the value of the SO_KEEPALIVE socket option.
  245. void setReuseAddress(bool flag);
  246. /// Sets the value of the SO_REUSEADDR socket option.
  247. bool getReuseAddress();
  248. /// Returns the value of the SO_REUSEADDR socket option.
  249. void setReusePort(bool flag);
  250. /// Sets the value of the SO_REUSEPORT socket option.
  251. /// Does nothing if the socket implementation does not
  252. /// support SO_REUSEPORT.
  253. bool getReusePort();
  254. /// Returns the value of the SO_REUSEPORT socket option.
  255. ///
  256. /// Returns false if the socket implementation does not
  257. /// support SO_REUSEPORT.
  258. void setOOBInline(bool flag);
  259. /// Sets the value of the SO_OOBINLINE socket option.
  260. bool getOOBInline();
  261. /// Returns the value of the SO_OOBINLINE socket option.
  262. void setBroadcast(bool flag);
  263. /// Sets the value of the SO_BROADCAST socket option.
  264. bool getBroadcast();
  265. /// Returns the value of the SO_BROADCAST socket option.
  266. virtual void setBlocking(bool flag);
  267. /// Sets the socket in blocking mode if flag is true,
  268. /// disables blocking mode if flag is false.
  269. virtual bool getBlocking() const;
  270. /// Returns the blocking mode of the socket.
  271. /// This method will only work if the blocking modes of
  272. /// the socket are changed via the setBlocking method!
  273. virtual bool secure() const;
  274. /// Returns true iff the socket's connection is secure
  275. /// (using SSL or TLS).
  276. int socketError();
  277. /// Returns the value of the SO_ERROR socket option.
  278. poco_socket_t sockfd() const;
  279. /// Returns the socket descriptor for the
  280. /// underlying native socket.
  281. void ioctl(poco_ioctl_request_t request, int& arg);
  282. /// A wrapper for the ioctl system call.
  283. void ioctl(poco_ioctl_request_t request, void* arg);
  284. /// A wrapper for the ioctl system call.
  285. #if defined(POCO_OS_FAMILY_UNIX)
  286. int fcntl(poco_fcntl_request_t request);
  287. /// A wrapper for the fcntl system call.
  288. int fcntl(poco_fcntl_request_t request, long arg);
  289. /// A wrapper for the fcntl system call.
  290. #endif
  291. bool initialized() const;
  292. /// Returns true iff the underlying socket is initialized.
  293. protected:
  294. SocketImpl();
  295. /// Creates a SocketImpl.
  296. SocketImpl(poco_socket_t sockfd);
  297. /// Creates a SocketImpl using the given native socket.
  298. virtual ~SocketImpl();
  299. /// Destroys the SocketImpl.
  300. /// Closes the socket if it is still open.
  301. virtual void init(int af);
  302. /// Creates the underlying native socket.
  303. ///
  304. /// Subclasses must implement this method so
  305. /// that it calls initSocket() with the
  306. /// appropriate arguments.
  307. ///
  308. /// The default implementation creates a
  309. /// stream socket.
  310. void initSocket(int af, int type, int proto = 0);
  311. /// Creates the underlying native socket.
  312. ///
  313. /// The first argument, af, specifies the address family
  314. /// used by the socket, which should be either AF_INET or
  315. /// AF_INET6.
  316. ///
  317. /// The second argument, type, specifies the type of the
  318. /// socket, which can be one of SOCK_STREAM, SOCK_DGRAM
  319. /// or SOCK_RAW.
  320. ///
  321. /// The third argument, proto, is normally set to 0,
  322. /// except for raw sockets.
  323. void reset(poco_socket_t fd = POCO_INVALID_SOCKET);
  324. /// Allows subclasses to set the socket manually, iff no valid socket is set yet.
  325. static int lastError();
  326. /// Returns the last error code.
  327. static void error();
  328. /// Throws an appropriate exception for the last error.
  329. static void error(const std::string& arg);
  330. /// Throws an appropriate exception for the last error.
  331. static void error(int code);
  332. /// Throws an appropriate exception for the given error code.
  333. static void error(int code, const std::string& arg);
  334. /// Throws an appropriate exception for the given error code.
  335. private:
  336. SocketImpl(const SocketImpl&);
  337. SocketImpl& operator = (const SocketImpl&);
  338. poco_socket_t _sockfd;
  339. #if defined(POCO_BROKEN_TIMEOUTS)
  340. Poco::Timespan _recvTimeout;
  341. Poco::Timespan _sndTimeout;
  342. #endif
  343. bool _blocking;
  344. friend class Socket;
  345. friend class SecureSocketImpl;
  346. };
  347. //
  348. // inlines
  349. //
  350. inline poco_socket_t SocketImpl::sockfd() const
  351. {
  352. return _sockfd;
  353. }
  354. inline bool SocketImpl::initialized() const
  355. {
  356. return _sockfd != POCO_INVALID_SOCKET;
  357. }
  358. inline int SocketImpl::lastError()
  359. {
  360. #if defined(_WIN32)
  361. return WSAGetLastError();
  362. #else
  363. return errno;
  364. #endif
  365. }
  366. inline bool SocketImpl::getBlocking() const
  367. {
  368. return _blocking;
  369. }
  370. } } // namespace Poco::Net
  371. #endif // Net_SocketImpl_INCLUDED