Ejemplo n.º 1
0
static void
read_cb (GObject *source, GAsyncResult *asyncResult, gpointer data)
{
    GMainLoop *loop = (GMainLoop*)data;
    GInputStream *stream = G_INPUT_STREAM (source);
    GError *error = NULL;
    gssize bytes_read = g_input_stream_read_finish (stream, asyncResult, &error);

    if (error) {
        debug_printf (1, "  failed read: %s\n", error->message);
        errors++;

        g_object_unref (stream);
        g_main_loop_quit (loop);
        return;
    }

    if (!bytes_read) {
        g_input_stream_close (stream, NULL, &error);
        g_object_unref (stream);

        if (error) {
            debug_printf (1, "  failed close: %s\n", error->message);
            errors++;
        }

        g_main_loop_quit (loop);
        return;
    }

    g_input_stream_read_async (stream, buffer, READ_BUFFER_SIZE,
                               G_PRIORITY_DEFAULT, NULL,
                               read_cb, data);
}
static void inputStreamReadReadyCallback(GInputStream* stream, GAsyncResult* result, gpointer userData)
{
    std::unique_ptr<ReadAsyncData> asyncData(static_cast<ReadAsyncData*>(userData));
    gssize bytesRead = g_input_stream_read_finish(stream, result, nullptr);
    if (bytesRead == -1) {
        asyncData->completionHandler(asyncData->data, -1);
        return;
    }

    if (!bytesRead) {
        asyncData->completionHandler(asyncData->data, 0);
        return;
    }

    ASSERT(bytesRead > 0);
    fillDataFromReadBuffer(asyncData->buffer.get(), static_cast<size_t>(bytesRead), asyncData->data);

    size_t pendingBytesToRead = asyncData->bytesToRead - asyncData->data.size();
    if (!pendingBytesToRead) {
        asyncData->completionHandler(asyncData->data, 0);
        return;
    }

    size_t bytesToRead = std::min(pendingBytesToRead, asyncData->buffer->length);
    // Use a local variable for the data buffer to pass it to g_input_stream_read_async(), because ReadAsyncData is released.
    auto data = const_cast<char*>(asyncData->buffer->data);
    g_input_stream_read_async(stream, data, bytesToRead, G_PRIORITY_DEFAULT, nullptr,
        reinterpret_cast<GAsyncReadyCallback>(inputStreamReadReadyCallback), asyncData.release());
}
Ejemplo n.º 3
0
/* Called by gpgme to read data */
static ssize_t
vfs_data_read (void *handle, void *buffer, size_t size)
{
    VfsAsyncHandle* ah = (VfsAsyncHandle*)handle;
    ssize_t sz = 0;
    
    /* Just in case we have an operation, like open */
    if (!vfs_data_wait_results(ah, TRUE))
        return -1;
        
    g_assert (ah->state == VFS_ASYNC_READY);
    
    /* Start async operation */
    ah->buffer = buffer;
    ah->state = VFS_ASYNC_PROCESSING;
    ah->operation = VFS_OP_READING;
    g_input_stream_read_async (ah->istream, buffer, size, G_PRIORITY_DEFAULT, ah->cancellable, vfs_data_read_done, ah);
    
    /* Wait for it */
    if (!vfs_data_wait_results (ah, TRUE))
        return -1;
    
    /* Return results */
    sz = (ssize_t)ah->processed;
    ah->state = VFS_ASYNC_READY;
    
    ah->buffer = NULL;
    ah->processed = 0;
    
    return sz;
}
Ejemplo n.º 4
0
static void readCallback(GObject* source, GAsyncResult* res, gpointer data)
{
    ResourceHandle* handle = static_cast<ResourceHandle*>(data);
    ResourceHandleInternal* d = handle->getInternal();
    ResourceHandleClient* client = handle->client();

    if (d->m_cancelled || !client) {
        cleanupGioOperation(handle);
        return;
    }

    gssize nread;
    GError *error = 0;

    nread = g_input_stream_read_finish(d->m_input_stream, res, &error);
    if (error) {
        cleanupGioOperation(handle);
        // FIXME: error
        client->didFinishLoading(handle);
        return;
    } else if (!nread) {
        client->didFinishLoading(handle);
        g_input_stream_close_async(d->m_input_stream, G_PRIORITY_DEFAULT,
                                   NULL, closeCallback, handle);
        return;
    }

    d->m_total += nread;
    client->didReceiveData(handle, d->m_buffer, nread, d->m_total);

    g_input_stream_read_async(d->m_input_stream, d->m_buffer, d->m_bufsize,
                              G_PRIORITY_DEFAULT, d->m_cancellable,
                              readCallback, handle);
}
Ejemplo n.º 5
0
static void openCallback(GObject* source, GAsyncResult* res, gpointer data)
{
    ResourceHandle* handle = static_cast<ResourceHandle*>(data);
    ResourceHandleInternal* d = handle->getInternal();
    ResourceHandleClient* client = handle->client();

    if (d->m_cancelled || !client) {
        cleanupGioOperation(handle);
        return;
    }

    GFileInputStream* in;
    GError *error = NULL;
    in = g_file_read_finish(G_FILE(source), res, &error);
    if (error) {
        cleanupGioOperation(handle);
        // FIXME: error
        client->didFinishLoading(handle);
        return;
    }

    d->m_input_stream = G_INPUT_STREAM(in);
    d->m_bufsize = 8192;
    d->m_buffer = static_cast<char*>(g_malloc(d->m_bufsize));
    d->m_total = 0;
    g_input_stream_read_async(d->m_input_stream, d->m_buffer, d->m_bufsize,
                              G_PRIORITY_DEFAULT, d->m_cancellable,
                              readCallback, handle);
}
Ejemplo n.º 6
0
static void
read_all_cb (GObject *source_object, GAsyncResult *res, gpointer user_data)
{
  GInputStream *stream = G_INPUT_STREAM (source_object);
  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);

  gsize bytes_read;
  GError *err = NULL;
  ReadAllData *read_data;

  bytes_read = g_input_stream_read_finish (stream, res, &err);
  if (bytes_read == -1)
  {
    g_simple_async_result_take_error (simple, err);
    goto done;
  }

  read_data = g_simple_async_result_get_op_res_gpointer (simple);

  read_data->bytes_read += bytes_read;
  if (read_data->bytes_read < read_data->count)
  {
    g_input_stream_read_async (stream,
                               (guint8 *)read_data->buffer + read_data->bytes_read,
                               read_data->count - read_data->bytes_read, 0,
                               read_data->cancellable, read_all_cb, simple);
    return;
  }

done:
  g_simple_async_result_complete (simple);
  g_object_unref (simple);
}
Ejemplo n.º 7
0
static void
read_all_async (GInputStream        *stream,
                void                *buffer,
                gsize                count,
                int                  io_priority,
                GCancellable        *cancellable,
                GAsyncReadyCallback  callback,
                gpointer             user_data)
{
  ReadAllData *read_data;
  GSimpleAsyncResult *simple;

  read_data = g_slice_new0 (ReadAllData);
  read_data->buffer = buffer;
  read_data->count = count;
  read_data->io_priority = io_priority;
  if (cancellable)
    read_data->cancellable = g_object_ref (cancellable);
  
  simple = g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
                                      read_all_async);
  g_simple_async_result_set_op_res_gpointer (simple, read_data,
                                             (GDestroyNotify)free_read_all_data);

  g_input_stream_read_async (stream, buffer, count, io_priority, cancellable,
                             read_all_cb, simple);
}
Ejemplo n.º 8
0
static void
stream_read_async_cb (GObject *obj, GAsyncResult *res, gpointer data)
{
	RBChunkLoader *loader = RB_CHUNK_LOADER (data);
	gssize done;

	done = g_input_stream_read_finish (G_INPUT_STREAM (obj),
					   res,
					   &loader->priv->error);
	if (done == -1) {
		rb_debug ("error reading from stream: %s", loader->priv->error->message);
		loader->priv->callback (loader, NULL, 0, loader->priv->callback_data);
		cleanup (loader);
	} else if (done == 0) {
		rb_debug ("reached end up input stream");
		loader->priv->callback (loader, NULL, 0, loader->priv->callback_data);
		cleanup (loader);
	} else {
		loader->priv->chunk_string.len = done;
		loader->priv->callback (loader, &loader->priv->chunk_string, loader->priv->total, loader->priv->callback_data);
		g_input_stream_read_async (G_INPUT_STREAM (loader->priv->stream),
					   loader->priv->chunk,
					   loader->priv->chunk_size,
					   G_PRIORITY_DEFAULT,
					   loader->priv->cancel,
					   stream_read_async_cb,
					   loader);
	}
}
Ejemplo n.º 9
0
static void sendIncompleteRequest(InspectorServerTest* test, gconstpointer)
{
    GOwnPtr<GError> error;

    // Connect to the inspector server.
    GRefPtr<GSocketClient> client = adoptGRef(g_socket_client_new());
    GRefPtr<GSocketConnection> connection = adoptGRef(g_socket_client_connect_to_host(client.get(), "127.0.0.1", 2999, 0, &error.outPtr()));
    g_assert_no_error(error.get());

    // Send incomplete request (missing blank line after headers) and check if inspector server
    // replies. The server should not reply to an incomplete request and the test should timeout
    // on read.
    GOutputStream* ostream = g_io_stream_get_output_stream(G_IO_STREAM(connection.get()));
    // Request missing blank line after headers.
    const gchar* incompleteRequest = "GET /devtools/page/1 HTTP/1.1\r\nHost: Localhost\r\n";
    g_output_stream_write(ostream, incompleteRequest, strlen(incompleteRequest), 0, &error.outPtr());
    g_assert_no_error(error.get());

    GInputStream* istream = g_io_stream_get_input_stream(G_IO_STREAM(connection.get()));
    char response[16];
    memset(response, 0, sizeof(response));
    GRefPtr<GCancellable> cancel = adoptGRef(g_cancellable_new());
    g_input_stream_read_async(istream, response, sizeof(response) - 1, G_PRIORITY_DEFAULT, cancel.get(), 0, 0);
    // Give a chance for the server to reply.
    test->wait(1);
    g_cancellable_cancel(cancel.get());
    // If we got any answer it means the server replied to an incomplete request, lets fail.
    g_assert(response[0] == '\0');
}
Ejemplo n.º 10
0
static void 
input_stream_ready_callback (GObject *source_object,
			     GAsyncResult *res,
			     gpointer user_data)
{
    GnomeGdkPixbufAsyncHandle *handle = user_data;

    handle->file_input_stream = g_file_read_finish (G_FILE (source_object),
						    res, NULL);
    if (handle->file_input_stream == NULL) {
	/* TODO: could map the GError more precisely to the GnomeVFSError */
	load_done (handle, GNOME_VFS_ERROR_GENERIC, NULL);
	return;
    }

    handle->loader = gdk_pixbuf_loader_new ();

    g_input_stream_read_async (G_INPUT_STREAM (handle->file_input_stream),
			       handle->buffer,
			       sizeof (handle->buffer),
			       G_PRIORITY_DEFAULT,
			       handle->cancellable,
			       input_stream_read_callback,
			       handle);
}
static void 
state_read_buffer (GcrImporter *self, gboolean async)
{
	GError *error = NULL;
	gssize count;
	gsize at;
	
	g_assert (GCR_IS_IMPORTER (self));
	g_assert (G_IS_INPUT_STREAM (self->pv->input));
	
	if (!self->pv->buffer)
		self->pv->buffer = g_byte_array_sized_new (BLOCK);

	at = self->pv->buffer->len;
	g_byte_array_set_size (self->pv->buffer, at + BLOCK);
	
	if (async) {
		g_input_stream_read_async (self->pv->input, self->pv->buffer->data + at, 
		                           BLOCK, G_PRIORITY_DEFAULT, self->pv->cancel,
		                           on_read_buffer, self);
	} else {
		count = g_input_stream_read (self->pv->input, self->pv->buffer->data + at, 
		                             BLOCK, self->pv->cancel, &error);
		complete_read_buffer (self, count, error);
	}
}
Ejemplo n.º 12
0
void
sourceview_io_open (SourceviewIO* sio, GFile* file)
{
	GFileInputStream* input_stream;
	GError* err = NULL;

	g_return_if_fail (file != NULL);

	if (sio->file)
		g_object_unref (sio->file);
	sio->file = file;
	g_object_ref (sio->file);
	set_display_name(sio);

	input_stream = g_file_read (file, NULL, &err);
	if (!input_stream)
	{
		g_signal_emit_by_name (sio, "open-failed", err);
		g_error_free (err);
		return;
	}
	sio->read_buffer = g_realloc (sio->read_buffer, READ_SIZE);
	g_input_stream_read_async (G_INPUT_STREAM (input_stream),
							   sio->read_buffer,
							   READ_SIZE,
							   G_PRIORITY_LOW,
							   sio->cancel,
							   on_read_finished,
							   sio);
}
Ejemplo n.º 13
0
static void
update_system_bookmark_list_ready (GObject      *source_object,
                                   GAsyncResult *result,
                                   gpointer      user_data)
{
    UpdateBookmarksData *data = user_data;
    gssize               size;

    size = g_input_stream_read_finish (data->stream, result, NULL);
    if (size < 0) {
        update_bookmakrs_data_free (data);
        return;
    }

    if (size > 0) {
        data->buffer[size + 1] = '\0';
        g_string_append (data->file_content, data->buffer);

        g_input_stream_read_async (data->stream,
                                   data->buffer,
                                   BUFFER_SIZE - 1,
                                   G_PRIORITY_DEFAULT,
                                   NULL,
                                   update_system_bookmark_list_ready,
                                   data);
        return;
    }

    update_system_bookmark_list_from_content (data->browser, data->file_content->str);
    update_bookmakrs_data_free (data);
}
Ejemplo n.º 14
0
/**
 * json_parser_load_from_stream_async:
 * @parser: a #JsonParser
 * @stream: a #GInputStream
 * @cancellable: (allow-none): a #GCancellable, or %NULL
 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
 * @user_data: the data to pass to @callback
 *
 * Asynchronously reads the contents of @stream.
 *
 * For more details, see json_parser_load_from_stream() which is the
 * synchronous version of this call.
 *
 * When the operation is finished, @callback will be called. You should
 * then call json_parser_load_from_stream_finish() to get the result
 * of the operation.
 *
 * Since: 0.12
 */
void
json_parser_load_from_stream_async (JsonParser          *parser,
                                    GInputStream        *stream,
                                    GCancellable        *cancellable,
                                    GAsyncReadyCallback  callback,
                                    gpointer             user_data)
{
  LoadStreamData *data;

  g_return_if_fail (JSON_IS_PARSER (parser));
  g_return_if_fail (G_IS_INPUT_STREAM (stream));
  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));

  data = g_new0 (LoadStreamData, 1);

  if (cancellable != NULL)
    data->cancellable = g_object_ref (cancellable);

  data->callback = callback;
  data->user_data = user_data;
  data->content = g_byte_array_new ();
  data->parser = g_object_ref (parser);

  g_byte_array_set_size (data->content, data->pos + GET_DATA_BLOCK_SIZE);
  g_input_stream_read_async (stream, data->content->data + data->pos,
                             GET_DATA_BLOCK_SIZE, 0,
                             data->cancellable,
                             load_stream_data_read_callback,
                             data);
}
Ejemplo n.º 15
0
/*
 *  * Read from g_input_stream until we get 0 bytes read.  Then process
 *   * using the value of stream_type.  Finally try and read another multipart.
 *    */
static void
read_cb (GObject *source, GAsyncResult *async_result, gpointer user_data)
{
    //g_print ("read_cb: Enter\n");
    GInputStream *in = G_INPUT_STREAM (source);
    MultiPartData *multipart_data = (MultiPartData *) user_data;
    SoupMultipartInputStream *multipart = SOUP_MULTIPART_INPUT_STREAM (multipart_data->multipart);
    gssize bytes_read;

    bytes_read = g_input_stream_read_finish (in, async_result, &multipart_data->error);

    /* Read 0 bytes - try to start reading another part. */
    if (bytes_read <= 0) {
        g_input_stream_close_async (in,
                                    G_PRIORITY_DEFAULT,
                                    multipart_data->cancellable,
                                    close_cb,
                                    user_data);
        if (multipart_data->callback) {
            SoupBuffer *soup_buffer;
            //g_print ("callback\n");
            soup_buffer = soup_buffer_new(SOUP_MEMORY_TEMPORARY,
                                (guchar *) multipart_data->buffer->str,
                                multipart_data->buffer->len);
            multipart_data->callback (multipart_data->method,
                                      multipart_data->path,
                                      multipart_data->cancellable,
                                      multipart_data->error,
                                      multipart_data->headers,
                                      soup_buffer,
                                      multipart_data->user_data);
            soup_buffer_free(soup_buffer);
        }
        g_string_free (multipart_data->buffer, TRUE);
        if (multipart_data->error) {
            g_input_stream_close_async (G_INPUT_STREAM (multipart),
                                        G_PRIORITY_DEFAULT,
                                        multipart_data->cancellable,
                                        close_base_cb,
                                        user_data);
            return;
        }
        soup_multipart_input_stream_next_part_async (multipart_data->multipart,
                                                     G_PRIORITY_DEFAULT,
                                                     multipart_data->cancellable,
                                                     next_part_cb,
                                                     user_data);
        return;
    }
    multipart_data->buffer = g_string_append_len (multipart_data->buffer, buffer, bytes_read);
    g_input_stream_read_async (in,
                               buffer,
                               READ_BUFFER_SIZE,
                               G_PRIORITY_DEFAULT,
                               multipart_data->cancellable,
                               read_cb,
                               user_data);
    //g_print ("read_cb: Exit\n");
}
Ejemplo n.º 16
0
static void
file_open_callback (GObject      *object,
		    GAsyncResult *res,
		    gpointer      user_data)
{
	FileOpenData *data = user_data;
	NemoImagePropertiesPage *page = data->page;
	GFile *file;
	GFileInputStream *stream;
	GError *error;
	char *uri;

	file = G_FILE (object);
	uri = g_file_get_uri (file);
	
	error = NULL;
	stream = g_file_read_finish (file, res, &error);
	if (stream) {
		char *mime_type;

		mime_type = nemo_file_info_get_mime_type (data->info);
		page->details->loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, &error);
		if (error != NULL) {
			g_warning ("Error creating loader for %s: %s", uri, error->message);
			g_clear_error (&error);
		}
		page->details->pixbuf_still_loading = TRUE;
		page->details->width = 0;
		page->details->height = 0;
#ifdef HAVE_EXIF
		page->details->exifldr = exif_loader_new ();
#endif /*HAVE_EXIF*/
		g_free (mime_type);

		g_signal_connect (page->details->loader,
				  "size_prepared",
				  G_CALLBACK (size_prepared_callback),
				  page);

		g_input_stream_read_async (G_INPUT_STREAM (stream),
					   page->details->buffer,
					   sizeof (page->details->buffer),
					   0,
					   page->details->cancellable,
					   file_read_callback,
					   page);

		g_object_unref (stream);
	} else {
		g_warning ("Error reading %s: %s", uri, error->message);
		g_clear_error (&error);
		load_finished (page);
	}

	g_free (uri);
	g_free (data);
}
Ejemplo n.º 17
0
static void
load_stream_data_read_callback (GObject      *object,
                                GAsyncResult *read_res,
                                gpointer      user_data)
{
  GInputStream *stream = G_INPUT_STREAM (object);
  LoadStreamData *data = user_data;
  GError *error = NULL;
  gssize read_size;

  read_size = g_input_stream_read_finish (stream, read_res, &error);
  if (read_size < 0)
    {
      if (error != NULL)
        data->error = error;
      else
        {
          GSimpleAsyncResult *res;

          /* EOF */
          res = g_simple_async_result_new (G_OBJECT (data->parser),
                                           data->callback,
                                           data->user_data,
                                           json_parser_load_from_stream_async);
          g_simple_async_result_set_op_res_gpointer (res, data, load_stream_data_free);
          g_simple_async_result_complete (res);
          g_object_unref (res);
        }
    }
  else if (read_size > 0)
    {
      data->pos += read_size;

      g_byte_array_set_size (data->content, data->pos + GET_DATA_BLOCK_SIZE);

      g_input_stream_read_async (stream, data->content->data + data->pos,
                                 GET_DATA_BLOCK_SIZE,
                                 0,
                                 data->cancellable,
                                 load_stream_data_read_callback,
                                 data);
    }
  else
    {
      GSimpleAsyncResult *res;

      res = g_simple_async_result_new (G_OBJECT (data->parser),
                                       data->callback,
                                       data->user_data,
                                       json_parser_load_from_stream_async);
      g_simple_async_result_set_op_res_gpointer (res, data, load_stream_data_free);
      g_simple_async_result_complete (res);
      g_object_unref (res);
    }
}
Ejemplo n.º 18
0
static void
read_stream (LatexilaPostProcessor *pp)
{
  g_input_stream_read_async (pp->priv->stream,
                             &pp->priv->buffer,
                             BUFFER_SIZE,
                             G_PRIORITY_DEFAULT,
                             g_task_get_cancellable (pp->priv->task),
                             (GAsyncReadyCallback) read_stream_cb,
                             pp);
}
Ejemplo n.º 19
0
static void
do_read (GAsyncReadyCallback callback, GTask *task, ConnectAsyncData *data)
{
   GInputStream *in;
   in = g_io_stream_get_input_stream (data->io_stream);
   g_input_stream_read_async (in,
			      data->buffer + data->offset,
			      data->length - data->offset,
			      g_task_get_priority (task),
			      g_task_get_cancellable (task),
			      callback, task);
}
Ejemplo n.º 20
0
static GByteArray *
do_single_coding_req_test (SoupRequest *req,
			   const char *expected_encoding,
			   const char *expected_content_type,
			   MessageContentStatus status)
{
	GInputStream *stream;
	SoupMessage *msg;
	GByteArray *data;
	guchar buf[1024];
	gssize nread;
	GError *error = NULL;

	data = g_byte_array_new ();

	msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (req));

	stream = soup_test_request_send (req, NULL, &error);
	if (error) {
		debug_printf (1, "    Error sending request: %s\n",
			      error->message);
		g_error_free (error);
		errors++;
		return data;
	}

	do {
		nread = -2;
		g_input_stream_read_async (stream, buf, sizeof (buf),
					   G_PRIORITY_DEFAULT,
					   NULL, read_finished, &nread);
		while (nread == -2)
			g_main_context_iteration (NULL, TRUE);

		if (nread > 0)
			g_byte_array_append (data, buf, nread);
	} while (nread > 0);

	soup_test_request_close_stream (req, stream, NULL, &error);
	if (error) {
		debug_printf (1, "    error closing stream: %s\n",
			      error->message);
		g_error_free (error);
		errors++;
	}
	g_object_unref (stream);

	check_response (msg, expected_encoding, expected_content_type, status);
	g_object_unref (msg);

	return data;
}
Ejemplo n.º 21
0
static void gtkhash_hash_file_read(struct hash_file_s *data)
{
	if (G_UNLIKELY(g_cancellable_is_cancelled(data->cancellable))) {
		gtkhash_hash_file_set_state(data, HASH_FILE_STATE_CLOSE);
		return;
	}

	gtkhash_hash_file_remove_source(data);
	g_input_stream_read_async(G_INPUT_STREAM(data->stream),
		data->buffer, HASH_FILE_BUFFER_SIZE, G_PRIORITY_DEFAULT,
		data->cancellable, (GAsyncReadyCallback)gtkhash_hash_file_read_finish,
		data);
}
void IOChannel::read(size_t offset, size_t size, WorkQueue*, std::function<void (Data&, int error)> completionHandler)
{
    ASSERT(m_inputStream);

    size_t bufferSize = std::min(size, gDefaultReadBufferSize);
    uint8_t* bufferData = static_cast<uint8_t*>(fastMalloc(bufferSize));
    GRefPtr<SoupBuffer> buffer = adoptGRef(soup_buffer_new_with_owner(bufferData, bufferSize, bufferData, fastFree));
    ReadAsyncData* asyncData = new ReadAsyncData { this, buffer.get(), size, completionHandler, { } };

    // FIXME: implement offset.
    g_input_stream_read_async(m_inputStream.get(), const_cast<char*>(buffer->data), bufferSize, G_PRIORITY_DEFAULT, nullptr,
        reinterpret_cast<GAsyncReadyCallback>(inputStreamReadReadyCallback), asyncData);
}
Ejemplo n.º 23
0
static void
connect_cb(GObject *source,
           GAsyncResult *res,
           gpointer user_data)
{
    GSocketConnection *socket_conn;
    PnNode *conn;
    GError *error = NULL;

    conn = PN_NODE(user_data);
    socket_conn = g_socket_client_connect_to_host_finish(G_SOCKET_CLIENT(source), res, &error);

    g_object_unref(source);

    if (error) {
        g_error_free(error);
        return;
    }

    g_object_ref(conn);

    if (socket_conn) {
        GSocket *socket;
        GInputStream *input;

        conn->socket_conn = socket_conn;
        socket = g_socket_connection_get_socket(socket_conn);

        conn->status = PN_NODE_STATUS_OPEN;

        input = g_io_stream_get_input_stream (G_IO_STREAM (conn->socket_conn));
        g_object_ref (conn);
        g_input_stream_read_async (input, conn->input_buffer, PN_BUF_LEN,
                                   G_PRIORITY_DEFAULT, NULL,
                                   read_cb, conn);
    }
    else {
        conn->error = g_error_new_literal(PN_NODE_ERROR, PN_NODE_ERROR_OPEN,
                                          "Unable to connect");

        pn_node_error(conn);
    }

    {
        PnNodeClass *class;
        class = g_type_class_peek(PN_NODE_TYPE);
        g_signal_emit(G_OBJECT(conn), class->open_sig, 0, conn);
    }

    g_object_unref(conn);
}
Ejemplo n.º 24
0
static void
start_tunnel (SoupMessage *msg, gpointer user_data)
{
	Tunnel *tunnel = user_data;

	tunnel->client.iostream = soup_client_context_steal_connection (tunnel->context);
	tunnel->client.istream = g_io_stream_get_input_stream (tunnel->client.iostream);
	tunnel->client.ostream = g_io_stream_get_output_stream (tunnel->client.iostream);

	tunnel->client.buffer = g_malloc (BUFSIZE);
	tunnel->server.buffer = g_malloc (BUFSIZE);

	tunnel->cancellable = g_cancellable_new ();

	g_input_stream_read_async (tunnel->client.istream,
				   tunnel->client.buffer, BUFSIZE,
				   G_PRIORITY_DEFAULT, tunnel->cancellable,
				   tunnel_read_cb, tunnel);
	g_input_stream_read_async (tunnel->server.istream,
				   tunnel->server.buffer, BUFSIZE,
				   G_PRIORITY_DEFAULT, tunnel->cancellable,
				   tunnel_read_cb, tunnel);
}
Ejemplo n.º 25
0
void SocketStreamHandle::connected(GRefPtr<GSocketConnection>&& socketConnection)
{
    m_socketConnection = WTFMove(socketConnection);
    m_outputStream = G_POLLABLE_OUTPUT_STREAM(g_io_stream_get_output_stream(G_IO_STREAM(m_socketConnection.get())));
    m_inputStream = g_io_stream_get_input_stream(G_IO_STREAM(m_socketConnection.get()));
    m_readBuffer = std::make_unique<char[]>(READ_BUFFER_SIZE);

    RefPtr<SocketStreamHandle> protectedThis(this);
    g_input_stream_read_async(m_inputStream.get(), m_readBuffer.get(), READ_BUFFER_SIZE, G_PRIORITY_DEFAULT, m_cancellable.get(),
        reinterpret_cast<GAsyncReadyCallback>(readReadyCallback), protectedThis.leakRef());

    m_state = Open;
    m_client.didOpenSocketStream(*this);
}
Ejemplo n.º 26
0
static void read_input_stream (GDownloadable * download)
{
	//g_message ("reading input stream ...\n");
	g_assert (download != NULL);
	
	if (download->priv->status != G_DOWNLOADABLE_DOWNLOADING)
		return;
	
	GioDownload *gio_download = GIO_DOWNLOAD (download); 

	g_input_stream_read_async (G_INPUT_STREAM(gio_download->priv->input), gio_download->priv->chunk_buff, CHUNK_BUFF_SIZE,
							   G_PRIORITY_DEFAULT, NULL, on_input_stream_read_ready, download);
	//g_message ("reading input stream ...\n");
} 
Ejemplo n.º 27
0
static void
file_read_callback (GObject *source_object,
                    GAsyncResult *res,
                    gpointer user_data)
{
    EelPixbufLoadHandle *handle;
    gssize bytes_read;
    GError *error;

    handle = user_data;

    if (g_cancellable_is_cancelled (handle->cancellable))
    {
        free_pixbuf_load_handle (handle);
        return;
    }

    error = NULL;
    bytes_read = g_input_stream_read_finish  (G_INPUT_STREAM (source_object),
                 res, &error);

    if (bytes_read > 0)
    {
        if (!gdk_pixbuf_loader_write (handle->loader,
                                      handle->buffer,
                                      bytes_read,
                                      &error))
        {
            bytes_read = -1;
        }
        else
        {
            g_input_stream_read_async (handle->stream,
                                       handle->buffer,
                                       sizeof (handle->buffer),
                                       0,
                                       handle->cancellable,
                                       file_read_callback, handle);
            return;
        }
    }

    load_done (handle, error, bytes_read == 0);

    if (error != NULL)
    {
        g_error_free (error);
    }
}
Ejemplo n.º 28
0
void SocketStreamHandle::readBytes(gssize bytesRead)
{
    if (!bytesRead) {
        close();
        return;
    }

    // The client can close the handle, potentially removing the last reference.
    RefPtr<SocketStreamHandle> protectedThis(this);
    m_client.didReceiveSocketStreamData(*this, m_readBuffer.get(), bytesRead);
    if (m_inputStream) {
        g_input_stream_read_async(m_inputStream.get(), m_readBuffer.get(), READ_BUFFER_SIZE, G_PRIORITY_DEFAULT, m_cancellable.get(),
            reinterpret_cast<GAsyncReadyCallback>(readReadyCallback), protectedThis.leakRef());
    }
}
Ejemplo n.º 29
0
static gboolean
on_incoming_connection (GSocketService * service,
                        GSocketConnection * connection,
                        GObject * source_object,
                        gpointer user_data)
{
  GInputStream * input;
  void * buf;

  input = g_io_stream_get_input_stream (G_IO_STREAM (connection));
  buf = g_malloc (1);
  g_input_stream_read_async (input, buf, 1, G_PRIORITY_DEFAULT, NULL,
      on_read_ready, NULL);

  return TRUE;
}
Ejemplo n.º 30
0
static void
_gth_browser_update_system_bookmark_list (GthBrowser *browser)
{
    BrowserData         *browser_data;
    GFile               *bookmark_file;
    GFileInputStream    *input_stream;
    UpdateBookmarksData *data;

    browser_data = g_object_get_data (G_OBJECT (browser), BROWSER_DATA_KEY);
    g_return_if_fail (browser_data != NULL);

    g_menu_remove_all (browser_data->system_bookmarks_menu);

    /* give priority to XDG_CONFIG_HOME/gtk-3.0/bookmarks if not found
     * try the old ~/.gtk-bookmarks */

    bookmark_file = gth_user_dir_get_file_for_read (GTH_DIR_CONFIG, "gtk-3.0", "bookmarks", NULL);
    if (! g_file_query_exists (bookmark_file, NULL)) {
        char *path;

        g_object_unref (bookmark_file);
        path = g_build_filename (g_get_home_dir (), ".gtk-bookmarks", NULL);
        bookmark_file = g_file_new_for_path (path);

        g_free (path);
    }

    input_stream = g_file_read (bookmark_file, NULL, NULL);
    g_object_unref (bookmark_file);

    if (input_stream == NULL)
        return;

    data = g_new0 (UpdateBookmarksData, 1);
    data->browser = g_object_ref (browser);
    data->stream = (GInputStream*) input_stream;
    data->file_content = g_string_new ("");

    g_input_stream_read_async (data->stream,
                               data->buffer,
                               BUFFER_SIZE - 1,
                               G_PRIORITY_DEFAULT,
                               NULL,
                               update_system_bookmark_list_ready,
                               data);
}