more 'static' adding in testcases
[platform/upstream/glib.git] / gio / tests / socket-common.c
1 /* #included into both socket-client.c and socket-server.c */
2
3 static const char *unix_socket_address_types[] = {
4   "invalid",
5   "anonymous",
6   "path",
7   "abstract",
8   "padded"
9 };
10
11 static char *
12 socket_address_to_string (GSocketAddress *address)
13 {
14   char *res = NULL;
15
16   if (G_IS_INET_SOCKET_ADDRESS (address))
17     {
18       GInetAddress *inet_address;
19       char *str;
20       int port;
21
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);
26       g_free (str);
27     }
28 #ifdef G_OS_UNIX
29   else if (G_IS_UNIX_SOCKET_ADDRESS (address))
30     {
31       GUnixSocketAddress *uaddr = G_UNIX_SOCKET_ADDRESS (address);
32
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));
36     }
37 #endif
38
39   return res;
40 }
41
42 static GSocketAddress *
43 socket_address_from_string (const char *name)
44 {
45 #ifdef G_OS_UNIX
46   int i, len;
47
48   for (i = 0; i < G_N_ELEMENTS (unix_socket_address_types); i++)
49     {
50       len = strlen (unix_socket_address_types[i]);
51       if (!strncmp (name, unix_socket_address_types[i], len) &&
52           name[len] == ':')
53         {
54           return g_unix_socket_address_new_with_type (name + len + 1, -1,
55                                                       (GUnixSocketAddressType)i);
56         }
57     }
58 #endif
59   return NULL;
60 }
61
62 static gboolean
63 source_ready (GPollableInputStream *stream,
64               gpointer              data)
65 {
66   g_main_loop_quit (loop);
67   return FALSE;
68 }
69
70 static void
71 ensure_socket_condition (GSocket      *socket,
72                          GIOCondition  condition,
73                          GCancellable *cancellable)
74 {
75   GSource *source;
76
77   if (!non_blocking)
78     return;
79
80   source = g_socket_create_source (socket, condition, cancellable);
81   g_source_set_callback (source,
82                          (GSourceFunc) source_ready,
83                          NULL, NULL);
84   g_source_attach (source, NULL);
85   g_source_unref (source);
86   g_main_loop_run (loop);
87 }
88
89 static void
90 ensure_connection_condition (GIOStream    *stream,
91                              GIOCondition  condition,
92                              GCancellable *cancellable)
93 {
94   GSource *source;
95
96   if (!non_blocking)
97     return;
98
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);
101   else
102     source = g_pollable_output_stream_create_source (G_POLLABLE_OUTPUT_STREAM (g_io_stream_get_output_stream (stream)), cancellable);
103
104   g_source_set_callback (source,
105                          (GSourceFunc) source_ready,
106                          NULL, NULL);
107   g_source_attach (source, NULL);
108   g_source_unref (source);
109   g_main_loop_run (loop);
110 }
111
112 static gpointer
113 cancel_thread (gpointer data)
114 {
115   GCancellable *cancellable = data;
116
117   g_usleep (1000*1000*cancel_timeout);
118   g_print ("Cancelling\n");
119   g_cancellable_cancel (cancellable);
120   return NULL;
121 }