2 #include <gio/gunixsocketaddress.h>
7 #include "socket-common.c"
12 gboolean verbose = FALSE;
13 gboolean dont_reuse_address = FALSE;
14 gboolean non_blocking = FALSE;
15 gboolean use_udp = FALSE;
16 gboolean use_source = FALSE;
17 int cancel_timeout = 0;
18 gboolean unix_socket = FALSE;
20 static GOptionEntry cmd_entries[] = {
21 {"port", 'p', 0, G_OPTION_ARG_INT, &port,
22 "Local port to bind to", NULL},
23 {"cancel", 'c', 0, G_OPTION_ARG_INT, &cancel_timeout,
24 "Cancel any op after the specified amount of seconds", NULL},
25 {"udp", 'u', 0, G_OPTION_ARG_NONE, &use_udp,
26 "Use udp instead of tcp", NULL},
27 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
29 {"no-reuse", 0, 0, G_OPTION_ARG_NONE, &dont_reuse_address,
30 "Don't SOADDRREUSE", NULL},
31 {"non-blocking", 'n', 0, G_OPTION_ARG_NONE, &non_blocking,
32 "Enable non-blocking i/o", NULL},
33 {"use-source", 's', 0, G_OPTION_ARG_NONE, &use_source,
34 "Use GSource to wait for non-blocking i/o", NULL},
36 {"unix", 'U', 0, G_OPTION_ARG_NONE, &unix_socket,
37 "Use a unix socket instead of IP", NULL},
43 source_ready (gpointer data,
44 GIOCondition condition)
46 g_main_loop_quit (loop);
51 ensure_condition (GSocket *socket,
53 GCancellable *cancellable,
54 GIOCondition condition)
64 source = g_socket_create_source (socket,
67 g_source_set_callback (source,
68 (GSourceFunc) source_ready,
70 g_source_attach (source, NULL);
71 g_source_unref (source);
72 g_main_loop_run (loop);
76 if (!g_socket_condition_wait (socket, condition, cancellable, &error))
78 g_printerr ("condition wait error for %s: %s\n",
87 cancel_thread (gpointer data)
89 GCancellable *cancellable = data;
91 g_usleep (1000*1000*cancel_timeout);
92 g_print ("Cancelling\n");
93 g_cancellable_cancel (cancellable);
101 GSocket *socket, *new_socket, *recv_socket;
102 GSocketAddress *src_address;
103 GSocketAddress *address;
104 GSocketType socket_type;
105 GSocketFamily socket_family;
106 GError *error = NULL;
107 GOptionContext *context;
108 GCancellable *cancellable;
111 g_thread_init (NULL);
115 context = g_option_context_new (" - Test GSocket server stuff");
116 g_option_context_add_main_entries (context, cmd_entries, NULL);
117 if (!g_option_context_parse (context, &argc, &argv, &error))
119 g_printerr ("%s: %s\n", argv[0], error->message);
123 if (unix_socket && argc != 2)
125 g_printerr ("%s: %s\n", argv[0], "Need to specify unix socket name");
131 cancellable = g_cancellable_new ();
132 g_thread_create (cancel_thread, cancellable, FALSE, NULL);
139 loop = g_main_loop_new (NULL, FALSE);
142 socket_type = G_SOCKET_TYPE_DATAGRAM;
144 socket_type = G_SOCKET_TYPE_STREAM;
147 socket_family = G_SOCKET_FAMILY_UNIX;
149 socket_family = G_SOCKET_FAMILY_IPV4;
151 socket = g_socket_new (socket_family, socket_type, 0, &error);
155 g_printerr ("%s: %s\n", argv[0], error->message);
160 g_socket_set_blocking (socket, FALSE);
164 src_address = socket_address_from_string (argv[1]);
165 if (src_address == NULL)
167 g_printerr ("%s: Could not parse '%s' as unix socket name\n", argv[0], argv[1]);
173 src_address = g_inet_socket_address_new (g_inet_address_new_any (G_SOCKET_FAMILY_IPV4), port);
176 if (!g_socket_bind (socket, src_address, !dont_reuse_address, &error))
178 g_printerr ("Can't bind socket: %s\n", error->message);
181 g_object_unref (src_address);
185 if (!g_socket_listen (socket, &error))
187 g_printerr ("Can't listen on socket: %s\n", error->message);
191 address = g_socket_get_local_address (socket, &error);
194 g_printerr ("Error getting local address: %s\n",
198 display_addr = socket_address_to_string (address);
199 g_print ("listening on %s...\n", display_addr);
200 g_free (display_addr);
202 ensure_condition (socket, "accept", cancellable, G_IO_IN);
203 new_socket = g_socket_accept (socket, cancellable, &error);
206 g_printerr ("Error accepting socket: %s\n",
212 g_socket_set_blocking (new_socket, FALSE);
214 address = g_socket_get_remote_address (new_socket, &error);
217 g_printerr ("Error getting remote address: %s\n",
222 display_addr = socket_address_to_string (address);
223 g_print ("got a new connection from %s\n", display_addr);
224 g_free(display_addr);
225 g_object_unref (address);
227 recv_socket = new_socket;
231 recv_socket = socket;
238 gchar buffer[4096] = { };
242 ensure_condition (recv_socket, "receive", cancellable, G_IO_IN);
244 size = g_socket_receive_from (recv_socket, &address,
245 buffer, sizeof buffer,
246 cancellable, &error);
248 size = g_socket_receive (recv_socket, buffer, sizeof buffer,
249 cancellable, &error);
253 g_printerr ("Error receiving from socket: %s\n",
261 g_print ("received %" G_GSSIZE_FORMAT " bytes of data", size);
263 g_print (" from %s", socket_address_to_string (address));
267 g_print ("-------------------------\n"
269 "-------------------------\n",
276 ensure_condition (recv_socket, "send", cancellable, G_IO_OUT);
278 size = g_socket_send_to (recv_socket, address,
279 buffer, to_send, cancellable, &error);
281 size = g_socket_send (recv_socket, buffer, to_send,
282 cancellable, &error);
286 if (g_error_matches (error,
288 G_IO_ERROR_WOULD_BLOCK))
290 g_print ("socket send would block, handling\n");
291 g_error_free (error);
297 g_printerr ("Error sending to socket: %s\n",
303 g_print ("sent %" G_GSSIZE_FORMAT " bytes of data\n", size);
307 g_printerr ("Unexpected short write\n");
315 g_print ("connection closed\n");
319 if (!g_socket_close (new_socket, &error))
321 g_printerr ("Error closing connection socket: %s\n",
326 g_object_unref (G_OBJECT (new_socket));
329 if (!g_socket_close (socket, &error))
331 g_printerr ("Error closing master socket: %s\n",
336 g_object_unref (G_OBJECT (socket));