Index: sockets.c =================================================================== RCS file: /cvs/gnome/pan/pan/sockets.c,v retrieving revision 1.113 retrieving revision 1.114 diff -u -u -r1.113 -r1.114 --- sockets.c 8 Apr 2003 20:13:33 -0000 1.113 +++ sockets.c 13 May 2003 14:35:34 -0000 1.114 @@ -69,8 +69,13 @@ guint bytes_read; guint bytes_written; time_t byte_count_start_time; + + int consecutive_failed_operations; }; + +#define MAX_CONSECUTIVE_FAILED_OPERATIONS 60 + /********************* ********************** Variables *********************/ @@ -126,6 +131,7 @@ sock->bytes_read = 0u; sock->bytes_written = 0u; sock->byte_count_start_time = time(NULL); + sock->consecutive_failed_operations = 0; sock->line_buffer = g_string_sized_new (512); sock->error = FALSE; sock->need_auth = FALSE; @@ -142,6 +148,7 @@ sock->gio_channel = gnet_tcp_socket_get_iochannel (sock->gnet_socket); if (g_io_channel_get_encoding (sock->gio_channel) != NULL) g_io_channel_set_encoding (sock->gio_channel, NULL, NULL); + g_io_channel_set_flags (sock->gio_channel, G_IO_FLAG_NONBLOCK, NULL); g_io_channel_set_buffered (sock->gio_channel, TRUE); g_io_channel_set_line_term (sock->gio_channel, "\n", 1); log_add_va (LOG_INFO, _("New connection %p for %s, port %d"), sock, server_address, port); @@ -219,11 +226,13 @@ status = g_io_channel_read_line_string (sock->gio_channel, sock->line_buffer, NULL, &err); switch (status) { case G_IO_STATUS_AGAIN: - if (err != NULL) - g_error_free (err); - g_usleep (G_USEC_PER_SEC); - continue; + if (++sock->consecutive_failed_operations <= MAX_CONSECUTIVE_FAILED_OPERATIONS ) { + g_usleep (G_USEC_PER_SEC); + continue; + } + /* fall through */ case G_IO_STATUS_ERROR: + /* fall through */ case G_IO_STATUS_EOF: if (err != NULL) { log_add_va (LOG_ERROR, _("Error reading from socket: %s"), err->message); @@ -232,6 +241,7 @@ pan_socket_set_error_flag (sock, TRUE); return -1; case G_IO_STATUS_NORMAL: + sock->consecutive_failed_operations = 0; done = TRUE; break; } @@ -278,15 +288,15 @@ switch (status) { case G_IO_STATUS_AGAIN: - if (err != NULL) - g_error_free (err); - g_usleep (G_USEC_PER_SEC); + if (++sock->consecutive_failed_operations <= MAX_CONSECUTIVE_FAILED_OPERATIONS) { + nleft -= bytes_written; + line += bytes_written; + g_usleep (G_USEC_PER_SEC); + continue; + } /* fall through */ - case G_IO_STATUS_NORMAL: - nleft -= bytes_written; - line += bytes_written; - continue; case G_IO_STATUS_EOF: + /* fall through */ case G_IO_STATUS_ERROR: if (err != NULL) { log_add_va (LOG_ERROR, _("Error writing to socket: %s"), err->message); @@ -294,6 +304,12 @@ } pan_socket_set_error_flag (sock, TRUE); return -1; + + case G_IO_STATUS_NORMAL: + nleft -= bytes_written; + line += bytes_written; + sock->consecutive_failed_operations = 0; + continue; } }