Starting the NetworkStreams. HTTP to www.google.com works
[cascardo/gnio.git] / gnio / gnetworkinputstream.c
1 /* GNIO - GLib Network Layer of GIO
2  *
3  * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Authors: Christian Kellner <gicmo@gnome.org>
21  *          Samuel Cormier-Iijima <sciyoshi@gmail.com>
22  */
23
24 #include <config.h>
25 #include <glib.h>
26 #include <gio/gio.h>
27
28 #include "gsocket.h"
29 #include "gnetworkinputstream.h"
30
31 G_DEFINE_TYPE (GNetworkInputStream, g_network_input_stream, G_TYPE_INPUT_STREAM);
32
33 enum
34 {
35   PROP_0,
36   PROP_SOCKET
37 };
38
39 struct _GNetworkInputStreamPrivate
40 {
41   GSocket *socket;
42 };
43
44 static void
45 g_network_input_stream_get_property (GObject    *object,
46                                      guint       prop_id,
47                                      GValue     *value,
48                                      GParamSpec *pspec)
49 {
50   GNetworkInputStream *stream = G_NETWORK_INPUT_STREAM (object);
51
52   switch (prop_id)
53     {
54       case PROP_SOCKET:
55         g_value_set_object (value, stream->priv->socket);
56         break;
57
58       default:
59         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
60     }
61 }
62
63 static void
64 g_network_input_stream_set_property (GObject      *object,
65                                      guint         prop_id,
66                                      const GValue *value,
67                                      GParamSpec   *pspec)
68 {
69   GNetworkInputStream *stream = G_NETWORK_INPUT_STREAM (object);
70
71   switch (prop_id)
72     {
73       case PROP_SOCKET:
74         stream->priv->socket = g_value_dup_object (value);
75         break;
76
77       default:
78         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
79     }
80 }
81
82 static void
83 g_network_input_stream_finalize (GObject *object)
84 {
85   if (G_OBJECT_CLASS (g_network_input_stream_parent_class)->finalize)
86     (*G_OBJECT_CLASS (g_network_input_stream_parent_class)->finalize) (object);
87 }
88
89 static void
90 g_network_input_stream_dispose (GObject *object)
91 {
92   if (G_OBJECT_CLASS (g_network_input_stream_parent_class)->dispose)
93     (*G_OBJECT_CLASS (g_network_input_stream_parent_class)->dispose) (object);
94 }
95
96 static gssize
97 g_network_input_stream_read (GInputStream  *stream,
98                              void          *buffer,
99                              gsize          count,
100                              GCancellable  *cancellable,
101                              GError       **error)
102 {
103   GNetworkInputStream *input_stream = G_NETWORK_INPUT_STREAM (stream);
104
105   return g_socket_receive (input_stream->priv->socket, (gchar *) buffer, count, error);
106 }
107
108 static void
109 g_network_input_stream_class_init (GNetworkInputStreamClass *klass)
110 {
111   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
112   GInputStreamClass *ginputstream_class = G_INPUT_STREAM_CLASS (klass);
113
114   g_type_class_add_private (klass, sizeof (GNetworkInputStreamPrivate));
115
116   gobject_class->finalize = g_network_input_stream_finalize;
117   gobject_class->dispose = g_network_input_stream_dispose;
118   gobject_class->get_property = g_network_input_stream_get_property;
119   gobject_class->set_property = g_network_input_stream_set_property;
120
121   ginputstream_class->read_fn = g_network_input_stream_read;
122
123   g_object_class_install_property (gobject_class, PROP_SOCKET,
124                                    g_param_spec_object ("socket",
125                                                         "socket",
126                                                         "the socket that this stream wraps",
127                                                         G_TYPE_SOCKET,
128                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
129 }
130
131 static void
132 g_network_input_stream_init (GNetworkInputStream *stream)
133 {
134   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream, G_TYPE_NETWORK_INPUT_STREAM, GNetworkInputStreamPrivate);
135
136   stream->priv->socket = NULL;
137 }
138
139 GNetworkInputStream *
140 _g_network_input_stream_new (GSocket *socket)
141 {
142   return G_NETWORK_INPUT_STREAM (g_object_new (G_TYPE_NETWORK_INPUT_STREAM, "socket", socket, NULL));
143 }