purple-socket.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* purple
  2. *
  3. * Purple is the legal property of its developers, whose names are too numerous
  4. * to list here. Please refer to the COPYRIGHT file distributed with this
  5. * source distribution.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
  20. */
  21. #ifndef _PURPLE_SOCKET_H_
  22. #define _PURPLE_SOCKET_H_
  23. /**
  24. * SECTION:purple-socket
  25. * @section_id: libpurple-purple-socket
  26. * @short_description: <filename>purple-socket.h</filename>
  27. * @title: Generic Sockets
  28. */
  29. #include "connection.h"
  30. /**
  31. * PurpleSocket:
  32. *
  33. * A structure holding all resources needed for the TCP connection.
  34. */
  35. typedef struct _PurpleSocket PurpleSocket;
  36. /**
  37. * PurpleSocketConnectCb:
  38. * @ps: The socket.
  39. * @error: Error message, or NULL if connection was successful.
  40. * @user_data: The user data passed with callback function.
  41. *
  42. * A callback fired after (successfully or not) establishing a connection.
  43. */
  44. typedef void (*PurpleSocketConnectCb)(PurpleSocket *ps, const gchar *error,
  45. gpointer user_data);
  46. /**
  47. * purple_socket_new:
  48. * @gc: The connection for which the socket is needed, or NULL.
  49. *
  50. * Creates new, disconnected socket.
  51. *
  52. * Passing a PurpleConnection allows for proper proxy handling.
  53. *
  54. * Returns: The new socket struct.
  55. */
  56. PurpleSocket *
  57. purple_socket_new(PurpleConnection *gc);
  58. /**
  59. * purple_socket_get_connection:
  60. * @ps: The socket.
  61. *
  62. * Gets PurpleConnection tied with specified socket.
  63. *
  64. * Returns: The PurpleConnection object.
  65. */
  66. PurpleConnection *
  67. purple_socket_get_connection(PurpleSocket *ps);
  68. /**
  69. * purple_socket_set_tls:
  70. * @ps: The socket.
  71. * @is_tls: TRUE, if TLS should be handled transparently, FALSE otherwise.
  72. *
  73. * Determines, if socket should handle TLS.
  74. */
  75. void
  76. purple_socket_set_tls(PurpleSocket *ps, gboolean is_tls);
  77. /**
  78. * purple_socket_set_host:
  79. * @ps: The socket.
  80. * @host: The connection host.
  81. *
  82. * Sets connection host.
  83. */
  84. void
  85. purple_socket_set_host(PurpleSocket *ps, const gchar *host);
  86. /**
  87. * purple_socket_set_port:
  88. * @ps: The socket.
  89. * @port: The connection port.
  90. *
  91. * Sets connection port.
  92. */
  93. void
  94. purple_socket_set_port(PurpleSocket *ps, int port);
  95. /**
  96. * purple_socket_connect:
  97. * @ps: The socket.
  98. * @cb: The function to call after establishing a connection, or on
  99. * error.
  100. * @user_data: The user data to be passed to callback function.
  101. *
  102. * Establishes a connection.
  103. *
  104. * Returns: TRUE on success (this doesn't mean it's connected yet), FALSE
  105. * otherwise.
  106. */
  107. gboolean
  108. purple_socket_connect(PurpleSocket *ps, PurpleSocketConnectCb cb,
  109. gpointer user_data);
  110. /**
  111. * purple_socket_read:
  112. * @ps: The socket.
  113. * @buf: The buffer to write data to.
  114. * @len: The buffer size.
  115. *
  116. * Reads incoming data from socket.
  117. *
  118. * This function deals with TLS, if the socket is configured to do it.
  119. *
  120. * Returns: Amount of data written, or -1 on error (errno will be also be set).
  121. */
  122. gssize
  123. purple_socket_read(PurpleSocket *ps, guchar *buf, size_t len);
  124. /**
  125. * purple_socket_write:
  126. * @ps: The socket.
  127. * @buf: The buffer to read data from.
  128. * @len: The amount of data to read and send.
  129. *
  130. * Sends data through socket.
  131. *
  132. * This function deals with TLS, if the socket is configured to do it.
  133. *
  134. * Returns: Amount of data sent, or -1 on error (errno will albo be set).
  135. */
  136. gssize
  137. purple_socket_write(PurpleSocket *ps, const guchar *buf, size_t len);
  138. /**
  139. * purple_socket_watch:
  140. * @ps: The socket.
  141. * @cond: The condition type.
  142. * @func: The callback function for data, or NULL to remove any
  143. * existing callbacks.
  144. * @user_data: The user data to be passed to callback function.
  145. *
  146. * Adds an input handler for the socket.
  147. *
  148. * If the specified socket had input handler already registered, it will be
  149. * removed. To remove any input handlers, pass an NULL handler function.
  150. */
  151. void
  152. purple_socket_watch(PurpleSocket *ps, PurpleInputCondition cond,
  153. PurpleInputFunction func, gpointer user_data);
  154. /**
  155. * purple_socket_get_fd:
  156. * @ps: The socket
  157. *
  158. * Gets underlying file descriptor for socket.
  159. *
  160. * It's not meant to read/write data (use purple_socket_read/
  161. * purple_socket_write), rather for watching for changes with select().
  162. *
  163. * Returns: The file descriptor, or -1 on error.
  164. */
  165. int
  166. purple_socket_get_fd(PurpleSocket *ps);
  167. /**
  168. * purple_socket_set_data:
  169. * @ps: The socket.
  170. * @key: The unique key.
  171. * @data: The data to assign, or NULL to remove.
  172. *
  173. * Sets extra data for a socket.
  174. */
  175. void
  176. purple_socket_set_data(PurpleSocket *ps, const gchar *key, gpointer data);
  177. /**
  178. * purple_socket_get_data:
  179. * @ps: The socket.
  180. * @key: The unqiue key.
  181. *
  182. * Returns extra data in a socket.
  183. *
  184. * Returns: The data associated with the key.
  185. */
  186. gpointer
  187. purple_socket_get_data(PurpleSocket *ps, const gchar *key);
  188. /**
  189. * purple_socket_destroy:
  190. * @ps: The socket.
  191. *
  192. * Destroys the socket, closes connection and frees all resources.
  193. *
  194. * If file descriptor for the socket was extracted with purple_socket_get_fd and
  195. * added to event loop, it have to be removed prior this.
  196. */
  197. void
  198. purple_socket_destroy(PurpleSocket *ps);
  199. #endif /* _PURPLE_SOCKET_H_ */