1 /* GNet - Networking library 2 * Copyright (C) 2000 David Helder 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public 15 * License along with this library; if not, write to the 16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 * Boston, MA 02111-1307, USA. 18 */ 19 20 21 #ifndef _GNET_CONN_H 22 #define _GNET_CONN_H 23 24 #include <glib.h> 25 #include "tcp.h" 26 #include "iochannel.h" 27 28 G_BEGIN_DECLS 29 30 /** 31 * GConnEventType 32 * @GNET_CONN_ERROR: Connection error 33 * @GNET_CONN_CONNECT: Connection complete 34 * @GNET_CONN_CLOSE: Connection closed 35 * @GNET_CONN_TIMEOUT: Timeout 36 * @GNET_CONN_READ: Read complete 37 * @GNET_CONN_WRITE: Write complete 38 * @GNET_CONN_READABLE: Connection is readable 39 * @GNET_CONN_WRITABLE: Connection is writable 40 * 41 * Event type. Used by #GConnEvent. 42 * 43 **/ 44 typedef enum { 45 GNET_CONN_ERROR, 46 GNET_CONN_CONNECT, 47 GNET_CONN_CLOSE, 48 GNET_CONN_TIMEOUT, 49 GNET_CONN_READ, 50 GNET_CONN_WRITE, 51 GNET_CONN_READABLE, 52 GNET_CONN_WRITABLE 53 } GConnEventType; 54 55 56 /** 57 * GConnEvent 58 * @type: event type 59 * @buffer: buffer 60 * @length: buffer length 61 * 62 * GConn Event. @buffer and @length are set only on #GNET_CONN_READ 63 * events. The buffer is caller-owned. 64 * 65 **/ 66 typedef struct _GConnEvent GConnEvent; 67 struct _GConnEvent 68 { 69 GConnEventType type; 70 gchar* buffer; 71 gint length; 72 }; 73 74 75 76 /** 77 * GConn 78 * @hostname: host name 79 * @port: port 80 * @iochannel: IO channel 81 * @socket: socket 82 * @inetaddr: address 83 * @ref_count: [private] 84 * @ref_count_internal: [private] 85 * @connect_id: [private] 86 * @new_id: [private] 87 * @write_queue: [private] 88 * @bytes_written: [private] 89 * @buffer: [private] 90 * @length: [private] 91 * @bytes_read: [private] 92 * @read_eof: [private] 93 * @read_queue: [private] 94 * @process_buffer_timeout: [private] 95 * @watch_readable: [private] 96 * @watch_writable: [private] 97 * @watch_flags: [private] 98 * @watch: [private] 99 * @timer: [private] 100 * @func: [private] 101 * @user_data: [private] 102 * 103 * TCP Connection. Some of the fields are public, but do not set 104 * these fields. 105 * 106 **/ 107 typedef struct _GConn GConn; 108 109 110 /** 111 * GConnFunc 112 * @conn: #GConn 113 * @event: event (caller owned) 114 * @user_data: user data specified in gnet_conn_new() 115 * 116 * Callback for #GConn. 117 * 118 * Possible events: 119 * 120 * %GNET_CONN_ERROR: #GConn error. The event occurs if the connection 121 * fails somehow. The connection is closed before this event occurs. 122 * 123 * %GNET_CONN_CONNECT: Completion of gnet_conn_connect(). 124 * 125 * %GNET_CONN_CLOSE: Connection has been closed. The event does not 126 * occur as a result of calling gnet_conn_disconnect(), 127 * gnet_conn_unref(), or gnet_conn_delete(). 128 * 129 * %GNET_CONN_TIMEOUT: Timer set by gnet_conn_timeout() expires. 130 * 131 * %GNET_CONN_READ: Data has been read. This event occurs as a result 132 * of calling gnet_conn_read(), gnet_conn_readn(), or 133 * gnet_conn_readline(). buffer and length are set in the event 134 * object. The buffer is caller owned. 135 * 136 * %GNET_CONN_WRITE: Data has been written. This event occurs as a 137 * result of calling gnet_conn_write() or gnet_conn_write_direct(). 138 * 139 * %GNET_CONN_READABLE: The connection is readable. 140 * 141 * %GNET_CONN_WRITABLE: The connection is writable. 142 * 143 **/ 144 typedef void (*GConnFunc)(GConn* conn, GConnEvent* event, gpointer user_data); 145 146 147 struct _GConn 148 { 149 /* Public */ 150 gchar* hostname; 151 gint port; 152 153 GIOChannel* iochannel; 154 GTcpSocket* socket; 155 GInetAddr* inetaddr; 156 157 158 /* Private */ 159 guint ref_count; 160 guint ref_count_internal; 161 162 /* Connect */ 163 GTcpSocketConnectAsyncID connect_id; 164 GTcpSocketNewAsyncID new_id; 165 166 /* Write */ 167 GList* write_queue; 168 guint bytes_written; 169 170 /* Read */ 171 gchar* buffer; 172 guint length; 173 guint bytes_read; 174 gboolean read_eof; 175 GList* read_queue; 176 guint process_buffer_timeout; 177 178 /* Readable/writable */ 179 gboolean watch_readable; 180 gboolean watch_writable; 181 182 /* IO watch */ 183 guint watch_flags; 184 guint watch; 185 186 /* Timer */ 187 guint timer; 188 189 /* User data */ 190 GConnFunc func; 191 gpointer user_data; 192 193 GMainContext * context; 194 gint priority; 195 }; 196 197 198 199 /* ********** */ 200 201 GConn* gnet_conn_new (const gchar* hostname, gint port, 202 GConnFunc func, gpointer user_data); 203 GConn* gnet_conn_new_inetaddr (const GInetAddr* inetaddr, 204 GConnFunc func, gpointer user_data); 205 GConn* gnet_conn_new_socket (GTcpSocket* socket, 206 GConnFunc func, gpointer user_data); 207 208 void gnet_conn_delete (GConn* conn); 209 210 void gnet_conn_ref (GConn* conn); 211 void gnet_conn_unref (GConn* conn); 212 213 void gnet_conn_set_callback (GConn* conn, 214 GConnFunc func, gpointer user_data); 215 216 gboolean gnet_conn_set_main_context (GConn * conn, GMainContext * context); 217 218 /* ********** */ 219 220 void gnet_conn_connect (GConn* conn); 221 void gnet_conn_disconnect (GConn* conn); 222 223 gboolean gnet_conn_is_connected (const GConn* conn); 224 225 226 /* ********** */ 227 228 void gnet_conn_read (GConn* conn); 229 void gnet_conn_readn (GConn* conn, gint length); 230 void gnet_conn_readline (GConn* conn); 231 232 void gnet_conn_write (GConn* conn, gchar* buffer, gint length); 233 void gnet_conn_write_direct (GConn* conn, gchar* buffer, gint length, 234 GDestroyNotify buffer_destroy_cb); 235 236 void gnet_conn_set_watch_readable (GConn* conn, gboolean enable); 237 void gnet_conn_set_watch_writable (GConn* conn, gboolean enable); 238 void gnet_conn_set_watch_error (GConn* conn, gboolean enable); 239 240 void gnet_conn_timeout (GConn* conn, guint timeout); 241 242 G_END_DECLS 243 244 #endif /* _GNET_CONN_H */ 245