# HG changeset patch # User melunko # Date 1168468294 0 # Node ID 835d5cc01a0a473149451417de0d64b32a917c2c # Parent 86e15690a1048dd81b6bfa7faf2393337784d85b [svn r259] Added gmyth_socket connection timeout method diff -r 86e15690a104 -r 835d5cc01a0a gmyth/src/gmyth_socket.c --- a/gmyth/src/gmyth_socket.c Wed Jan 10 21:47:24 2007 +0000 +++ b/gmyth/src/gmyth_socket.c Wed Jan 10 22:31:34 2007 +0000 @@ -472,6 +472,7 @@ FD_ZERO( &fd_w ); FD_SET( fd, &fd_w ); + printf ("xxxxxxxxxxxx %d\n", timeout->tv_sec); *err = select( FD_SETSIZE, NULL, &fd_w, NULL, timeout); if ( *err < 0 ) { g_warning( "[%s] Connection unsucessfull (timed out).\n", __FUNCTION__ ); @@ -513,10 +514,20 @@ * @param port The backend port. * @return TRUE if success, FALSE if error. */ + + gboolean gmyth_socket_connect (GMythSocket *gmyth_socket, const gchar *hostname, gint port) { + return gmyth_socket_connect_with_timeout (gmyth_socket, + hostname, port, 0); +} + +gboolean +gmyth_socket_connect_with_timeout (GMythSocket *gmyth_socket, + const gchar *hostname, gint port, guint timeout) +{ struct addrinfo *addr_info_data = NULL, *addr_info0 = NULL; gint ret_code = -1; gint errno; @@ -548,14 +559,17 @@ if ( gmyth_socket->sd < 0 ) continue; - struct timeval timeout; + struct timeval *timeout_val = NULL; + if (timeout != 0) { + timeout_val = g_new0 (struct timeval, 1); + + timeout_val->tv_sec = timeout; + printf ("XXXXXXXXXXX setting timeout %d\n", timeout_val->tv_sec); + timeout_val->tv_usec = 0; + } - /* using 40 seconds timeout */ - 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 ) + addr_info0->ai_addrlen, timeout_val, &ret_code ) < 0 ) { g_printerr( "[%s] Error connecting to backend!\n", __FUNCTION__ ); if (ret_code == ETIMEDOUT) @@ -564,12 +578,16 @@ close (gmyth_socket->sd); gmyth_socket->sd = -1; g_printerr ("ERROR: %s\n", gai_strerror(ret_code)); + g_free (timeout_val); continue; } + g_free (timeout_val); + /* only will be reached if none of the error above occurred */ break; } + freeaddrinfo (addr_info_data); addr_info_data = NULL; diff -r 86e15690a104 -r 835d5cc01a0a gmyth/src/gmyth_socket.h --- a/gmyth/src/gmyth_socket.h Wed Jan 10 21:47:24 2007 +0000 +++ b/gmyth/src/gmyth_socket.h Wed Jan 10 22:31:34 2007 +0000 @@ -97,6 +97,9 @@ gboolean gmyth_socket_connect (GMythSocket *gmyth_socket, const gchar *hostname, gint port); +gboolean gmyth_socket_connect_with_timeout (GMythSocket *gmyth_socket, + const gchar *hostname, gint port, guint timeout); + gboolean gmyth_socket_connect_to_backend (GMythSocket *gmyth_socket, const gchar *hostname_backend, gint port, gboolean blocking_client); diff -r 86e15690a104 -r 835d5cc01a0a gmyth/tests/gmyth_test_connection.c --- a/gmyth/tests/gmyth_test_connection.c Wed Jan 10 21:47:24 2007 +0000 +++ b/gmyth/tests/gmyth_test_connection.c Wed Jan 10 22:31:34 2007 +0000 @@ -9,9 +9,9 @@ test_backend_connection1 (GMythBackendInfo *backend_info) { GMythSocket *socket = gmyth_socket_new (); - if (gmyth_socket_connect (socket, + if (gmyth_socket_connect_with_timeout (socket, gmyth_backend_info_get_hostname (backend_info), - gmyth_backend_info_get_port (backend_info)) == TRUE) { + gmyth_backend_info_get_port (backend_info), 4) == TRUE) { g_debug ("Connection success"); return TRUE; } else { @@ -47,8 +47,8 @@ backend_info = gmyth_backend_info_new_with_uri (argv[1]); + test_backend_connection1 (backend_info); test_mysql_connection1 (backend_info); - test_backend_connection1 (backend_info); }