on_incoming_connection (GSocketService     *service,
                        GSocketConnection  *connection,
                        GObject            *source_object,
                        gpointer            user_data)
{
  TestConnection *test = user_data;
  GOutputStream *stream;
  GTlsCertificate *cert;
  GError *error = NULL;

  if (test->incoming_connection_delay != 0)
    g_usleep (test->incoming_connection_delay);

  g_assert_null (test->server_connection);
  test->server_connection = g_tls_server_connection_new (G_IO_STREAM (connection),
                                                         test->server_certificate, &error);
  g_assert_no_error (error);

  if (!test->server_certificate)
    {
      cert = g_tls_certificate_new_from_file (tls_test_file_path ("server-and-key.pem"), &error);
      g_assert_no_error (error);
      g_tls_connection_set_certificate (G_TLS_CONNECTION (test->server_connection), cert);
      g_object_unref (cert);
    }

  g_object_set (test->server_connection, "authentication-mode", test->auth_mode, NULL);
  g_signal_connect (test->server_connection, "accept-certificate",
                    G_CALLBACK (on_accept_certificate), test);

  if (test->database)
    g_tls_connection_set_database (G_TLS_CONNECTION (test->server_connection), test->database);

  if (test->server_protocols)
    {
      g_tls_connection_set_advertised_protocols (G_TLS_CONNECTION (test->server_connection),
                                                 test->server_protocols);
    }

  stream = g_io_stream_get_output_stream (test->server_connection);

  if (test->connection_received_strategy == WRITE_THEN_CLOSE ||
      test->connection_received_strategy == WRITE_THEN_WAIT)
    {
      g_output_stream_write_async (stream, TEST_DATA,
                                   test->rehandshake ? TEST_DATA_LENGTH / 2 : TEST_DATA_LENGTH,
                                   G_PRIORITY_DEFAULT, NULL,
                                   on_output_write_finish, test);
    }
  else
    {
      g_tls_connection_handshake_async (G_TLS_CONNECTION (test->server_connection),
                                        G_PRIORITY_DEFAULT, NULL,
                                        on_server_handshake_finish, test);
    }

  return FALSE;
}
on_incoming_connection (GSocketService     *service,
                        GSocketConnection  *connection,
                        GObject            *source_object,
                        gpointer            user_data)
{
  TestConnection *test = user_data;
  GOutputStream *stream;
  GTlsCertificate *cert;
  GError *error = NULL;

  if (test->incoming_connection_delay != 0)
    g_usleep (test->incoming_connection_delay);

  g_assert_null (test->server_connection);
  test->server_connection = g_tls_server_connection_new (G_IO_STREAM (connection),
                                                         test->server_certificate, &error);
  g_assert_no_error (error);

  if (!test->server_certificate)
    {
      cert = g_tls_certificate_new_from_file (tls_test_file_path ("server-and-key.pem"), &error);
      g_assert_no_error (error);
      g_tls_connection_set_certificate (G_TLS_CONNECTION (test->server_connection), cert);
      g_object_unref (cert);
    }

  g_object_set (test->server_connection, "authentication-mode", test->auth_mode, NULL);
  g_signal_connect (test->server_connection, "accept-certificate",
                    G_CALLBACK (on_accept_certificate), test);

  if (test->database)
    g_tls_connection_set_database (G_TLS_CONNECTION (test->server_connection), test->database);

  if (test->server_protocols)
    {
      g_tls_connection_set_advertised_protocols (G_TLS_CONNECTION (test->server_connection),
                                                 test->server_protocols);
    }

  stream = g_io_stream_get_output_stream (test->server_connection);

  if (test->connection_received_strategy == WRITE_THEN_CLOSE ||
      test->connection_received_strategy == WRITE_THEN_WAIT)
    {
      g_output_stream_write_async (stream, TEST_DATA,
                                   test->rehandshake ? TEST_DATA_LENGTH / 2 : TEST_DATA_LENGTH,
                                   G_PRIORITY_DEFAULT, NULL,
                                   on_output_write_finish, test);
    }
  else
    {
      g_tls_connection_handshake_async (G_TLS_CONNECTION (test->server_connection),
                                        G_PRIORITY_DEFAULT, NULL,
                                        on_server_handshake_finish, test);
    }

  return FALSE;
}