[Spice-devel] [PATCH 06/17] Move stream read/write callbacks to reds_stream.c

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


Now that stream creation and SSL enabling are done by helpers
in reds_stream.c, we can move the initialization of the vfunc
read/write pointers there too.
---
 server/reds.c        | 77 ----------------------------------------------------
 server/reds_stream.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 77 deletions(-)

diff --git a/server/reds.c b/server/reds.c
index 9b9628d..e8654b0 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -191,73 +191,6 @@ void reds_handle_channel_event(int event, SpiceChannelEventInfo *info)
     }
 }
 
-static ssize_t stream_write_cb(RedsStream *s, const void *buf, size_t size)
-{
-    return write(s->socket, buf, size);
-}
-
-static ssize_t stream_writev_cb(RedsStream *s, const struct iovec *iov, int iovcnt)
-{
-    ssize_t ret = 0;
-    do {
-        int tosend;
-        ssize_t n, expected = 0;
-        int i;
-#ifdef IOV_MAX
-        tosend = MIN(iovcnt, IOV_MAX);
-#else
-        tosend = iovcnt;
-#endif
-        for (i = 0; i < tosend; i++) {
-            expected += iov[i].iov_len;
-        }
-        n = writev(s->socket, iov, tosend);
-        if (n <= expected) {
-            if (n > 0)
-                ret += n;
-            return ret == 0 ? n : ret;
-        }
-        ret += n;
-        iov += tosend;
-        iovcnt -= tosend;
-    } while(iovcnt > 0);
-
-    return ret;
-}
-
-static ssize_t stream_read_cb(RedsStream *s, void *buf, size_t size)
-{
-    return read(s->socket, buf, size);
-}
-
-static ssize_t stream_ssl_write_cb(RedsStream *s, const void *buf, size_t size)
-{
-    int return_code;
-    SPICE_GNUC_UNUSED int ssl_error;
-
-    return_code = SSL_write(s->ssl, buf, size);
-
-    if (return_code < 0) {
-        ssl_error = SSL_get_error(s->ssl, return_code);
-    }
-
-    return return_code;
-}
-
-static ssize_t stream_ssl_read_cb(RedsStream *s, void *buf, size_t size)
-{
-    int return_code;
-    SPICE_GNUC_UNUSED int ssl_error;
-
-    return_code = SSL_read(s->ssl, buf, size);
-
-    if (return_code < 0) {
-        ssl_error = SSL_get_error(s->ssl, return_code);
-    }
-
-    return return_code;
-}
-
 static void reds_link_free(RedLinkInfo *link)
 {
     reds_stream_free(link->stream);
@@ -2737,10 +2670,6 @@ static RedLinkInfo *reds_init_client_ssl_connection(int socket)
     if (link == NULL)
         goto error;
 
-    link->stream->write = stream_ssl_write_cb;
-    link->stream->read = stream_ssl_read_cb;
-    link->stream->writev = NULL;
-
     ssl_status = reds_stream_enable_ssl(link->stream, reds->ctx);
     switch (ssl_status) {
         case REDS_STREAM_SSL_STATUS_OK:
@@ -2800,7 +2729,6 @@ static void reds_accept(int fd, int event, void *data)
 SPICE_GNUC_VISIBLE int spice_server_add_client(SpiceServer *s, int socket, int skip_auth)
 {
     RedLinkInfo *link;
-    RedsStream *stream;
 
     spice_assert(reds == s);
     if (!(link = reds_init_client_connection(socket))) {
@@ -2810,11 +2738,6 @@ SPICE_GNUC_VISIBLE int spice_server_add_client(SpiceServer *s, int socket, int s
 
     link->skip_auth = skip_auth;
 
-    stream = link->stream;
-    stream->read = stream_read_cb;
-    stream->write = stream_write_cb;
-    stream->writev = stream_writev_cb;
-
     reds_handle_new_link(link);
     return 0;
 }
diff --git a/server/reds_stream.c b/server/reds_stream.c
index e9337cf..b3614e6 100644
--- a/server/reds_stream.c
+++ b/server/reds_stream.c
@@ -33,6 +33,73 @@
 
 extern SpiceCoreInterface *core;
 
+static ssize_t stream_write_cb(RedsStream *s, const void *buf, size_t size)
+{
+    return write(s->socket, buf, size);
+}
+
+static ssize_t stream_writev_cb(RedsStream *s, const struct iovec *iov, int iovcnt)
+{
+    ssize_t ret = 0;
+    do {
+        int tosend;
+        ssize_t n, expected = 0;
+        int i;
+#ifdef IOV_MAX
+        tosend = MIN(iovcnt, IOV_MAX);
+#else
+        tosend = iovcnt;
+#endif
+        for (i = 0; i < tosend; i++) {
+            expected += iov[i].iov_len;
+        }
+        n = writev(s->socket, iov, tosend);
+        if (n <= expected) {
+            if (n > 0)
+                ret += n;
+            return ret == 0 ? n : ret;
+        }
+        ret += n;
+        iov += tosend;
+        iovcnt -= tosend;
+    } while(iovcnt > 0);
+
+    return ret;
+}
+
+static ssize_t stream_read_cb(RedsStream *s, void *buf, size_t size)
+{
+    return read(s->socket, buf, size);
+}
+
+static ssize_t stream_ssl_write_cb(RedsStream *s, const void *buf, size_t size)
+{
+    int return_code;
+    SPICE_GNUC_UNUSED int ssl_error;
+
+    return_code = SSL_write(s->ssl, buf, size);
+
+    if (return_code < 0) {
+        ssl_error = SSL_get_error(s->ssl, return_code);
+    }
+
+    return return_code;
+}
+
+static ssize_t stream_ssl_read_cb(RedsStream *s, void *buf, size_t size)
+{
+    int return_code;
+    SPICE_GNUC_UNUSED int ssl_error;
+
+    return_code = SSL_read(s->ssl, buf, size);
+
+    if (return_code < 0) {
+        ssl_error = SSL_get_error(s->ssl, return_code);
+    }
+
+    return return_code;
+}
+
 void reds_stream_remove_watch(RedsStream* s)
 {
     if (s->watch) {
@@ -175,6 +242,10 @@ RedsStream *reds_stream_new(int socket)
     stream->info = spice_new0(SpiceChannelEventInfo, 1);
     reds_stream_set_socket(stream, socket);
 
+    stream->read = stream_read_cb;
+    stream->write = stream_write_cb;
+    stream->writev = stream_writev_cb;
+
     return stream;
 }
 
@@ -225,6 +296,10 @@ int reds_stream_enable_ssl(RedsStream *stream, SSL_CTX *ctx)
 
     SSL_set_bio(stream->ssl, sbio, sbio);
 
+    stream->write = stream_ssl_write_cb;
+    stream->read = stream_ssl_read_cb;
+    stream->writev = NULL;
+
     return reds_stream_ssl_accept(stream);
 }
 
-- 
1.8.4.2



More information about the Spice-devel mailing list