[gnio] Add support to test the socket source
- From: Alexander Larsson <alexl src gnome org>
- To: svn-commits-list gnome org
- Subject: [gnio] Add support to test the socket source
- Date: Tue, 5 May 2009 06:32:00 -0400 (EDT)
commit 8ad17e4f35a951e8aadfed558185153768db28eb
Author: Alexander Larsson <alexl redhat com>
Date: Tue May 5 12:29:15 2009 +0200
Add support to test the socket source
---
test/server.c | 72 ++++++++++++++++++++++++++++++++++++++------------------
1 files changed, 49 insertions(+), 23 deletions(-)
diff --git a/test/server.c b/test/server.c
index 28ddc37..a67dc9b 100644
--- a/test/server.c
+++ b/test/server.c
@@ -2,6 +2,7 @@
#include <gio/gsocket.h>
#include <glib.h>
#include <errno.h>
+#include <stdlib.h>
GMainLoop *loop;
@@ -10,6 +11,7 @@ gboolean verbose = FALSE;
gboolean dont_reuse_address = FALSE;
gboolean non_blocking = FALSE;
gboolean use_udp = FALSE;
+gboolean use_source = FALSE;
static GOptionEntry cmd_entries[] = {
{"port", 'p', 0, G_OPTION_ARG_INT, &port,
@@ -22,6 +24,8 @@ static GOptionEntry cmd_entries[] = {
"Don't SOADDRREUSE", NULL},
{"non-blocking", 'n', 0, G_OPTION_ARG_NONE, &non_blocking,
"Enable non-blocking i/o", NULL},
+ {"use-source", 's', 0, G_OPTION_ARG_NONE, &use_source,
+ "Use GSource to wait for non-blocking i/o", NULL},
{NULL}
};
@@ -40,6 +44,48 @@ socket_address_to_string (GSocketAddress *address)
return res;
}
+static gboolean
+source_ready (gpointer data,
+ GIOCondition condition)
+{
+ g_main_loop_quit (loop);
+ return FALSE;
+}
+
+static void
+ensure_condition (GSocket *socket,
+ const char *where,
+ GIOCondition condition)
+{
+ GError *error = NULL;
+ GSource *source;
+
+ if (!non_blocking)
+ return;
+
+ if (use_source)
+ {
+ source = g_socket_create_source (socket,
+ condition,
+ NULL);
+ g_source_set_callback (source,
+ (GSourceFunc) source_ready,
+ NULL, NULL);
+ g_source_attach (source, NULL);
+ g_main_loop_run (loop);
+ }
+ else
+ {
+ if (!g_socket_condition_wait (socket, condition, NULL, &error))
+ {
+ g_printerr ("condition wait error for %s: %s\n",
+ where,
+ error->message);
+ exit (1);
+ }
+ }
+}
+
int
main (int argc,
char *argv[])
@@ -101,13 +147,7 @@ main (int argc,
g_print ("listening on port %d...\n", port);
- if (non_blocking &&
- !g_socket_condition_wait (socket, G_IO_IN, NULL, &error))
- {
- g_printerr ("accepting condition wait error: %s\n",
- error->message);
- return 1;
- }
+ ensure_condition (socket, "accept", G_IO_IN);
new_socket = g_socket_accept (socket, &error);
if (!new_socket)
{
@@ -151,14 +191,7 @@ main (int argc,
v.buffer = buffer;
ov.buffer = buffer;
- if (non_blocking &&
- !g_socket_condition_wait (recv_socket, G_IO_IN, NULL, &error))
- {
- g_printerr ("receive condition wait error: %s\n",
- error->message);
- return 1;
- }
-
+ ensure_condition (recv_socket, "receive", G_IO_IN);
if (use_udp)
{
v.size = sizeof buffer;
@@ -195,14 +228,7 @@ main (int argc,
while (to_send > 0)
{
- if (non_blocking &&
- !g_socket_condition_wait (recv_socket, G_IO_OUT, NULL, &error))
- {
- g_printerr ("send condition wait error: %s\n",
- error->message);
- return 1;
- }
-
+ ensure_condition (recv_socket, "send", G_IO_OUT);
if (use_udp)
{
ov.size = to_send;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]