From: Daniel Carbonell Fraj Date: Mon, 10 Feb 2003 13:42:41 +0000 (+0000) Subject: fix the GUI hangup while resolving the hostname X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=0d43c9135b378e8ab4c4c1b6801a75a06ca36789;p=gtetrinet fix the GUI hangup while resolving the hostname --- diff --git a/ChangeLog b/ChangeLog index aab712b..34cc194 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,22 @@ +2003-02-10 Dani Carbonell + + * src/Makefile.am: added -DGDK_DISABLE_DEPRECATED to the compilation + parameters. + + * src/client.c: fixed the connecting dialog behaviour, now GTetrinet + doesn't hang up while the hostname is resolved. + (client_process): this function now launches the thread that + resolvers the hostname, does gtk_iterations while the thread finishes, + and continues the normal connection protocol. + (client_connect): renamed to client_resolv_hostname, now it just + resolves the hostname, the rest of the functionality was moved to + client_process. + + * src/fields.c: removed two GDK obsolete function calls. + + * src/gtetrinet.c: start the GThread system, if it's not started + yet. + 2003-02-09 Jordi Mallach * configure.in: add debug flags if compiling from CVS. diff --git a/src/Makefile.am b/src/Makefile.am index 91f7184..9f70b39 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,7 +2,7 @@ SUBDIRS = images localedir = $(datadir)/locale -INCLUDES = -DLOCALEDIR=\"$(localedir)\" -DPIXMAPSDIR=\""$(datadir)/pixmaps"\" -DG_DISABLE_DEPRECATED -DGTK_DISABLE_DEPRECATED -DGNOME_DISABLE_DEPRECATED $(GTET_CFLAGS) +INCLUDES = -DLOCALEDIR=\"$(localedir)\" -DPIXMAPSDIR=\""$(datadir)/pixmaps"\" -DG_DISABLE_DEPRECATED -DGDK_DISABLE_DEPRECATED -DGTK_DISABLE_DEPRECATED -DGNOME_DISABLE_DEPRECATED $(GTET_CFLAGS) LDADD = $(GTET_LIBS) AM_CPPFLAGS = -DGTETRINET_DATA=\"$(pkgdatadir)\" diff --git a/src/client.c b/src/client.c index d30ac29..f79e346 100644 --- a/src/client.c +++ b/src/client.c @@ -51,6 +51,7 @@ char server[128]; static int sock; static GIOChannel *io_channel; static guint source; +static int resolved; /* structures and arrays for message translation */ @@ -140,7 +141,7 @@ struct outmsgt outmsgtable[] = { /* functions which set up the connection */ static void client_process (void); -static int client_connect (void); +static gpointer client_resolv_hostname (void); static void client_connected (void); /* some other useful functions */ @@ -158,15 +159,16 @@ void client_init (const char *s, const char *n) GTET_O_STRCPY(server, s); GTET_O_STRCPY(nick, n); + connectingdialog_new (); + /* wipe spaces off the nick */ for (i = 0; nick[i]; i ++) - if (isspace (nick[i])) nick[i] = 0; - - connectingdialog_new (); + if (isspace (nick[i])) + nick[i] = 0; /* set the game mode */ inmsg_change(); - + client_process (); } @@ -206,20 +208,82 @@ void client_inmessage (char *str) void client_process (void) { - errno = 0; - if (client_connect () == -1) { - char errmsg[1024]; + GString *s1 = g_string_sized_new(80); + GString *s2 = g_string_sized_new(80); + unsigned char ip[4]; + GString *iphashbuf = g_string_sized_new(11); + unsigned int i, len; + int l; + GThread *thread; + + errno = 0; + resolved = 0; + + thread = g_thread_create ((GThreadFunc) client_resolv_hostname, NULL, FALSE, NULL); + + /* wait until the hostname is resolved */ + while (resolved == 0) + { + if (gtk_events_pending ()) + gtk_main_iteration (); + } - GTET_O_STRCPY(errmsg, "noconnecting "); + if (resolved == -1) { + char errmsg[1024]; - if (errno) GTET_O_STRCAT(errmsg, strerror (errno)); - else if (h_errno) GTET_O_STRCAT(errmsg, _("Could not resolve host.")); + GTET_O_STRCPY(errmsg, "noconnecting "); - client_inmessage (errmsg); - } + if (errno) GTET_O_STRCAT(errmsg, strerror (errno)); + else if (h_errno) GTET_O_STRCAT(errmsg, _("Could not resolve host.")); + + client_inmessage (errmsg); + + return; + } + + /** + * Set up the g_io_channel + * We should set it with no encoding and no buffering, just to simplify things */ + io_channel = g_io_channel_unix_new (sock); + g_io_channel_set_encoding (io_channel, NULL, NULL); + g_io_channel_set_buffered (io_channel, FALSE); + source = g_io_add_watch (io_channel, G_IO_IN, (GIOFunc)io_channel_cb, NULL); + + /* construct message */ + if (gamemode == TETRIFAST) + g_string_printf (s1, "tetrifaster %s 1.13", nick); + else + g_string_printf (s1, "tetrisstart %s 1.13", nick); + + /* do that encoding thingy */ + server_ip (ip); + g_string_printf (iphashbuf, "%d", + ip[0]*54 + ip[1]*41 + ip[2]*29 + ip[3]*17); + l = iphashbuf->len; + + g_string_append_c(s2, 0); + for (i = 0; s1->str[i]; i ++) + g_string_append_c(s2, ((((s2->str[i] & 0xFF) + + (s1->str[i] & 0xFF)) % 255) ^ + iphashbuf->str[i % l])); + g_assert(s1->len == i); + g_assert(s2->len == (i + 1)); + len = i + 1; + + g_string_truncate(s1, 0); + for (i = 0; i < len; i ++) + g_string_append_printf(s1, "%02X", s2->str[i] & 0xFF); + + /* now send to server */ + client_sendmsg (s1->str); + + g_string_free(s1, TRUE); + g_string_free(s2, TRUE); + g_string_free(iphashbuf, TRUE); } -int client_connect (void) + +gpointer client_resolv_hostname (void) { #ifdef USE_IPV6 char hbuf[NI_MAXHOST]; @@ -241,7 +305,8 @@ int client_connect (void) if (getaddrinfo(server, service, &hints, &res0)) { /* set errno = 0 so that we know it's a getaddrinfo error */ errno = 0; - return -1; + resolved = -1; + g_thread_exit (GINT_TO_POINTER (-1)); } for (res = res0; res; res = res->ai_next) { sock = socket (res->ai_family, res->ai_socktype, res->ai_protocol); @@ -250,7 +315,8 @@ int client_connect (void) continue; else { freeaddrinfo(res0); - return -1; + resolved = -1; + g_thread_exit (GINT_TO_POINTER (-1)); } } getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf), NULL, 0, 0); @@ -261,7 +327,8 @@ int client_connect (void) } else { close(sock); freeaddrinfo(res0); - return -1; + resolved = -1; + g_thread_exit (GINT_TO_POINTER (-1)); } } break; @@ -272,7 +339,8 @@ int client_connect (void) if (h == 0) { /* set errno = 0 so that we know it's a gethostbyname error */ errno = 0; - return -1; + resolved = -1; + g_thread_exit (GINT_TO_POINTER (-1)); } memset (&sa, 0, sizeof (sa)); memcpy (&sa.sin_addr, h->h_addr, h->h_length); @@ -280,62 +348,18 @@ int client_connect (void) sa.sin_port = htons (spectating?SPECPORT:PORT); sock = socket (sa.sin_family, SOCK_STREAM, 0); - if (sock < 0) return -1; + if (sock < 0) + g_thread_exit (GINT_TO_POINTER (-1)); if (connect (sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) - return -1; -#endif - - /** - * Set up the g_io_channel - * We should set it with no encoding and no buffering, just to simplify things */ - io_channel = g_io_channel_unix_new (sock); - g_io_channel_set_encoding (io_channel, NULL, NULL); - g_io_channel_set_buffered (io_channel, FALSE); - source = g_io_add_watch (io_channel, G_IO_IN, (GIOFunc)io_channel_cb, NULL); - - /* say hello to the server */ { - GString *s1 = g_string_sized_new(80); - GString *s2 = g_string_sized_new(80); - unsigned char ip[4]; - GString *iphashbuf = g_string_sized_new(11); - unsigned int i, len; - int l; - - /* construct message */ - if (gamemode == TETRIFAST) - g_string_printf (s1, "tetrifaster %s 1.13", nick); - else - g_string_printf (s1, "tetrisstart %s 1.13", nick); - - /* do that encoding thingy */ - server_ip (ip); - g_string_printf (iphashbuf, "%d", - ip[0]*54 + ip[1]*41 + ip[2]*29 + ip[3]*17); - l = iphashbuf->len; - - g_string_append_c(s2, 0); - for (i = 0; s1->str[i]; i ++) - g_string_append_c(s2, ((((s2->str[i] & 0xFF) + - (s1->str[i] & 0xFF)) % 255) ^ - iphashbuf->str[i % l])); - g_assert(s1->len == i); - g_assert(s2->len == (i + 1)); - len = i + 1; - - g_string_truncate(s1, 0); - for (i = 0; i < len; i ++) - g_string_append_printf(s1, "%02X", s2->str[i] & 0xFF); - - /* now send to server */ - client_sendmsg (s1->str); - - g_string_free(s1, TRUE); - g_string_free(s2, TRUE); - g_string_free(iphashbuf, TRUE); + resolved = -1; + g_thread_exit (GINT_TO_POINTER (-1)); } - return 0; +#endif + + resolved = 1; + return (GINT_TO_POINTER (1)); } void client_connected (void) diff --git a/src/fields.c b/src/fields.c index 6517988..a3ae279 100644 --- a/src/fields.c +++ b/src/fields.c @@ -457,10 +457,10 @@ void fields_drawspecials (void) int i; for (i = 0; i < 18; i ++) { if (i < specialblocknum) { - gdk_draw_pixmap (specialwidget->window, - specialwidget->style->black_gc, - blockpix, (specialblocks[i]-1)*BLOCKSIZE, - 0, BLOCKSIZE*i, 0, BLOCKSIZE, BLOCKSIZE); + gdk_draw_drawable (specialwidget->window, + specialwidget->style->black_gc, + blockpix, (specialblocks[i]-1)*BLOCKSIZE, + 0, BLOCKSIZE*i, 0, BLOCKSIZE, BLOCKSIZE); } else { gdk_draw_rectangle (specialwidget->window, specialwidget->style->black_gc, @@ -485,12 +485,12 @@ void fields_drawnextblock (TETRISBLOCK block) for (y = ystart; y < 4; y ++) for (x = xstart; x < 4; x ++) { if (block[y][x]) { - gdk_draw_pixmap (nextpiecewidget->window, - nextpiecewidget->style->black_gc, - blockpix, (block[y][x]-1)*BLOCKSIZE, 0, - BLOCKSIZE*(x-xstart)+BLOCKSIZE/4, - BLOCKSIZE*(y-ystart)+BLOCKSIZE/4, - BLOCKSIZE, BLOCKSIZE); + gdk_draw_drawable (nextpiecewidget->window, + nextpiecewidget->style->black_gc, + blockpix, (block[y][x]-1)*BLOCKSIZE, 0, + BLOCKSIZE*(x-xstart)+BLOCKSIZE/4, + BLOCKSIZE*(y-ystart)+BLOCKSIZE/4, + BLOCKSIZE, BLOCKSIZE); } } memcpy (displayblock, block, 16); diff --git a/src/gtetrinet.c b/src/gtetrinet.c index 2cc3a6c..14ce08c 100644 --- a/src/gtetrinet.c +++ b/src/gtetrinet.c @@ -188,6 +188,7 @@ int main (int argc, char *argv[]) /* initialise some stuff */ fields_init (); + if (!g_thread_supported()) g_thread_init (NULL); /* first set up the display */