diff libpurple/protocols/qq/qq_trans.c @ 24026:25f62d21b3f8

disapproval of revision '8cebefbc6cd5d84acb69c74e69e8821f11dd225d'
author Daniel Atallah <daniel.atallah@gmail.com>
date Mon, 15 Sep 2008 03:04:07 +0000
parents 147ada94a1d8
children 225e0e9e1055
line wrap: on
line diff
--- a/libpurple/protocols/qq/qq_trans.c	Thu Sep 11 13:25:07 2008 +0000
+++ b/libpurple/protocols/qq/qq_trans.c	Mon Sep 15 03:04:07 2008 +0000
@@ -37,35 +37,8 @@
 
 #define QQ_RESEND_MAX               3	/* max resend per packet */
 
-enum {
-	QQ_TRANS_IS_SERVER = 0x01,			/* Is server command or client command */
-	QQ_TRANS_IS_IMPORT = 0x02,			/* Only notice if not get reply; or resend, disconn if reties get 0*/
-	QQ_TRANS_BEFORE_LOGIN = 0x04,		/* server command before login*/
-};
-
-struct _qq_transaction {
-	guint8 flag;
-	guint16 seq;
-	guint16 cmd;
-
-	guint8 room_cmd;
-	guint32 room_id;
-
-	guint8 *data;
-	gint data_len;
-
-	gint fd;
-	gint send_retries;
-	gint rcved_times;
-	gint scan_times;
-
-	gint update_class;
-	guint32 ship32;
-};
-
-qq_transaction *qq_trans_find_rcved(PurpleConnection *gc, guint16 cmd, guint16 seq)
+qq_transaction *qq_trans_find_rcved(qq_data *qd, guint16 cmd, guint16 seq)
 {
-	qq_data *qd = (qq_data *)gc->proto_data;
 	GList *curr;
 	GList *next;
 	qq_transaction *trans;
@@ -77,21 +50,19 @@
 	next = qd->transactions;
 	while( (curr = next) ) {
 		next = curr->next;
-
+		
 		trans = (qq_transaction *) (curr->data);
 		if(trans->cmd == cmd && trans->seq == seq) {
 			if (trans->rcved_times == 0) {
 				trans->scan_times = 0;
 			}
 			trans->rcved_times++;
-			/* server may not get our confirm reply before, send reply again*/
-			/* only rcved buffer stored in transaction
 			if (qq_trans_is_server(trans) && qq_trans_is_dup(trans)) {
+				/* server may not get our confirm reply before, send reply again*/
 				if (trans->data != NULL && trans->data_len > 0) {
-					qq_send_cmd_encrypted(gc, trans->cmd, trans->seq, trans->data, trans->data_len, FALSE);
+					qq_send_data(qd, trans->cmd, trans->seq, FALSE, trans->data, trans->data_len);
 				}
 			}
-			*/
 			return trans;
 		}
 	}
@@ -99,20 +70,20 @@
 	return NULL;
 }
 
-gboolean qq_trans_is_server(qq_transaction *trans)
+gboolean qq_trans_is_server(qq_transaction *trans) 
 {
 	g_return_val_if_fail(trans != NULL, FALSE);
-
+	
 	if (trans->flag & QQ_TRANS_IS_SERVER)
 		return TRUE;
 	else
 		return FALSE;
 }
 
-gboolean qq_trans_is_dup(qq_transaction *trans)
+gboolean qq_trans_is_dup(qq_transaction *trans) 
 {
 	g_return_val_if_fail(trans != NULL, TRUE);
-
+	
 	if (trans->rcved_times > 1)
 		return TRUE;
 	else
@@ -131,122 +102,115 @@
 	return trans->room_id;
 }
 
-gint qq_trans_get_class(qq_transaction *trans)
-{
-	g_return_val_if_fail(trans != NULL, QQ_CMD_CLASS_NONE);
-	return trans->update_class;
-}
-
-gint qq_trans_get_ship(qq_transaction *trans)
-{
-	g_return_val_if_fail(trans != NULL, 0);
-	return trans->ship32;
-}
-
-static qq_transaction *trans_create(PurpleConnection *gc, gint fd,
-	guint16 cmd, guint16 seq, guint8 *data, gint data_len, gint update_class, guint32 ship32)
+/* Remove a packet with seq from send trans */
+static void trans_remove(qq_data *qd, qq_transaction *trans) 
 {
-	qq_data *qd;
-	qq_transaction *trans;
-
-	g_return_val_if_fail(gc != NULL && gc->proto_data != NULL, NULL);
-	qd = (qq_data *) gc->proto_data;
-
-	trans = g_new0(qq_transaction, 1);
-
-	memset(trans, 0, sizeof(qq_transaction));
-	trans->fd = fd;
-	trans->cmd = cmd;
-	trans->seq = seq;
-
-	trans->data = NULL;
-	trans->data_len = 0;
-	if (data != NULL && data_len > 0) {
-		/* don't use g_strdup, may have 0x00 */
-		trans->data = g_memdup(data, data_len);
-		trans->data_len = data_len;
-	}
-
-	trans->update_class = update_class;
-	return trans;
-}
-
-/* Remove a packet with seq from send trans */
-static void trans_remove(PurpleConnection *gc, qq_transaction *trans)
-{
-	qq_data *qd = (qq_data *)gc->proto_data;
 	g_return_if_fail(qd != NULL && trans != NULL);
-
-#if 0
-	purple_debug_info("QQ_TRANS",
+	
+	purple_debug(PURPLE_DEBUG_INFO, "QQ_TRANS",
 				"Remove [%s%05d] retry %d rcved %d scan %d %s\n",
 				(trans->flag & QQ_TRANS_IS_SERVER) ? "SRV-" : "",
 				trans->seq,
 				trans->send_retries, trans->rcved_times, trans->scan_times,
 				qq_get_cmd_desc(trans->cmd));
-#endif
+
 	if (trans->data)	g_free(trans->data);
 	qd->transactions = g_list_remove(qd->transactions, trans);
 	g_free(trans);
 }
 
-void qq_trans_add_client_cmd(PurpleConnection *gc,
-	guint16 cmd, guint16 seq, guint8 *data, gint data_len, gint update_class, guint32 ship32)
+void qq_trans_add_client_cmd(qq_data *qd, guint16 cmd, guint16 seq, guint8 *data, gint data_len)
 {
-	qq_data *qd = (qq_data *)gc->proto_data;
-	qq_transaction *trans = trans_create(gc, qd->fd, cmd, seq, data, data_len, update_class, ship32);
+	qq_transaction *trans = g_new0(qq_transaction, 1);
 
+	g_return_if_fail(trans != NULL);
+
+	trans->flag = 0;
 	if (cmd == QQ_CMD_TOKEN || cmd == QQ_CMD_LOGIN || cmd == QQ_CMD_KEEP_ALIVE) {
-		trans->flag |= QQ_TRANS_IS_IMPORT;
+		trans->flag |= QQ_TRANS_CLI_IMPORT;
 	}
+	trans->fd = qd->fd;
+	trans->cmd = cmd;
+	trans->seq = seq;
+	trans->room_cmd = 0;
+	trans->room_id = 0;
 	trans->send_retries = QQ_RESEND_MAX;
-#if 0
-	purple_debug_info("QQ_TRANS", "Add client cmd, seq %d, data %p, len %d\n",
+	trans->rcved_times = 0;
+	trans->scan_times = 0;
+
+	trans->data = NULL;
+	trans->data_len = 0;
+	if (data != NULL && data_len > 0) {
+		trans->data = g_memdup(data, data_len);	/* don't use g_strdup, may have 0x00 */
+		trans->data_len = data_len;
+	}
+	purple_debug(PURPLE_DEBUG_INFO, "QQ_TRANS",
+			"Add client cmd, seq = %d, data = %p, len = %d\n",
 			trans->seq, trans->data, trans->data_len);
-#endif
 	qd->transactions = g_list_append(qd->transactions, trans);
 }
 
-void qq_trans_add_room_cmd(PurpleConnection *gc,
-		guint16 seq, guint8 room_cmd, guint32 room_id, guint8 *data, gint data_len,
-		gint update_class, guint32 ship32)
+void qq_trans_add_room_cmd(qq_data *qd, guint16 seq, guint8 room_cmd, guint32 room_id,
+		guint8 *data, gint data_len)
 {
-	qq_data *qd = (qq_data *)gc->proto_data;
-	qq_transaction *trans = trans_create(gc, qd->fd, QQ_CMD_ROOM, seq, data, data_len,
-			update_class, ship32);
+	qq_transaction *trans = g_new0(qq_transaction, 1);
+
+	g_return_if_fail(trans != NULL);
 
+	trans->flag = 0;
+	trans->fd = qd->fd;
+	trans->seq = seq;
+	trans->cmd = QQ_CMD_ROOM;
 	trans->room_cmd = room_cmd;
 	trans->room_id = room_id;
 	trans->send_retries = QQ_RESEND_MAX;
-#if 0
-	purple_debug_info("QQ_TRANS", "Add room cmd, seq %d, data %p, len %d\n",
+	trans->rcved_times = 0;
+	trans->scan_times = 0;
+
+	trans->data = NULL;
+	trans->data_len = 0;
+	if (data != NULL && data_len > 0) {
+		trans->data = g_memdup(data, data_len);	/* don't use g_strdup, may have 0x00 */
+		trans->data_len = data_len;
+	}
+	purple_debug(PURPLE_DEBUG_INFO, "QQ_TRANS",
+			"Add room cmd, seq = %d, data = %p, len = %d\n",
 			trans->seq, trans->data, trans->data_len);
-#endif
 	qd->transactions = g_list_append(qd->transactions, trans);
 }
 
-void qq_trans_add_server_cmd(PurpleConnection *gc,
-	guint16 cmd, guint16 seq, guint8 *data, gint data_len)
+void qq_trans_add_server_cmd(qq_data *qd, guint16 cmd, guint16 seq, guint8 *data, gint data_len)
 {
-	qq_data *qd = (qq_data *)gc->proto_data;
-	qq_transaction *trans = trans_create(gc, qd->fd, cmd, seq, data, data_len, QQ_CMD_CLASS_NONE, 0);
+	qq_transaction *trans = g_new0(qq_transaction, 1);
+
+	g_return_if_fail(trans != NULL);
 
 	trans->flag = QQ_TRANS_IS_SERVER;
-	if ( !qd->is_login ) {
+	if ( !qd->logged_in ) {
 		trans->flag |= QQ_TRANS_BEFORE_LOGIN;
 	}
+	trans->fd = qd->fd;
+	trans->cmd = cmd;
+	trans->seq = seq;
+	trans->room_cmd = 0;
+	trans->room_id = 0;
 	trans->send_retries = 0;
 	trans->rcved_times = 1;
-#if 0
-	purple_debug_info("QQ_TRANS", "Add server cmd, seq %d, data %p, len %d\n",
+	trans->scan_times = 0;
+	trans->data = NULL;
+	trans->data_len = 0;
+	if (data != NULL && data_len > 0) {
+		trans->data = g_memdup(data, data_len);	/* don't use g_strdup, may have 0x00 */
+		trans->data_len = data_len;
+	}
+	purple_debug(PURPLE_DEBUG_INFO, "QQ_TRANS",
+			"Add server cmd, seq = %d, data = %p, len = %d\n",
 			trans->seq, trans->data, trans->data_len);
-#endif
 	qd->transactions = g_list_append(qd->transactions, trans);
 }
 
-void qq_trans_process_before_login(PurpleConnection *gc)
+void qq_trans_process_before_login(qq_data *qd)
 {
-	qq_data *qd = (qq_data *)gc->proto_data;
 	GList *curr;
 	GList *next;
 	qq_transaction *trans;
@@ -257,44 +221,42 @@
 	while( (curr = next) ) {
 		next = curr->next;
 		trans = (qq_transaction *) (curr->data);
-#if 0
-		purple_debug_info("QQ_TRANS", "Scan [%d]\n", trans->seq);
-#endif
+		/* purple_debug(PURPLE_DEBUG_ERROR, "QQ_TRANS", "Scan [%d]\n", trans->seq); */
+		
 		if ( !(trans->flag & QQ_TRANS_IS_SERVER) ) {
 			continue;
 		}
 		if ( !(trans->flag & QQ_TRANS_BEFORE_LOGIN) ) {
 			continue;
 		}
-		/* set QQ_TRANS_BEFORE_LOGIN off */
+		// set QQ_TRANS_BEFORE_LOGIN off
 		trans->flag &= ~QQ_TRANS_BEFORE_LOGIN;
 
-		purple_debug_info("QQ_TRANS",
+		purple_debug(PURPLE_DEBUG_ERROR, "QQ_TRANS",
 				"Process server cmd before login, seq %d, data %p, len %d, send_retries %d\n",
 				trans->seq, trans->data, trans->data_len, trans->send_retries);
 
-		qq_proc_cmd_reply(gc, trans->seq, trans->cmd, trans->data, trans->data_len, trans->update_class, trans->ship32);
+		qq_proc_cmd_reply(qd->gc, trans->seq, trans->cmd, trans->data, trans->data_len);
 	}
 
-	/* purple_debug_info("QQ_TRANS", "Scan finished\n"); */
+	/* purple_debug(PURPLE_DEBUG_INFO, "QQ_TRANS", "Scan finished\n"); */
 	return;
 }
 
-gboolean qq_trans_scan(PurpleConnection *gc)
+gboolean qq_trans_scan(qq_data *qd)
 {
-	qq_data *qd = (qq_data *)gc->proto_data;
 	GList *curr;
 	GList *next;
 	qq_transaction *trans;
 
 	g_return_val_if_fail(qd != NULL, FALSE);
-
+	
 	next = qd->transactions;
 	while( (curr = next) ) {
 		next = curr->next;
 		trans = (qq_transaction *) (curr->data);
-		/* purple_debug_info("QQ_TRANS", "Scan [%d]\n", trans->seq); */
-
+		/* purple_debug(PURPLE_DEBUG_INFO, "QQ_TRANS", "Scan [%d]\n", trans->seq); */
+		
 		if (trans->flag & QQ_TRANS_BEFORE_LOGIN) {
 			/* keep server cmd before login*/
 			continue;
@@ -308,60 +270,67 @@
 
 		if (trans->rcved_times > 0) {
 			/* Has been received */
-			trans_remove(gc, trans);
+			trans_remove(qd, trans);
 			continue;
 		}
 
 		if (trans->flag & QQ_TRANS_IS_SERVER) {
 			continue;
 		}
-
+		
 		/* Never get reply */
 		trans->send_retries--;
 		if (trans->send_retries <= 0) {
-			purple_debug_warning("QQ_TRANS",
+			purple_debug(PURPLE_DEBUG_WARNING, "QQ_TRANS",
 				"[%d] %s is lost.\n",
 				trans->seq, qq_get_cmd_desc(trans->cmd));
-			if (trans->flag & QQ_TRANS_IS_IMPORT) {
+			if (trans->flag & QQ_TRANS_CLI_IMPORT) {
 				return TRUE;
 			}
 
-			purple_debug_error("QQ_TRANS",
+			purple_debug(PURPLE_DEBUG_ERROR, "QQ_TRANS",
 				"Lost [%d] %s, data %p, len %d, retries %d\n",
 				trans->seq, qq_get_cmd_desc(trans->cmd),
 				trans->data, trans->data_len, trans->send_retries);
-			trans_remove(gc, trans);
+			trans_remove(qd, trans);
 			continue;
 		}
 
-		purple_debug_error("QQ_TRANS",
+		purple_debug(PURPLE_DEBUG_ERROR, "QQ_TRANS",
 				"Resend [%d] %s data %p, len %d, send_retries %d\n",
 				trans->seq, qq_get_cmd_desc(trans->cmd),
 				trans->data, trans->data_len, trans->send_retries);
-		qq_send_cmd_encrypted(gc, trans->cmd, trans->seq, trans->data, trans->data_len, FALSE);
+		qq_send_data(qd, trans->cmd, trans->seq, FALSE, trans->data, trans->data_len);
 	}
 
-	/* purple_debug_info("QQ_TRANS", "Scan finished\n"); */
+	/* purple_debug(PURPLE_DEBUG_INFO, "QQ_TRANS", "Scan finished\n"); */
 	return FALSE;
 }
 
 /* clean up send trans and free all contents */
-void qq_trans_remove_all(PurpleConnection *gc)
+void qq_trans_remove_all(qq_data *qd)
 {
-	qq_data *qd = (qq_data *)gc->proto_data;
+	GList *curr;
+	GList *next;
 	qq_transaction *trans;
 	gint count = 0;
 
-	while(qd->transactions != NULL) {
-		trans = (qq_transaction *) (qd->transactions->data);
-		qd->transactions = g_list_remove(qd->transactions, trans);
-
-		if (trans->data)	g_free(trans->data);
-		g_free(trans);
+	curr = qd->transactions;
+	while(curr) {
+		next = curr->next;
+		
+		trans = (qq_transaction *) (curr->data);
+		/*
+		purple_debug(PURPLE_DEBUG_ERROR, "QQ_TRANS",
+			"Remove to transaction, seq = %d, buf = %p, len = %d\n",
+			trans->seq, trans->buf, trans->len);
+		*/
+		trans_remove(qd, trans);
 
 		count++;
+		curr = next;
 	}
-	if (count > 0) {
-		purple_debug_info("QQ_TRANS", "Free all %d packets\n", count);
-	}
+	g_list_free(qd->transactions);
+
+	purple_debug(PURPLE_DEBUG_INFO, "QQ_TRANS", "Free all %d packets\n", count);
 }