示例#1
0
static void
dsk_ssl_stream_update_traps (DskSslStream *stream)
{
  dsk_boolean read_trap = DSK_FALSE, write_trap = DSK_FALSE;
  if (stream->base_instance.sink == NULL
   || stream->base_instance.source == NULL)
    return;
  if (stream->underlying_sink == NULL
   || stream->underlying_source == NULL)
    return;
  if (stream->handshaking)
    {
      if (stream->read_needed_to_handshake)
        read_trap = DSK_TRUE;
      else if (stream->write_needed_to_handshake)
        write_trap = DSK_TRUE;
    }
  else
    {
      dsk_boolean w = dsk_hook_is_trapped (&stream->base_instance.sink->writable_hook);
      dsk_boolean r = dsk_hook_is_trapped (&stream->base_instance.source->readable_hook);
      if (w)
        {
          if (stream->read_needed_to_write)
            read_trap = DSK_TRUE;
          else
            write_trap = DSK_TRUE;
        }
      if (r)
        {
          if (stream->write_needed_to_read)
            write_trap = DSK_TRUE;
          else
            read_trap = DSK_TRUE;
        }
    }
  if (read_trap && stream->underlying_read == NULL)
    stream->underlying_read = dsk_hook_trap (&stream->base_instance.source->readable_hook,
                                             (DskHookFunc) handle_underlying_readable,
                                             stream,
                                             (DskHookDestroy) handle_underlying_read_destroy);
  else if (!read_trap && stream->underlying_read != NULL)
    {
      dsk_hook_trap_free (stream->underlying_read);
      stream->underlying_read = NULL;
    }
  if (write_trap && stream->underlying_write == NULL)
    stream->underlying_write = dsk_hook_trap (&stream->base_instance.sink->writable_hook,
                                              (DskHookFunc) handle_underlying_writable,
                                              stream,
                                              (DskHookDestroy) handle_underlying_write_destroy);
  else if (!write_trap && stream->underlying_write != NULL)
    {
      /* NOTE: dsk_hook_trap_free is intended to NOT call destroy() */
      dsk_hook_trap_free (stream->underlying_write);
      stream->underlying_write = NULL;
    }
}
示例#2
0
static dsk_boolean
handle_port (const char *arg_name,
             const char *arg_value,
             void       *callback_data,
             DskError  **error)
{
  DskOctetListenerSocketOptions options
    = DSK_OCTET_LISTENER_SOCKET_OPTIONS_DEFAULT;
  DskOctetListener *listener;
  options.bind_port = strtoul (arg_value, &end, 10);
  if (arg_value == end || *end != 0)
    {
      dsk_set_error (error, "error parsing integer from '%s'", arg_value);
      return DSK_FALSE;
    }

  listener = dsk_octet_listener_socket_new (&options, error);
  if (listener == NULL)
    return DSK_FALSE;
  dsk_hook_trap (&listener->incoming,
                 (DskHookFunc) handle_incoming_connection,
                 NULL,
                 NULL);
  dsk_main_add_object (listener);
  return DSK_TRUE;
}
示例#3
0
static dsk_boolean
handle_incoming_connection (DskOctetListener *listener)
{
  DskHttpServerStream *stream;

  /* accept connection */
  switch (dsk_octet_listener_accept (listener, NULL,
                                     &source, &sink, &error))
    {
    case DSK_IO_RESULT_SUCCESS:
      /* create http-server stream */
      stream = dsk_http_server_stream_new (sink, source, &server_stream_options);
      dsk_assert (stream != NULL);
      dsk_hook_trap (&stream->request_available,
                     (DskHookFunc) handle_http_stream_request_available,
                     stream,
                     dsk_object_unref);
      break;
    case DSK_IO_RESULT_AGAIN:
      break;
    case DSK_IO_RESULT_ERROR:
      dsk_main_exit (1);
      dsk_warning ("cannot accept a new socket: %s",
                   error->message);
      dsk_error_unref (error);
      error = NULL;
      return DSK_FALSE;
    case DSK_IO_RESULT_EOF:
      dsk_assert_not_reached ();
    }

  /* invoke this handler when the next incoming connection is available. */
  return DSK_TRUE;
}