Branch data Line data Source code
1 : : /* #included into both socket-testclient.c and socket-testserver.c */
2 : :
3 : : #ifdef G_OS_UNIX
4 : : static const char *unix_socket_address_types[] = {
5 : : "invalid",
6 : : "anonymous",
7 : : "path",
8 : : "abstract",
9 : : "padded"
10 : : };
11 : : #endif
12 : :
13 : : static char *
14 : 0 : socket_address_to_string (GSocketAddress *address)
15 : : {
16 : 0 : char *res = NULL;
17 : :
18 : 0 : if (G_IS_INET_SOCKET_ADDRESS (address))
19 : : {
20 : : GInetSocketAddress *socket_address;
21 : : GInetAddress *inet_address;
22 : : char *str;
23 : : int port;
24 : : guint32 scope_id;
25 : :
26 : 0 : socket_address = G_INET_SOCKET_ADDRESS (address);
27 : 0 : scope_id = g_inet_socket_address_get_scope_id (socket_address);
28 : 0 : inet_address = g_inet_socket_address_get_address (socket_address);
29 : 0 : str = g_inet_address_to_string (inet_address);
30 : 0 : port = g_inet_socket_address_get_port (socket_address);
31 : 0 : if (scope_id)
32 : 0 : res = g_strdup_printf ("[%s%%%u]:%d", str, scope_id, port);
33 : 0 : else if (g_inet_address_get_family (inet_address) == G_SOCKET_FAMILY_IPV6)
34 : 0 : res = g_strdup_printf ("[%s]:%d", str, port);
35 : : else
36 : 0 : res = g_strdup_printf ("%s:%d", str, port);
37 : 0 : g_free (str);
38 : : }
39 : : #ifdef G_OS_UNIX
40 : 0 : else if (G_IS_UNIX_SOCKET_ADDRESS (address))
41 : : {
42 : 0 : GUnixSocketAddress *uaddr = G_UNIX_SOCKET_ADDRESS (address);
43 : :
44 : 0 : res = g_strdup_printf ("%s:%s",
45 : 0 : unix_socket_address_types[g_unix_socket_address_get_address_type (uaddr)],
46 : : g_unix_socket_address_get_path (uaddr));
47 : : }
48 : : #endif
49 : :
50 : 0 : return res;
51 : : }
52 : :
53 : : static GSocketAddress *
54 : 0 : socket_address_from_string (const char *name)
55 : : {
56 : : #ifdef G_OS_UNIX
57 : : gsize i, len;
58 : :
59 : 0 : for (i = 0; i < G_N_ELEMENTS (unix_socket_address_types); i++)
60 : : {
61 : 0 : len = strlen (unix_socket_address_types[i]);
62 : 0 : if (!strncmp (name, unix_socket_address_types[i], len) &&
63 : 0 : name[len] == ':')
64 : : {
65 : 0 : return g_unix_socket_address_new_with_type (name + len + 1, -1,
66 : : (GUnixSocketAddressType)i);
67 : : }
68 : : }
69 : : #endif
70 : 0 : return NULL;
71 : : }
72 : :
73 : : static gboolean
74 : 0 : source_ready (GPollableInputStream *stream,
75 : : gpointer data)
76 : : {
77 : 0 : g_main_loop_quit (loop);
78 : 0 : return FALSE;
79 : : }
80 : :
81 : : static void
82 : 0 : ensure_socket_condition (GSocket *socket,
83 : : GIOCondition condition,
84 : : GCancellable *cancellable)
85 : : {
86 : : GSource *source;
87 : :
88 : 0 : if (!non_blocking)
89 : 0 : return;
90 : :
91 : 0 : source = g_socket_create_source (socket, condition, cancellable);
92 : 0 : g_source_set_callback (source,
93 : : (GSourceFunc) source_ready,
94 : : NULL, NULL);
95 : 0 : g_source_attach (source, NULL);
96 : 0 : g_source_unref (source);
97 : 0 : g_main_loop_run (loop);
98 : : }
99 : :
100 : : static void
101 : 0 : ensure_connection_condition (GIOStream *stream,
102 : : GIOCondition condition,
103 : : GCancellable *cancellable)
104 : : {
105 : : GSource *source;
106 : :
107 : 0 : if (!non_blocking)
108 : 0 : return;
109 : :
110 : 0 : if (condition & G_IO_IN)
111 : 0 : source = g_pollable_input_stream_create_source (G_POLLABLE_INPUT_STREAM (g_io_stream_get_input_stream (stream)), cancellable);
112 : : else
113 : 0 : source = g_pollable_output_stream_create_source (G_POLLABLE_OUTPUT_STREAM (g_io_stream_get_output_stream (stream)), cancellable);
114 : :
115 : 0 : g_source_set_callback (source,
116 : : (GSourceFunc) source_ready,
117 : : NULL, NULL);
118 : 0 : g_source_attach (source, NULL);
119 : 0 : g_source_unref (source);
120 : 0 : g_main_loop_run (loop);
121 : : }
122 : :
123 : : static gpointer
124 : 0 : cancel_thread (gpointer data)
125 : : {
126 : 0 : GCancellable *cancellable = data;
127 : :
128 : 0 : g_usleep (1000*1000*cancel_timeout);
129 : 0 : g_print ("Cancelling\n");
130 : 0 : g_cancellable_cancel (cancellable);
131 : 0 : return NULL;
132 : : }
|