Re: g_io_add_watch() callback called constantly



Thanks for the reply, this helps because I got the
(mis)impression from the docs that
g_io_channel_new_file() was the best cross platform
approach... but I didn't really realize it really was
just a plain "fopen()" file! I guess I thought is was
a "magic" file :-)

But it was a fun investigation -- my goofy little test
server/client is below, as requested. And it does sort
of work! Like tin cans and string...

But sockets do seem the right solution. Ironically, I
first thought to use them a month ago... but to avoid
maintaining separate platform code, I first tried
dbus-glib, then hacked a cross-compilable solution in
low-level dbus... which ran great in windows, but
because of my crude implementation, went haywire in
Linux with any call to gtk_file_chooser_dialog_new()!

So now I'm back to sockets. Thanks again.

--- Tor Lillqvist <tml iki fi> wrote:
I wrote:
What is it that you create the GIOChannel for?

Ah, you said it yourself, g_iochannel_new_file(), so
for a plain file
presumably. You mean that the processes take turns
appending their
"messages" to the file, and the other process then
reads it from the
file, and then appends its own "reply"?

I am not sure using watched GIOChannels connected to
plain files for
inter-process communication is necessarily something
that has been
tested at all, especially not on Windows. It sounds
like a quite, eh,
unique idea. I don't know if there even exists any
mechanism on
Windows that would enable the kind of select() or
poll() functionality
that presumably works fine also for plain files on
Unix. You might
have better luck using pipes or sockets like more or
less everybody
else.

--tml

/*
Client: 
Sends a struct with an ID and a double.
Compile:
gcc `pkg-config --libs --cflags gtk+-2.0` \
client.c -o GIOChannel-server
 */
#include <glib.h>
#include <gtk/gtk.h>

struct
{
 char ID;
 double Value;
} message;
const gssize message_size =
 sizeof (message);

int main (int argc, char **argv)
{
 char a;
 gsize written = 0;
 GError *error = NULL;
 gchar *filename =
  g_strconcat (g_get_tmp_dir (),
               G_DIR_SEPARATOR_S,
               "MyTempFile", NULL);
 GIOChannel *ioch =
  g_io_channel_new_file (filename, "a+",
                         &error);

 g_print ("filename:\%s\n", filename);
 g_free (filename);

 if (G_IO_STATUS_NORMAL ==
     g_io_channel_set_encoding (ioch,
                                NULL,
                                NULL))
  g_print
   ("Port initialization success\n");
 else
  g_print ("Not right\n");
 for (a = 'a'; a <= 'z'; a++)
 {
  message.ID = a;
  message.Value = a;
  g_io_channel_seek_position (ioch, 0,
                              G_SEEK_SET,
                              &error);
  g_io_channel_write_chars (ioch,
                            (gchar *) &
                            message,
                            message_size,
                            &written,
                            &error);
  g_io_channel_flush (ioch, &error);
  g_usleep (100000);
 }
 g_io_channel_flush (ioch, &error);
 g_io_channel_shutdown (ioch, TRUE,
                        &error);
 g_io_channel_unref (ioch);
 return 0;
}

//===============

/*
Server:
Recieves a struct with an ID and a double
Compile:
gcc `pkg-config --libs --cflags gtk+-2.0` \
server.c -o GIOChannel-server
 */
#include <gtk/gtk.h>

struct
{
 char ID;
 double Value;
} message;
const gssize message_size =
 sizeof (message);

gboolean io_callback (GIOChannel * ioch,
                      GIOCondition cond,
                      gpointer data)
{
 gsize bytes_read;
 static int count = 0;

 do
 {
  g_io_channel_read_chars (ioch,
                           (gchar *) &
                           message,
                           message_size,
                           &bytes_read,
                           NULL);
  if (message_size == bytes_read)
  {
   g_print ("\nID: %d, Value: %g",
            message.ID, message.Value);
  }
  else
   g_print (".");
 }
 while (bytes_read);
 g_usleep (100000);

 if (50 < ++count)
  gtk_main_quit ();
 return TRUE;
}

int main (int argc, char **argv)
{
 GError *error = NULL;
 gchar *filename =
  g_strconcat (g_get_tmp_dir (),
               G_DIR_SEPARATOR_S,
               "MyTempFile", NULL);
 GIOChannel *ioch =
  g_io_channel_new_file (filename, "w+",
                         &error);

 g_type_init ();
 g_print ("filename:\%s\n", filename);
 g_free (filename);

 if (G_IO_STATUS_NORMAL ==
     g_io_channel_set_encoding (ioch,
                                NULL,
                                NULL))
  g_print
   ("File initialization success\n");
 else
  g_print
   ("File initialization failure\n");
 guint iowatch =
  g_io_add_watch (ioch, G_IO_IN,
                  io_callback, NULL);

 if (0 == iowatch)
  g_error ("Error creating watch\n");
 gtk_main ();
 g_source_remove (iowatch);
 g_io_channel_shutdown (ioch, TRUE,
                        &error);
 g_io_channel_unref (ioch);
 return 0;
}




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