gst-plugins-mythtv/src/myth_file_transfer.c
branchtrunk
changeset 35 a615ba8f8804
parent 31 eb4a812d4073
child 37 324e04989738
     1.1 --- a/gst-plugins-mythtv/src/myth_file_transfer.c	Tue Oct 17 01:03:06 2006 +0100
     1.2 +++ b/gst-plugins-mythtv/src/myth_file_transfer.c	Fri Oct 20 22:48:04 2006 +0100
     1.3 @@ -32,7 +32,7 @@
     1.4  #define MYTHTV_RETRIES			1
     1.5  #define MYTHTV_FILE_SIZE		-1
     1.6  
     1.7 -#define MYTHTV_BUFFER_SIZE		2048
     1.8 +#define MYTHTV_BUFFER_SIZE		256*1024
     1.9  
    1.10  #define MYTHTV_VERSION			30
    1.11  
    1.12 @@ -686,14 +686,13 @@
    1.13    return NULL;
    1.14  }
    1.15  
    1.16 -  gint 
    1.17 +gint 
    1.18  myth_file_transfer_read(MythFileTransfer *transfer, void *data, gint size, gboolean read_unlimited)
    1.19  {
    1.20    gint recv = 0;
    1.21    gsize bytes_read = 0;
    1.22 -
    1.23    gint sent = 0;
    1.24 -  //guint zerocnt = 0;
    1.25 +  guint remaining = 0;  
    1.26    gboolean response = FALSE;
    1.27  
    1.28    GIOChannel *io_channel;
    1.29 @@ -727,7 +726,6 @@
    1.30    if ( io_status == G_IO_STATUS_NORMAL )
    1.31      g_print( "[%s] Setting encoding to binary data socket).\n", __FUNCTION__ );
    1.32  
    1.33 -
    1.34    io_cond = g_io_channel_get_buffer_condition( io_channel );  
    1.35  
    1.36    io_cond_control = g_io_channel_get_buffer_condition( io_channel );
    1.37 @@ -754,16 +752,23 @@
    1.38    if ( ( io_cond & G_IO_IN ) != 0 ) {
    1.39      do 
    1.40      {
    1.41 +      trash = g_new0( gchar, MYTHTV_BUFFER_SIZE );
    1.42  
    1.43 -      io_status = g_io_channel_read_line( io_channel, &trash, &bytes_read, NULL, &error);
    1.44 +      io_status = g_io_channel_read_chars( io_channel, trash, 
    1.45 +      		MYTHTV_BUFFER_SIZE, &bytes_read, &error);
    1.46  
    1.47 -      g_print( "[%s] cleaning buffer on IO binary channel... (%s)\n", __FUNCTION__, trash );
    1.48 +      g_print( "[%s] cleaning buffer on IO binary channel... %d bytes gone!\n", 
    1.49 +      		__FUNCTION__, bytes_read );
    1.50 +      
    1.51 +      if ( trash != NULL )
    1.52 +      	g_free( trash );
    1.53 +      	
    1.54        io_cond = g_io_channel_get_buffer_condition( io_channel );
    1.55  
    1.56 -    } while ( ( io_cond & G_IO_IN ) != 0 && ( io_status != G_IO_STATUS_ERROR ) );
    1.57 +    } while ( ( io_cond & G_IO_IN ) != 0 && ( io_status != G_IO_STATUS_ERROR ) && (error == NULL) );
    1.58  
    1.59 -    if ( trash!= NULL )
    1.60 -      g_free( trash );
    1.61 +    //if ( trash!= NULL )
    1.62 +    //  g_free( trash );
    1.63    }
    1.64  
    1.65    if ( ( io_cond_control & G_IO_IN ) != 0 ) {
    1.66 @@ -789,36 +794,40 @@
    1.67        /* transfer->live_tv ? transfer->card_id :*/ transfer->recordernum ); // transfer->recordernum
    1.68    g_print( "\t[%s] Transfer_query = %s\n", __FUNCTION__, transfer->query->str );
    1.69    
    1.70 -  strlist = gmyth_string_list_new();
    1.71 -
    1.72 -  gmyth_string_list_append_char_array( strlist, transfer->query->str );
    1.73 -  gmyth_string_list_append_char_array( strlist, 
    1.74 -  		/*transfer->live_tv ? "REQUEST_BLOCK_RINGBUF" :*/ "REQUEST_BLOCK" );
    1.75 -  gmyth_string_list_append_int( strlist, size );
    1.76 -
    1.77 -  gmyth_socket_write_stringlist( transfer->control_sock, strlist );
    1.78    sent = size;
    1.79 +  remaining = size - recv;  
    1.80    //g_static_mutex_unlock( &mutex );
    1.81    //data = (void*)g_new0( gchar, size );
    1.82  
    1.83 -  g_io_channel_flush( io_channel_control, NULL );
    1.84    //g_io_channel_flush( io_channel, NULL );
    1.85  
    1.86 -  g_static_mutex_lock( &mutex );
    1.87 +  //g_static_mutex_lock( &mutex );
    1.88  
    1.89    io_cond = g_io_channel_get_buffer_condition( io_channel );
    1.90  
    1.91 -  while ( ( recv < sent ) )//&& ( io_cond & G_IO_IN ) != 0 )
    1.92 +  while ( recv < size  && !response )//&& ( io_cond & G_IO_IN ) != 0 )
    1.93    {
    1.94 +  	g_io_channel_flush( io_channel_control, NULL );
    1.95 +  	
    1.96 +    strlist = gmyth_string_list_new();
    1.97 +    gmyth_string_list_append_char_array( strlist, transfer->query->str );
    1.98 +    gmyth_string_list_append_char_array( strlist, 
    1.99 +    		/*transfer->live_tv ? "REQUEST_BLOCK_RINGBUF" :*/ "REQUEST_BLOCK" );
   1.100 +    gmyth_string_list_append_int( strlist, remaining );
   1.101 +	gmyth_socket_write_stringlist( transfer->control_sock, strlist );
   1.102 +	
   1.103 +	guint count_bytes = 0;
   1.104 +      
   1.105      do
   1.106      {
   1.107 -      //while ( ( io_cond & G_IO_IN ) == 0 ) {
   1.108 -      //usleep(200);
   1.109 -      //
   1.110 -      //io_cond = g_io_channel_get_buffer_condition( io_channel );
   1.111 -      //g_static_mutex_lock( &mutex );
   1.112 -
   1.113 -      buf_len = ( sent - recv ) > MYTHTV_BUFFER_SIZE ? MYTHTV_BUFFER_SIZE : ( sent - recv );
   1.114 +      //buf_len = ( sent - recv ) > MYTHTV_BUFFER_SIZE ? MYTHTV_BUFFER_SIZE : ( sent - recv );
   1.115 +      if ( remaining > MYTHTV_BUFFER_SIZE ) {
   1.116 +      	buf_len = MYTHTV_BUFFER_SIZE;
   1.117 +      } else {
   1.118 +      	buf_len = remaining;
   1.119 +      }
   1.120 +	  
   1.121 +      bytes_read = 0;
   1.122  
   1.123        io_status = g_io_channel_read_chars( io_channel, data + recv, 
   1.124  	  		buf_len, &bytes_read, &error );
   1.125 @@ -832,55 +841,106 @@
   1.126  	 */
   1.127        if ( bytes_read > 0 )
   1.128        {
   1.129 -	if ( bytes_read <= buf_len )
   1.130 -	  recv += bytes_read;
   1.131 -      } 
   1.132 +	//if ( bytes_read <= buf_len )
   1.133 +		  recv += bytes_read;
   1.134 +		  count_bytes += bytes_read;
   1.135 +		  remaining -= bytes_read;
   1.136 +		  g_print( "[%s] Reading buffer (bytes read = %d, remaining = %d)\n", __FUNCTION__, bytes_read, remaining );
   1.137 +		  if ( remaining == 0 ) {
   1.138 +		  	break;
   1.139 +		  }
   1.140 +      } else {
   1.141 +      	break;
   1.142 +      }
   1.143 +      
   1.144 +      //if ( remaining > 0 ) {
   1.145  
   1.146        if ( io_status == G_IO_STATUS_EOF ) {
   1.147 -	g_printerr( "[%s] got EOS!", __FUNCTION__ );
   1.148 +	g_print( "[%s] got EOS!", __FUNCTION__ );
   1.149  	break;
   1.150        } else if ( io_status == G_IO_STATUS_ERROR ) {
   1.151 -	g_printerr( "[%s] myth_file_transfer_read(): socket error.\n", __FUNCTION__ );
   1.152 +	g_print( "[%s] myth_file_transfer_read(): socket error.\n", __FUNCTION__ );
   1.153  	break;
   1.154        }
   1.155 +      //}
   1.156  
   1.157        /* increase buffer size, to allow get more data (do not obey to the buffer size) */
   1.158        if ( read_unlimited == TRUE ) {
   1.159 +      	// FOR NOW, DO NOTHING!!!
   1.160  	//if ( recv > buf_len )
   1.161  	// sent += (bytes_read - buf_len) + 1;
   1.162        }
   1.163 -      if ( bytes_read == buf_len )
   1.164 -	break;
   1.165  
   1.166        /* verify if the input (read) buffer is ready to receive data */
   1.167        io_cond = g_io_channel_get_buffer_condition( io_channel );
   1.168  
   1.169        g_print( "[%s]\t io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, 
   1.170        		( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
   1.171 +      		
   1.172 +      //if ( recv == size )
   1.173 +		//break;
   1.174  
   1.175 -    } while ( recv < sent && ( ( io_cond & G_IO_IN ) != 0 ) && ( io_status == G_IO_STATUS_NORMAL ) );
   1.176 +    } while ( remaining > 0 );//&& ( io_status == G_IO_STATUS_NORMAL ) );
   1.177 +    
   1.178 +   // if ( ( recv < size ) ) {
   1.179 +    //	finish_read = FALSE;
   1.180 +    //}
   1.181  
   1.182      io_cond_control = g_io_channel_get_buffer_condition( io_channel_control );
   1.183 -    if ( ( io_status == G_IO_STATUS_EOF ) || ( ( io_cond_control & G_IO_IN ) != 0 ) )
   1.184 +    if ( remaining == 0 )//( io_cond_control & G_IO_IN ) != 0  )
   1.185      {
   1.186        gmyth_socket_read_stringlist( transfer->control_sock, strlist );
   1.187 -      sent = gmyth_string_list_get_int( strlist,  0 ); // -1 on backend error      
   1.188 -      g_print( "[%s]\t sent = %d, io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, 
   1.189 -	  		sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
   1.190 -      response = TRUE;
   1.191 -      break;
   1.192 -    }
   1.193 -  }
   1.194 +      if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 ) 
   1.195 +      {
   1.196 +      	sent = gmyth_string_list_get_int( strlist,  0 ); // -1 on backend error
   1.197 +      	g_print( "[%s] got SENT buffer message = %d\n", __FUNCTION__, sent );
   1.198 +      	if ( sent != 0 )
   1.199 +      	{
   1.200 +      		g_print( "[%s]\t received = %d bytes, backend says %d bytes sent, "\
   1.201 +      			"io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, 
   1.202 +	  			recv, sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
   1.203  
   1.204 -  if ( ( ( error == NULL ) && ( response == FALSE ) ) || 
   1.205 -  		( io_status == G_IO_STATUS_EOF ) || ( ( io_cond & G_IO_IN ) == 0 ) )
   1.206 +	  		if ( sent == count_bytes )
   1.207 +	  		{
   1.208 +	  			response = ( recv == size ); 
   1.209 +	  			g_print( "[%s]\t\tsent %d, which is equals to bytes_read = %d\n\n", 
   1.210 +	  						__FUNCTION__, sent, count_bytes );
   1.211 +	  			if ( response == TRUE )
   1.212 +      				break;
   1.213 +	  		}
   1.214 +	  		else
   1.215 +	  		{
   1.216 +	  			g_print( "[%s]\t\tsent %d, which is NOT equals to bytes_read = %d\n\n", 
   1.217 +	  						__FUNCTION__, sent, count_bytes );
   1.218 +	  			goto cleanup;
   1.219 +	  			//response = FALSE;
   1.220 +	  			//break;
   1.221 +	  		}
   1.222 +      	} else {
   1.223 +      		break;
   1.224 +      		//goto cleanup;
   1.225 +      	} // if
   1.226 +      } // if - reading control response from backend
   1.227 +    } else {
   1.228 +    	response = FALSE;
   1.229 +    } // if - stringlist response
   1.230 +    
   1.231 +  } // while
   1.232 +  
   1.233 +  io_cond_control = g_io_channel_get_buffer_condition( io_channel_control );
   1.234 + // io_cond = g_io_channel_get_buffer_condition( io_channel );
   1.235 +
   1.236 +  if ( ( ( io_cond_control & G_IO_IN ) != 0 ) &&  		 
   1.237 +  		( response || ( recv == size ) ) )
   1.238    {
   1.239      if ( gmyth_socket_read_stringlist( transfer->control_sock, strlist ) > 0 )
   1.240      {
   1.241 -      if ( strlist != NULL && gmyth_string_list_length(strlist) > 0 ) {
   1.242 -	sent = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error
   1.243 -	g_print( "[%s]\t sent = %d, raw buffer I/O socket %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, 
   1.244 -	    sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
   1.245 +      if ( strlist != NULL && gmyth_string_list_length(strlist) > 0 ) 
   1.246 +      {
   1.247 +		sent = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error
   1.248 +		g_print( "[%s]\t received = %d bytes -\tNOW returning from reading buffer I/O socket "\
   1.249 +				 "[%s prepared for reading]! (G_IO_IN) !!!\n\n", __FUNCTION__, 
   1.250 +	    	sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
   1.251        }
   1.252      }
   1.253      else
   1.254 @@ -889,13 +949,14 @@
   1.255        recv = -1;
   1.256      }
   1.257      
   1.258 -  } else if ( error!=NULL ) {
   1.259 +  } 
   1.260 +  else if ( error != NULL )
   1.261 +  {
   1.262        g_printerr( "[%s] Error occurred: (%d, %s)\n", __FUNCTION__, error->code, error->message );
   1.263 -      //g_error_free( error );
   1.264 -    }
   1.265 +  }
   1.266  
   1.267  cleanup:
   1.268 -  g_static_mutex_unlock (&mutex);
   1.269 +  //g_static_mutex_unlock (&mutex);
   1.270  
   1.271    if ( trash != NULL )
   1.272      g_free( trash );
   1.273 @@ -907,8 +968,8 @@
   1.274        "(rcvd and rept MUST be the same!)\n", size, 
   1.275        recv, sent );
   1.276  
   1.277 -  //if ( sent != recv ) {
   1.278 -  //  recv = -1;
   1.279 +  //if ( ( recv != size ) || ( sent != size ) ) {
   1.280 +    //recv = size;
   1.281    //}
   1.282  
   1.283    if ( error != NULL ) {