2004-08-25 Mikael Hallendal <micke@imendio.com>
authorhallski <hallski>
Tue, 24 Aug 2004 23:37:38 +0000
changeset 90 3f4b38614416
parent 89 e756a937e540
child 91 d51b8e4f43d9
2004-08-25 Mikael Hallendal <micke@imendio.com> * loudmouth/lm-connection.[ch]: (connection_send_keep_alive), (connection_start_keep_alive), (connection_stop_keep_alive): Added (lm_connection_set_keep_alive_rate): - Added support to have Loudmouth send keep alive packages, a single space, at regular intervals.
ChangeLog
loudmouth/lm-connection.c
loudmouth/lm-connection.h
--- a/ChangeLog	Sun Aug 08 23:07:23 2004 +0000
+++ b/ChangeLog	Tue Aug 24 23:37:38 2004 +0000
@@ -1,3 +1,13 @@
+2004-08-25  Mikael Hallendal  <micke@imendio.com>
+
+	* loudmouth/lm-connection.[ch]: 
+	(connection_send_keep_alive),
+	(connection_start_keep_alive), 
+	(connection_stop_keep_alive): Added
+	(lm_connection_set_keep_alive_rate): 
+	- Added support to have Loudmouth send keep alive packages, a single 
+	  space, at regular intervals.
+
 2004-08-09  Mikael Hallendal  <micke@imendio.com>
 
 	* examples/test-tunnel.c: Updated.
--- a/loudmouth/lm-connection.c	Sun Aug 08 23:07:23 2004 +0000
+++ b/loudmouth/lm-connection.c	Tue Aug 24 23:37:38 2004 +0000
@@ -93,6 +93,9 @@
 
 	LmConnectionState state;
 
+	guint         keep_alive_rate;
+	guint         keep_alive_id;
+
 	gint          ref_count;
 };
 
@@ -166,8 +169,9 @@
 						 GIOCondition   condition,
 						 GIOFunc        func,
 						 gpointer       user_data);
-
-
+static gboolean connection_send_keep_alive      (LmConnection  *connection);
+static void     connection_start_keep_alive     (LmConnection  *connection);
+static void     connection_stop_keep_alive      (LmConnection  *connection);
 
 static GSourceFuncs incoming_funcs = {
 	connection_incoming_prepare,
@@ -519,6 +523,38 @@
 	return id;
 }
 
+static gboolean
+connection_send_keep_alive (LmConnection *connection)
+{ 
+	if (!connection_send (connection, " ", -1, NULL)) {
+		lm_verbose ("Error while sending keep alive package");
+	}
+
+	return TRUE;
+}
+
+static void
+connection_start_keep_alive (LmConnection *connection)
+{
+	if (connection->keep_alive_id != 0) {
+		connection_stop_keep_alive (connection);
+	}
+
+	connection->keep_alive_id = g_timeout_add (connection->keep_alive_rate,
+						   (GSourceFunc) connection_send_keep_alive,
+						   connection);
+}
+
+static void
+connection_stop_keep_alive (LmConnection *connection)
+{
+	if (connection->keep_alive_id != 0) {
+		g_source_remove (connection->keep_alive_id);
+	}
+
+	connection->keep_alive_id = 0;
+}
+
 /* Returns directly */
 /* Setups all data needed to start the connection attempts */
 static gboolean
@@ -610,6 +646,8 @@
 static void
 connection_do_close (LmConnection *connection)
 {
+	connection_stop_keep_alive (connection);
+
 	if (connection->io_channel) {
 		g_source_remove (connection->io_watch_in);
 		g_source_remove (connection->io_watch_err);
@@ -625,7 +663,7 @@
 	if (!lm_connection_is_open (connection)) {
 		return;
 	}
-
+	
 	connection->state = LM_CONNECTION_STATE_DISCONNECTED;
 
 	if (connection->ssl) {
@@ -966,6 +1004,8 @@
 	/* Check to see if the stream is correctly set up */
 	result = TRUE;
 
+	connection_start_keep_alive (connection);
+
 	if (connection->open_cb && connection->open_cb->func) {
 		LmCallback *cb = connection->open_cb;
 		
@@ -1077,6 +1117,7 @@
 	connection->incoming_messages = g_queue_new ();
 	connection->cancel_open       = FALSE;
 	connection->state             = LM_CONNECTION_STATE_DISCONNECTED;
+	connection->keep_alive_id     = 0;
 	
 	connection->id_handlers = g_hash_table_new_full (g_str_hash, 
 							 g_str_equal,
@@ -1179,6 +1220,7 @@
 	}
 
 	if (lm_connection_is_open (connection)) {
+		connection_start_keep_alive (connection);
 		return TRUE;
 	}
 
@@ -1221,7 +1263,7 @@
 			     "Connection is not open, call lm_connection_open() first");
 		return FALSE;
 	}
-	
+
 	lm_verbose ("Disconnecting from: %s:%d\n", 
 		    connection->server, connection->port);
 	
@@ -1383,6 +1425,33 @@
 }
 
 /**
+ * lm_connection_set_keep_alive_rate:
+ * @connection: #LmConnection to check if it is open.
+ * @rate: Number of seconds between keep alive packages are sent.
+ * 
+ * Set the keep alive rate, in seconds. Set to 0 to prevent keep alive messages to be sent.
+ * A keep alive message is a single space character.
+ **/
+void
+lm_connection_set_keep_alive_rate (LmConnection *connection, guint rate)
+{
+	g_return_if_fail (connection != NULL);
+
+	connection_stop_keep_alive (connection);
+
+	if (rate == 0) {
+		connection->keep_alive_id = 0;
+		return;
+	}
+
+	connection->keep_alive_rate = rate * 1000;
+	
+	if (lm_connection_is_open (connection)) {
+		connection_start_keep_alive (connection);
+	}
+}
+
+/**
  * lm_connection_is_open:
  * @connection: #LmConnection to check if it is open.
  * 
--- a/loudmouth/lm-connection.h	Sun Aug 08 23:07:23 2004 +0000
+++ b/loudmouth/lm-connection.h	Tue Aug 24 23:37:38 2004 +0000
@@ -85,9 +85,7 @@
 gboolean      lm_connection_open_and_block    (LmConnection       *connection,
 					       GError            **error);
 
-void          lm_connection_cancel_open        (LmConnection      *connection);
-						
-
+void          lm_connection_cancel_open       (LmConnection      *connection);
 gboolean      lm_connection_close             (LmConnection       *connection,
 					       GError            **error);
 gboolean      lm_connection_authenticate      (LmConnection       *connection,
@@ -104,6 +102,9 @@
 					       const gchar        *password,
 					       const gchar        *resource,
 					       GError            **error);
+void        lm_connection_set_keep_alive_rate (LmConnection       *connection,
+					       guint               rate);
+
 gboolean      lm_connection_is_open           (LmConnection       *connection);
 gboolean      lm_connection_is_authenticated  (LmConnection       *connection);