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;
17 gboolean unix_socket = FALSE;
19 static GOptionEntry cmd_entries[] = {
20 {"cancel", 'c', 0, G_OPTION_ARG_INT, &cancel_timeout,
21 "Cancel any op after the specified amount of seconds", NULL},
22 {"udp", 'u', 0, G_OPTION_ARG_NONE, &use_udp,
23 "Use udp instead of tcp", NULL},
24 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
26 {"non-blocking", 'n', 0, G_OPTION_ARG_NONE, &non_blocking,
27 "Enable non-blocking i/o", NULL},
28 {"use-source", 's', 0, G_OPTION_ARG_NONE, &use_source,
29 "Use GSource to wait for non-blocking i/o", NULL},
31 {"unix", 'U', 0, G_OPTION_ARG_NONE, &unix_socket,
32 "Use a unix socket instead of IP", NULL},
38 source_ready (gpointer data,
39 GIOCondition condition)
41 g_main_loop_quit (loop);
46 ensure_condition (GSocket *socket,
48 GCancellable *cancellable,
49 GIOCondition condition)
59 source = g_socket_create_source (socket,
62 g_source_set_callback (source,
63 (GSourceFunc) source_ready,
65 g_source_attach (source, NULL);
66 g_source_unref (source);
67 g_main_loop_run (loop);
71 if (!g_socket_condition_wait (socket, condition, cancellable, &error))
73 g_printerr ("condition wait error for %s: %s\n",
82 cancel_thread (gpointer data)
84 GCancellable *cancellable = data;
86 g_usleep (1000*1000*cancel_timeout);
87 g_print ("Cancelling\n");
88 g_cancellable_cancel (cancellable);
97 GSocketAddress *src_address;
98 GSocketAddress *address;
99 GSocketType socket_type;
100 GSocketFamily socket_family;
101 GError *error = NULL;
102 GOptionContext *context;
103 GCancellable *cancellable;
104 GSocketAddressEnumerator *enumerator;
105 GSocketConnectable *connectable;
107 g_thread_init (NULL);
111 context = g_option_context_new (" <hostname>[:port] - Test GSocket client stuff");
112 g_option_context_add_main_entries (context, cmd_entries, NULL);
113 if (!g_option_context_parse (context, &argc, &argv, &error))
115 g_printerr ("%s: %s\n", argv[0], error->message);
121 g_printerr ("%s: %s\n", argv[0], "Need to specify hostname / unix socket name");
127 cancellable = g_cancellable_new ();
128 g_thread_create (cancel_thread, cancellable, FALSE, NULL);
135 loop = g_main_loop_new (NULL, FALSE);
138 socket_type = G_SOCKET_TYPE_DATAGRAM;
140 socket_type = G_SOCKET_TYPE_STREAM;
143 socket_family = G_SOCKET_FAMILY_UNIX;
145 socket_family = G_SOCKET_FAMILY_IPV4;
147 socket = g_socket_new (socket_family, socket_type, 0, &error);
150 g_printerr ("%s: %s\n", argv[0], error->message);
156 GSocketAddress *addr;
158 addr = socket_address_from_string (argv[1]);
161 g_printerr ("%s: Could not parse '%s' as unix socket name\n", argv[0], argv[1]);
164 connectable = G_SOCKET_CONNECTABLE (addr);
168 connectable = g_network_address_parse (argv[1], 7777, &error);
169 if (connectable == NULL)
171 g_printerr ("%s: %s\n", argv[0], error->message);
176 enumerator = g_socket_connectable_enumerate (connectable);
179 address = g_socket_address_enumerator_next (enumerator, cancellable, &error);
183 g_printerr ("%s: No more addresses to try\n", argv[0]);
185 g_printerr ("%s: %s\n", argv[0], error->message);
189 if (g_socket_connect (socket, address, cancellable, &error))
191 g_printerr ("%s: Connection to %s failed: %s, trying next\n", argv[0], socket_address_to_string (address), error->message);
192 g_error_free (error);
195 g_object_unref (address);
197 g_object_unref (enumerator);
198 g_object_unref (connectable);
200 g_print ("Connected to %s\n",
201 socket_address_to_string (address));
203 /* TODO: Test non-blocking connect */
205 g_socket_set_blocking (socket, FALSE);
207 src_address = g_socket_get_local_address (socket, &error);
210 g_printerr ("Error getting local address: %s\n",
214 g_print ("local address: %s\n",
215 socket_address_to_string (src_address));
216 g_object_unref (src_address);
220 gchar buffer[4096] = { };
224 if (fgets (buffer, sizeof buffer, stdin) == NULL)
227 to_send = strlen (buffer);
230 ensure_condition (socket, "send", cancellable, G_IO_OUT);
232 size = g_socket_send_to (socket, address,
234 cancellable, &error);
236 size = g_socket_send (socket, buffer, to_send,
237 cancellable, &error);
241 if (g_error_matches (error,
243 G_IO_ERROR_WOULD_BLOCK))
245 g_print ("socket send would block, handling\n");
246 g_error_free (error);
252 g_printerr ("Error sending to socket: %s\n",
258 g_print ("sent %" G_GSSIZE_FORMAT " bytes of data\n", size);
262 g_printerr ("Unexpected short write\n");
269 ensure_condition (socket, "receive", cancellable, G_IO_IN);
271 size = g_socket_receive_from (socket, &src_address,
272 buffer, sizeof buffer,
273 cancellable, &error);
275 size = g_socket_receive (socket, buffer, sizeof buffer,
276 cancellable, &error);
280 g_printerr ("Error receiving from socket: %s\n",
288 g_print ("received %" G_GSSIZE_FORMAT " bytes of data", size);
290 g_print (" from %s", socket_address_to_string (src_address));
294 g_print ("-------------------------\n"
296 "-------------------------\n",
301 g_print ("closing socket\n");
303 if (!g_socket_close (socket, &error))
305 g_printerr ("Error closing master socket: %s\n",
310 g_object_unref (G_OBJECT (socket));
311 g_object_unref (G_OBJECT (address));