Пример #1
0
static void
use_socket_client (gboolean synchronous)
{
  GError *error = NULL;
  GSocketClient *client;

  client = g_socket_client_new ();

  printf ("Proxies for URI '%s' are:\n", info);

  if (synchronous)
    {
      GSocketConnection *connection;
      GSocketAddress *proxy_addr;

      connection = g_socket_client_connect_to_uri (client,
						   info,
						   0,
      						   cancellable,
						   &error);

      if (connection)
	{
	  proxy_addr = g_socket_connection_get_remote_address (connection, NULL);
	  print_proxy_address (proxy_addr);
	}
      else
	{
	  print_and_free_error (error);
	}
    }
  else
    {
      GMainLoop *loop = g_main_loop_new (NULL, FALSE);

      g_socket_client_connect_to_uri_async (client,
					    info,
					    0,
					    cancellable,
					    _socket_connect_cb,
					    loop);

      g_main_loop_run (loop);
      g_main_loop_unref (loop);
    }

  g_object_unref (client);
}
Пример #2
0
static void
web_socket_client_constructed (GObject *object)
{
  WebSocketClient *self = WEB_SOCKET_CLIENT (object);
  WebSocketConnection *conn = WEB_SOCKET_CONNECTION (object);
  GSocketClient *client;
  const gchar *url;
  guint16 default_port;
  gchar *scheme;

  G_OBJECT_CLASS (web_socket_client_parent_class)->constructed (object);

  if (web_socket_connection_get_io_stream (conn))
    {
      /* Start handshake from the main context */
      self->idle_start = g_idle_source_new ();
      g_source_set_priority (self->idle_start, G_PRIORITY_HIGH);
      g_source_set_callback (self->idle_start, (GSourceFunc)on_idle_do_handshake,
                             self, NULL);
      g_source_attach (self->idle_start, _web_socket_connection_get_main_context (conn));
    }
  else
    {
      client = g_socket_client_new ();
      self->cancellable = g_cancellable_new ();

      url = web_socket_connection_get_url (WEB_SOCKET_CONNECTION (self));
      scheme = g_uri_parse_scheme (url);
      if (scheme && (g_str_equal (scheme, "wss") || g_str_equal (scheme, "https")))
        {
          g_socket_client_set_tls (client, TRUE);
          default_port = 443;
        }
      else
        {
          default_port = 80;
        }
      g_free (scheme);

      g_socket_client_connect_to_uri_async (client, url, default_port,
                                            self->cancellable, on_connect_to_uri,
                                            g_object_ref (self));
      g_object_unref (client);
    }
}