[Spice-devel] [PATCH 05/17] Introduce reds_stream_new() helper

Christophe Fergeau cfergeau at redhat.com
Tue Jan 7 03:14:31 PST 2014


Initializing a new stream means initializing quite a few fields.
This commit factors this initialization in a dedicated reds_stream_new
helper. This also helps moving more code from reds.c to reds_stream.c
---
 server/reds.c        | 22 ++--------------------
 server/reds_stream.c | 29 +++++++++++++++++++++++++++++
 server/reds_stream.h |  1 +
 3 files changed, 32 insertions(+), 20 deletions(-)

diff --git a/server/reds.c b/server/reds.c
index cf1dca3..9b9628d 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -2693,7 +2693,6 @@ static void reds_handle_ssl_accept(int fd, int event, void *data)
 static RedLinkInfo *reds_init_client_connection(int socket)
 {
     RedLinkInfo *link;
-    RedsStream *stream;
     int delay_val = 1;
     int flags;
 
@@ -2714,28 +2713,11 @@ static RedLinkInfo *reds_init_client_connection(int socket)
     }
 
     link = spice_new0(RedLinkInfo, 1);
-    stream = spice_new0(RedsStream, 1);
-    stream->info = spice_new0(SpiceChannelEventInfo, 1);
-    link->stream = stream;
+    link->stream = reds_stream_new(socket);
 
-    stream->socket = socket;
     /* gather info + send event */
 
-    /* deprecated fields. Filling them for backward compatibility */
-    stream->info->llen = sizeof(stream->info->laddr);
-    stream->info->plen = sizeof(stream->info->paddr);
-    getsockname(stream->socket, (struct sockaddr*)(&stream->info->laddr), &stream->info->llen);
-    getpeername(stream->socket, (struct sockaddr*)(&stream->info->paddr), &stream->info->plen);
-
-    stream->info->flags |= SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT;
-    stream->info->llen_ext = sizeof(stream->info->laddr_ext);
-    stream->info->plen_ext = sizeof(stream->info->paddr_ext);
-    getsockname(stream->socket, (struct sockaddr*)(&stream->info->laddr_ext),
-                &stream->info->llen_ext);
-    getpeername(stream->socket, (struct sockaddr*)(&stream->info->paddr_ext),
-                &stream->info->plen_ext);
-
-    reds_stream_push_channel_event(stream, SPICE_CHANNEL_EVENT_CONNECTED);
+    reds_stream_push_channel_event(link->stream, SPICE_CHANNEL_EVENT_CONNECTED);
 
     openssl_init(link);
 
diff --git a/server/reds_stream.c b/server/reds_stream.c
index 5ec0efa..e9337cf 100644
--- a/server/reds_stream.c
+++ b/server/reds_stream.c
@@ -149,6 +149,35 @@ void reds_stream_push_channel_event(RedsStream *s, int event)
     main_dispatcher_channel_event(event, s->info);
 }
 
+static void reds_stream_set_socket(RedsStream *stream, int socket)
+{
+    stream->socket = socket;
+    /* deprecated fields. Filling them for backward compatibility */
+    stream->info->llen = sizeof(stream->info->laddr);
+    stream->info->plen = sizeof(stream->info->paddr);
+    getsockname(stream->socket, (struct sockaddr*)(&stream->info->laddr), &stream->info->llen);
+    getpeername(stream->socket, (struct sockaddr*)(&stream->info->paddr), &stream->info->plen);
+
+    stream->info->flags |= SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT;
+    stream->info->llen_ext = sizeof(stream->info->laddr_ext);
+    stream->info->plen_ext = sizeof(stream->info->paddr_ext);
+    getsockname(stream->socket, (struct sockaddr*)(&stream->info->laddr_ext),
+                &stream->info->llen_ext);
+    getpeername(stream->socket, (struct sockaddr*)(&stream->info->paddr_ext),
+                &stream->info->plen_ext);
+}
+
+RedsStream *reds_stream_new(int socket)
+{
+    RedsStream *stream;
+
+    stream = spice_new0(RedsStream, 1);
+    stream->info = spice_new0(SpiceChannelEventInfo, 1);
+    reds_stream_set_socket(stream, socket);
+
+    return stream;
+}
+
 RedsStreamSslStatus reds_stream_ssl_accept(RedsStream *stream)
 {
     int ssl_error;
diff --git a/server/reds_stream.h b/server/reds_stream.h
index a0a3651..ed1c61d 100644
--- a/server/reds_stream.h
+++ b/server/reds_stream.h
@@ -101,6 +101,7 @@ void reds_stream_free(RedsStream *s);
 
 void reds_stream_push_channel_event(RedsStream *s, int event);
 void reds_stream_remove_watch(RedsStream* s);
+RedsStream *reds_stream_new(int socket);
 RedsStreamSslStatus reds_stream_ssl_accept(RedsStream *stream);
 int reds_stream_enable_ssl(RedsStream *stream, SSL_CTX *ctx);
 
-- 
1.8.4.2



More information about the Spice-devel mailing list