Re: [gnet] Gnet-2.0.7 and win32



Hello again gnet list,

In the previous post I wrote about my problems with
gnet on win32. Now I finally found a path that works.

If you remember, last time I tried to solve the problem by
spawning a read_thread (with g_thread_create) which
called gnet_io_channel_readline_strdup() in a loop
to read incoming data.

It worked, but on win32 the CPU usage went up to 99%.

A much nicer way is to use g_io_add_watch() to register a
callback routine that is automatically called when data comes
in from the network. The problem is that I could not get
this to work on win32 with gnet_tcp_socket_connect_async()
(connection works but the callback is never called...).

After banging my head frenetically to the wall for two days
I found that if the connection is started with gnet_tcp_socket_new()
instead, then g_io_add_watch() works, even on win32. And CPU usage is
normal. Strange indeed.

In case someone else runs into the same problem, the working code:


  ---8<---

  inetaddr = gnet_inetaddr_new(hostname, port);
  g_return_val_if_fail(inetaddr != NULL, NULL);
  socket = gnet_tcp_socket_new(inetaddr);
  gnet_inetaddr_delete(inetaddr);
  g_return_val_if_fail(socket != NULL, NULL);
  channel = gnet_tcp_socket_get_io_channel(socket);
  ...
  g_io_add_watch(channel, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
                 async_client_sin_iofunc, NULL);

  --->8---


Then in the callback I use
gnet_io_channel_readline_strdup() to read lines.

The downside is that gnet_tcp_socket_new() blocks while connecting.
But it's worth it ;-)


Regards, Albert



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]