[libsoup/giobased: 4/11] API simplification
- From: Dan Winship <danw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libsoup/giobased: 4/11] API simplification
- Date: Tue, 22 Mar 2011 13:06:59 +0000 (UTC)
commit c7dd00f232dec8d8bad40392c2d11eff4f57f9af
Author: Dan Winship <danw gnome org>
Date: Thu Dec 9 11:46:19 2010 +0100
API simplification
libsoup/soup-input-stream.c | 103 +++++++++++++++++--------------------------
libsoup/soup-input-stream.h | 35 ++++++++-------
libsoup/soup-message-io.c | 70 ++++++++++-------------------
3 files changed, 85 insertions(+), 123 deletions(-)
---
diff --git a/libsoup/soup-input-stream.c b/libsoup/soup-input-stream.c
index 2525126..78c651d 100644
--- a/libsoup/soup-input-stream.c
+++ b/libsoup/soup-input-stream.c
@@ -132,15 +132,10 @@ again:
switch (sstream->priv->chunked_state) {
case SOUP_INPUT_STREAM_STATE_CHUNK_SIZE:
case SOUP_INPUT_STREAM_STATE_CHUNK_END:
- if (blocking) {
- nread = soup_input_stream_read_line (
- sstream, metabuf, sizeof (metabuf),
- cancellable, error);
- } else {
- nread = soup_input_stream_read_line_nonblocking (
- sstream, metabuf, sizeof (metabuf),
- cancellable, error);
- }
+ nread = soup_input_stream_read_line (sstream,
+ metabuf, sizeof (metabuf),
+ blocking,
+ cancellable, error);
if (nread <= 0)
return nread;
if (metabuf[nread - 1] != '\n')
@@ -168,13 +163,8 @@ again:
return nread;
case SOUP_INPUT_STREAM_STATE_TRAILERS:
- if (blocking) {
- nread = soup_input_stream_read_line (
- sstream, buffer, count, cancellable, error);
- } else {
- nread = soup_input_stream_read_line_nonblocking (
- sstream, buffer, count, cancellable, error);
- }
+ nread = soup_input_stream_read_line (sstream, buffer, count,
+ blocking, cancellable, error);
if (nread <= 0)
return nread;
@@ -190,11 +180,11 @@ again:
}
static gssize
-soup_input_stream_read (GInputStream *stream,
- void *buffer,
- gsize count,
- GCancellable *cancellable,
- GError **error)
+soup_input_stream_read_fn (GInputStream *stream,
+ void *buffer,
+ gsize count,
+ GCancellable *cancellable,
+ GError **error)
{
SoupInputStream *sstream = SOUP_INPUT_STREAM (stream);
gssize nread;
@@ -301,7 +291,7 @@ soup_input_stream_class_init (SoupInputStreamClass *stream_class)
object_class->constructed = constructed;
object_class->finalize = finalize;
- input_stream_class->read_fn = soup_input_stream_read;
+ input_stream_class->read_fn = soup_input_stream_read_fn;
}
static void
@@ -313,7 +303,7 @@ soup_input_stream_pollable_init (GPollableInputStreamInterface *pollable_interfa
pollable_interface->read_nonblocking = soup_input_stream_read_nonblocking;
}
-GInputStream *
+SoupInputStream *
soup_input_stream_new (GInputStream *base_stream)
{
return g_object_new (SOUP_TYPE_INPUT_STREAM,
@@ -334,9 +324,29 @@ soup_input_stream_set_encoding (SoupInputStream *sstream,
}
gssize
+soup_input_stream_read (SoupInputStream *sstream,
+ void *buffer,
+ gsize count,
+ gboolean blocking,
+ GCancellable *cancellable,
+ GError **error)
+{
+ if (blocking) {
+ return g_input_stream_read (G_INPUT_STREAM (sstream),
+ buffer, count,
+ cancellable, error);
+ } else {
+ return g_pollable_input_stream_read_nonblocking (
+ G_POLLABLE_INPUT_STREAM (sstream),
+ buffer, count, cancellable, error);
+ }
+}
+
+gssize
soup_input_stream_read_line (SoupInputStream *sstream,
void *buffer,
gsize length,
+ gboolean blocking,
GCancellable *cancellable,
GError **error)
{
@@ -353,46 +363,15 @@ soup_input_stream_read_line (SoupInputStream *sstream,
return read_from_buf (sstream, buffer, nread);
}
- nread = g_input_stream_read (sstream->priv->base_stream,
- buffer, length,
- cancellable, error);
- if (nread <= 0)
- return nread;
-
- p = memchr (buffer, '\n', nread);
- if (!p || p == buf + nread - 1)
- return nread;
-
- p++;
- sstream->priv->buf = g_byte_array_new ();
- g_byte_array_append (sstream->priv->buf,
- p, nread - (p - buf));
- return p - buf;
-}
-
-gssize
-soup_input_stream_read_line_nonblocking (SoupInputStream *sstream,
- void *buffer,
- gsize length,
- GCancellable *cancellable,
- GError **error)
-{
- gssize nread;
- guint8 *p, *buf = buffer;
-
- g_return_val_if_fail (SOUP_IS_INPUT_STREAM (sstream), -1);
-
- if (sstream->priv->buf) {
- GByteArray *buf = sstream->priv->buf;
-
- p = memchr (buf->data, '\n', buf->len);
- nread = p ? p + 1 - buf->data : buf->len;
- return read_from_buf (sstream, buffer, nread);
+ if (blocking) {
+ nread = g_input_stream_read (G_INPUT_STREAM (sstream->priv->base_stream),
+ buffer, length,
+ cancellable, error);
+ } else {
+ nread = g_pollable_input_stream_read_nonblocking (
+ G_POLLABLE_INPUT_STREAM (sstream->priv->base_stream),
+ buffer, length, cancellable, error);
}
-
- nread = g_pollable_input_stream_read_nonblocking (
- G_POLLABLE_INPUT_STREAM (sstream->priv->base_stream),
- buffer, length, cancellable, error);
if (nread <= 0)
return nread;
diff --git a/libsoup/soup-input-stream.h b/libsoup/soup-input-stream.h
index 91141f5..b70d876 100644
--- a/libsoup/soup-input-stream.h
+++ b/libsoup/soup-input-stream.h
@@ -38,22 +38,25 @@ typedef struct {
GType soup_input_stream_get_type (void);
-GInputStream *soup_input_stream_new (GInputStream *base_stream);
-
-void soup_input_stream_set_encoding (SoupInputStream *sstream,
- SoupEncoding encoding,
- goffset content_length);
-
-gssize soup_input_stream_read_line (SoupInputStream *sstream,
- void *buffer,
- gsize length,
- GCancellable *cancellable,
- GError **error);
-gssize soup_input_stream_read_line_nonblocking (SoupInputStream *sstream,
- void *buffer,
- gsize length,
- GCancellable *cancellable,
- GError **error);
+SoupInputStream *soup_input_stream_new (GInputStream *base_stream);
+
+void soup_input_stream_set_encoding (SoupInputStream *sstream,
+ SoupEncoding encoding,
+ goffset content_length);
+
+gssize soup_input_stream_read (SoupInputStream *sstream,
+ void *buffer,
+ gsize count,
+ gboolean blocking,
+ GCancellable *cancellable,
+ GError **error);
+
+gssize soup_input_stream_read_line (SoupInputStream *sstream,
+ void *buffer,
+ gsize length,
+ gboolean blocking,
+ GCancellable *cancellable,
+ GError **error);
G_END_DECLS
diff --git a/libsoup/soup-message-io.c b/libsoup/soup-message-io.c
index 31c043c..39b6856 100644
--- a/libsoup/soup-message-io.c
+++ b/libsoup/soup-message-io.c
@@ -50,10 +50,10 @@ typedef struct {
GCancellable *cancellable;
SoupSocket *sock;
- GInputStream *istream;
+ SoupInputStream *istream;
GOutputStream *ostream;
GMainContext *async_context;
- gboolean non_blocking;
+ gboolean blocking;
SoupMessageIOState read_state;
SoupEncoding read_encoding;
@@ -180,7 +180,7 @@ soup_message_io_finished (SoupMessage *msg)
g_object_unref (msg);
}
-static gboolean io_read (GInputStream *stream, SoupMessage *msg);
+static gboolean io_read (SoupInputStream *stream, SoupMessage *msg);
static gboolean io_write (GOutputStream *stream, SoupMessage *msg);
static gboolean
@@ -326,17 +326,10 @@ read_metadata (SoupMessage *msg, gboolean to_blank)
}
while (1) {
- if (io->non_blocking) {
- nread = soup_input_stream_read_line_nonblocking (
- SOUP_INPUT_STREAM (io->istream),
- read_buf, sizeof (read_buf),
- io->cancellable, &error);
- } else {
- nread = soup_input_stream_read_line (
- SOUP_INPUT_STREAM (io->istream),
- read_buf, sizeof (read_buf),
- io->cancellable, &error);
- }
+ nread = soup_input_stream_read_line (io->istream, read_buf,
+ sizeof (read_buf),
+ io->blocking,
+ io->cancellable, &error);
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) {
g_error_free (error);
@@ -514,18 +507,11 @@ read_body_chunk (SoupMessage *msg)
RESPONSE_BLOCK_SIZE);
}
- if (io->non_blocking) {
- nread = g_pollable_input_stream_read_nonblocking (
- G_POLLABLE_INPUT_STREAM (io->istream),
- (guchar *)buffer->data, buffer->length,
- io->cancellable, &error);
- } else {
- nread = g_input_stream_read (io->istream,
- (guchar *)buffer->data,
- buffer->length,
- io->cancellable, &error);
- }
-
+ nread = soup_input_stream_read (io->istream,
+ (guchar *)buffer->data,
+ buffer->length,
+ io->blocking,
+ io->cancellable, &error);
if (nread > 0) {
buffer->length = nread;
io->read_length -= nread;
@@ -594,16 +580,16 @@ write_data (SoupMessage *msg, const char *data, guint len, gboolean body)
}
while (len > io->written) {
- if (io->non_blocking) {
- nwrote = g_pollable_output_stream_write_nonblocking (
- G_POLLABLE_OUTPUT_STREAM (io->ostream),
- data + io->written, len - io->written,
- io->cancellable, &error);
- } else {
+ if (io->blocking) {
nwrote = g_output_stream_write (io->ostream,
data + io->written,
len - io->written,
io->cancellable, &error);
+ } else {
+ nwrote = g_pollable_output_stream_write_nonblocking (
+ G_POLLABLE_OUTPUT_STREAM (io->ostream),
+ data + io->written, len - io->written,
+ io->cancellable, &error);
}
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) {
@@ -907,7 +893,7 @@ io_write (GOutputStream *stream, SoupMessage *msg)
}
static gboolean
-io_read (GInputStream *stream, SoupMessage *msg)
+io_read (SoupInputStream *stream, SoupMessage *msg)
{
SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
SoupMessageIOData *io = priv->io_data;
@@ -979,7 +965,7 @@ io_read (GInputStream *stream, SoupMessage *msg)
}
}
- soup_input_stream_set_encoding (SOUP_INPUT_STREAM (io->istream),
+ soup_input_stream_set_encoding (io->istream,
io->read_encoding,
io->read_length);
@@ -1118,6 +1104,7 @@ new_iostate (SoupMessage *msg, SoupSocket *sock, SoupMessageIOMode mode,
SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
SoupMessageIOData *io;
GIOStream *iostream;
+ gboolean non_blocking;
io = g_slice_new0 (SoupMessageIOData);
io->mode = mode;
@@ -1134,9 +1121,10 @@ new_iostate (SoupMessage *msg, SoupSocket *sock, SoupMessageIOMode mode,
io->ostream = g_io_stream_get_output_stream (iostream);
}
g_object_get (io->sock,
- SOUP_SOCKET_FLAG_NONBLOCKING, &io->non_blocking,
+ SOUP_SOCKET_FLAG_NONBLOCKING, &non_blocking,
SOUP_SOCKET_ASYNC_CONTEXT, &io->async_context,
NULL);
+ io->blocking = !non_blocking;
io->read_meta_buf = g_byte_array_new ();
io->write_buf = g_string_new (NULL);
@@ -1253,24 +1241,16 @@ soup_message_io_unpause (SoupMessage *msg)
{
SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
SoupMessageIOData *io = priv->io_data;
- gboolean non_blocking;
- GMainContext *async_context;
g_return_if_fail (io != NULL);
- g_object_get (io->sock,
- SOUP_SOCKET_FLAG_NONBLOCKING, &non_blocking,
- SOUP_SOCKET_ASYNC_CONTEXT, &async_context,
- NULL);
- if (non_blocking) {
+ if (!io->blocking) {
if (!io->unpause_source) {
io->unpause_source = soup_add_completion (
- async_context, io_unpause_internal, msg);
+ io->async_context, io_unpause_internal, msg);
}
} else
io_unpause_internal (msg);
- if (async_context)
- g_main_context_unref (async_context);
}
/**
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]