[Spice-devel] [PATCH spice-server 16/30] Better encapsulation

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


Move websocket structure declarations to C file.
Make some functions static as now not used externally.
Introduce a websocket_free function for symmetry.
---
 server/reds-stream.c |  2 +-
 server/websocket.c   | 36 +++++++++++++++++++++++++++++++++---
 server/websocket.h   | 32 +++-----------------------------
 3 files changed, 37 insertions(+), 33 deletions(-)

diff --git a/server/reds-stream.c b/server/reds-stream.c
index 125aa68..5c8bb80 100644
--- a/server/reds-stream.c
+++ b/server/reds-stream.c
@@ -344,7 +344,7 @@ void reds_stream_free(RedsStream *s)
         SSL_free(s->priv->ssl);
     }
 
-    free(s->priv->ws);
+    websocket_free(s->priv->ws);
 
     reds_stream_remove_watch(s);
     spice_info("close socket fd %d", s->socket);
diff --git a/server/websocket.c b/server/websocket.c
index de48b3b..6a7a96b 100644
--- a/server/websocket.c
+++ b/server/websocket.c
@@ -54,6 +54,31 @@
 
 #define WEBSOCKET_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
 
+#define WEBSOCKET_MAX_HEADER_SIZE (1 + 9 + 4)
+
+typedef struct {
+    int type;
+    int masked;
+    guint8 header[WEBSOCKET_MAX_HEADER_SIZE];
+    int header_pos;
+    int frame_ready:1;
+    guint8 mask[4];
+    guint64 relayed;
+    guint64 expected_len;
+} websocket_frame_t;
+
+struct RedsWebSocket {
+    gboolean closed;
+
+    websocket_frame_t read_frame;
+    guint64 write_remainder;
+
+    void *raw_stream;
+    websocket_read_cb_t raw_read;
+    websocket_write_cb_t raw_write;
+    websocket_writev_cb_t raw_writev;
+};
+
 static void websocket_ack_close(void *opaque, websocket_write_cb_t write_cb);
 
 /* Perform a case insensitive search for needle in haystack.
@@ -446,7 +471,7 @@ static void websocket_ack_close(void *opaque, websocket_write_cb_t write_cb)
     write_cb(opaque, header, sizeof(header));
 }
 
-bool websocket_is_start(gchar *buf)
+static bool websocket_is_start(gchar *buf)
 {
     if (strncmp(buf, "GET ", 4) == 0 &&
             // TODO strip, do not assume a single space
@@ -458,7 +483,7 @@ bool websocket_is_start(gchar *buf)
     return FALSE;
 }
 
-void websocket_create_reply(gchar *buf, gchar *outbuf)
+static void websocket_create_reply(gchar *buf, gchar *outbuf)
 {
     gchar *key;
 
@@ -471,7 +496,7 @@ void websocket_create_reply(gchar *buf, gchar *outbuf)
     g_free(key);
 }
 
-RedsWebSocket *websocket_new(gchar *rbuf, struct RedsStream *stream, websocket_read_cb_t read_cb,
+RedsWebSocket *websocket_new(gchar *rbuf, void *stream, websocket_read_cb_t read_cb,
                              websocket_write_cb_t write_cb, websocket_writev_cb_t writev_cb)
 {
     if (!websocket_is_start(rbuf)) {
@@ -495,3 +520,8 @@ RedsWebSocket *websocket_new(gchar *rbuf, struct RedsStream *stream, websocket_r
 
     return ws;
 }
+
+void websocket_free(RedsWebSocket *ws)
+{
+    free(ws);
+}
diff --git a/server/websocket.h b/server/websocket.h
index 853820e..0b62d57 100644
--- a/server/websocket.h
+++ b/server/websocket.h
@@ -15,41 +15,15 @@
  *  License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
-#define WEBSOCKET_MAX_HEADER_SIZE (1 + 9 + 4)
-
-struct RedsStream;
-
-typedef struct {
-    int type;
-    int masked;
-    guint8 header[WEBSOCKET_MAX_HEADER_SIZE];
-    int header_pos;
-    int frame_ready:1;
-    guint8 mask[4];
-    guint64 relayed;
-    guint64 expected_len;
-} websocket_frame_t;
-
 typedef ssize_t (*websocket_read_cb_t)(void *opaque, const void *buf, size_t nbyte);
 typedef ssize_t (*websocket_write_cb_t)(void *opaque, const void *buf, size_t nbyte);
 typedef ssize_t (*websocket_writev_cb_t)(void *opaque, struct iovec *iov, int iovcnt);
 
-typedef struct {
-    int closed;
-
-    websocket_frame_t read_frame;
-    guint64 write_remainder;
-
-    struct RedsStream *raw_stream;
-    websocket_read_cb_t raw_read;
-    websocket_write_cb_t raw_write;
-    websocket_writev_cb_t raw_writev;
-} RedsWebSocket;
+typedef struct RedsWebSocket RedsWebSocket;
 
-RedsWebSocket *websocket_new(gchar *buf, struct RedsStream *s, websocket_read_cb_t read_cb,
+RedsWebSocket *websocket_new(gchar *buf, void *stream, 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);
+void websocket_free(RedsWebSocket *ws);
 int websocket_read(RedsWebSocket *ws, guchar *buf, size_t len);
 int websocket_write(RedsWebSocket *ws, const void *buf, size_t len);
 int websocket_writev(RedsWebSocket *ws, const struct iovec *iov, int iovcnt);
-- 
2.7.4



More information about the Spice-devel mailing list