Esempio n. 1
0
static void
test_happy_eyeballs_cancel_instant (void)
{
  GSocketClient *client;
  GSocketService *service;
  GError *error = NULL;
  guint16 port;
  GMainLoop *loop;
  GCancellable *cancel;
  gboolean got_completed_event = FALSE;

  /* This tests the same things as above, test_happy_eyeballs_cancel_delayed(), but
   * with different timing since it sends an already cancelled cancellable */

  loop = g_main_loop_new (NULL, FALSE);

  service = g_socket_service_new ();
  port = g_socket_listener_add_any_inet_port (G_SOCKET_LISTENER (service), NULL, &error);
  g_assert_no_error (error);
  g_socket_service_start (service);

  client = g_socket_client_new ();
  cancel = g_cancellable_new ();
  g_cancellable_cancel (cancel);
  g_socket_client_connect_to_host_async (client, "localhost", port, cancel, on_connected_cancelled, loop);
  g_signal_connect (client, "event", G_CALLBACK (on_event), &got_completed_event);
  g_main_loop_run (loop);

  g_assert_true (got_completed_event);
  g_main_loop_unref (loop);
  g_object_unref (service);
  g_object_unref (client);
  g_object_unref (cancel);
}
Esempio n. 2
0
static void
test_happy_eyeballs (void)
{
  GSocketClient *client;
  GSocketService *service;
  GError *error = NULL;
  guint16 port;
  GMainLoop *loop;

  loop = g_main_loop_new (NULL, FALSE);

  service = g_socket_service_new ();
  port = g_socket_listener_add_any_inet_port (G_SOCKET_LISTENER (service), NULL, &error);
  g_assert_no_error (error);
  g_socket_service_start (service);

  /* All of the magic here actually happens in slow-connect-preload.c
   * which as you would guess is preloaded. So this is just making a
   * normal connection that happens to take 600ms each time. This will
   * trigger the logic to make multiple parallel connections.
   */
  client = g_socket_client_new ();
  g_socket_client_connect_to_host_async (client, "localhost", port, NULL, on_connected, loop);
  g_main_loop_run (loop);

  g_main_loop_unref (loop);
  g_object_unref (service);
  g_object_unref (client);
}
Esempio n. 3
0
int main (int argc, char *argv[])
{
  GMainLoop     *loop   = NULL;
  GSocketClient *client = NULL;

  g_type_init ();

  map = open("dummy.dat", O_RDWR | O_CREAT);
  if (-1 == map)
  {
    perror ("open");
    return 0;
  }

  data = mmap (NULL, 1024 * 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE, map, 0);
  if (MAP_FAILED == data)
  {
    perror ("mmap");
    return 0;
  }

  client = g_socket_client_new ();

  g_socket_client_connect_to_host_async (client, "127.0.0.1", 8080, NULL, on_connected, client);

  loop = g_main_loop_new (NULL, FALSE);
  
  g_main_loop_run (loop);

  g_main_loop_unref (loop);

  return 0;
}
Esempio n. 4
0
static void
test_happy_eyeballs_cancel_delayed (void)
{
  GSocketClient *client;
  GSocketService *service;
  GError *error = NULL;
  guint16 port;
  GMainLoop *loop;
  GCancellable *cancel;
  gboolean got_completed_event = FALSE;

  /* This just tests that cancellation works as expected, still emits the completed signal,
   * and never returns a connection */

  loop = g_main_loop_new (NULL, FALSE);

  service = g_socket_service_new ();
  port = g_socket_listener_add_any_inet_port (G_SOCKET_LISTENER (service), NULL, &error);
  g_assert_no_error (error);
  g_socket_service_start (service);

  client = g_socket_client_new ();
  cancel = g_cancellable_new ();
  g_socket_client_connect_to_host_async (client, "localhost", port, cancel, on_connected_cancelled, loop);
  g_timeout_add (1, (GSourceFunc) on_timer, cancel);
  g_signal_connect (client, "event", G_CALLBACK (on_event), &got_completed_event);
  g_main_loop_run (loop);

  g_assert_true (got_completed_event);
  g_main_loop_unref (loop);
  g_object_unref (service);
  g_object_unref (client);
  g_object_unref (cancel);
}
void idle_server_connection_connect_async(IdleServerConnection *conn, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) {
	IdleServerConnectionPrivate *priv = IDLE_SERVER_CONNECTION_GET_PRIVATE(conn);
	GSimpleAsyncResult *result;

	if (priv->state != SERVER_CONNECTION_STATE_NOT_CONNECTED) {
		IDLE_DEBUG("already connecting or connected!");
		g_simple_async_report_error_in_idle(G_OBJECT(conn),
			callback, user_data,
			TP_ERROR, TP_ERROR_NOT_AVAILABLE,
			"already connecting or connected!");
		return;
	}

	if ((priv->host == NULL) || (priv->host[0] == '\0')) {
		IDLE_DEBUG("host not set!");
		g_simple_async_report_error_in_idle(G_OBJECT(conn),
			callback, user_data,
			TP_ERROR, TP_ERROR_NOT_AVAILABLE,
			"host not set!");
		return;
	}

	if (priv->port == 0) {
		IDLE_DEBUG("port not set!");
		g_simple_async_report_error_in_idle(G_OBJECT(conn),
			callback, user_data,
			TP_ERROR, TP_ERROR_NOT_AVAILABLE,
			"port not set!");
		return;
	}

	result = g_simple_async_result_new(G_OBJECT(conn), callback, user_data, idle_server_connection_connect_async);
	g_socket_client_connect_to_host_async(priv->socket_client, priv->host, priv->port, cancellable, _connect_to_host_ready, result);
	change_state(conn, SERVER_CONNECTION_STATE_CONNECTING, SERVER_CONNECTION_STATE_REASON_REQUESTED);
}
SocketStreamHandle::SocketStreamHandle(const KURL& url, SocketStreamHandleClient* client)
    : SocketStreamHandleBase(url, client)
    , m_readBuffer(0)
{
    unsigned int port = url.hasPort() ? url.port() : (url.protocolIs("wss") ? 443 : 80);

    m_id = activateHandle(this);
    GRefPtr<GSocketClient> socketClient = adoptGRef(g_socket_client_new());
    if (url.protocolIs("wss"))
        g_socket_client_set_tls(socketClient.get(), TRUE);
    g_socket_client_connect_to_host_async(socketClient.get(), url.host().utf8().data(), port, 0,
        reinterpret_cast<GAsyncReadyCallback>(connectedCallback), m_id);
}
Esempio n. 7
0
Ref<SocketStreamHandle> SocketStreamHandle::create(const URL& url, SocketStreamHandleClient& client, NetworkingContext&, SessionID)
{
    Ref<SocketStreamHandle> socket = adoptRef(*new SocketStreamHandle(url, client));

    unsigned port = url.hasPort() ? url.port() : (url.protocolIs("wss") ? 443 : 80);
    GRefPtr<GSocketClient> socketClient = adoptGRef(g_socket_client_new());
    if (url.protocolIs("wss"))
        g_socket_client_set_tls(socketClient.get(), TRUE);
    Ref<SocketStreamHandle> protectedSocketStreamHandle = socket.copyRef();
    g_socket_client_connect_to_host_async(socketClient.get(), url.host().utf8().data(), port, socket->m_cancellable.get(),
        reinterpret_cast<GAsyncReadyCallback>(connectedCallback), &protectedSocketStreamHandle.leakRef());
    return socket;
}
Esempio n. 8
0
static gboolean setup_async_connect(GSocketClient *sock, struct ekg_connection_starter *cs) {
	if (*(cs->current_server)) {
		debug_function("setup_async_connect(), trying %s (defport: %d)\n",
				*(cs->current_server), cs->defport);
		g_socket_client_connect_to_host_async(
				sock, *(cs->current_server), cs->defport,
				cs->cancellable,
				done_async_connect,
				cs);
		cs->current_server++;
		return TRUE;
	} else
		return FALSE;
}
Esempio n. 9
0
/* Test whether given host has an AppSocket/HP JetDirect printer connected.
   See http://en.wikipedia.org/wiki/JetDirect
       http://www.cups.org/documentation.php/network.html */
void
pp_host_get_jetdirect_devices_async (PpHost              *host,
                                     GCancellable        *cancellable,
                                     GAsyncReadyCallback  callback,
                                     gpointer             user_data)
{
  PpHostPrivate *priv = host->priv;
  GSocketClient *client;
  JetDirectData *data;
  GTask         *task;
  gchar         *address;
  gpointer       result;

  data = g_new0 (JetDirectData, 1);
  data->host = g_object_ref (host);
  data->devices = g_new0 (PpDevicesList, 1);

  if (priv->port == PP_HOST_UNSET_PORT)
    data->port = PP_HOST_DEFAULT_JETDIRECT_PORT;
  else
    data->port = priv->port;

  task = g_task_new (G_OBJECT (host), cancellable, callback, user_data);
  g_task_set_task_data (task, data, (GDestroyNotify) jetdirect_data_free);

  address = g_strdup_printf ("%s:%d", priv->hostname, data->port);
  if (address != NULL && address[0] != '/')
    {
      client = g_socket_client_new ();

      g_socket_client_connect_to_host_async (client,
                                             address,
                                             data->port,
                                             cancellable,
                                             jetdirect_connection_test_cb,
                                             task);

      g_object_unref (client);
    }
  else
    {
      result = data->devices;
      data->devices = NULL;
      g_task_return_pointer (task, result, (GDestroyNotify) pp_devices_list_free);
      g_object_unref (task);
    }

  g_free (address);
}
Esempio n. 10
0
static void
connect_impl (PnNode *conn,
              const gchar *hostname,
              gint port)
{
    g_return_if_fail (conn);

    pn_log ("begin");

    pn_debug ("conn=%p,name=%s", conn, conn->name);
    pn_debug ("hostname=%s,port=%d", hostname, port);
    pn_debug ("next=%p", conn->next);

    g_free (conn->hostname);
    conn->hostname = g_strdup (hostname);
    conn->port = port;

    if (conn->next)
    {
        conn->status = PN_NODE_STATUS_CONNECTING;

        conn->next->prev = conn;
        pn_node_connect (conn->next, hostname, port);
        conn->next->prev = NULL;
    }
    else
    {
        pn_node_close (conn);

        conn->status = PN_NODE_STATUS_CONNECTING;

#if defined(USE_GIO)
        GSocketClient *client;
        client = g_socket_client_new();
        conn->socket_cancel = g_cancellable_new();
        g_socket_client_connect_to_host_async(client, hostname, port,
                                              conn->socket_cancel, connect_cb, conn);
#elif defined(HAVE_LIBPURPLE)
        conn->connect_data = purple_proxy_connect (NULL, msn_session_get_user_data (conn->session),
                             hostname, port, connect_cb, conn);
#endif
    }

    pn_log ("end");
}
static void
process_one_address (NewConnectionData *data)
{
  GInetSocketAddress *addr;
  gchar *host;

  if (g_cancellable_is_cancelled (data->cancellable))
    {
      g_simple_async_result_set_error (data->simple, G_IO_ERROR,
          G_IO_ERROR_CANCELLED, "Operation cancelled");
      g_simple_async_result_complete (data->simple);
      free_new_connection_data (data);
      return;
    }

  addr = g_queue_pop_head (data->addresses);

  /* check we haven't gotten to the end of the list */
  if (addr == NULL)
    {
      g_simple_async_result_set_error (data->simple,
          WOCKY_LL_CONNECTION_FACTORY_ERROR,
          WOCKY_LL_CONNECTION_FACTORY_ERROR_NO_CONTACT_ADDRESS_CAN_BE_CONNECTED_TO,
          "Failed to connect to any of the contact's addresses");
      g_simple_async_result_complete (data->simple);
      free_new_connection_data (data);
      return;
    }

  host = g_inet_address_to_string (g_inet_socket_address_get_address (addr));

  DEBUG ("connecting to %s (port %" G_GUINT16_FORMAT ")", host,
      g_inet_socket_address_get_port (addr));

  g_socket_client_connect_to_host_async (data->self->priv->client,
      host, g_inet_socket_address_get_port (addr),
      data->cancellable, connect_to_host_cb, data);

  g_free (host);

  g_object_unref (addr);
}
Esempio n. 12
0
static void
try_tunnel (SoupServer *server, SoupMessage *msg, SoupClientContext *context)
{
	Tunnel *tunnel;
	SoupURI *dest_uri;
	GSocketClient *sclient;

	soup_server_pause_message (server, msg);

	tunnel = g_new0 (Tunnel, 1);
	tunnel->self = g_object_ref (server);
	tunnel->msg = g_object_ref (msg);
	tunnel->context = context;

	dest_uri = soup_message_get_uri (msg);
	sclient = g_socket_client_new ();
	g_socket_client_connect_to_host_async (sclient, dest_uri->host, dest_uri->port,
					       NULL, tunnel_connected_cb, tunnel);
	g_object_unref (sclient);
}
Esempio n. 13
0
static VALUE
rg_connect_to_host_async(int argc, VALUE *argv, VALUE self)
{
        VALUE rbhost_and_port, rbdefault_port, rbcancellable, block;
        GCancellable *cancellable;
        const gchar *host_and_port;
        guint16 default_port;

        rb_scan_args(argc, argv, "21&", &rbhost_and_port, &rbdefault_port, &rbcancellable, &block);
        host_and_port = RVAL2CSTR(rbhost_and_port);
        default_port = RVAL2GUINT16(rbdefault_port);
        cancellable = RVAL2GCANCELLABLE(rbcancellable);
        SAVE_BLOCK(block);
        g_socket_client_connect_to_host_async(_SELF(self),
                                              host_and_port,
                                              default_port,
                                              cancellable,
                                              rbgio_async_ready_callback,
                                              (gpointer)block);

        return self;
}
Esempio n. 14
0
int
main (int argc, char *argv[])
{
  GOptionContext *context;
  GSocketClient *client;
  GSocketConnection *connection;
  GSocketAddress *address;
  GCancellable *cancellable;
  GOutputStream *out;
  GError *error = NULL;
  char buffer[1000];

  g_type_init ();

  context = g_option_context_new (" <hostname>[:port] - send data to tcp host");
  g_option_context_add_main_entries (context, cmd_entries, NULL);
  if (!g_option_context_parse (context, &argc, &argv, &error))
    {
      g_printerr ("%s: %s\n", argv[0], error->message);
      return 1;
    }

  if (argc != 2)
    {
      g_printerr ("%s: %s\n", argv[0], "Need to specify hostname");
      return 1;
    }

  if (async)
    loop = g_main_loop_new (NULL, FALSE);

  if (cancel_timeout)
    {
      GThread *thread;
      cancellable = g_cancellable_new ();
      thread = g_thread_new ("cancel", cancel_thread, cancellable);
      g_thread_unref (thread);
    }
  else
    {
      cancellable = NULL;
    }

  client = g_socket_client_new ();
  if (io_timeout)
    g_socket_client_set_timeout (client, io_timeout);
  if (verbose)
    g_signal_connect (client, "event", G_CALLBACK (socket_client_event), NULL);

  if (async)
    {
      GAsyncResult *res;
      g_socket_client_connect_to_host_async (client, argv[1], 7777,
					     cancellable, async_cb, &res);
      g_main_loop_run (loop);
      connection = g_socket_client_connect_to_host_finish (client, res, &error);
      g_object_unref (res);
    }
  else
    {
      connection = g_socket_client_connect_to_host (client,
						    argv[1],
						    7777,
						    cancellable, &error);
    }
  if (connection == NULL)
    {
      g_printerr ("%s can't connect: %s\n", argv[0], error->message);
      return 1;
    }
  g_object_unref (client);

  address = g_socket_connection_get_remote_address (connection, &error);
  if (!address)
    {
      g_printerr ("Error getting remote address: %s\n",
		  error->message);
      return 1;
    }
  g_print ("Connected to address: %s\n",
	   socket_address_to_string (address));
  g_object_unref (address);

  if (graceful)
    g_tcp_connection_set_graceful_disconnect (G_TCP_CONNECTION (connection), TRUE);

  out = g_io_stream_get_output_stream (G_IO_STREAM (connection));

  while (fgets(buffer, sizeof (buffer), stdin) != NULL)
    {
      /* FIXME if (async) */
      if (!g_output_stream_write_all (out, buffer, strlen (buffer),
				      NULL, cancellable, &error))
	{
	  g_warning ("send error: %s\n",  error->message);
	  g_error_free (error);
	  error = NULL;
	}
    }

  g_print ("closing stream\n");
  if (async)
    {
      GAsyncResult *res;
      g_io_stream_close_async (G_IO_STREAM (connection),
			       0, cancellable, async_cb, &res);
      g_main_loop_run (loop);
      if (!g_io_stream_close_finish (G_IO_STREAM (connection),
				     res, &error))
	{
	  g_object_unref (res);
	  g_warning ("close error: %s\n",  error->message);
	  return 1;
	}
      g_object_unref (res);
    }
  else
    {
      if (!g_io_stream_close (G_IO_STREAM (connection), cancellable, &error))
	{
	  g_warning ("close error: %s\n",  error->message);
	  return 1;
	}
    }

  g_object_unref (connection);

  return 0;
}