[Spice-devel] [PATCH spice-server 08/23] websocket: New interface to create RedsWebSocket
Frediano Ziglio
fziglio at redhat.com
Tue Jun 25 16:11:32 UTC 2019
Less coupling. This is a preparation for next patch.
Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
server/red-stream.c | 31 +++++++++++--------------------
server/websocket.c | 25 +++++++++++++++++++++++++
server/websocket.h | 8 +++++---
3 files changed, 41 insertions(+), 23 deletions(-)
diff --git a/server/red-stream.c b/server/red-stream.c
index d6a00dd9f..fd3fdced8 100644
--- a/server/red-stream.c
+++ b/server/red-stream.c
@@ -1215,28 +1215,19 @@ bool red_stream_is_websocket(RedStream *stream, const void *buf, size_t 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 = g_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 1b18aa614..03d931023 100644
--- a/server/websocket.c
+++ b/server/websocket.c
@@ -478,3 +478,28 @@ void websocket_create_reply(char *buf, char *outbuf)
"Sec-WebSocket-Protocol: binary\r\n\r\n", key);
g_free(key);
}
+
+RedsWebSocket *websocket_new(char *rbuf, struct RedStream *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 = g_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 173109050..ab9eb8126 100644
--- a/server/websocket.h
+++ b/server/websocket.h
@@ -41,11 +41,13 @@ typedef struct {
uint64_t write_remainder;
struct RedStream *raw_stream;
- ssize_t (*raw_read)(struct RedStream *s, void *buf, size_t nbyte);
- ssize_t (*raw_write)(struct RedStream *s, const void *buf, size_t nbyte);
- ssize_t (*raw_writev)(struct RedStream *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(char *buf, struct RedStream *s, websocket_read_cb_t read_cb,
+ websocket_write_cb_t write_cb, websocket_writev_cb_t writev_cb);
bool websocket_is_start(char *buf);
void websocket_create_reply(char *buf, char *outbuf);
int websocket_read(RedsWebSocket *ws, uint8_t *buf, size_t len);
--
2.20.1
More information about the Spice-devel
mailing list