1 /* GNet - Networking library 2 * Copyright (C) 2000-3 David Helder 3 * Copyright (C) 2000 Andrew Lanoix 4 * Copyright (C) 2007 Tim-Philipp Müller <tim at centricular dot net> 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Library General Public 8 * License as published by the Free Software Foundation; either 9 * version 2 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Library General Public License for more details. 15 * 16 * You should have received a copy of the GNU Library General Public 17 * License along with this library; if not, write to the 18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 19 * Boston, MA 02111-1307, USA. 20 */ 21 22 23 #ifndef _GNET_TCP_H 24 #define _GNET_TCP_H 25 26 #include "inetaddr.h" 27 28 #include <glib.h> 29 30 G_BEGIN_DECLS 31 32 /** 33 * GTcpSocket 34 * 35 * A #GTcpSocket structure represents a TCP socket. The 36 * implementation is hidden. 37 * 38 **/ 39 typedef struct _GTcpSocket GTcpSocket; 40 41 42 /* **************************************** */ 43 44 GTcpSocket* gnet_tcp_socket_connect (const gchar* hostname, gint port); 45 GTcpSocket* gnet_tcp_socket_new (const GInetAddr* addr); 46 GTcpSocket* gnet_tcp_socket_new_direct (const GInetAddr* addr); 47 48 void gnet_tcp_socket_delete (GTcpSocket* socket); 49 50 void gnet_tcp_socket_ref (GTcpSocket* socket); 51 void gnet_tcp_socket_unref (GTcpSocket* socket); 52 53 54 GIOChannel* gnet_tcp_socket_get_io_channel (GTcpSocket* socket); 55 GInetAddr* gnet_tcp_socket_get_remote_inetaddr (const GTcpSocket* socket); 56 GInetAddr* gnet_tcp_socket_get_local_inetaddr (const GTcpSocket* socket); 57 58 59 GTcpSocket* gnet_tcp_socket_server_new (void); 60 GTcpSocket* gnet_tcp_socket_server_new_with_port (gint port); 61 GTcpSocket* gnet_tcp_socket_server_new_full (const GInetAddr* iface, gint port); 62 63 GTcpSocket* gnet_tcp_socket_server_accept (GTcpSocket* socket); 64 GTcpSocket* gnet_tcp_socket_server_accept_nonblock (GTcpSocket* socket); 65 66 gint gnet_tcp_socket_get_port (const GTcpSocket* socket); 67 68 69 /* ********** */ 70 71 /** 72 * GNetTOS 73 * @GNET_TOS_NONE: Unspecified 74 * @GNET_TOS_LOWDELAY: Low delay 75 * @GNET_TOS_THROUGHPUT: High throughput 76 * @GNET_TOS_RELIABILITY: High reliability 77 * @GNET_TOS_LOWCOST: Low cost 78 * 79 * Type-of-service. 80 * 81 **/ 82 typedef enum 83 { 84 GNET_TOS_NONE, 85 GNET_TOS_LOWDELAY, 86 GNET_TOS_THROUGHPUT, 87 GNET_TOS_RELIABILITY, 88 GNET_TOS_LOWCOST 89 90 } GNetTOS; 91 92 void gnet_tcp_socket_set_tos (GTcpSocket* socket, GNetTOS tos); 93 94 95 96 /* **************************************** */ 97 /* Asynchronous functions */ 98 99 100 /** 101 * GTcpSocketConnectAsyncID 102 * 103 * ID of an asynchronous connection started with 104 * gnet_tcp_socket_connect_async(). The connection can be canceled 105 * by calling gnet_tcp_socket_connect_async_cancel() with the ID. 106 * 107 **/ 108 typedef struct _GTcpSocketConnectState * GTcpSocketConnectAsyncID; 109 110 111 112 /** 113 * GTcpSocketConnectAsyncStatus 114 * @GTCP_SOCKET_CONNECT_ASYNC_STATUS_OK: Connection succeeded 115 * @GTCP_SOCKET_CONNECT_ASYNC_STATUS_INETADDR_ERROR: Error, address lookup failed 116 * @GTCP_SOCKET_CONNECT_ASYNC_STATUS_TCP_ERROR: Error, could not connect 117 * 118 * Status for connecting via gnet_tcp_socket_connect_async(), passed 119 * by GTcpSocketConnectAsyncFunc. More errors may be added in the 120 * future, so it's best to compare against 121 * %GTCP_SOCKET_CONNECT_ASYNC_STATUS_OK. 122 * 123 **/ 124 typedef enum { 125 GTCP_SOCKET_CONNECT_ASYNC_STATUS_OK, 126 GTCP_SOCKET_CONNECT_ASYNC_STATUS_INETADDR_ERROR, 127 GTCP_SOCKET_CONNECT_ASYNC_STATUS_TCP_ERROR 128 } GTcpSocketConnectAsyncStatus; 129 130 131 132 /** 133 * GTcpSocketConnectAsyncFunc: 134 * @socket: TcpSocket that was connecting (callee owned) 135 * @status: Status of the connection 136 * @data: User data 137 * 138 * Callback for gnet_tcp_socket_connect_async(). 139 * 140 **/ 141 typedef void (*GTcpSocketConnectAsyncFunc) (GTcpSocket * socket, 142 GTcpSocketConnectAsyncStatus status, 143 gpointer data); 144 145 146 GTcpSocketConnectAsyncID gnet_tcp_socket_connect_async (const gchar * hostname, 147 gint port, 148 GTcpSocketConnectAsyncFunc func, 149 gpointer data); 150 151 GTcpSocketConnectAsyncID gnet_tcp_socket_connect_async_full (const gchar * hostname, 152 gint port, 153 GTcpSocketConnectAsyncFunc func, 154 gpointer data, 155 GDestroyNotify notify, 156 GMainContext * context, 157 gint priority); 158 159 void gnet_tcp_socket_connect_async_cancel (GTcpSocketConnectAsyncID id); 160 161 /* ********** */ 162 163 164 /** 165 * GTcpSocketNewAsyncID: 166 * 167 * ID of an asynchronous tcp socket creation started with 168 * gnet_tcp_socket_new_async(). The creation can be canceled by 169 * calling gnet_tcp_socket_new_async_cancel() with the ID. 170 * 171 **/ 172 typedef struct _GTcpSocketAsyncState * GTcpSocketNewAsyncID; 173 174 175 176 /** 177 * GTcpSocketNewAsyncFunc: 178 * @socket: Socket that was connecting 179 * @data: User data 180 * 181 * Callback for gnet_tcp_socket_new_async(). The socket will be 182 * NULL if the connection failed. 183 * 184 **/ 185 typedef void (*GTcpSocketNewAsyncFunc) (GTcpSocket * socket, gpointer data); 186 187 GTcpSocketNewAsyncID gnet_tcp_socket_new_async (const GInetAddr * addr, 188 GTcpSocketNewAsyncFunc func, 189 gpointer data); 190 191 GTcpSocketNewAsyncID gnet_tcp_socket_new_async_full (const GInetAddr * addr, 192 GTcpSocketNewAsyncFunc func, 193 gpointer data, 194 GDestroyNotify notify, 195 GMainContext * context, 196 gint priority); 197 198 void gnet_tcp_socket_new_async_cancel (GTcpSocketNewAsyncID id); 199 200 201 GTcpSocketNewAsyncID gnet_tcp_socket_new_async_direct (const GInetAddr * addr, 202 GTcpSocketNewAsyncFunc func, 203 gpointer data); 204 205 GTcpSocketNewAsyncID gnet_tcp_socket_new_async_direct_full (const GInetAddr * addr, 206 GTcpSocketNewAsyncFunc func, 207 gpointer data, 208 GDestroyNotify notify, 209 GMainContext * context, 210 gint priority); 211 212 213 /* ********** */ 214 215 216 /** 217 * GTcpSocketAcceptFunc: 218 * @server: Server socket 219 * @client: Client socket 220 * @data: User data 221 * 222 * Callback for gnet_tcp_socket_server_accept_async(). The socket 223 * had an irrecoverable error if client_socket is NULL. 224 * 225 **/ 226 typedef void (*GTcpSocketAcceptFunc)(GTcpSocket* server, 227 GTcpSocket* client, 228 gpointer data); 229 230 void gnet_tcp_socket_server_accept_async (GTcpSocket* socket, 231 GTcpSocketAcceptFunc accept_func, 232 gpointer user_data); 233 void gnet_tcp_socket_server_accept_async_cancel (GTcpSocket* socket); 234 235 236 G_END_DECLS 237 238 #endif /* _GNET_TCP_H */ 239