changeset 24390:d72c185d0be3

Separate the parsing of data into a separate function from the reading of data, to remove a big chunk of duplicate code in the read_cb callbacks for servconn and httpconn
author Mark Doliner <mark@kingant.net>
date Sat, 15 Nov 2008 16:15:33 +0000
parents ae66b7b3086a
children 71c42ce1a1df
files libpurple/protocols/msn/httpconn.c libpurple/protocols/msn/servconn.c libpurple/protocols/msn/servconn.h
diffstat 3 files changed, 19 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/msn/httpconn.c	Sat Nov 15 09:44:05 2008 +0000
+++ b/libpurple/protocols/msn/httpconn.c	Sat Nov 15 16:15:33 2008 +0000
@@ -269,21 +269,17 @@
 {
 	MsnHttpConn *httpconn;
 	MsnServConn *servconn;
-	MsnSession *session;
 	char buf[MSN_BUF_LEN];
-	char *cur, *end, *old_rx_buf;
 	gssize len;
-	int cur_len;
 	char *result_msg = NULL;
 	size_t result_len = 0;
 	gboolean error = FALSE;
 
 	httpconn = data;
 	servconn = httpconn->servconn;
-	session = httpconn->session;
 
 	if (servconn->type == MSN_SERVCONN_NS)
-		session->account->gc->last_received = time(NULL);
+		servconn->session->account->gc->last_received = time(NULL);
 
 	len = read(httpconn->fd, buf, sizeof(buf) - 1);
 	if (len < 0 && errno == EAGAIN)
@@ -339,64 +335,7 @@
 	servconn->rx_buf = result_msg;
 	servconn->rx_len = result_len;
 
-	end = old_rx_buf = servconn->rx_buf;
-
-	servconn->processing = TRUE;
-
-	do
-	{
-		cur = end;
-
-		if (servconn->payload_len)
-		{
-			if (servconn->payload_len > servconn->rx_len)
-				/* The payload is still not complete. */
-				break;
-
-			cur_len = servconn->payload_len;
-			end += cur_len;
-		}
-		else
-		{
-			end = strstr(cur, "\r\n");
-
-			if (end == NULL)
-				/* The command is still not complete. */
-				break;
-
-			*end = '\0';
-			end += 2;
-			cur_len = end - cur;
-		}
-
-		servconn->rx_len -= cur_len;
-
-		if (servconn->payload_len)
-		{
-			msn_cmdproc_process_payload(servconn->cmdproc, cur, cur_len);
-			servconn->payload_len = 0;
-		}
-		else
-		{
-			msn_cmdproc_process_cmd_text(servconn->cmdproc, cur);
-			servconn->payload_len = servconn->cmdproc->last_cmd->payload_len;
-		}
-	} while (servconn->connected && !servconn->wasted && servconn->rx_len > 0);
-
-	if (servconn->connected && !servconn->wasted)
-	{
-		if (servconn->rx_len > 0)
-			servconn->rx_buf = g_memdup(cur, servconn->rx_len);
-		else
-			servconn->rx_buf = NULL;
-	}
-
-	servconn->processing = FALSE;
-
-	if (servconn->wasted)
-		msn_servconn_destroy(servconn);
-
-	g_free(old_rx_buf);
+	msn_servconn_process_data(servconn);
 }
 
 static void
--- a/libpurple/protocols/msn/servconn.c	Sat Nov 15 09:44:05 2008 +0000
+++ b/libpurple/protocols/msn/servconn.c	Sat Nov 15 16:15:33 2008 +0000
@@ -366,17 +366,13 @@
 read_cb(gpointer data, gint source, PurpleInputCondition cond)
 {
 	MsnServConn *servconn;
-	MsnSession *session;
 	char buf[MSN_BUF_LEN];
-	char *cur, *end, *old_rx_buf;
 	gssize len;
-	int cur_len;
 
 	servconn = data;
-	session = servconn->session;
 
 	if (servconn->type == MSN_SERVCONN_NS)
-		session->account->gc->last_received = time(NULL);
+		servconn->session->account->gc->last_received = time(NULL);
 
 	len = read(servconn->fd, buf, sizeof(buf) - 1);
 	if (len < 0 && errno == EAGAIN)
@@ -396,6 +392,14 @@
 	memcpy(servconn->rx_buf + servconn->rx_len, buf, len + 1);
 	servconn->rx_len += len;
 
+	msn_servconn_process_data(servconn);
+}
+
+void msn_servconn_process_data(MsnServConn *servconn)
+{
+	char *cur, *end, *old_rx_buf;
+	int cur_len;
+
 	end = old_rx_buf = servconn->rx_buf;
 
 	servconn->processing = TRUE;
--- a/libpurple/protocols/msn/servconn.h	Sat Nov 15 09:44:05 2008 +0000
+++ b/libpurple/protocols/msn/servconn.h	Sat Nov 15 16:15:33 2008 +0000
@@ -170,4 +170,12 @@
  */
 void msn_servconn_got_error(MsnServConn *servconn, MsnServConnError error);
 
+/**
+ * Process the data in servconn->rx_buf.  This is called after reading
+ * data from the socket.
+ *
+ * @param servconn The servconn.
+ */
+void msn_servconn_process_data(MsnServConn *servconn);
+
 #endif /* _MSN_SERVCONN_H_ */