8 gboolean verbose = FALSE;
9 gboolean dont_reuse_address = 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 {"port", 'p', 0, G_OPTION_ARG_INT, &port,
17 "Local port to bind to", NULL},
18 {"cancel", 'c', 0, G_OPTION_ARG_INT, &cancel_timeout,
19 "Cancel any op after the specified amount of seconds", NULL},
20 {"udp", 'u', 0, G_OPTION_ARG_NONE, &use_udp,
21 "Use udp instead of tcp", NULL},
22 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
24 {"no-reuse", 0, 0, G_OPTION_ARG_NONE, &dont_reuse_address,
25 "Don't SOADDRREUSE", NULL},
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},
34 socket_address_to_string (GSocketAddress *address)
36 GInetAddress *inet_address;
40 inet_address = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
41 str = g_inet_address_to_string (inet_address);
42 the_port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
43 res = g_strdup_printf ("%s:%d", str, the_port);
49 source_ready (gpointer data,
50 GIOCondition condition)
52 g_main_loop_quit (loop);
57 ensure_condition (GSocket *socket,
59 GCancellable *cancellable,
60 GIOCondition condition)
70 source = g_socket_create_source (socket,
73 g_source_set_callback (source,
74 (GSourceFunc) source_ready,
76 g_source_attach (source, NULL);
77 g_source_unref (source);
78 g_main_loop_run (loop);
82 if (!g_socket_condition_wait (socket, condition, cancellable, &error))
84 g_printerr ("condition wait error for %s: %s\n",
93 cancel_thread (gpointer data)
95 GCancellable *cancellable = data;
97 g_usleep (1000*1000*cancel_timeout);
98 g_print ("Cancelling\n");
99 g_cancellable_cancel (cancellable);
107 GSocket *socket, *new_socket, *recv_socket;
108 GSocketAddress *src_address;
109 GSocketAddress *address;
110 GSocketType socket_type;
111 GError *error = NULL;
112 GOptionContext *context;
113 GCancellable *cancellable;
115 g_thread_init (NULL);
119 context = g_option_context_new (" - Test GSocket server stuff");
120 g_option_context_add_main_entries (context, cmd_entries, NULL);
121 if (!g_option_context_parse (context, &argc, &argv, &error))
123 g_printerr ("%s: %s\n", argv[0], error->message);
129 cancellable = g_cancellable_new ();
130 g_thread_create (cancel_thread, cancellable, FALSE, NULL);
137 loop = g_main_loop_new (NULL, FALSE);
140 socket_type = G_SOCKET_TYPE_DATAGRAM;
142 socket_type = G_SOCKET_TYPE_STREAM;
144 socket = g_socket_new (G_SOCKET_FAMILY_IPV4, socket_type, 0, &error);
148 g_printerr ("%s: %s\n", argv[0], error->message);
153 g_socket_set_blocking (socket, FALSE);
155 src_address = g_inet_socket_address_new (g_inet_address_new_any (G_SOCKET_FAMILY_IPV4), port);
156 if (!g_socket_bind (socket, src_address, !dont_reuse_address, &error))
158 g_printerr ("Can't bind socket: %s\n", error->message);
161 g_object_unref (src_address);
165 if (!g_socket_listen (socket, &error))
167 g_printerr ("Can't listen on socket: %s\n", error->message);
171 g_print ("listening on port %d...\n", port);
173 ensure_condition (socket, "accept", cancellable, G_IO_IN);
174 new_socket = g_socket_accept (socket, cancellable, &error);
177 g_printerr ("Error accepting socket: %s\n",
183 g_socket_set_blocking (new_socket, FALSE);
185 address = g_socket_get_remote_address (new_socket, &error);
188 g_printerr ("Error getting remote address: %s\n",
193 g_print ("got a new connection from %s\n",
194 socket_address_to_string (address));
195 g_object_unref (address);
197 recv_socket = new_socket;
201 recv_socket = socket;
208 gchar buffer[4096] = { };
212 ensure_condition (recv_socket, "receive", cancellable, G_IO_IN);
214 size = g_socket_receive_from (recv_socket, &address,
215 buffer, sizeof buffer,
216 cancellable, &error);
218 size = g_socket_receive (recv_socket, buffer, sizeof buffer,
219 cancellable, &error);
223 g_printerr ("Error receiving from socket: %s\n",
231 g_print ("received %" G_GSSIZE_FORMAT " bytes of data", size);
233 g_print (" from %s", socket_address_to_string (address));
237 g_print ("-------------------------\n"
239 "-------------------------\n",
246 ensure_condition (recv_socket, "send", cancellable, G_IO_OUT);
248 size = g_socket_send_to (recv_socket, address,
249 buffer, to_send, cancellable, &error);
251 size = g_socket_send (recv_socket, buffer, to_send,
252 cancellable, &error);
256 if (g_error_matches (error,
258 G_IO_ERROR_WOULD_BLOCK))
260 g_print ("socket send would block, handling\n");
261 g_error_free (error);
267 g_printerr ("Error sending to socket: %s\n",
273 g_print ("sent %" G_GSSIZE_FORMAT " bytes of data\n", size);
277 g_printerr ("Unexpected short write\n");
285 g_print ("connection closed\n");
289 if (!g_socket_close (new_socket, &error))
291 g_printerr ("Error closing connection socket: %s\n",
296 g_object_unref (G_OBJECT (new_socket));
299 if (!g_socket_close (socket, &error))
301 g_printerr ("Error closing master socket: %s\n",
306 g_object_unref (G_OBJECT (socket));