2 * This file is part of the Nice GLib ICE library.
4 * (C) 2010 Collabora Ltd.
5 * Contact: Youness Alaoui
8 * The contents of this file are subject to the Mozilla Public License Version
9 * 1.1 (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 * http://www.mozilla.org/MPL/
13 * Software distributed under the License is distributed on an "AS IS" basis,
14 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
15 * for the specific language governing rights and limitations under the
18 * The Original Code is the Nice GLib ICE library.
20 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
21 * Corporation. All Rights Reserved.
24 * Youness Alaoui, Collabora Ltd.
26 * Alternatively, the contents of this file may be used under the terms of the
27 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
28 * case the provisions of LGPL are applicable instead of those above. If you
29 * wish to allow use of your version of this file only under the terms of the
30 * LGPL and not to allow others to use your version of this file under the
31 * MPL, indicate your decision by deleting the provisions above and replace
32 * them with the notice and other provisions required by the LGPL. If you do
33 * not delete the provisions above, a recipient may use your version of this
34 * file under either the MPL or the LGPL.
42 * @short_description: Pseudo TCP implementation
43 * @include: pseudotcp.h
46 * The #PseudoTcpSocket is an object implementing a Pseudo Tcp Socket for use
48 * The socket will implement a subset of the TCP stack to allow for a reliable
49 * transport over non-reliable sockets (such as UDP).
51 * See the file tests/test-pseudotcp.c in the source package for an example
52 * of how to use the object.
59 #include <glib-object.h>
66 * The #PseudoTcpSocket is the GObject implementing the Pseudo TCP Socket
70 typedef struct _PseudoTcpSocket PseudoTcpSocket
;
72 typedef struct _PseudoTcpSocketClass PseudoTcpSocketClass
;
74 GType
pseudo_tcp_socket_get_type (void);
77 #define PSEUDO_TCP_SOCKET_TYPE \
78 (pseudo_tcp_socket_get_type ())
79 #define PSEUDO_TCP_SOCKET(obj) \
80 (G_TYPE_CHECK_INSTANCE_CAST((obj), PSEUDO_TCP_SOCKET_TYPE, \
82 #define PSEUDO_TCP_SOCKET_CLASS(klass) \
83 (G_TYPE_CHECK_CLASS_CAST((klass), PSEUDO_TCP_SOCKET_TYPE, \
84 PseudoTcpSocketClass))
85 #define IS_PSEUDO_TCP_SOCKET(obj) \
86 (G_TYPE_CHECK_INSTANCE_TYPE((obj), PSEUDO_TCP_SOCKET_TYPE))
87 #define IS_PSEUDO_TCP_SOCKET_CLASS(klass) \
88 (G_TYPE_CHECK_CLASS_TYPE((klass), PSEUDO_TCP_SOCKET_TYPE))
89 #define PSEUDOTCP_SOCKET_GET_CLASS(obj) \
90 (G_TYPE_INSTANCE_GET_CLASS ((obj), PSEUDO_TCP_SOCKET_TYPE, \
91 PseudoTcpSocketClass))
93 struct _PseudoTcpSocketClass
{
94 GObjectClass parent_class
;
97 typedef struct _PseudoTcpSocketPrivate PseudoTcpSocketPrivate
;
99 struct _PseudoTcpSocket
{
101 PseudoTcpSocketPrivate
*priv
;
105 * PseudoTcpDebugLevel:
106 * @PSEUDO_TCP_DEBUG_NONE: Disable debug messages
107 * @PSEUDO_TCP_DEBUG_NORMAL: Enable basic debug messages
108 * @PSEUDO_TCP_DEBUG_VERBOSE: Enable verbose debug messages
110 * Valid values of debug levels to be set.
115 PSEUDO_TCP_DEBUG_NONE
= 0,
116 PSEUDO_TCP_DEBUG_NORMAL
,
117 PSEUDO_TCP_DEBUG_VERBOSE
,
118 } PseudoTcpDebugLevel
;
122 * @TCP_LISTEN: The socket's initial state. The socket isn't connected and is
123 * listening for an incoming connection
124 * @TCP_SYN_SENT: The socket has sent a connection request (SYN) packet and is
125 * waiting for an answer
126 * @TCP_SYN_RECEIVED: The socket has received a connection request (SYN) packet.
127 * @TCP_ESTABLISHED: The socket is connected
128 * @TCP_CLOSED: The socket has been closed
130 * An enum representing the state of the #PseudoTcpSocket.
131 * <para> See also: #PseudoTcpSocket:state </para>
144 * PseudoTcpWriteResult:
145 * @WR_SUCCESS: The write operation was successful
146 * @WR_TOO_LARGE: The socket type requires that message be sent atomically
147 * and the size of the message to be sent made this impossible.
148 * @WR_FAIL: There was an error sending the message
150 * An enum representing the result value of the write operation requested by
151 * the #PseudoTcpSocket.
152 * <para> See also: %PseudoTcpCallbacks:WritePacket </para>
160 } PseudoTcpWriteResult
;
163 * PseudoTcpCallbacks:
164 * @user_data: A user defined pointer to be passed to the callbacks
165 * @PseudoTcpOpened: The #PseudoTcpSocket is now connected
166 * @PseudoTcpReadable: The socket is readable
167 * @PseudoTcpWritable: The socket is writable
168 * @PseudoTcpClosed: The socket was closed
169 * @WritePacket: This callback is called when the socket needs to send data.
171 * A structure containing callbacks functions that will be called by the
172 * #PseudoTcpSocket when some events happen.
173 * <para> See also: #PseudoTcpWriteResult </para>
179 void (*PseudoTcpOpened
) (PseudoTcpSocket
*tcp
, gpointer data
);
180 void (*PseudoTcpReadable
) (PseudoTcpSocket
*tcp
, gpointer data
);
181 void (*PseudoTcpWritable
) (PseudoTcpSocket
*tcp
, gpointer data
);
182 void (*PseudoTcpClosed
) (PseudoTcpSocket
*tcp
, guint32 error
, gpointer data
);
183 PseudoTcpWriteResult (*WritePacket
) (PseudoTcpSocket
*tcp
,
184 const gchar
* buffer
, guint32 len
, gpointer data
);
185 } PseudoTcpCallbacks
;
188 * pseudo_tcp_socket_new:
189 * @conversation: The conversation id for the socket.
190 * @callbacks: A pointer to the #PseudoTcpCallbacks structure for getting
191 * notified of the #PseudoTcpSocket events.
193 * Creates a new #PseudoTcpSocket for the specified conversation
197 The @callbacks must be non-NULL, in order to get notified of packets the
198 socket needs to send.
201 If the @callbacks structure was dynamicly allocated, it can be freed
202 after the call @pseudo_tcp_socket_new
206 * Returns: The new #PseudoTcpSocket object, %NULL on error
210 PseudoTcpSocket
*pseudo_tcp_socket_new (guint32 conversation
,
211 PseudoTcpCallbacks
*callbacks
);
215 * pseudo_tcp_socket_connect:
216 * @self: The #PseudoTcpSocket object.
218 * Connects the #PseudoTcpSocket to the peer with the same conversation id.
219 * The connection will only be successful after the
220 * %PseudoTcpCallbacks:PseudoTcpOpened callback is called
222 * Returns: %TRUE on success, %FALSE on failure (not in %TCP_LISTEN state)
223 * <para> See also: pseudo_tcp_socket_get_error() </para>
227 gboolean
pseudo_tcp_socket_connect(PseudoTcpSocket
*self
);
231 * pseudo_tcp_socket_recv:
232 * @self: The #PseudoTcpSocket object.
233 * @buffer: The buffer to fill with received data
234 * @len: The length of @buffer
236 * Receive data from the socket.
240 Only call this on the %PseudoTcpCallbacks:PseudoTcpReadable callback.
243 This function should be called in a loop. If this function does not
244 return -1 with EWOULDBLOCK as the error, the
245 %PseudoTcpCallbacks:PseudoTcpReadable callback will not be called again.
249 * Returns: The number of bytes received or -1 in case of error
250 * <para> See also: pseudo_tcp_socket_get_error() </para>
254 gint
pseudo_tcp_socket_recv(PseudoTcpSocket
*self
, char * buffer
, size_t len
);
258 * pseudo_tcp_socket_send:
259 * @self: The #PseudoTcpSocket object.
260 * @buffer: The buffer with data to send
261 * @len: The length of @buffer
263 * Send data on the socket.
267 If this function return -1 with EWOULDBLOCK as the error, or if the return
268 value is lower than @len, then the %PseudoTcpCallbacks:PseudoTcpWritable
269 callback will be called when the socket will become writable.
273 * Returns: The number of bytes sent or -1 in case of error
274 * <para> See also: pseudo_tcp_socket_get_error() </para>
278 gint
pseudo_tcp_socket_send(PseudoTcpSocket
*self
, const char * buffer
,
283 * pseudo_tcp_socket_close:
284 * @self: The #PseudoTcpSocket object.
285 * @force: %TRUE to close the socket forcefully, %FALSE to close it gracefully
287 * Close the socket. IF @force is set to %FALSE, the socket will finish sending
288 * pending data before closing.
292 The %PseudoTcpCallbacks:PseudoTcpClosed callback will not be called once
293 the socket gets closed. It is only used for aborted connection.
294 Instead, the socket gets closed when the pseudo_tcp_socket_get_next_clock()
295 function returns FALSE.
299 * <para> See also: pseudo_tcp_socket_get_next_clock() </para>
303 void pseudo_tcp_socket_close(PseudoTcpSocket
*self
, gboolean force
);
307 * pseudo_tcp_socket_get_error:
308 * @self: The #PseudoTcpSocket object.
310 * Return the last encountered error.
314 The return value can be :
316 EINVAL (for pseudo_tcp_socket_connect()).
319 EWOULDBLOCK or ENOTCONN (for pseudo_tcp_socket_recv() and
320 pseudo_tcp_socket_send()).
325 * Returns: The error code
326 * <para> See also: pseudo_tcp_socket_connect() </para>
327 * <para> See also: pseudo_tcp_socket_recv() </para>
328 * <para> See also: pseudo_tcp_socket_send() </para>
332 int pseudo_tcp_socket_get_error(PseudoTcpSocket
*self
);
336 * pseudo_tcp_socket_get_next_clock:
337 * @self: The #PseudoTcpSocket object.
338 * @timeout: A pointer to be filled with the new timeout.
340 * Call this to determine the timeout needed before the next time call
341 * to pseudo_tcp_socket_notify_clock() should be made.
343 * Returns: %TRUE if @timeout was filled, %FALSE if the socket is closed and
344 * ready to be destroyed.
346 * <para> See also: pseudo_tcp_socket_notify_clock() </para>
350 gboolean
pseudo_tcp_socket_get_next_clock(PseudoTcpSocket
*self
, long *timeout
);
354 * pseudo_tcp_socket_notify_clock:
355 * @self: The #PseudoTcpSocket object.
357 * Start the processing of receiving data, pending data or syn/acks.
358 * Call this based on timeout value returned by
359 * pseudo_tcp_socket_get_next_clock().
360 * It's ok to call this too frequently.
362 * <para> See also: pseudo_tcp_socket_get_next_clock() </para>
366 void pseudo_tcp_socket_notify_clock(PseudoTcpSocket
*self
);
370 * pseudo_tcp_socket_notify_mtu:
371 * @self: The #PseudoTcpSocket object.
372 * @mtu: The new MTU of the socket
374 * Set the MTU of the socket
378 void pseudo_tcp_socket_notify_mtu(PseudoTcpSocket
*self
, guint16 mtu
);
382 * pseudo_tcp_socket_notify_packet:
383 * @self: The #PseudoTcpSocket object.
384 * @buffer: The buffer containing the received data
385 * @len: The length of @buffer
387 * Notify the #PseudoTcpSocket when a new packet arrives
389 * Returns: %TRUE if the packet was processed successfully, %FALSE otherwise
393 gboolean
pseudo_tcp_socket_notify_packet(PseudoTcpSocket
*self
,
394 const gchar
* buffer
, guint32 len
);
398 * pseudo_tcp_set_debug_level:
399 * @level: The level of debug to set
401 * Sets the debug level to enable/disable normal/verbose debug messages.
405 void pseudo_tcp_set_debug_level (PseudoTcpDebugLevel level
);
409 #endif /* _PSEUDOTCP_H */