9 gboolean verbose = FALSE;
10 gboolean non_blocking = FALSE;
11 gboolean use_udp = FALSE;
12 gboolean use_source = FALSE;
13 int cancel_timeout = 0;
15 static GOptionEntry cmd_entries[] = {
16 {"cancel", 'c', 0, G_OPTION_ARG_INT, &cancel_timeout,
17 "Cancel any op after the specified amount of seconds", NULL},
18 {"udp", 'u', 0, G_OPTION_ARG_NONE, &use_udp,
19 "Use udp instead of tcp", NULL},
20 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
22 {"non-blocking", 'n', 0, G_OPTION_ARG_NONE, &non_blocking,
23 "Enable non-blocking i/o", NULL},
24 {"use-source", 's', 0, G_OPTION_ARG_NONE, &use_source,
25 "Use GSource to wait for non-blocking i/o", NULL},
30 socket_address_to_string (GSocketAddress *address)
32 GInetAddress *inet_address;
36 inet_address = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
37 str = g_inet_address_to_string (inet_address);
38 port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
39 res = g_strdup_printf ("%s:%d", str, port);
45 source_ready (gpointer data,
46 GIOCondition condition)
48 g_main_loop_quit (loop);
53 ensure_condition (GSocket *socket,
55 GCancellable *cancellable,
56 GIOCondition condition)
66 source = g_socket_create_source (socket,
69 g_source_set_callback (source,
70 (GSourceFunc) source_ready,
72 g_source_attach (source, NULL);
73 g_source_unref (source);
74 g_main_loop_run (loop);
78 if (!g_socket_condition_wait (socket, condition, cancellable, &error))
80 g_printerr ("condition wait error for %s: %s\n",
89 cancel_thread (gpointer data)
91 GCancellable *cancellable = data;
93 g_usleep (1000*1000*cancel_timeout);
94 g_print ("Cancelling\n");
95 g_cancellable_cancel (cancellable);
104 GSocketAddress *src_address;
105 GSocketAddress *address;
106 GSocketType socket_type;
107 GError *error = NULL;
108 GOptionContext *context;
109 GCancellable *cancellable;
110 GSocketAddressEnumerator *enumerator;
111 GSocketConnectable *connectable;
113 g_thread_init (NULL);
117 context = g_option_context_new (" <hostname>[:port] - Test GSocket client stuff");
118 g_option_context_add_main_entries (context, cmd_entries, NULL);
119 if (!g_option_context_parse (context, &argc, &argv, &error))
121 g_printerr ("%s: %s\n", argv[0], error->message);
127 g_printerr ("%s: %s\n", argv[0], "Need to specify hostname");
133 cancellable = g_cancellable_new ();
134 g_thread_create (cancel_thread, cancellable, FALSE, NULL);
141 loop = g_main_loop_new (NULL, FALSE);
144 socket_type = G_SOCKET_TYPE_DATAGRAM;
146 socket_type = G_SOCKET_TYPE_STREAM;
148 socket = g_socket_new (G_SOCKET_FAMILY_IPV4, socket_type, 0, &error);
151 g_printerr ("%s: %s\n", argv[0], error->message);
155 connectable = g_network_address_parse (argv[1], 7777, &error);
156 if (connectable == NULL)
158 g_printerr ("%s: %s\n", argv[0], error->message);
162 enumerator = g_socket_connectable_enumerate (connectable);
165 address = g_socket_address_enumerator_next (enumerator, cancellable, &error);
169 g_printerr ("%s: No more addresses to try\n", argv[0]);
171 g_printerr ("%s: %s\n", argv[0], error->message);
175 if (g_socket_connect (socket, address, cancellable, &error))
177 g_printerr ("%s: Connection to %s failed: %s, trying next\n", argv[0], socket_address_to_string (address), error->message);
178 g_error_free (error);
181 g_object_unref (address);
183 g_object_unref (enumerator);
184 g_object_unref (connectable);
186 g_print ("Connected to %s\n",
187 socket_address_to_string (address));
189 /* TODO: Test non-blocking connect */
191 g_socket_set_blocking (socket, FALSE);
193 src_address = g_socket_get_local_address (socket, &error);
196 g_printerr ("Error getting local address: %s\n",
200 g_print ("local address: %s\n",
201 socket_address_to_string (src_address));
202 g_object_unref (src_address);
206 gchar buffer[4096] = { };
210 if (fgets (buffer, sizeof buffer, stdin) == NULL)
213 to_send = strlen (buffer);
216 ensure_condition (socket, "send", cancellable, G_IO_OUT);
218 size = g_socket_send_to (socket, address,
220 cancellable, &error);
222 size = g_socket_send (socket, buffer, to_send,
223 cancellable, &error);
227 if (g_error_matches (error,
229 G_IO_ERROR_WOULD_BLOCK))
231 g_print ("socket send would block, handling\n");
232 g_error_free (error);
238 g_printerr ("Error sending to socket: %s\n",
244 g_print ("sent %" G_GSSIZE_FORMAT " bytes of data\n", size);
248 g_printerr ("Unexpected short write\n");
255 ensure_condition (socket, "receive", cancellable, G_IO_IN);
257 size = g_socket_receive_from (socket, &src_address,
258 buffer, sizeof buffer,
259 cancellable, &error);
261 size = g_socket_receive (socket, buffer, sizeof buffer,
262 cancellable, &error);
266 g_printerr ("Error receiving from socket: %s\n",
274 g_print ("received %" G_GSSIZE_FORMAT " bytes of data", size);
276 g_print (" from %s", socket_address_to_string (src_address));
280 g_print ("-------------------------\n"
282 "-------------------------\n",
287 g_print ("closing socket\n");
289 if (!g_socket_close (socket, &error))
291 g_printerr ("Error closing master socket: %s\n",
296 g_object_unref (G_OBJECT (socket));
297 g_object_unref (G_OBJECT (address));