PageRenderTime 33ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/glibmm-2.33.3/gio/giomm/socket.h

#
C Header | 1080 lines | 227 code | 152 blank | 701 comment | 0 complexity | 0246f83a5c6779551cb9e38f2c6ecabb MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. // -*- c++ -*-
  2. // Generated by gtkmmproc -- DO NOT MODIFY!
  3. #ifndef _GIOMM_SOCKET_H
  4. #define _GIOMM_SOCKET_H
  5. #include <glibmm/ustring.h>
  6. #include <sigc++/sigc++.h>
  7. // -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  8. /* Copyright (C) 2009 Jonathon Jongsma
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free
  22. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <glibmm/iochannel.h>
  25. #include <glibmm/object.h>
  26. #include <giomm/initable.h>
  27. #include <giomm/credentials.h>
  28. #include <giomm/socketconnectable.h>
  29. #include <giomm/socketaddress.h>
  30. #include <giomm/enums.h>
  31. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  32. typedef struct _GSocket GSocket;
  33. typedef struct _GSocketClass GSocketClass;
  34. #endif /* DOXYGEN_SHOULD_SKIP_THIS */
  35. namespace Gio
  36. { class Socket_Class; } // namespace Gio
  37. namespace Gio
  38. {
  39. /** @addtogroup giommEnums giomm Enums and Flags */
  40. /**
  41. * @ingroup giommEnums
  42. */
  43. enum SocketType
  44. {
  45. SOCKET_TYPE_INVALID,
  46. SOCKET_TYPE_STREAM,
  47. SOCKET_TYPE_DATAGRAM,
  48. SOCKET_TYPE_SEQPACKET
  49. };
  50. } // namespace Gio
  51. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  52. namespace Glib
  53. {
  54. template <>
  55. class Value<Gio::SocketType> : public Glib::Value_Enum<Gio::SocketType>
  56. {
  57. public:
  58. static GType value_type() G_GNUC_CONST;
  59. };
  60. } // namespace Glib
  61. #endif /* DOXYGEN_SHOULD_SKIP_THIS */
  62. namespace Gio
  63. {
  64. /**
  65. * @ingroup giommEnums
  66. */
  67. enum SocketProtocol
  68. {
  69. SOCKET_PROTOCOL_UNKNOWN = -1,
  70. SOCKET_PROTOCOL_DEFAULT = 0,
  71. SOCKET_PROTOCOL_TCP = 6,
  72. SOCKET_PROTOCOL_UDP = 17,
  73. SOCKET_PROTOCOL_SCTP = 132
  74. };
  75. } // namespace Gio
  76. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  77. namespace Glib
  78. {
  79. template <>
  80. class Value<Gio::SocketProtocol> : public Glib::Value_Enum<Gio::SocketProtocol>
  81. {
  82. public:
  83. static GType value_type() G_GNUC_CONST;
  84. };
  85. } // namespace Glib
  86. #endif /* DOXYGEN_SHOULD_SKIP_THIS */
  87. namespace Gio
  88. {
  89. /**
  90. * @ingroup giommEnums
  91. * @par Bitwise operators:
  92. * <tt>%SocketMsgFlags operator|(SocketMsgFlags, SocketMsgFlags)</tt><br>
  93. * <tt>%SocketMsgFlags operator&(SocketMsgFlags, SocketMsgFlags)</tt><br>
  94. * <tt>%SocketMsgFlags operator^(SocketMsgFlags, SocketMsgFlags)</tt><br>
  95. * <tt>%SocketMsgFlags operator~(SocketMsgFlags)</tt><br>
  96. * <tt>%SocketMsgFlags& operator|=(SocketMsgFlags&, SocketMsgFlags)</tt><br>
  97. * <tt>%SocketMsgFlags& operator&=(SocketMsgFlags&, SocketMsgFlags)</tt><br>
  98. * <tt>%SocketMsgFlags& operator^=(SocketMsgFlags&, SocketMsgFlags)</tt><br>
  99. */
  100. enum SocketMsgFlags
  101. {
  102. SOCKET_MSG_NONE = 0x0,
  103. SOCKET_MSG_OOB = GLIB_SYSDEF_MSG_OOB,
  104. SOCKET_MSG_PEEK = GLIB_SYSDEF_MSG_PEEK,
  105. SOCKET_MSG_DONTROUTE = GLIB_SYSDEF_MSG_DONTROUTE
  106. };
  107. /** @ingroup giommEnums */
  108. inline SocketMsgFlags operator|(SocketMsgFlags lhs, SocketMsgFlags rhs)
  109. { return static_cast<SocketMsgFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs)); }
  110. /** @ingroup giommEnums */
  111. inline SocketMsgFlags operator&(SocketMsgFlags lhs, SocketMsgFlags rhs)
  112. { return static_cast<SocketMsgFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs)); }
  113. /** @ingroup giommEnums */
  114. inline SocketMsgFlags operator^(SocketMsgFlags lhs, SocketMsgFlags rhs)
  115. { return static_cast<SocketMsgFlags>(static_cast<unsigned>(lhs) ^ static_cast<unsigned>(rhs)); }
  116. /** @ingroup giommEnums */
  117. inline SocketMsgFlags operator~(SocketMsgFlags flags)
  118. { return static_cast<SocketMsgFlags>(~static_cast<unsigned>(flags)); }
  119. /** @ingroup giommEnums */
  120. inline SocketMsgFlags& operator|=(SocketMsgFlags& lhs, SocketMsgFlags rhs)
  121. { return (lhs = static_cast<SocketMsgFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs))); }
  122. /** @ingroup giommEnums */
  123. inline SocketMsgFlags& operator&=(SocketMsgFlags& lhs, SocketMsgFlags rhs)
  124. { return (lhs = static_cast<SocketMsgFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs))); }
  125. /** @ingroup giommEnums */
  126. inline SocketMsgFlags& operator^=(SocketMsgFlags& lhs, SocketMsgFlags rhs)
  127. { return (lhs = static_cast<SocketMsgFlags>(static_cast<unsigned>(lhs) ^ static_cast<unsigned>(rhs))); }
  128. } // namespace Gio
  129. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  130. namespace Glib
  131. {
  132. template <>
  133. class Value<Gio::SocketMsgFlags> : public Glib::Value_Flags<Gio::SocketMsgFlags>
  134. {
  135. public:
  136. static GType value_type() G_GNUC_CONST;
  137. };
  138. } // namespace Glib
  139. #endif /* DOXYGEN_SHOULD_SKIP_THIS */
  140. namespace Gio
  141. {
  142. /** @defgroup NetworkIO Portable Network I/O Functionality
  143. */
  144. /** Low-level socket object
  145. *
  146. * A Socket is a low-level networking primitive. It is a more or less direct
  147. * mapping of the BSD socket API in a portable GObject based API. It supports
  148. * both the UNIX socket implementations and winsock2 on Windows.
  149. *
  150. * Socket is the platform independent base upon which the higher level network
  151. * primitives are based. Applications are not typically meant to use it
  152. * directly, but rather through classes like SocketClient, SocketService and
  153. * SocketConnection. However there may be cases where direct use of Socket is
  154. * useful.
  155. *
  156. * Socket implements the Initable interface, and since initialization can fail,
  157. * the constructor may throw an exception.
  158. *
  159. * Sockets operate in two general modes, blocking or non-blocking. When in
  160. * blocking mode all operations block until the requested operation is finished
  161. * or there is an error. In non-blocking mode all calls that would block return
  162. * immediately with a Gio::Error::WOULD_BLOCK error. To know when a call would
  163. * successfully run you can call condition_check(), or condition_wait(). You can
  164. * also use create_source() and attach it to a Glib::MainContext to get
  165. * callbacks when I/O is possible. Note that all sockets are always set to non
  166. * blocking mode in the system, and blocking mode is emulated in Socket.
  167. *
  168. * When working in non-blocking mode applications should always be able to
  169. * handle getting a Gio::Error::WOULD_BLOCK error even when some other function
  170. * said that I/O was possible. This can easily happen in case of a race
  171. * condition in the application, but it can also happen for other reasons. For
  172. * instance, on Windows a socket is always seen as writable until a write
  173. * returns Gio::Error::WOULD_BLOCK.
  174. *
  175. * Sockets can be either connection oriented or datagram based. For connection
  176. * oriented types you must first establish a connection by either connecting to
  177. * an address or accepting a connection from another address. For connectionless
  178. * socket types the target/source address is specified or received in each I/O
  179. * operation.
  180. *
  181. * All socket file descriptors are set to be close-on-exec.
  182. *
  183. * Note that creating a Socket causes the signal SIGPIPE to be ignored for the
  184. * remainder of the program. If you are writing a command-line utility that uses
  185. * Socket, you may need to take into account the fact that your program will not
  186. * automatically be killed if it tries to write to stdout after it has been
  187. * closed.
  188. *
  189. * @newin{2,24}
  190. * @ingroup NetworkIO
  191. */
  192. class Socket : public Glib::Object, public Initable
  193. {
  194. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  195. public:
  196. typedef Socket CppObjectType;
  197. typedef Socket_Class CppClassType;
  198. typedef GSocket BaseObjectType;
  199. typedef GSocketClass BaseClassType;
  200. private: friend class Socket_Class;
  201. static CppClassType socket_class_;
  202. private:
  203. // noncopyable
  204. Socket(const Socket&);
  205. Socket& operator=(const Socket&);
  206. protected:
  207. explicit Socket(const Glib::ConstructParams& construct_params);
  208. explicit Socket(GSocket* castitem);
  209. #endif /* DOXYGEN_SHOULD_SKIP_THIS */
  210. public:
  211. virtual ~Socket();
  212. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  213. static GType get_type() G_GNUC_CONST;
  214. static GType get_base_type() G_GNUC_CONST;
  215. #endif
  216. ///Provides access to the underlying C GObject.
  217. GSocket* gobj() { return reinterpret_cast<GSocket*>(gobject_); }
  218. ///Provides access to the underlying C GObject.
  219. const GSocket* gobj() const { return reinterpret_cast<GSocket*>(gobject_); }
  220. ///Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs.
  221. GSocket* gobj_copy();
  222. private:
  223. protected:
  224. Socket(SocketFamily family, SocketType type, SocketProtocol protocol,
  225. const Glib::RefPtr<Cancellable>& cancellable);
  226. Socket(int fd, const Glib::RefPtr<Cancellable>& cancellable);
  227. public:
  228. /** Creates a new Socket with the defined family, type and protocol.
  229. * If @a protocol is 0 (SOCKET_PROTOCOL_DEFAULT) the default protocol type
  230. * for the family and type is used.
  231. *
  232. * The @a protocol is a family and type specific int that specifies what
  233. * kind of protocol to use. SocketProtocol lists several common ones.
  234. * Many families only support one protocol, and use 0 for this, others
  235. * support several and using 0 means to use the default protocol for
  236. * the family and type.
  237. *
  238. * The protocol id is passed directly to the operating
  239. * system, so you can use protocols not listed in SocketProtocol if you
  240. * know the protocol number used for it.
  241. *
  242. * @newin{2,22}
  243. * @param family The socket family to use, e.g. SOCKET_FAMILY_IPV4.
  244. * @param type The socket type to use.
  245. * @param protocol The id of the protocol to use, or 0 for default.
  246. * @return A Socket or <tt>0</tt> on error.
  247. * Free the returned object with Glib::object_unref().
  248. */
  249. /** @throw Glib::Error
  250. */
  251. static Glib::RefPtr<Socket>
  252. create(SocketFamily family, SocketType type, SocketProtocol protocol,
  253. const Glib::RefPtr<Cancellable>& cancellable = Glib::RefPtr<Cancellable>());
  254. // gmmproc thinks that this function should be wrapped in this class because
  255. // its only parameter is a GSocket. In fact, it is wrapped in the
  256. // SocketConnection class.
  257. /** Creates a new Socket from a native file descriptor
  258. * or winsock SOCKET handle.
  259. *
  260. * This reads all the settings from the file descriptor so that
  261. * all properties should work. Note that the file descriptor
  262. * will be set to non-blocking mode, independent on the blocking
  263. * mode of the Socket.
  264. *
  265. * @newin{2,22}
  266. * @param fd A native socket file descriptor.
  267. * @return A Socket or <tt>0</tt> on error.
  268. * Free the returned object with Glib::object_unref().
  269. */
  270. /** @throw Glib::Error
  271. */
  272. static Glib::RefPtr<Socket> create_from_fd(int fd, const Glib::RefPtr<Cancellable>&
  273. cancellable = Glib::RefPtr<Cancellable>());
  274. //TODO: Write custom documetation, mentioning, for instance, the exception, instead of a bool return.
  275. /** When a socket is created it is attached to an address family, but it
  276. * doesn't have an address in this family. g_socket_bind() assigns the
  277. * address (sometimes called name) of the socket.
  278. *
  279. * It is generally required to bind to a local address before you can
  280. * receive connections. (See g_socket_listen() and g_socket_accept() ).
  281. * In certain situations, you may also want to bind a socket that will be
  282. * used to initiate connections, though this is not normally required.
  283. *
  284. * @a allow_reuse should be <tt>true</tt> for server sockets (sockets that you will
  285. * eventually call g_socket_accept() on), and <tt>false</tt> for client sockets.
  286. * (Specifically, if it is <tt>true</tt>, then g_socket_bind() will set the
  287. * So::REUSEADDR flag on the socket, allowing it to bind @a address even if
  288. * that address was previously used by another socket that has not yet been
  289. * fully cleaned-up by the kernel. Failing to set this flag on a server
  290. * socket may cause the bind call to return IO_ERROR_ADDRESS_IN_USE if
  291. * the server program is stopped and then immediately restarted.)
  292. *
  293. * @newin{2,22}
  294. * @param address A SocketAddress specifying the local address.
  295. * @param allow_reuse Whether to allow reusing this address.
  296. * @return <tt>true</tt> on success, <tt>false</tt> on error.
  297. */
  298. void bind(const Glib::RefPtr<SocketAddress>& address, bool allow_reuse);
  299. /** Marks the socket as a server socket, i.e.\ a socket that is used
  300. * to accept incoming requests using g_socket_accept().
  301. *
  302. * Before calling this the socket must be bound to a local address using
  303. * g_socket_bind().
  304. *
  305. * To set the maximum amount of outstanding clients, use
  306. * g_socket_set_listen_backlog().
  307. *
  308. * @newin{2,22}
  309. * @return <tt>true</tt> on success, <tt>false</tt> on error.
  310. */
  311. void listen();
  312. /** Accept incoming connections on a connection-based socket. This removes
  313. * the first outstanding connection request from the listening socket and
  314. * creates a Socket object for it.
  315. *
  316. * The @a socket must be bound to a local address with g_socket_bind() and
  317. * must be listening for incoming connections (g_socket_listen()).
  318. *
  319. * If there are no outstanding connections then the operation will block
  320. * or return IO_ERROR_WOULD_BLOCK if non-blocking I/O is enabled.
  321. * To be notified of an incoming connection, wait for the IO_IN condition.
  322. *
  323. * @newin{2,22}
  324. * @param cancellable A Cancellable or <tt>0</tt>.
  325. * @return A new Socket, or <tt>0</tt> on error.
  326. * Free the returned object with Glib::object_unref().
  327. */
  328. Glib::RefPtr<Socket> accept(const Glib::RefPtr<Cancellable>& cancellable);
  329. Glib::RefPtr<Socket> accept();
  330. //TODO: Write custom documetation, mentioning, for instance, the exception, instead of a bool return.
  331. /** Connect the socket to the specified remote address.
  332. *
  333. * For connection oriented socket this generally means we attempt to make
  334. * a connection to the @a address. For a connection-less socket it sets
  335. * the default address for g_socket_send() and discards all incoming datagrams
  336. * from other sources.
  337. *
  338. * Generally connection oriented sockets can only connect once, but
  339. * connection-less sockets can connect multiple times to change the
  340. * default address.
  341. *
  342. * If the connect call needs to do network I/O it will block, unless
  343. * non-blocking I/O is enabled. Then IO_ERROR_PENDING is returned
  344. * and the user can be notified of the connection finishing by waiting
  345. * for the G_IO_OUT condition. The result of the connection must then be
  346. * checked with g_socket_check_connect_result().
  347. *
  348. * @newin{2,22}
  349. * @param address A SocketAddress specifying the remote address.
  350. * @param cancellable A Cancellable or <tt>0</tt>.
  351. * @return <tt>true</tt> if connected, <tt>false</tt> on error.
  352. */
  353. void connect(const Glib::RefPtr<SocketAddress>& address, const Glib::RefPtr<Cancellable>& cancellable);
  354. void connect(const Glib::RefPtr<SocketAddress>& address);
  355. // FIXME: it doesn't really seem like this is a proper use of exceptions...
  356. /** Checks and resets the pending connect error for the socket.
  357. * This is used to check for errors when g_socket_connect() is
  358. * used in non-blocking mode.
  359. *
  360. * @newin{2,22}
  361. * @return <tt>true</tt> if no error, <tt>false</tt> otherwise, setting @a error to the error.
  362. */
  363. void check_connect_result();
  364. // TODO: std::string overload?
  365. /** Receive data (up to @a size bytes) from a socket. This is mainly used by
  366. * connection-oriented sockets; it is identical to g_socket_receive_from()
  367. * with @a address set to <tt>0</tt>.
  368. *
  369. * For SOCKET_TYPE_DATAGRAM and SOCKET_TYPE_SEQPACKET sockets,
  370. * g_socket_receive() will always read either 0 or 1 complete messages from
  371. * the socket. If the received message is too large to fit in @a buffer, then
  372. * the data beyond @a size bytes will be discarded, without any explicit
  373. * indication that this has occurred.
  374. *
  375. * For SOCKET_TYPE_STREAM sockets, g_socket_receive() can return any
  376. * number of bytes, up to @a size. If more than @a size bytes have been
  377. * received, the additional data will be returned in future calls to
  378. * g_socket_receive().
  379. *
  380. * If the socket is in blocking mode the call will block until there
  381. * is some data to receive, the connection is closed, or there is an
  382. * error. If there is no data available and the socket is in
  383. * non-blocking mode, a IO_ERROR_WOULD_BLOCK error will be
  384. * returned. To be notified when data is available, wait for the
  385. * IO_IN condition.
  386. *
  387. * On error -1 is returned and @a error is set accordingly.
  388. *
  389. * @newin{2,22}
  390. * @param buffer A buffer to read data into (which should be at least @a size
  391. * bytes long).
  392. * @param size The number of bytes you want to read from the socket.
  393. * @param cancellable A Cancellable or <tt>0</tt>.
  394. * @return Number of bytes read, or 0 if the connection was closed by
  395. * the peer, or -1 on error.
  396. */
  397. gssize receive(char* buffer, gsize size, const Glib::RefPtr<Cancellable>& cancellable);
  398. gssize receive(char* buffer, gsize size);
  399. gssize receive_from(Glib::RefPtr<SocketAddress>& address, char* buffer, gsize size, const Glib::RefPtr<Cancellable>& cancellable);
  400. gssize receive_from(Glib::RefPtr<SocketAddress>& address, char* buffer, gsize size);
  401. // TODO: wrap g_socket_receive_message -- figure out this GInputVector thing
  402. // TODO: std::string overload?
  403. /** Tries to send @a size bytes from @a buffer on the socket. This is
  404. * mainly used by connection-oriented sockets; it is identical to
  405. * g_socket_send_to() with @a address set to <tt>0</tt>.
  406. *
  407. * If the socket is in blocking mode the call will block until there is
  408. * space for the data in the socket queue. If there is no space available
  409. * and the socket is in non-blocking mode a IO_ERROR_WOULD_BLOCK error
  410. * will be returned. To be notified when space is available, wait for the
  411. * IO_OUT condition. Note though that you may still receive
  412. * IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously
  413. * notified of a IO_OUT condition. (On Windows in particular, this is
  414. * very common due to the way the underlying APIs work.)
  415. *
  416. * On error -1 is returned and @a error is set accordingly.
  417. *
  418. * @newin{2,22}
  419. * @param buffer The buffer
  420. * containing the data to send.
  421. * @param size The number of bytes to send.
  422. * @param cancellable A Cancellable or <tt>0</tt>.
  423. * @return Number of bytes written (which may be less than @a size), or -1
  424. * on error.
  425. */
  426. gssize send(const gchar* buffer, gsize size, const Glib::RefPtr<Cancellable>& cancellable);
  427. gssize send(const gchar* buffer, gsize size);
  428. // TODO: std::string overload?
  429. /** Tries to send @a size bytes from @a buffer to @a address. If @a address is
  430. * <tt>0</tt> then the message is sent to the default receiver (set by
  431. * g_socket_connect()).
  432. *
  433. * See g_socket_send() for additional information.
  434. *
  435. * @newin{2,22}
  436. * @param address A SocketAddress, or <tt>0</tt>.
  437. * @param buffer The buffer
  438. * containing the data to send.
  439. * @param size The number of bytes to send.
  440. * @param cancellable A Cancellable or <tt>0</tt>.
  441. * @return Number of bytes written (which may be less than @a size), or -1
  442. * on error.
  443. */
  444. gssize send_to(const Glib::RefPtr<SocketAddress>& address, const char* buffer, gsize size, const Glib::RefPtr<Cancellable>& cancellable);
  445. gssize send_to(const Glib::RefPtr<SocketAddress>& address, const char* buffer, gsize size);
  446. // TODO: wrap g_socket_send_message -- figure out this GOutputVector thing
  447. /** Closes the socket, shutting down any active connection.
  448. *
  449. * Closing a socket does not wait for all outstanding I/O operations
  450. * to finish, so the caller should not rely on them to be guaranteed
  451. * to complete even if the close returns with no error.
  452. *
  453. * Once the socket is closed, all other operations will return
  454. * IO_ERROR_CLOSED. Closing a socket multiple times will not
  455. * return an error.
  456. *
  457. * Sockets will be automatically closed when the last reference
  458. * is dropped, but you might want to call this function to make sure
  459. * resources are released as early as possible.
  460. *
  461. * Beware that due to the way that TCP works, it is possible for
  462. * recently-sent data to be lost if either you close a socket while the
  463. * IO_IN condition is set, or else if the remote connection tries to
  464. * send something to you after you close the socket but before it has
  465. * finished reading all of the data you sent. There is no easy generic
  466. * way to avoid this problem; the easiest fix is to design the network
  467. * protocol such that the client will never send data "out of turn".
  468. * Another solution is for the server to half-close the connection by
  469. * calling g_socket_shutdown() with only the @a shutdown_write flag set,
  470. * and then wait for the client to notice this and close its side of the
  471. * connection, after which the server can safely call g_socket_close().
  472. * (This is what TcpConnection does if you call
  473. * g_tcp_connection_set_graceful_disconnect(). But of course, this
  474. * only works if the client will close its connection after the server
  475. * does.)
  476. *
  477. * @newin{2,22}
  478. * @return <tt>true</tt> on success, <tt>false</tt> on error.
  479. */
  480. void close();
  481. /** Checks whether a socket is closed.
  482. *
  483. * @newin{2,22}
  484. * @return <tt>true</tt> if socket is closed, <tt>false</tt> otherwise.
  485. */
  486. bool is_closed();
  487. /** Shut down part of a full-duplex connection.
  488. *
  489. * If @a shutdown_read is <tt>true</tt> then the receiving side of the connection
  490. * is shut down, and further reading is disallowed.
  491. *
  492. * If @a shutdown_write is <tt>true</tt> then the sending side of the connection
  493. * is shut down, and further writing is disallowed.
  494. *
  495. * It is allowed for both @a shutdown_read and @a shutdown_write to be <tt>true</tt>.
  496. *
  497. * One example where this is used is graceful disconnect for TCP connections
  498. * where you close the sending side, then wait for the other side to close
  499. * the connection, thus ensuring that the other side saw all sent data.
  500. *
  501. * @newin{2,22}
  502. * @param shutdown_read Whether to shut down the read side.
  503. * @param shutdown_write Whether to shut down the write side.
  504. * @return <tt>true</tt> on success, <tt>false</tt> on error.
  505. */
  506. void shutdown(bool shutdown_read, bool shutdown_write);
  507. /** Check whether the socket is connected. This is only useful for
  508. * connection-oriented sockets.
  509. *
  510. * @newin{2,22}
  511. * @return <tt>true</tt> if socket is connected, <tt>false</tt> otherwise.
  512. */
  513. bool is_connected();
  514. // TODO: non-cancellable version
  515. // This won't work because Glib::Source is abstract, and Glib::IOSource has no
  516. // constructor that takes a GSource*
  517. //#m4 __CONVERSION(`GSource*',`Glib::RefPtr<Glib::Source>',`Glib::RefPtr<Glib::Source>(new ::Glib::Source($3))')
  518. //_WRAP_METHOD(Glib::RefPtr<Glib::Source> create_source(Glib::IOCondition condition, const Glib::RefPtr<Cancellable>& cancellable), g_socket_create_source)
  519. /** Checks on the readiness of @a socket to perform operations.
  520. * The operations specified in @a condition are checked for and masked
  521. * against the currently-satisfied conditions on @a socket. The result
  522. * is returned.
  523. *
  524. * Note that on Windows, it is possible for an operation to return
  525. * IO_ERROR_WOULD_BLOCK even immediately after
  526. * g_socket_condition_check() has claimed that the socket is ready for
  527. * writing. Rather than calling g_socket_condition_check() and then
  528. * writing to the socket if it succeeds, it is generally better to
  529. * simply try writing to the socket right away, and try again later if
  530. * the initial attempt returns IO_ERROR_WOULD_BLOCK.
  531. *
  532. * It is meaningless to specify IO_ERR or IO_HUP in condition;
  533. * these conditions will always be set in the output if they are true.
  534. *
  535. * This call never blocks.
  536. *
  537. * @newin{2,22}
  538. * @param condition A IOCondition mask to check.
  539. * @return The @a GIOCondition mask of the current state.
  540. */
  541. Glib::IOCondition condition_check(Glib::IOCondition condition);
  542. /** Waits for @a condition to become true on @a socket. When the condition
  543. * is met, <tt>true</tt> is returned.
  544. *
  545. * If @a cancellable is cancelled before the condition is met, or if the
  546. * socket has a timeout set and it is reached before the condition is
  547. * met, then <tt>false</tt> is returned and @a error, if non-<tt>0</tt>, is set to
  548. * the appropriate value (IO_ERROR_CANCELLED or
  549. * IO_ERROR_TIMED_OUT).
  550. *
  551. * See also g_socket_condition_timed_wait().
  552. *
  553. * @newin{2,22}
  554. * @param condition A IOCondition mask to wait for.
  555. * @param cancellable A Cancellable, or <tt>0</tt>.
  556. * @return <tt>true</tt> if the condition was met, <tt>false</tt> otherwise.
  557. */
  558. void condition_wait(Glib::IOCondition condition, const Glib::RefPtr<Cancellable>& cancellable);
  559. void condition_wait(Glib::IOCondition condition);
  560. /** Sets the maximum number of outstanding connections allowed
  561. * when listening on this socket. If more clients than this are
  562. * connecting to the socket and the application is not handling them
  563. * on time then the new connections will be refused.
  564. *
  565. * Note that this must be called before g_socket_listen() and has no
  566. * effect if called after that.
  567. *
  568. * @newin{2,22}
  569. * @param backlog The maximum number of pending connections.
  570. */
  571. void set_listen_backlog(int backlog);
  572. /** Gets the listen backlog setting of the socket. For details on this,
  573. * see g_socket_set_listen_backlog().
  574. *
  575. * @newin{2,22}
  576. * @return The maximum number of pending connections.
  577. */
  578. int get_listen_backlog() const;
  579. /** Sets the blocking mode of the socket. In blocking mode
  580. * all operations block until they succeed or there is an error. In
  581. * non-blocking mode all functions return results immediately or
  582. * with a IO_ERROR_WOULD_BLOCK error.
  583. *
  584. * All sockets are created in blocking mode. However, note that the
  585. * platform level socket is always non-blocking, and blocking mode
  586. * is a GSocket level feature.
  587. *
  588. * @newin{2,22}
  589. * @param blocking Whether to use blocking I/O or not.
  590. */
  591. void set_blocking(bool blocking);
  592. /** Gets the blocking mode of the socket. For details on blocking I/O,
  593. * see g_socket_set_blocking().
  594. *
  595. * @newin{2,22}
  596. * @return <tt>true</tt> if blocking I/O is used, <tt>false</tt> otherwise.
  597. */
  598. bool get_blocking() const;
  599. /** Sets or unsets the So::KEEPALIVE flag on the underlying socket. When
  600. * this flag is set on a socket, the system will attempt to verify that the
  601. * remote socket endpoint is still present if a sufficiently long period of
  602. * time passes with no data being exchanged. If the system is unable to
  603. * verify the presence of the remote endpoint, it will automatically close
  604. * the connection.
  605. *
  606. * This option is only functional on certain kinds of sockets. (Notably,
  607. * SOCKET_PROTOCOL_TCP sockets.)
  608. *
  609. * The exact time between pings is system- and protocol-dependent, but will
  610. * normally be at least two hours. Most commonly, you would set this flag
  611. * on a server socket if you want to allow clients to remain idle for long
  612. * periods of time, but also want to ensure that connections are eventually
  613. * garbage-collected if clients crash or become unreachable.
  614. *
  615. * @newin{2,22}
  616. * @param keepalive Value for the keepalive flag.
  617. */
  618. void set_keepalive(bool keepalive);
  619. /** Gets the keepalive mode of the socket. For details on this,
  620. * see g_socket_set_keepalive().
  621. *
  622. * @newin{2,22}
  623. * @return <tt>true</tt> if keepalive is active, <tt>false</tt> otherwise.
  624. */
  625. bool get_keepalive() const;
  626. /** Gets the socket family of the socket.
  627. *
  628. * @newin{2,22}
  629. * @return A SocketFamily.
  630. */
  631. SocketFamily get_family() const;
  632. /** Returns the underlying OS socket object. On unix this
  633. * is a socket file descriptor, and on windows this is
  634. * a Winsock2 SOCKET handle. This may be useful for
  635. * doing platform specific or otherwise unusual operations
  636. * on the socket.
  637. *
  638. * @newin{2,22}
  639. * @return The file descriptor of the socket.
  640. */
  641. int get_fd() const;
  642. /** Try to get the local address of a bound socket. This is only
  643. * useful if the socket has been bound to a local address,
  644. * either explicitly or implicitly when connecting.
  645. *
  646. * @newin{2,22}
  647. * @return A SocketAddress or <tt>0</tt> on error.
  648. * Free the returned object with Glib::object_unref().
  649. */
  650. Glib::RefPtr<SocketAddress> get_local_address() const;
  651. /** Try to get the remove address of a connected socket. This is only
  652. * useful for connection oriented sockets that have been connected.
  653. *
  654. * @newin{2,22}
  655. * @return A SocketAddress or <tt>0</tt> on error.
  656. * Free the returned object with Glib::object_unref().
  657. */
  658. Glib::RefPtr<SocketAddress> get_remote_address() const;
  659. /** Gets the socket protocol id the socket was created with.
  660. * In case the protocol is unknown, -1 is returned.
  661. *
  662. * @newin{2,22}
  663. * @return A protocol id, or -1 if unknown.
  664. */
  665. SocketProtocol get_protocol() const;
  666. /** Gets the socket type of the socket.
  667. *
  668. * @newin{2,22}
  669. * @return A SocketType.
  670. */
  671. SocketType get_socket_type() const;
  672. /** Checks if a socket is capable of speaking IPv4.
  673. *
  674. * IPv4 sockets are capable of speaking IPv4. On some operating systems
  675. * and under some combinations of circumstances IPv6 sockets are also
  676. * capable of speaking IPv4. See RFC 3493 section 3.7 for more
  677. * information.
  678. *
  679. * No other types of sockets are currently considered as being capable
  680. * of speaking IPv4.
  681. *
  682. * @newin{2,22}
  683. * @return <tt>true</tt> if this socket can be used with IPv4.
  684. */
  685. bool speaks_ipv4() const;
  686. /** Returns the credentials of the foreign process connected to this
  687. * socket, if any (e.g.\ it is only supported for SOCKET_FAMILY_UNIX
  688. * sockets).
  689. *
  690. * If this operation isn't supported on the OS, the method fails with
  691. * the IO_ERROR_NOT_SUPPORTED error. On Linux this is implemented
  692. * by reading the So::PEERCRED option on the underlying socket.
  693. *
  694. * Other ways to obtain credentials from a foreign peer includes the
  695. * UnixCredentialsMessage type and
  696. * g_unix_connection_send_credentials() /
  697. * g_unix_connection_receive_credentials() functions.
  698. *
  699. * @newin{2,26}
  700. * @return <tt>0</tt> if @a error is set, otherwise a Credentials object
  701. * that must be freed with Glib::object_unref().
  702. */
  703. Glib::RefPtr<Credentials> get_credentials();
  704. /** Returns the credentials of the foreign process connected to this
  705. * socket, if any (e.g.\ it is only supported for SOCKET_FAMILY_UNIX
  706. * sockets).
  707. *
  708. * If this operation isn't supported on the OS, the method fails with
  709. * the IO_ERROR_NOT_SUPPORTED error. On Linux this is implemented
  710. * by reading the So::PEERCRED option on the underlying socket.
  711. *
  712. * Other ways to obtain credentials from a foreign peer includes the
  713. * UnixCredentialsMessage type and
  714. * g_unix_connection_send_credentials() /
  715. * g_unix_connection_receive_credentials() functions.
  716. *
  717. * @newin{2,26}
  718. * @return <tt>0</tt> if @a error is set, otherwise a Credentials object
  719. * that must be freed with Glib::object_unref().
  720. */
  721. Glib::RefPtr<const Credentials> get_credentials() const;
  722. /** Gets the timeout setting of the socket. For details on this, see
  723. * g_socket_set_timeout().
  724. *
  725. * @newin{2,26}
  726. * @return The timeout in seconds.
  727. */
  728. guint get_timeout() const;
  729. /** Sets the time in seconds after which I/O operations on @a socket will
  730. * time out if they have not yet completed.
  731. *
  732. * On a blocking socket, this means that any blocking Socket
  733. * operation will time out after @a timeout seconds of inactivity,
  734. * returning IO_ERROR_TIMED_OUT.
  735. *
  736. * On a non-blocking socket, calls to g_socket_condition_wait() will
  737. * also fail with IO_ERROR_TIMED_OUT after the given time. Sources
  738. * created with g_socket_create_source() will trigger after
  739. * @a timeout seconds of inactivity, with the requested condition
  740. * set, at which point calling g_socket_receive(), g_socket_send(),
  741. * g_socket_check_connect_result(), etc, will fail with
  742. * IO_ERROR_TIMED_OUT.
  743. *
  744. * If @a timeout is 0 (the default), operations will never time out
  745. * on their own.
  746. *
  747. * Note that if an I/O operation is interrupted by a signal, this may
  748. * cause the timeout to be reset.
  749. *
  750. * @newin{2,26}
  751. * @param timeout The timeout for @a socket, in seconds, or 0 for none.
  752. */
  753. void set_timeout(guint timeout);
  754. /** This behaves exactly the same as g_socket_receive(), except that
  755. * the choice of blocking or non-blocking behavior is determined by
  756. * the @a blocking argument rather than by @a socket's properties.
  757. *
  758. * @newin{2,26}
  759. * @param buffer A buffer to read data into (which should be at least @a size
  760. * bytes long).
  761. * @param size The number of bytes you want to read from the socket.
  762. * @param blocking Whether to do blocking or non-blocking I/O.
  763. * @param cancellable A Cancellable or <tt>0</tt>.
  764. * @return Number of bytes read, or 0 if the connection was closed by
  765. * the peer, or -1 on error.
  766. */
  767. gssize receive_with_blocking(gchar* buffer, gsize size, bool blocking, const Glib::RefPtr<Cancellable>& cancellable = Glib::RefPtr<Cancellable>());
  768. /** This behaves exactly the same as g_socket_send(), except that
  769. * the choice of blocking or non-blocking behavior is determined by
  770. * the @a blocking argument rather than by @a socket's properties.
  771. *
  772. * @newin{2,26}
  773. * @param buffer The buffer
  774. * containing the data to send.
  775. * @param size The number of bytes to send.
  776. * @param blocking Whether to do blocking or non-blocking I/O.
  777. * @param cancellable A Cancellable or <tt>0</tt>.
  778. * @return Number of bytes written (which may be less than @a size), or -1
  779. * on error.
  780. */
  781. gssize send_with_blocking(gchar* buffer, gsize size, bool blocking, const Glib::RefPtr<Cancellable>& cancellable = Glib::RefPtr<Cancellable>());
  782. #ifdef GLIBMM_PROPERTIES_ENABLED
  783. /** Whether or not I/O on this socket is blocking.
  784. *
  785. * You rarely need to use properties because there are get_ and set_ methods for almost all of them.
  786. * @return A PropertyProxy that allows you to get or set the property of the value, or receive notification when
  787. * the value of the property changes.
  788. */
  789. Glib::PropertyProxy< bool > property_blocking() ;
  790. #endif //#GLIBMM_PROPERTIES_ENABLED
  791. #ifdef GLIBMM_PROPERTIES_ENABLED
  792. /** Whether or not I/O on this socket is blocking.
  793. *
  794. * You rarely need to use properties because there are get_ and set_ methods for almost all of them.
  795. * @return A PropertyProxy that allows you to get or set the property of the value, or receive notification when
  796. * the value of the property changes.
  797. */
  798. Glib::PropertyProxy_ReadOnly< bool > property_blocking() const;
  799. #endif //#GLIBMM_PROPERTIES_ENABLED
  800. #ifdef GLIBMM_PROPERTIES_ENABLED
  801. /** The sockets address family.
  802. *
  803. * You rarely need to use properties because there are get_ and set_ methods for almost all of them.
  804. * @return A PropertyProxy that allows you to get or set the property of the value, or receive notification when
  805. * the value of the property changes.
  806. */
  807. Glib::PropertyProxy_ReadOnly< SocketFamily > property_family() const;
  808. #endif //#GLIBMM_PROPERTIES_ENABLED
  809. #ifdef GLIBMM_PROPERTIES_ENABLED
  810. /** The sockets file descriptor.
  811. *
  812. * You rarely need to use properties because there are get_ and set_ methods for almost all of them.
  813. * @return A PropertyProxy that allows you to get or set the property of the value, or receive notification when
  814. * the value of the property changes.
  815. */
  816. Glib::PropertyProxy_ReadOnly< int > property_fd() const;
  817. #endif //#GLIBMM_PROPERTIES_ENABLED
  818. #ifdef GLIBMM_PROPERTIES_ENABLED
  819. /** Keep connection alive by sending periodic pings.
  820. *
  821. * You rarely need to use properties because there are get_ and set_ methods for almost all of them.
  822. * @return A PropertyProxy that allows you to get or set the property of the value, or receive notification when
  823. * the value of the property changes.
  824. */
  825. Glib::PropertyProxy< bool > property_keepalive() ;
  826. #endif //#GLIBMM_PROPERTIES_ENABLED
  827. #ifdef GLIBMM_PROPERTIES_ENABLED
  828. /** Keep connection alive by sending periodic pings.
  829. *
  830. * You rarely need to use properties because there are get_ and set_ methods for almost all of them.
  831. * @return A PropertyProxy that allows you to get or set the property of the value, or receive notification when
  832. * the value of the property changes.
  833. */
  834. Glib::PropertyProxy_ReadOnly< bool > property_keepalive() const;
  835. #endif //#GLIBMM_PROPERTIES_ENABLED
  836. #ifdef GLIBMM_PROPERTIES_ENABLED
  837. /** Outstanding connections in the listen queue.
  838. *
  839. * You rarely need to use properties because there are get_ and set_ methods for almost all of them.
  840. * @return A PropertyProxy that allows you to get or set the property of the value, or receive notification when
  841. * the value of the property changes.
  842. */
  843. Glib::PropertyProxy< int > property_listen_backlog() ;
  844. #endif //#GLIBMM_PROPERTIES_ENABLED
  845. #ifdef GLIBMM_PROPERTIES_ENABLED
  846. /** Outstanding connections in the listen queue.
  847. *
  848. * You rarely need to use properties because there are get_ and set_ methods for almost all of them.
  849. * @return A PropertyProxy that allows you to get or set the property of the value, or receive notification when
  850. * the value of the property changes.
  851. */
  852. Glib::PropertyProxy_ReadOnly< int > property_listen_backlog() const;
  853. #endif //#GLIBMM_PROPERTIES_ENABLED
  854. #ifdef GLIBMM_PROPERTIES_ENABLED
  855. /** The local address the socket is bound to.
  856. *
  857. * You rarely need to use properties because there are get_ and set_ methods for almost all of them.
  858. * @return A PropertyProxy that allows you to get or set the property of the value, or receive notification when
  859. * the value of the property changes.
  860. */
  861. Glib::PropertyProxy_ReadOnly< Glib::RefPtr<SocketAddress> > property_local_address() const;
  862. #endif //#GLIBMM_PROPERTIES_ENABLED
  863. #ifdef GLIBMM_PROPERTIES_ENABLED
  864. /** The remote address the socket is connected to.
  865. *
  866. * You rarely need to use properties because there are get_ and set_ methods for almost all of them.
  867. * @return A PropertyProxy that allows you to get or set the property of the value, or receive notification when
  868. * the value of the property changes.
  869. */
  870. Glib::PropertyProxy_ReadOnly< Glib::RefPtr<SocketAddress> > property_remote_address() const;
  871. #endif //#GLIBMM_PROPERTIES_ENABLED
  872. #ifdef GLIBMM_PROPERTIES_ENABLED
  873. /** The timeout in seconds on socket I/O.
  874. *
  875. * You rarely need to use properties because there are get_ and set_ methods for almost all of them.
  876. * @return A PropertyProxy that allows you to get or set the property of the value, or receive notification when
  877. * the value of the property changes.
  878. */
  879. Glib::PropertyProxy< guint > property_timeout() ;
  880. #endif //#GLIBMM_PROPERTIES_ENABLED
  881. #ifdef GLIBMM_PROPERTIES_ENABLED
  882. /** The timeout in seconds on socket I/O.
  883. *
  884. * You rarely need to use properties because there are get_ and set_ methods for almost all of them.
  885. * @return A PropertyProxy that allows you to get or set the property of the value, or receive notification when
  886. * the value of the property changes.
  887. */
  888. Glib::PropertyProxy_ReadOnly< guint > property_timeout() const;
  889. #endif //#GLIBMM_PROPERTIES_ENABLED
  890. #ifdef GLIBMM_PROPERTIES_ENABLED
  891. /** The id of the protocol to use, or -1 for unknown.
  892. *
  893. * You rarely need to use properties because there are get_ and set_ methods for almost all of them.
  894. * @return A PropertyProxy that allows you to get or set the property of the value, or receive notification when
  895. * the value of the property changes.
  896. */
  897. Glib::PropertyProxy_ReadOnly< SocketProtocol > property_protocol() const;
  898. #endif //#GLIBMM_PROPERTIES_ENABLED
  899. #ifdef GLIBMM_PROPERTIES_ENABLED
  900. /** The sockets type.
  901. *
  902. * You rarely need to use properties because there are get_ and set_ methods for almost all of them.
  903. * @return A PropertyProxy that allows you to get or set the property of the value, or receive notification when
  904. * the value of the property changes.
  905. */
  906. Glib::PropertyProxy_ReadOnly< SocketType > property_type() const;
  907. #endif //#GLIBMM_PROPERTIES_ENABLED
  908. public:
  909. public:
  910. //C++ methods used to invoke GTK+ virtual functions:
  911. protected:
  912. //GTK+ Virtual Functions (override these to change behaviour):
  913. //Default Signal Handlers::
  914. };
  915. } // namespace Gio
  916. namespace Glib
  917. {
  918. /** A Glib::wrap() method for this object.
  919. *
  920. * @param object The C instance.
  921. * @param take_copy False if the result should take ownership of the C instance. True if it should take a new copy or ref.
  922. * @result A C++ instance that wraps this C instance.
  923. *
  924. * @relates Gio::Socket
  925. */
  926. Glib::RefPtr<Gio::Socket> wrap(GSocket* object, bool take_copy = false);
  927. }
  928. #endif /* _GIOMM_SOCKET_H */