2 #include <gio/gunixsocketaddress.h>
8 #include "socket-common.c"
12 gboolean verbose = FALSE;
13 gboolean non_blocking = FALSE;
14 gboolean use_udp = FALSE;
15 gboolean use_source = FALSE;
16 int cancel_timeout = 0;
18 gboolean unix_socket = FALSE;
20 static GOptionEntry cmd_entries[] = {
21 {"cancel", 'c', 0, G_OPTION_ARG_INT, &cancel_timeout,
22 "Cancel any op after the specified amount of seconds", NULL},
23 {"udp", 'u', 0, G_OPTION_ARG_NONE, &use_udp,
24 "Use udp instead of tcp", NULL},
25 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
27 {"non-blocking", 'n', 0, G_OPTION_ARG_NONE, &non_blocking,
28 "Enable non-blocking i/o", NULL},
29 {"use-source", 's', 0, G_OPTION_ARG_NONE, &use_source,
30 "Use GSource to wait for non-blocking i/o", NULL},
32 {"unix", 'U', 0, G_OPTION_ARG_NONE, &unix_socket,
33 "Use a unix socket instead of IP", NULL},
35 {"timeout", 't', 0, G_OPTION_ARG_INT, &read_timeout,
36 "Time out reads after the specified number of seconds", NULL},
41 source_ready (gpointer data,
42 GIOCondition condition)
44 g_main_loop_quit (loop);
49 ensure_condition (GSocket *socket,
51 GCancellable *cancellable,
52 GIOCondition condition)
62 source = g_socket_create_source (socket,
65 g_source_set_callback (source,
66 (GSourceFunc) source_ready,
68 g_source_attach (source, NULL);
69 g_source_unref (source);
70 g_main_loop_run (loop);
74 if (!g_socket_condition_wait (socket, condition, cancellable, &error))
76 g_printerr ("condition wait error for %s: %s\n",
85 cancel_thread (gpointer data)
87 GCancellable *cancellable = data;
89 g_usleep (1000*1000*cancel_timeout);
90 g_print ("Cancelling\n");
91 g_cancellable_cancel (cancellable);
100 GSocketAddress *src_address;
101 GSocketAddress *address;
102 GSocketType socket_type;
103 GSocketFamily socket_family;
104 GError *error = NULL;
105 GOptionContext *context;
106 GCancellable *cancellable;
107 GSocketAddressEnumerator *enumerator;
108 GSocketConnectable *connectable;
110 g_thread_init (NULL);
114 context = g_option_context_new (" <hostname>[:port] - Test GSocket client stuff");
115 g_option_context_add_main_entries (context, cmd_entries, NULL);
116 if (!g_option_context_parse (context, &argc, &argv, &error))
118 g_printerr ("%s: %s\n", argv[0], error->message);
124 g_printerr ("%s: %s\n", argv[0], "Need to specify hostname / unix socket name");
130 cancellable = g_cancellable_new ();
131 g_thread_create (cancel_thread, cancellable, FALSE, NULL);
138 loop = g_main_loop_new (NULL, FALSE);
141 socket_type = G_SOCKET_TYPE_DATAGRAM;
143 socket_type = G_SOCKET_TYPE_STREAM;
146 socket_family = G_SOCKET_FAMILY_UNIX;
148 socket_family = G_SOCKET_FAMILY_IPV4;
150 socket = g_socket_new (socket_family, socket_type, 0, &error);
153 g_printerr ("%s: %s\n", argv[0], error->message);
158 g_socket_set_timeout (socket, read_timeout);
162 GSocketAddress *addr;
164 addr = socket_address_from_string (argv[1]);
167 g_printerr ("%s: Could not parse '%s' as unix socket name\n", argv[0], argv[1]);
170 connectable = G_SOCKET_CONNECTABLE (addr);
174 connectable = g_network_address_parse (argv[1], 7777, &error);
175 if (connectable == NULL)
177 g_printerr ("%s: %s\n", argv[0], error->message);
182 enumerator = g_socket_connectable_enumerate (connectable);
185 address = g_socket_address_enumerator_next (enumerator, cancellable, &error);
189 g_printerr ("%s: No more addresses to try\n", argv[0]);
191 g_printerr ("%s: %s\n", argv[0], error->message);
195 if (g_socket_connect (socket, address, cancellable, &error))
197 g_printerr ("%s: Connection to %s failed: %s, trying next\n", argv[0], socket_address_to_string (address), error->message);
198 g_error_free (error);
201 g_object_unref (address);
203 g_object_unref (enumerator);
204 g_object_unref (connectable);
206 g_print ("Connected to %s\n",
207 socket_address_to_string (address));
209 /* TODO: Test non-blocking connect */
211 g_socket_set_blocking (socket, FALSE);
213 src_address = g_socket_get_local_address (socket, &error);
216 g_printerr ("Error getting local address: %s\n",
220 g_print ("local address: %s\n",
221 socket_address_to_string (src_address));
222 g_object_unref (src_address);
230 if (fgets (buffer, sizeof buffer, stdin) == NULL)
233 to_send = strlen (buffer);
236 ensure_condition (socket, "send", cancellable, G_IO_OUT);
238 size = g_socket_send_to (socket, address,
240 cancellable, &error);
242 size = g_socket_send (socket, buffer, to_send,
243 cancellable, &error);
247 if (g_error_matches (error,
249 G_IO_ERROR_WOULD_BLOCK))
251 g_print ("socket send would block, handling\n");
252 g_error_free (error);
258 g_printerr ("Error sending to socket: %s\n",
264 g_print ("sent %" G_GSSIZE_FORMAT " bytes of data\n", size);
268 g_printerr ("Unexpected short write\n");
275 ensure_condition (socket, "receive", cancellable, G_IO_IN);
277 size = g_socket_receive_from (socket, &src_address,
278 buffer, sizeof buffer,
279 cancellable, &error);
281 size = g_socket_receive (socket, buffer, sizeof buffer,
282 cancellable, &error);
286 g_printerr ("Error receiving from socket: %s\n",
294 g_print ("received %" G_GSSIZE_FORMAT " bytes of data", size);
296 g_print (" from %s", socket_address_to_string (src_address));
300 g_print ("-------------------------\n"
302 "-------------------------\n",
307 g_print ("closing socket\n");
309 if (!g_socket_close (socket, &error))
311 g_printerr ("Error closing master socket: %s\n",
316 g_object_unref (G_OBJECT (socket));
317 g_object_unref (G_OBJECT (address));