File: socket-common.c

package info (click to toggle)
glib2.0 2.86.1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 67,012 kB
  • sloc: ansic: 544,596; python: 9,702; sh: 1,612; xml: 1,482; perl: 1,222; cpp: 535; makefile: 321; javascript: 11
file content (132 lines) | stat: -rw-r--r-- 3,329 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* #included into both socket-testclient.c and socket-testserver.c */

#ifdef G_OS_UNIX
static const char *unix_socket_address_types[] = {
  "invalid",
  "anonymous",
  "path",
  "abstract",
  "padded"
};
#endif

static char *
socket_address_to_string (GSocketAddress *address)
{
  char *res = NULL;

  if (G_IS_INET_SOCKET_ADDRESS (address))
    {
      GInetSocketAddress *socket_address;
      GInetAddress *inet_address;
      char *str;
      int port;
      guint32 scope_id;

      socket_address = G_INET_SOCKET_ADDRESS (address);
      scope_id = g_inet_socket_address_get_scope_id (socket_address);
      inet_address = g_inet_socket_address_get_address (socket_address);
      str = g_inet_address_to_string (inet_address);
      port = g_inet_socket_address_get_port (socket_address);
      if (scope_id)
        res = g_strdup_printf ("[%s%%%u]:%d", str, scope_id, port);
      else if (g_inet_address_get_family (inet_address) == G_SOCKET_FAMILY_IPV6)
        res = g_strdup_printf ("[%s]:%d", str, port);
      else
        res = g_strdup_printf ("%s:%d", str, port);
      g_free (str);
    }
#ifdef G_OS_UNIX
  else if (G_IS_UNIX_SOCKET_ADDRESS (address))
    {
      GUnixSocketAddress *uaddr = G_UNIX_SOCKET_ADDRESS (address);

      res = g_strdup_printf ("%s:%s",
			     unix_socket_address_types[g_unix_socket_address_get_address_type (uaddr)],
			     g_unix_socket_address_get_path (uaddr));
    }
#endif

  return res;
}

static GSocketAddress *
socket_address_from_string (const char *name)
{
#ifdef G_OS_UNIX
  gsize i, len;

  for (i = 0; i < G_N_ELEMENTS (unix_socket_address_types); i++)
    {
      len = strlen (unix_socket_address_types[i]);
      if (!strncmp (name, unix_socket_address_types[i], len) &&
	  name[len] == ':')
	{
	  return g_unix_socket_address_new_with_type (name + len + 1, -1,
						      (GUnixSocketAddressType)i);
	}
    }
#endif
  return NULL;
}

static gboolean
source_ready (GPollableInputStream *stream,
	      gpointer              data)
{
  g_main_loop_quit (loop);
  return FALSE;
}

static void
ensure_socket_condition (GSocket      *socket,
			 GIOCondition  condition,
			 GCancellable *cancellable)
{
  GSource *source;

  if (!non_blocking)
    return;

  source = g_socket_create_source (socket, condition, cancellable);
  g_source_set_callback (source,
			 (GSourceFunc) source_ready,
			 NULL, NULL);
  g_source_attach (source, NULL);
  g_source_unref (source);
  g_main_loop_run (loop);
}

static void
ensure_connection_condition (GIOStream    *stream,
			     GIOCondition  condition,
			     GCancellable *cancellable)
{
  GSource *source;

  if (!non_blocking)
    return;

  if (condition & G_IO_IN)
    source = g_pollable_input_stream_create_source (G_POLLABLE_INPUT_STREAM (g_io_stream_get_input_stream (stream)), cancellable);
  else
    source = g_pollable_output_stream_create_source (G_POLLABLE_OUTPUT_STREAM (g_io_stream_get_output_stream (stream)), cancellable);

  g_source_set_callback (source,
			 (GSourceFunc) source_ready,
			 NULL, NULL);
  g_source_attach (source, NULL);
  g_source_unref (source);
  g_main_loop_run (loop);
}

static gpointer
cancel_thread (gpointer data)
{
  GCancellable *cancellable = data;

  g_usleep (1000*1000*cancel_timeout);
  g_print ("Cancelling\n");
  g_cancellable_cancel (cancellable);
  return NULL;
}