[Spice-devel] [PATCH spice-server 13/30] New interface to create RedsWebSocket

Frediano Ziglio fziglio at redhat.com
Mon Nov 21 12:52:00 UTC 2016


less coupling
---
 server/reds-stream.c | 31 +++++++++++--------------------
 server/websocket.c   | 25 +++++++++++++++++++++++++
 server/websocket.h   |  8 +++++---
 3 files changed, 41 insertions(+), 23 deletions(-)

diff --git a/server/reds-stream.c b/server/reds-stream.c
index bf0cbbe..125aa68 100644
--- a/server/reds-stream.c
+++ b/server/reds-stream.c
@@ -1191,28 +1191,19 @@ bool reds_stream_is_websocket(RedsStream *stream, unsigned char *buf, int len)
               so it seems wisest to live with this theoretical flaw.
     */
 
-    if (websocket_is_start(rbuf)) {
-        char outbuf[1024];
-
-        websocket_create_reply(rbuf, outbuf);
-        rc = stream->priv->write(stream, outbuf, strlen(outbuf));
-        if (rc == strlen(outbuf)) {
-            stream->priv->ws = spice_malloc0(sizeof(*stream->priv->ws));
-
-            stream->priv->ws->raw_stream = stream;
-            stream->priv->ws->raw_read = stream->priv->read;
-            stream->priv->ws->raw_write = stream->priv->write;
-
-            stream->priv->read = stream_websocket_read;
-            stream->priv->write = stream_websocket_write;
-
-            if (stream->priv->writev) {
-                stream->priv->ws->raw_writev = stream->priv->writev;
-                stream->priv->writev = stream_websocket_writev;
-            }
+    stream->priv->ws = websocket_new(rbuf, stream,
+                                     (websocket_read_cb_t) stream->priv->read,
+                                     (websocket_write_cb_t) stream->priv->write,
+                                     (websocket_writev_cb_t) stream->priv->writev);
+    if (stream->priv->ws) {
+        stream->priv->read = stream_websocket_read;
+        stream->priv->write = stream_websocket_write;
 
-            return TRUE;
+        if (stream->priv->writev) {
+            stream->priv->writev = stream_websocket_writev;
         }
+
+        return TRUE;
     }
 
     return FALSE;
diff --git a/server/websocket.c b/server/websocket.c
index 6f16f6f..fdf36c3 100644
--- a/server/websocket.c
+++ b/server/websocket.c
@@ -470,3 +470,28 @@ void websocket_create_reply(gchar *buf, gchar *outbuf)
                     "Sec-WebSocket-Protocol: binary\r\n\r\n", key);
     g_free(key);
 }
+
+RedsWebSocket *websocket_new(gchar *rbuf, struct RedsStream *stream, websocket_read_cb_t read_cb,
+                             websocket_write_cb_t write_cb, websocket_writev_cb_t writev_cb)
+{
+    if (!websocket_is_start(rbuf)) {
+        return NULL;
+    }
+
+    char outbuf[1024];
+
+    websocket_create_reply(rbuf, outbuf);
+    int rc = write_cb(stream, outbuf, strlen(outbuf));
+    if (rc != strlen(outbuf)) {
+        return NULL;
+    }
+
+    RedsWebSocket *ws = spice_new0(RedsWebSocket, 1);
+
+    ws->raw_stream = stream;
+    ws->raw_read = read_cb;
+    ws->raw_write = write_cb;
+    ws->raw_writev = writev_cb;
+
+    return ws;
+}
diff --git a/server/websocket.h b/server/websocket.h
index db02420..853820e 100644
--- a/server/websocket.h
+++ b/server/websocket.h
@@ -41,11 +41,13 @@ typedef struct {
     guint64 write_remainder;
 
     struct RedsStream *raw_stream;
-    ssize_t (*raw_read)(struct RedsStream *s, void *buf, size_t nbyte);
-    ssize_t (*raw_write)(struct RedsStream *s, const void *buf, size_t nbyte);
-    ssize_t (*raw_writev)(struct RedsStream *s, const struct iovec *iov, int iovcnt);
+    websocket_read_cb_t raw_read;
+    websocket_write_cb_t raw_write;
+    websocket_writev_cb_t raw_writev;
 } RedsWebSocket;
 
+RedsWebSocket *websocket_new(gchar *buf, struct RedsStream *s, websocket_read_cb_t read_cb,
+                             websocket_write_cb_t write_cb, websocket_writev_cb_t writev_cb);
 bool websocket_is_start(gchar *buf);
 void websocket_create_reply(gchar *buf, gchar *outbuf);
 int websocket_read(RedsWebSocket *ws, guchar *buf, size_t len);
-- 
2.7.4



More information about the Spice-devel mailing list