/soc/2013/ashmew2/filetransferX: 517e425e9423: Reworking buffer ...

Ashish Gupta ashmew2 at gmail.com
Mon Sep 9 01:49:02 EDT 2013


Changeset: 517e425e9423a99b91e8a1f1542c8e3b9273c2f1
Author:	 Ashish Gupta <ashmew2 at gmail.com>
Date:	 2013-09-09 11:06 +0530
Branch:	 filetransferX
URL: https://hg.pidgin.im/soc/2013/ashmew2/filetransferX/rev/517e425e9423

Description:

Reworking buffer usage for sending file

diffstat:

 libpurple/protocols/jabber/google/google_session.c |  176 +++++++++++++-------
 1 files changed, 116 insertions(+), 60 deletions(-)

diffs (230 lines):

diff --git a/libpurple/protocols/jabber/google/google_session.c b/libpurple/protocols/jabber/google/google_session.c
--- a/libpurple/protocols/jabber/google/google_session.c
+++ b/libpurple/protocols/jabber/google/google_session.c
@@ -61,9 +61,13 @@ typedef struct {
 	gint stream_id;
 	gboolean candidates_gathered;
 	gboolean pair_selected;
-	gboolean file_opened;
+	
+	gboolean file_opened;               /* Stuff for using file buffer for sending Gtalk Files.. */ 
 	FILE *file_to_send;
-	GList *remote_share_candidates; /* lists of PurpleMediaCandidate OR NiceCandidates(maybe?)*/
+	char *file_buf;
+	int file_buf_current_size;
+	int file_buf_sent;
+	GList *remote_share_candidates; /* lists of NiceCandidates */
 	GList *local_share_candidates;	
 } GoogleXferSessionData; 
 /*Thanks malu!*/
@@ -2076,7 +2080,7 @@ void cb_nice_component_state_changed(Nic
 		char *file_buf = NULL;
 		/*FILE *file_ptr = fopen(share_session->xfer->local_filename, "rb");*/
 		FILE *file_ptr = share_session->file_to_send;
-
+		
 		purple_debug_info("google_session", "STATE_READY for XFER_SEND! in cb_component_state_changed.");
 		purple_debug_info("google_session", "xfer->local_filename = %s\n",share_session->xfer->local_filename);
 		/*		if(share_session->channel_state == GTALK_XFER_CHANNEL_HTTP_OK_SENT) {
@@ -2187,13 +2191,41 @@ void cb_nice_component_state_changed(Nic
 
 /*TODO: Add more stuff related to state changes here*/
 
+void gtalk_xfer_get_next_block(GoogleXferSessionData *share_session)
+{
+	int fread_ret;
+	
+	if(!share_session)
+		return;
+	
+	purple_debug_info("google_session", "Making a buffer..Phase 1 Done.");
+	share_session->file_buf = malloc(65535);
+	purple_debug_info("google_session", "Phase 2Done.");
+	purple_debug_info("google_session", "Phase 3Done.");
+	fread_ret = fread(share_session->file_buf, 1, 65536, share_session->file_to_send);
+	purple_debug_info("google_session", "Phase 4 Done.fread_ret is %d\n",fread_ret);
+	share_session->file_buf_current_size = fread_ret;
+	purple_debug_info("google_session", "Phase 5 Done.\n");
+	share_session->file_buf_sent = 0;
+
+	if(!share_session->file_buf)
+		purple_debug_info("google_session", "file_buf for struct is NULL\n");
+	else
+		purple_debug_info("google_session", "file_buf for struct is NOT NULL\n");
+}
+
 void cb_nice_component_writable(NiceAgent *agent, guint stream_id, guint component_id,
 				gpointer user_data)  
 {
 	GoogleXferSessionData *share_session = (GoogleXferSessionData *)user_data;
 	PurpleXfer *xfer = share_session->xfer;
-
-	purple_debug_info("google_session", "__WRITABLE NOW ! (Inside cb_nice_component_writable\n");
+	
+	FILE *file_ptr = share_session->file_to_send;
+	FILE *file_buf = share_session->file_buf;
+	int fread_count = 0;
+	int send_ret, fread_ret;
+	
+	purple_debug_info("google_session", "_WRITABLE NOW ! (Inside cb_nice_component_writable\n");
 
 	if(share_session->agent_state!=NICE_COMPONENT_STATE_READY) {
 		purple_debug_info("google_session", "returning from _writable as state!=READY..\n");
@@ -2201,62 +2233,85 @@ void cb_nice_component_writable(NiceAgen
 	}
 
 	if(share_session->channel_state == GTALK_XFER_CHANNEL_HTTP_SENDING) {
-		FILE *file_ptr = share_session->file_to_send;
-		int send_ret, fread_ret;
+		purple_debug_info("google_session", "Checking for file_buf is NULL or not..");
+
+		if(share_session->file_buf == NULL) { /*This means that we should read the buffer*/
+			gtalk_xfer_get_next_block(share_session);
+			file_buf = share_session->file_buf;
+		}
+		
 		purple_debug_info("google_session", "Entering the FOREVER loop..\n");
+				
 		while (1) {
-		  char *file_buf = g_malloc(65536);
-
-		  purple_debug_info("google_session", "Inside writable(), SENDING_STATE!\n");
-		  purple_debug_info("google_session", "ftell gives : %ld\n", ftell(file_ptr));
-		  purple_debug_info("google_session", "\nBytes remaining: %llu",
-				    purple_xfer_get_bytes_remaining(xfer));
+			purple_debug_info("google_session", "ftell gives : %ld\n", ftell(file_ptr));
+			purple_debug_info("google_session", "\nBytes remaining: %llu",
+					  purple_xfer_get_bytes_remaining(xfer));
+			purple_debug_info("google_session","Bytes of buffer trying to send : %d\n", share_session->file_buf_current_size - 
+					  share_session->file_buf_sent);
+			
+			/*Change the magic number 65530 to something sensible.*/
+			if(!file_buf)
+				purple_debug_info("purple_debug", "file_buf is NULL..\n");
+			else
+				purple_debug_info("purple_debug", "file_buf is not NULL..\n");
+
+			send_ret = nice_agent_send(share_session->share_agent, share_session->stream_id, 1,
+						   share_session->file_buf_current_size - share_session->file_buf_sent, 
+						   file_buf+share_session->file_buf_sent);
+
+			purple_debug_info("google_session", "Send _ret is %d\n",send_ret);
+			
+			if(send_ret<=0){
+				purple_debug_info("google_session", "send_ret is negative or zero...\n");
+				break;
+			}
+			else if (send_ret>0) {
+				xfer->bytes_sent += send_ret;
+				xfer->bytes_remaining -= send_ret;
+				purple_xfer_update_progress(xfer);
+				share_session->file_buf_sent += send_ret;
+			}
+
+			if(purple_xfer_get_bytes_remaining(xfer) <= 0) {
+				purple_debug_info("google_session", "\nTransfer done!");
+				/* 
+				   Set up something here to check if CRLF CRLF is properly sent or not, 
+				   otherwise wait for writable signal again and resend:TODO
+				*/
+				nice_agent_send(share_session->share_agent, share_session->stream_id, 1,
+						strlen("\r\n\r\n"), "\r\n\r\n");
+				purple_xfer_set_completed(xfer, 1);
+				return;
+			}
+			purple_debug_info("google_session", "BYtes remaining : %llu\n", purple_xfer_get_bytes_remaining(xfer));
+			purple_debug_info("google_session", "Current buf size : %d\n",share_session->file_buf_current_size);
+			purple_debug_info("google_session", "Current Bytes Sent.. : %d\n",share_session->file_buf_sent);
+
+			if(share_session->file_buf_sent >= share_session->file_buf_current_size) {
+				free(file_buf);
+				share_session->file_buf = NULL;
+				share_session->file_buf_sent = 0;
+				share_session-> file_buf_current_size = 0;
+				purple_debug_info("google_session", "Buffer sent..Should grab next buffer\n");
+				/*TODO: Put a call to grab next buffer here*/
+				gtalk_xfer_get_next_block(share_session);
+			}
+			purple_debug_info("google_session", "Touching while TRUE's floor..\n");
+		}
+	}
+			
+
+		/*if buffer in use, 
+		      finish writing the buffered data,
+		  if not,
+		      read the next 64 KB into buffer
+		*/
+
+		/*whenever buf_sent is -1, means that the buffer of 64K should be read from file.
+		  Until the full buffer is transferred, no more data should be read from the file.
+		  fread automatically takes care of fseek and so we should avoid doing fseek on our own
+		*/
 		
-		  if(purple_xfer_get_bytes_remaining(xfer) == 0) {
-			  purple_debug_info("google_session", "\nZero bytes remaining!!");
-/*Set up something here to check if CRLF CRLF is properly sent or not, otherwise wait for writable signal again and resend:TODO*/
-			  nice_agent_send(share_session->share_agent, share_session->stream_id, 1,
-					  strlen("\r\n\r\n"), "\r\n\r\n");
-			  return;
-		  }
-		  /*Change the magic number 65530 to something sensible. Max trans now : 32570245*/
-		  fread_ret = fread(file_buf, 1, 65536, file_ptr);
-		  send_ret = nice_agent_send(share_session->share_agent, share_session->stream_id, 1,
-					     strlen(file_buf), file_buf);
-		  
-/*send_ret = nice_agent_send(share_session->share_agent, share_session->stream_id, 1,
-  purple_xfer_get_bytes_remaining(xfer), file_ptr);*/
-		  purple_debug_info("google_session", "\n_SENDING state..send_ret is : %d",send_ret);
-		  purple_debug_info("google_session", "\nSize of file_buf : %d\n",strlen(file_buf));
-		  free(file_buf);
-
-		  if(send_ret<0) {
-			  purple_debug_info("google_session", "send_ret is negative or zero...\n");
-			  break;
-		  }
-		  else if (send_ret>0) {
-			  fseek(file_ptr, send_ret,SEEK_CUR);
-			  purple_debug_info("google_session", "fseek completed..\n");
-			  purple_xfer_update_progress(xfer);
-			  xfer->bytes_sent += send_ret;
-			  xfer->bytes_remaining -= send_ret;
-		  }
-		  else if(send_ret == 0) {
-			  long old_ftell = ftell(file_ptr);
-			  purple_debug_info("google_session", "send_ret is zero..\nClosing and Reopening file..\n");
-			  fclose(file_ptr);
-			  
-			  share_session->file_to_send = fopen(xfer->local_filename, "rb");
-			  file_ptr = share_session->file_to_send;
-			  fseek(file_ptr, old_ftell,SEEK_SET); 
-		  }
-			  
-
-		  
-//TODO??		if (purple_xfer_get_bytes_remaining(xfer) == 0)
-//			exit(0);
-		  purple_debug_info("google_session", "Touching while TRUE's floor..\n");
-		}
                 //TODO: Remove redundant part
 		/*if(share_session->file_to_send)
 			{
@@ -2279,7 +2334,7 @@ void cb_nice_component_writable(NiceAgen
 									      }
 				
 				}*/
-	}
+
 
 	/*	if(share_session->channel_state == GTALK_XFER_CHANNEL_HTTP_OK_SENT)
 		share_session->channel_state = GTALK_XFER_CHANNEL_HTTP_TRANSFERRING;
@@ -2317,7 +2372,8 @@ void cb_nice_component_writable(NiceAgen
 		}
 	
 	*/
-	/*TODO: Whenever nice_agent_send returns -1, that means that either something is VERY WRONG,
+	/*
+	  TODO: Whenever nice_agent_send returns -1, that means that either something is VERY WRONG,
 	  or the output buffer is full. If the output buffer is full, that means we need to wait for the
 	  reliable-transport-writable signal in order to resume writing to the xfer. Should probbaly set up a loop 
 	  here or something



More information about the Commits mailing list