1 /* #included into both socket-client.c and socket-server.c */
3 static const char *unix_socket_address_types[] = {
12 socket_address_to_string (GSocketAddress *address)
16 if (G_IS_INET_SOCKET_ADDRESS (address))
18 GInetAddress *inet_address;
22 inet_address = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
23 str = g_inet_address_to_string (inet_address);
24 port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
25 res = g_strdup_printf ("%s:%d", str, port);
29 else if (G_IS_UNIX_SOCKET_ADDRESS (address))
31 GUnixSocketAddress *uaddr = G_UNIX_SOCKET_ADDRESS (address);
33 res = g_strdup_printf ("%s:%s",
34 unix_socket_address_types[g_unix_socket_address_get_address_type (uaddr)],
35 g_unix_socket_address_get_path (uaddr));
43 socket_address_from_string (const char *name)
48 for (i = 0; i < G_N_ELEMENTS (unix_socket_address_types); i++)
50 len = strlen (unix_socket_address_types[i]);
51 if (!strncmp (name, unix_socket_address_types[i], len) &&
54 return g_unix_socket_address_new_with_type (name + len + 1, -1,
55 (GUnixSocketAddressType)i);
63 source_ready (GPollableInputStream *stream,
66 g_main_loop_quit (loop);
71 ensure_socket_condition (GSocket *socket,
72 GIOCondition condition,
73 GCancellable *cancellable)
80 source = g_socket_create_source (socket, condition, cancellable);
81 g_source_set_callback (source,
82 (GSourceFunc) source_ready,
84 g_source_attach (source, NULL);
85 g_source_unref (source);
86 g_main_loop_run (loop);
90 ensure_connection_condition (GIOStream *stream,
91 GIOCondition condition,
92 GCancellable *cancellable)
99 if (condition & G_IO_IN)
100 source = g_pollable_input_stream_create_source (G_POLLABLE_INPUT_STREAM (g_io_stream_get_input_stream (stream)), cancellable);
102 source = g_pollable_output_stream_create_source (G_POLLABLE_OUTPUT_STREAM (g_io_stream_get_output_stream (stream)), cancellable);
104 g_source_set_callback (source,
105 (GSourceFunc) source_ready,
107 g_source_attach (source, NULL);
108 g_source_unref (source);
109 g_main_loop_run (loop);
113 cancel_thread (gpointer data)
115 GCancellable *cancellable = data;
117 g_usleep (1000*1000*cancel_timeout);
118 g_print ("Cancelling\n");
119 g_cancellable_cancel (cancellable);