Re: [gtk-list] glib problem
- From: David Benson <daveb idealab com>
- To: gtk-list redhat com
- Subject: Re: [gtk-list] glib problem
- Date: Tue, 15 Feb 2000 07:13:05 -0800 (PST)
In order to *sometimes* read from a socket, I think you must use
g_source_add and g_main_add_poll. Very briefly, something like:
GPollFD poll_fd;
gboolean needs_input; /* indicates whether the fd should be
* read from. */
static gboolean prepare(gpointer source_data,
GTimeVal* curtime,
gint* timeout,
gpointer user_data)
{
if (needs_input)
poll_fd.events = G_IO_IN;
else
poll_fd.events = 0;
return FALSE;
}
static gboolean check(gpointer source_data,
GTimeVal* curtime,
gpointer user_data)
{
return poll_fd.revents == 0 ? FALSE : TRUE;
}
static gboolean dispatch(gpointer source_data,
GTimeVal* curtime,
gpointer user_data)
{
/* Do stuff... (you can look at poll_fd.revents for exactly
* which IO conditions apply) */
}
void init_poll_fd(int fd)
{
poll_fd.fd = fd;
g_main_add_poll(&poll_fd, G_PRIORITY_DEFAULT);
g_source_add(G_PRIORITY_DEFAULT, FALSE,
&source_funcs, NULL, NULL, NULL);
}
Of course, don't use globals in real code :)
- Dave
On Tue, 15 Feb 2000, Giaslas Georgios wrote:
> hi guys, i have the following problem:
>
> i want to create a socket and call a function whenever there is something
> to read, using glib. When GIOFunc is set to return FALSE, there is no
> problem with the code, but i don't want the event source to be removed.
> So, i set myfunc to return TRUE but when there is something to read,
> GIOFunc is called once and then i get a segmentation fault.
> Can you help me with this, or suggest something else?
>
> Here is a part of my code:
> after the creation of the socket i use the following code:
> --------------------------------------------------------
> myloop=g_main_new(FALSE);
> mychannel=g_io_channel_unix_new(sockfd);
> buff="something";
> g_io_channel_write(mychannel,buff,9,written);
> eventid=g_io_add_watch(mychannel,G_IO_IN,&myfunc,NULL);
> g_main_run(myloop);
> g_io_channel_close(mychannel);
> ---------------------------------------------------------
> gboolean myfunc(GIOChannel *source, GIOCondition condition, gpointer data)
> {
> gchar *buff;
> guint *readden;
>
> buff=g_malloc(512);
> if (g_io_channel_read(source,buff,100,readden)!=G_IO_ERROR_NONE) g_print("failed to read\n");
> g_print("read : %s\n",buff);
> if (buff=="something") g_main_quit(myloop);
> return TRUE;
> }
>
>
> Thanx in advance
>
> --
> To unsubscribe: mail -s unsubscribe gtk-list-request@redhat.com < /dev/null
>
>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]