Refactoring to clear up interface between LmSocket and LmConnection.
authorMikael Hallendal <micke@imendio.com>
Mon, 26 Feb 2007 01:17:30 +0100
changeset 256 2266e56746ed
parent 255 704881ac7788
child 257 d2df6b962601
Refactoring to clear up interface between LmSocket and LmConnection. Continued the work to refactor out the socket code to LmSocket. Removed a couple of functions that were left in lm-internals to signal back from the socket to the connection. These are now callbacks given to LmSocket in the create function.
loudmouth/lm-connection.c
loudmouth/lm-internals.h
loudmouth/lm-socket.c
loudmouth/lm-socket.h
--- a/loudmouth/lm-connection.c	Sun Feb 25 22:48:39 2007 +0100
+++ b/loudmouth/lm-connection.c	Mon Feb 26 01:17:30 2007 +0100
@@ -115,6 +115,7 @@
 					      LmConnection         *connection);
 static gboolean connection_do_open           (LmConnection         *connection,
 					      GError              **error);
+void            connection_do_close          (LmConnection         *connection);
 
 
 static LmMessage *     connection_create_auth_req_msg (const gchar *username);
@@ -149,12 +150,25 @@
 						  GError         **error);
 static void      connection_message_queue_cb     (LmMessageQueue  *queue,
 						  LmConnection    *connection);
+static void      connection_signal_disconnect    (LmConnection       *connection,
+						  LmDisconnectReason  reason);
 static void      connection_incoming_data        (LmSocket        *socket, 
 						  const gchar     *buf,
 						  LmConnection    *connection);
+static void      connection_socket_closed_cb     (LmSocket        *socket,
+						  LmDisconnectReason reason,
+						  LmConnection       *connection);
+static void      connection_socket_connect_cb    (LmSocket           *socket,
+						  gboolean            result,
+						  LmConnection       *connection);
+
 static gboolean  connection_get_server_from_jid  (const gchar     *jid,
 						  gchar          **server);
 static void      connection_send_stream_header   (LmConnection    *connection);
+static LmHandlerResult connection_features_cb (LmMessageHandler *handler,
+					       LmConnection     *connection,
+					       LmMessage        *message,
+					       gpointer          user_data);
 
 static void
 connection_free (LmConnection *connection)
@@ -190,7 +204,7 @@
 
 	g_hash_table_destroy (connection->id_handlers);
 	if (connection->state >= LM_CONNECTION_STATE_OPENING) {
-		_lm_connection_do_close (connection);
+		connection_do_close (connection);
 	}
 
 	if (connection->open_cb) {
@@ -364,9 +378,9 @@
 	b_written = lm_socket_do_write (connection->socket, str, len);
 
 	if (b_written < 0) {
-		_lm_connection_error_event (connection->socket, 
-					    G_IO_HUP,
-					    connection);
+		connection_do_close (connection);
+		connection_signal_disconnect (connection, 
+					      LM_DISCONNECT_REASON_ERROR);
 		return FALSE;
 	}
 
@@ -438,6 +452,8 @@
 
 	connection->socket = lm_socket_create (connection->context,
 					       (IncomingDataFunc) connection_incoming_data,
+					       (SocketClosedFunc) connection_socket_closed_cb,
+					       (ConnectResultFunc) connection_socket_connect_cb,
 					       connection,
 					       connection,
 					       connection->blocking,
@@ -457,7 +473,7 @@
 }
 					
 void
-_lm_connection_do_close (LmConnection *connection)
+connection_do_close (LmConnection *connection)
 {
 	connection_stop_keep_alive (connection);
 
@@ -481,24 +497,6 @@
 	}
 }
 
-gboolean
-_lm_connection_error_event (LmSocket     *socket,
-			GIOCondition  condition,
-			LmConnection *connection)
-{
-	lm_verbose ("Error event: %d->'%s'\n", 
-		    condition, lm_misc_io_condition_to_str (condition));
-
-	if (!connection->socket) {
-		return FALSE;
-	}
-
-	_lm_connection_do_close (connection);
-	_lm_connection_signal_disconnect (connection, LM_DISCONNECT_REASON_ERROR);
-	
-	return TRUE;
-}
-
 typedef struct {
 	gchar        *username;
 	gchar        *password;
@@ -730,8 +728,8 @@
 	return b->priority - a->priority;
 }
 
-void
-_lm_connection_signal_disconnect (LmConnection       *connection,
+static void
+connection_signal_disconnect (LmConnection       *connection,
 			      LmDisconnectReason  reason)
 {
 	if (connection->disconnect_cb && connection->disconnect_cb->func) {
@@ -751,6 +749,50 @@
 	lm_parser_parse (connection->parser, buf);
 }
 
+static void
+connection_socket_closed_cb (LmSocket        *socket,
+			     LmDisconnectReason reason,
+			     LmConnection       *connection)
+{
+	connection_do_close (connection);
+	connection_signal_disconnect (connection, reason);
+}
+
+static void
+connection_socket_connect_cb (LmSocket           *socket,
+			      gboolean            result,
+			      LmConnection       *connection)
+{
+	if (!result) {
+		connection_do_close (connection);
+
+		if (connection->open_cb) {
+			LmCallback *cb = connection->open_cb;
+
+			connection->open_cb = NULL;
+			
+			(* ((LmResultFunction) cb->func)) (connection, FALSE,
+							   cb->user_data);
+			_lm_utils_free_callback (cb);
+		}
+	
+		return;
+	}
+	
+	/* FIXME: Set up according to XMPP 1.0 specification */
+	/*        StartTLS and the like */
+	if (!connection_send (connection, 
+			      "<?xml version='1.0' encoding='UTF-8'?>", -1,
+			      NULL)) {
+		lm_verbose ("Failed to send xml version and encoding\n");
+		connection_do_close (connection);
+
+		return;
+	}
+
+	connection_send_stream_header (connection);
+}
+
 static gboolean
 connection_get_server_from_jid (const gchar     *jid,
 				gchar          **server)
@@ -796,45 +838,12 @@
 
 	if (!lm_connection_send (connection, m, NULL)) {
 		lm_verbose ("Failed to send stream information\n");
-		_lm_connection_do_close (connection);
+		connection_do_close (connection);
 	}
 		
 	lm_message_unref (m);
 }
 
-void 
-_lm_connection_socket_result (LmConnection *connection, gboolean result)
-{
-	if (!result) {
-		_lm_connection_do_close (connection);
-
-		if (connection->open_cb) {
-			LmCallback *cb = connection->open_cb;
-
-			connection->open_cb = NULL;
-			
-			(* ((LmResultFunction) cb->func)) (connection, FALSE,
-							   cb->user_data);
-			_lm_utils_free_callback (cb);
-		}
-	
-		return;
-	}
-	
-	/* FIXME: Set up according to XMPP 1.0 specification */
-	/*        StartTLS and the like */
-	if (!connection_send (connection, 
-			      "<?xml version='1.0' encoding='UTF-8'?>", -1,
-			      NULL)) {
-		lm_verbose ("Failed to send xml version and encoding\n");
-		_lm_connection_do_close (connection);
-
-		return;
-	}
-
-	connection_send_stream_header (connection);
-}
-
 gboolean 
 _lm_connection_async_connect_waiting (LmConnection *connection)
 {
@@ -904,7 +913,7 @@
 	result = lm_connection_send (connection, m, NULL);
 	lm_message_unref (m);
 	if (result < 0) {
-		_lm_connection_do_close (connection);
+		connection_do_close (connection);
 	}
 
 	/* We may finally tell the client they're authorized */
@@ -914,7 +923,7 @@
 }
 
 static LmHandlerResult
-_lm_connection_features_cb (LmMessageHandler *handler,
+connection_features_cb (LmMessageHandler *handler,
 			    LmConnection     *connection,
 			    LmMessage        *message,
 			    gpointer          user_data)
@@ -955,7 +964,7 @@
 
 		if (result < 0) {
 			g_debug ("%s: can't send resource binding request", G_STRFUNC);
-			_lm_connection_do_close (connection);
+			connection_do_close (connection);
 		}
 	}
 
@@ -1180,8 +1189,8 @@
 		lm_socket_flush (connection->socket);
 	}
 	
-	_lm_connection_do_close (connection);
-	_lm_connection_signal_disconnect (connection, LM_DISCONNECT_REASON_OK);
+	connection_do_close (connection);
+	connection_signal_disconnect (connection, LM_DISCONNECT_REASON_OK);
 	
 	return no_errors;
 }
@@ -1259,7 +1268,7 @@
 		connection->resource = g_strdup (resource);
 
 		connection->features_cb  =
-			lm_message_handler_new (_lm_connection_features_cb,
+			lm_message_handler_new (connection_features_cb,
 				NULL, NULL);
 		lm_connection_register_message_handler (connection,
 			connection->features_cb,
--- a/loudmouth/lm-internals.h	Sun Feb 25 22:48:39 2007 +0100
+++ b/loudmouth/lm-internals.h	Mon Feb 26 01:17:30 2007 +0100
@@ -60,15 +60,6 @@
                                                    int error);
 gboolean         _lm_socket_failed            (LmConnectData *connect_data);
 void             _lm_socket_succeeded         (LmConnectData *connect_data);
-gboolean         _lm_connection_error_event (LmSocket     *socket,
-					     GIOCondition  condition,
-					     LmConnection *connection);
-void     _lm_connection_do_close           (LmConnection        *connection);
-void     _lm_connection_signal_disconnect (LmConnection       *connection,
-					   LmDisconnectReason  reason);
-
-void     _lm_connection_socket_result (LmConnection *connection,
-				       gboolean      result);
 gboolean _lm_connection_async_connect_waiting (LmConnection *connection);
 void _lm_connection_set_async_connect_waiting (LmConnection *connection,
 					       gboolean      waiting);
--- a/loudmouth/lm-socket.c	Sun Feb 25 22:48:39 2007 +0100
+++ b/loudmouth/lm-socket.c	Mon Feb 26 01:17:30 2007 +0100
@@ -71,7 +71,9 @@
 
 	LmConnectData *connect_data;
 
-	IncomingDataFunc func;
+	IncomingDataFunc data_func;
+	SocketClosedFunc closed_func;
+	ConnectResultFunc connect_func;
 	gpointer         user_data;
 
 	guint          ref_count;
@@ -88,6 +90,9 @@
 static gboolean     socket_hup_event          (GIOChannel     *source,
 					       GIOCondition    condition,
 					       LmSocket       *socket);
+static gboolean     socket_error_event        (GIOChannel     *source,
+					       GIOCondition    condition,
+					       LmSocket       *socket);
 static gboolean     socket_buffered_write_cb  (GIOChannel     *source, 
 					       GIOCondition    condition,
 					       LmSocket       *socket);
@@ -183,8 +188,7 @@
 			reason = LM_DISCONNECT_REASON_UNKNOWN;
 		}
 
-		_lm_connection_do_close (socket->connection);
-		_lm_connection_signal_disconnect (socket->connection, reason);
+		(socket->closed_func) (socket, reason, socket->user_data);
 
 		/* Notify connection_in_event that we hangup the connection */
 		*hangup = TRUE;
@@ -224,7 +228,7 @@
 		
 		lm_verbose ("Read: %d chars\n", (int)bytes_read);
 
-		(socket->func) (socket, buf, socket->user_data);
+		(socket->data_func) (socket, buf, socket->user_data);
 	}
 
 	if (hangup) {
@@ -246,9 +250,26 @@
 		return FALSE;
 	}
 
-	_lm_connection_do_close (socket->connection);
-	_lm_connection_signal_disconnect (socket->connection, 
-					  LM_DISCONNECT_REASON_HUP);
+	(socket->closed_func) (socket, LM_DISCONNECT_REASON_HUP, 
+			       socket->user_data);
+	
+	return TRUE;
+}
+
+static gboolean
+socket_error_event (GIOChannel   *source,
+		    GIOCondition  condition,
+		    LmSocket     *socket)
+{
+	lm_verbose ("ERROR event: %d->'%s'\n", 
+		    condition, lm_misc_io_condition_to_str (condition));
+
+	if (!socket->io_channel) {
+		return FALSE;
+	}
+
+	(socket->closed_func) (socket, LM_DISCONNECT_REASON_ERROR, 
+			       socket->user_data);
 	
 	return TRUE;
 }
@@ -268,7 +289,7 @@
 	/* Need some way to report error/success */
 	if (socket->cancel_open) {
 		lm_verbose ("Cancelling connection...\n");
-		_lm_connection_socket_result (socket->connection, FALSE);
+		(socket->connect_func) (socket, FALSE, socket->user_data);
 		return;
 	}
 	
@@ -303,9 +324,8 @@
  			_lm_sock_shutdown (socket->fd);
  			_lm_sock_close (socket->fd);
 
-			_lm_connection_do_close (socket->connection);
-
-			_lm_connection_socket_result (socket->connection, FALSE);
+			(socket->connect_func) (socket, FALSE, 
+						socket->user_data);
 			return;
 		}
 	
@@ -330,7 +350,7 @@
 		lm_misc_add_io_watch (socket->context,
 				      socket->io_channel,
 				      G_IO_ERR,
-				      (GIOFunc) _lm_connection_error_event,
+				      (GIOFunc) socket_error_event,
 				      socket);
 		
 	socket->watch_hup =
@@ -341,7 +361,7 @@
 				      socket);
 #endif
 
-	_lm_connection_socket_result (socket->connection, TRUE);
+	(socket->connect_func) (socket, TRUE, socket->user_data);
 }
 
 gboolean 
@@ -368,7 +388,7 @@
 	}
 	
 	if (connect_data->current_addr == NULL) {
-		_lm_connection_socket_result (socket->connection, FALSE);
+		(socket->connect_func) (socket, FALSE, socket->user_data);
 		
 		 /* if the user callback called connection_close(), this is already freed */
 		if (socket->connect_data != NULL) {
@@ -596,8 +616,8 @@
 	b_written = lm_socket_do_write (socket, out_buf->str, out_buf->len);
 
 	if (b_written < 0) {
-		_lm_connection_error_event (socket,
-					    G_IO_HUP, socket->connection);
+		(socket->closed_func) (socket, LM_DISCONNECT_REASON_ERROR, 
+				       socket->user_data);
 		return FALSE;
 	}
 
@@ -682,7 +702,9 @@
 
 LmSocket *
 lm_socket_create (GMainContext      *context,
-		  IncomingDataFunc   func,
+		  IncomingDataFunc   data_func,
+		  SocketClosedFunc   closed_func,
+		  ConnectResultFunc  connect_func,
 		  gpointer           user_data,
 		  LmConnection      *connection,
 		  gboolean           blocking,
@@ -702,7 +724,9 @@
 	
 	g_return_val_if_fail (server != NULL, NULL);
 	g_return_val_if_fail ((port >= MIN_PORT && port <= MAX_PORT), NULL);
-	g_return_val_if_fail (func != NULL, NULL);
+	g_return_val_if_fail (data_func != NULL, NULL);
+	g_return_val_if_fail (closed_func != NULL, NULL);
+	g_return_val_if_fail (connect_func != NULL, NULL);
 
 	socket = g_new0 (LmSocket, 1);
 
@@ -722,7 +746,9 @@
 	socket->ssl = NULL;
 	socket->proxy = NULL;
 	socket->blocking = blocking;
-	socket->func = func;
+	socket->data_func = data_func;
+	socket->closed_func = closed_func;
+	socket->connect_func = connect_func;
 	socket->user_data = user_data;
 
 	if (use_srv) {
--- a/loudmouth/lm-socket.h	Sun Feb 25 22:48:39 2007 +0100
+++ b/loudmouth/lm-socket.h	Mon Feb 26 01:17:30 2007 +0100
@@ -31,6 +31,14 @@
 				       const gchar    *buf,
 				       gpointer        user_data);
 
+typedef void    (* SocketClosedFunc)  (LmSocket       *socket,
+				       LmDisconnectReason reason,
+				       gpointer        user_data);
+
+typedef void    (* ConnectResultFunc) (LmSocket        *socket,
+				       gboolean         result,
+				       gpointer         user_data);
+
 gboolean  lm_socket_output_is_buffered    (LmSocket       *socket,
 					   const gchar    *buffer,
 					   gint            len);
@@ -42,7 +50,9 @@
 					   gint            len);
 
 LmSocket *  lm_socket_create              (GMainContext   *context, 
-					   IncomingDataFunc func,
+					   IncomingDataFunc data_func,
+					   SocketClosedFunc closed_func,
+					   ConnectResultFunc connect_func,
 					   gpointer         user_data,
 					   LmConnection   *connection,
 					   gboolean        blocking,