diff libpurple/protocols/msn/slpmsg_part.c @ 31599:34da321b60f1

Try to hide all P2P fields away behind accessor functions.
author Elliott Sales de Andrade <qulogic@pidgin.im>
date Thu, 20 Jan 2011 06:43:45 +0000
parents 3b8e763fb133
children 9562a7baa9e4
line wrap: on
line diff
--- a/libpurple/protocols/msn/slpmsg_part.c	Thu Jan 20 06:41:34 2011 +0000
+++ b/libpurple/protocols/msn/slpmsg_part.c	Thu Jan 20 06:43:45 2011 +0000
@@ -28,20 +28,14 @@
 #include "slpmsg.h"
 #include "slpmsg_part.h"
 
-MsnSlpMessagePart *msn_slpmsgpart_new(MsnP2PHeader *header, MsnP2PFooter *footer)
+MsnSlpMessagePart *msn_slpmsgpart_new(MsnP2PInfo *info)
 {
 	MsnSlpMessagePart *part;
 
 	part = g_new0(MsnSlpMessagePart, 1);
 
-	if (header) {
-		part->header = g_new0(MsnP2PHeader, 1);
-		*part->header = *header;
-	}
-	if (footer) {
-		part->footer = g_new0(MsnP2PFooter, 1);
-		*part->footer = *footer;
-	}
+	if (info)
+		part->info = msn_p2p_info_dup(info);
 
 	part->ack_cb = msn_slpmsgpart_ack;
 	part->nak_cb = msn_slpmsgpart_nak;
@@ -52,20 +46,22 @@
 MsnSlpMessagePart *msn_slpmsgpart_new_from_data(const char *data, size_t data_len)
 {
 	MsnSlpMessagePart *part;
+	size_t len;
 	int body_len;
 
 	if (data_len < P2P_PACKET_HEADER_SIZE) {
 		return NULL;
 	}
 
-	part = msn_slpmsgpart_new(NULL, NULL);
+	part = msn_slpmsgpart_new(NULL);
+	part->info = msn_p2p_info_new();
 
 	/* Extract the binary SLP header */
-	part->header = msn_p2p_header_from_wire(data);
-	data += P2P_PACKET_HEADER_SIZE;
+	len = msn_p2p_header_from_wire(part->info, data);
+	data += len;
 
 	/* Extract the body */
-	body_len = data_len - P2P_PACKET_HEADER_SIZE - P2P_PACKET_FOOTER_SIZE;
+	body_len = data_len - len - P2P_PACKET_FOOTER_SIZE;
 	/* msg->body_len = msg->msnslp_header.length; */
 
 	if (body_len > 0) {
@@ -77,15 +73,14 @@
 
 	/* Extract the footer */
 	if (body_len >= 0)
-		part->footer = msn_p2p_footer_from_wire(data);
+		msn_p2p_footer_from_wire(part->info, data);
 
 	return part;
 }
 
 static void msn_slpmsgpart_destroy(MsnSlpMessagePart *part)
 {
-	g_free(part->header);
-	g_free(part->footer);
+	g_free(part->info);
 	g_free(part->buffer);
 
 	g_free(part);
@@ -142,27 +137,25 @@
 	char *footer;
 	char *base;
 	char *tmp;
-	size_t siz;
+	size_t header_size, footer_size;
 
-	base = g_malloc(P2P_PACKET_HEADER_SIZE + part->size + P2P_PACKET_FOOTER_SIZE);
+	header = msn_p2p_header_to_wire(part->info, &header_size);
+	footer = msn_p2p_footer_to_wire(part->info, &footer_size);
+
+	base = g_malloc(header_size + part->size + footer_size);
 	tmp = base;
 
-	header = msn_p2p_header_to_wire(part->header);
-	footer = msn_p2p_footer_to_wire(part->footer);
-
-	siz = P2P_PACKET_HEADER_SIZE;
 	/* Copy header */
-	memcpy(tmp, header, siz);
-	tmp += siz;
+	memcpy(tmp, header, header_size);
+	tmp += header_size;
 
 	/* Copy body */
 	memcpy(tmp, part->buffer, part->size);
 	tmp += part->size;
 
 	/* Copy footer */
-	siz = P2P_PACKET_FOOTER_SIZE;
-	memcpy(tmp, footer, siz);
-	tmp += siz;
+	memcpy(tmp, footer, footer_size);
+	tmp += footer_size;
 
 	*ret_size = tmp - base;
 
@@ -171,23 +164,27 @@
 
 	return base;
 }
+
 /* We have received the message ack */
 void
 msn_slpmsgpart_ack(MsnSlpMessagePart *part, void *data)
 {
 	MsnSlpMessage *slpmsg;
+	guint64 offset;
 	long long real_size;
 
 	slpmsg = data;
 
-	real_size = (slpmsg->header->flags == P2P_ACK) ? 0 : slpmsg->size;
+	real_size = (msn_p2p_info_get_flags(slpmsg->p2p_info) == P2P_ACK) ? 0 : slpmsg->size;
 
-	slpmsg->header->offset += part->header->length;
+	offset = msn_p2p_info_get_offset(slpmsg->p2p_info);
+	offset += msn_p2p_info_get_length(part->info);
+	msn_p2p_info_set_offset(slpmsg->p2p_info, offset);
 
 	slpmsg->parts = g_list_remove(slpmsg->parts, part);
 	msn_slpmsgpart_unref(part);
 
-	if (slpmsg->header->offset < real_size)
+	if (offset < real_size)
 	{
 		if (slpmsg->slpcall->xfer && purple_xfer_get_status(slpmsg->slpcall->xfer) == PURPLE_XFER_STATUS_STARTED)
 		{
@@ -200,7 +197,7 @@
 	else
 	{
 		/* The whole message has been sent */
-		if (msn_p2p_msg_is_data(slpmsg->header->flags))
+		if (msn_p2p_msg_is_data(msn_p2p_info_get_flags(slpmsg->p2p_info)))
 		{
 			if (slpmsg->slpcall != NULL)
 			{
@@ -226,3 +223,9 @@
 	msn_slpmsgpart_unref(part);
 }
 
+void
+msn_slpmsgpart_to_string(MsnSlpMessagePart *part, GString *str)
+{
+	msn_p2p_info_to_string(part->info, str);
+}
+