GNOME Bugzilla – Bug 667735
Add function to wait for condition on a set of GSocket
Last modified: 2012-01-12 13:57:24 UTC
Hi, it'd be great if there's a function to wait for a condition on a set of GSocket instead of having this only for a single GSocket. This helps implementing servers that serve clients from a single thread for example. Attached is a patch that adds this, Windows part is untested though.
Created attachment 205062 [details] [review] Bug 667735 – Add function to wait for a condition on a set of GSocket Adds g_sockets_condition_wait() for waiting for a condition on a set of sockets. This is useful for implementing servers that serve multiple clients from a single thread. Closes: bgo#667735
Created attachment 205064 [details] [review] Bug 667735 – Add function to wait for a condition on a set of GSocket Adds g_sockets_condition_wait() for waiting for a condition on a set of sockets. This is useful for implementing servers that serve multiple clients from a single thread. Closes: bgo#667735
GMainContext?
What do you mean? There's no integration between a main context and GSocket
g_socket_create_source
Oops, sorry I missed that. But still, using a GMainContext in my code for this will be overly complex and ugly. Is there any reason not to add something simpler to poll a set of sockets? New patches comes soon that keeps the amount of code added much lower (g_socket_condition_wait() can be implemented with g_sockets_condition_wait()).
Created attachment 205084 [details] [review] Bug 667735 – Add function to wait for a condition on a set of GSocket Adds g_sockets_condition_wait() for waiting for a condition on a set of sockets. This is useful for implementing servers that serve multiple clients from a single thread. Closes: bgo#667735
Note that we need this in GStreamer to port some plugins from plain sockets to GIO.
Created attachment 205089 [details] [review] Bug 667735 – Add function to wait for a condition on a set of GSocket Adds g_sockets_condition_wait() for waiting for a condition on a set of sockets. This is useful for implementing servers that serve multiple clients from a single thread. Closes: bgo#667735
(In reply to comment #6) > Oops, sorry I missed that. But still, using a GMainContext in my code for this > will be overly complex and ugly. It's not *that* complex and ugly: context = g_main_context_new (); for (i = 0; i < nsockets; i++) { source = g_socket_create_source (sockets[i], condition, NULL); g_source_set_dummy_callback (source); g_source_attach (source, context); g_source_unref (source); } source = g_cancellable_source_new (cancellable); g_source_set_dummy_callback (source); g_source_attach (source, context); g_source_unref (source); g_main_context_iteration (context, TRUE); g_main_context_unref (context); Maybe that could be wrapped up in a "g_something_wait_for_any_source (source_list, cancellable, &error)" method somewhere... > Is there any reason not to add something > simpler to poll a set of sockets? Well, this functions solves your exact use case, but what if you need to wait for some of the sockets to be readable and others to be writable? Or if you need to wait on 3 sockets and 1 pipe?
And additionally another GSource for a timeout and detection if the iteration was finished because of a timeout or something else. But I guess you're right, my new function only covers very few use cases and I'll rewrite my code to use a GMainContext instead.