Make GTcpClient cache the NetworkStreams, and add the properties
[cascardo/gnio.git] / gnio / gtcpclient.c
index ca8b350..fc54dc5 100644 (file)
@@ -36,15 +36,19 @@ enum
   PROP_0,
   PROP_ADDRESS,
   PROP_HOSTNAME,
-  PROP_PORT
+  PROP_PORT,
+  PROP_INPUT_STREAM,
+  PROP_OUTPUT_STREAM
 };
 
 struct _GTcpClientPrivate
 {
-  GInetSocketAddress *address;
-  gchar              *hostname;
-  gushort             port;
-  GSocket            *socket;
+  GInetSocketAddress   *address;
+  gchar                *hostname;
+  gushort               port;
+  GSocket              *socket;
+  GNetworkInputStream  *input;
+  GNetworkOutputStream *output;
 };
 
 static void
@@ -83,6 +87,14 @@ g_tcp_client_get_property (GObject    *object,
         g_value_set_uint (value, client->priv->port);
         break;
 
+      case PROP_INPUT_STREAM:
+        g_value_set_object (value, g_tcp_client_get_input_stream (client));
+        break;
+
+      case PROP_OUTPUT_STREAM:
+        g_value_set_object (value, g_tcp_client_get_output_stream (client));
+        break;
+
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -175,6 +187,20 @@ g_tcp_client_class_init (GTcpClientClass *klass)
                                                       G_MAXUSHORT,
                                                       0,
                                                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
+
+  g_object_class_install_property (gobject_class, PROP_INPUT_STREAM,
+                                   g_param_spec_object ("input-stream",
+                                                        "input stream",
+                                                        "the GNetworkInputStream for reading from this socket",
+                                                        G_TYPE_NETWORK_INPUT_STREAM,
+                                                        G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
+
+  g_object_class_install_property (gobject_class, PROP_OUTPUT_STREAM,
+                                   g_param_spec_object ("output-stream",
+                                                        "output stream",
+                                                        "the GNetworkOutputStream for writing to this socket",
+                                                        G_TYPE_NETWORK_OUTPUT_STREAM,
+                                                        G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
 }
 
 static void
@@ -186,6 +212,8 @@ g_tcp_client_init (GTcpClient *client)
   client->priv->hostname = NULL;
   client->priv->port = 0;
   client->priv->socket = NULL;
+  client->priv->input = NULL;
+  client->priv->output = NULL;
 }
 
 GTcpClient *
@@ -207,7 +235,12 @@ g_tcp_client_get_input_stream (GTcpClient *client)
   if (!client->priv->socket)
     return NULL;
 
-  return _g_network_input_stream_new (client->priv->socket);
+  if (client->priv->input)
+    return client->priv->input;
+
+  // TODO: should we set g_object_notify here, or just create both these streams earlier?
+
+  return (client->priv->input = _g_network_input_stream_new (client->priv->socket));
 }
 
 GNetworkOutputStream *
@@ -216,7 +249,12 @@ g_tcp_client_get_output_stream (GTcpClient *client)
   if (!client->priv->socket)
     return NULL;
 
-  return _g_network_output_stream_new (client->priv->socket);
+  if (client->priv->output)
+    return client->priv->output;
+
+  // TODO: should we set g_object_notify here, or just create both these streams earlier?
+
+  return (client->priv->output = _g_network_output_stream_new (client->priv->socket));
 }
 
 gboolean