# HG changeset patch
# User renatofilho
# Date 1165415530 0
# Node ID f70332afb8d0a47ac16a40a240cce1e581411b67
# Parent  01879d8f56dbb88b480b99d93edfa88ea757f64f
[svn r207] code cleanup

diff -r 01879d8f56db -r f70332afb8d0 gmyth/src/gmyth_file_transfer.c
--- a/gmyth/src/gmyth_file_transfer.c	Wed Dec 06 14:25:19 2006 +0000
+++ b/gmyth/src/gmyth_file_transfer.c	Wed Dec 06 14:32:10 2006 +0000
@@ -581,7 +581,6 @@
 guint64
 gmyth_file_transfer_get_filesize (GMythFileTransfer *transfer)
 {
-    assert (transfer);
-    
+    g_return_val_if_fail (transfer != NULL, 0);
     return transfer->filesize;
 }
diff -r 01879d8f56db -r f70332afb8d0 gmyth/src/gmyth_socket.c
--- a/gmyth/src/gmyth_socket.c	Wed Dec 06 14:25:19 2006 +0000
+++ b/gmyth/src/gmyth_socket.c	Wed Dec 06 14:32:10 2006 +0000
@@ -108,7 +108,7 @@
  * @return gint that represents the error number from getaddrinfo(). 
  */
 static gint
-gmyth_socket_toaddrinfo (gchar *addr, gint port, struct addrinfo **addrInfo )
+gmyth_socket_toaddrinfo (const gchar *addr, gint port, struct addrinfo **addrInfo )
 {
     struct addrinfo *hints;
     gchar *portStr = NULL;
@@ -467,7 +467,7 @@
 	
 	  /* Get the return code from the connect */
 	  len = sizeof( ret );
-	  *err=getsockopt( fd, SOL_SOCKET, SO_ERROR, &ret, &len );
+	  *err=getsockopt( fd, SOL_SOCKET, SO_ERROR, &ret, (socklen_t *) &len);
 	  
 	  if( *err < 0 ) {
 	    g_warning( "[%s] Connection usnsucessfull.\n", __FUNCTION__ );
@@ -504,16 +504,20 @@
     if ( hostname == NULL )
 		g_printerr ( "[%s] Invalid hostname parameter!\n", __FUNCTION__ );
 
-    errno = gmyth_socket_toaddrinfo( hostname, port, &addr_info_data );
+    errno = gmyth_socket_toaddrinfo ( hostname, port, &addr_info_data );
 
     g_return_val_if_fail( addr_info_data != NULL, FALSE );
 
     /* store hostname and port number */
+    if (gmyth_socket->hostname != NULL) {
+        g_free (gmyth_socket->hostname);
+        gmyth_socket->hostname = NULL;
+    }
+
     gmyth_socket->hostname = g_strdup( hostname );
     gmyth_socket->port = port;
 
     for ( addr_info0 = addr_info_data; addr_info0; addr_info0 = addr_info_data->ai_next ) {
-        struct sockaddr_in *sa = (struct sockaddr_in*)addr_info0->ai_addr;
         /* init socket descriptor */
         gmyth_socket->sd = socket( addr_info0->ai_family, addr_info0->ai_socktype,
                          addr_info0->ai_protocol );
@@ -521,25 +525,22 @@
         if ( gmyth_socket->sd < 0 )
             continue;
 
-        gmyth_debug( "[%s] hostname = %s, sock_fd = %d, addr = %s, addr_len = %d, "\
-            "ai_family = %d, ai_protocol = %d\n",
-            __FUNCTION__, hostname, gmyth_socket->sd, inet_ntoa( sa->sin_addr ),
-            addr_info0->ai_addrlen, addr_info0->ai_family, addr_info0->ai_protocol );
-            
-        struct timeval *timeout = g_new0( struct timeval, 1 );
+        struct timeval timeout;
         
         /* using 40 seconds timeout */
-        timeout->tv_sec = 40;
-        timeout->tv_usec = 0;
+        timeout.tv_sec = 40;
+        timeout.tv_usec = 0;
             
-        if ( gmyth_socket_try_connect( gmyth_socket->sd, (struct sockaddr *)addr_info0->ai_addr,
-                addr_info0->ai_addrlen, timeout, &ret_code ) < 0 )
+        if (gmyth_socket_try_connect (gmyth_socket->sd, (struct sockaddr *)addr_info0->ai_addr,
+                addr_info0->ai_addrlen, &timeout, &ret_code ) < 0 )
         {
             g_printerr( "[%s] Error connecting to backend!\n", __FUNCTION__ );
-            if ( ret_code == ETIMEDOUT )
-            g_printerr( "[%s]\tBackend host unreachable!\n", __FUNCTION__ );
+            if (ret_code == ETIMEDOUT)
+                g_printerr( "[%s]\tBackend host unreachable!\n", __FUNCTION__ );
 
-            g_printerr( "ERROR: %s\n", gai_strerror(ret_code) );
+            close (gmyth_socket->sd);
+            gmyth_socket->sd = -1;
+            g_printerr ("ERROR: %s\n", gai_strerror(ret_code));
             continue;
         }
 
@@ -549,7 +550,12 @@
     freeaddrinfo (addr_info_data);
     addr_info_data = NULL;
 
-    gmyth_socket->sd_io_ch = g_io_channel_unix_new( gmyth_socket->sd );
+    if (gmyth_socket->sd_io_ch != NULL) {
+        g_io_channel_unref (gmyth_socket->sd_io_ch);
+        gmyth_socket->sd_io_ch = NULL;
+    }
+
+    gmyth_socket->sd_io_ch = g_io_channel_unix_new (gmyth_socket->sd);
     
     //GIOFlags flags = g_io_channel_get_flags (gmyth_socket->sd_io_ch);
 		/* unset the nonblock flag */
@@ -559,9 +565,7 @@
 		//g_io_channel_set_flags (gmyth_socket->sd_io_ch, flags, NULL);
 
     ret = ( ret_code == 0 ) ? TRUE : FALSE ;
-
     return ret;
-
 }
 
 /** Gets the GIOChannel associated to the given GMythSocket.
@@ -633,6 +637,8 @@
     GIOStatus io_status = G_IO_STATUS_NORMAL;
     //GIOCondition io_cond;
     GError* error = NULL;
+   
+    
     gchar *buffer = NULL;
 
     gsize bytes_written = 0;
@@ -796,6 +802,12 @@
 gmyth_socket_close_connection (GMythSocket *gmyth_socket)
 {
     close (gmyth_socket->sd);	
+    gmyth_socket->sd = -1;
+
+    if (gmyth_socket->sd_io_ch != NULL) {
+        g_io_channel_unref (gmyth_socket->sd_io_ch);
+        gmyth_socket->sd_io_ch = NULL;
+    }
 }