[Spice-devel] [PATCH spice 1/2] char-device: separate functions to get write buffer for client and server

Lukáš Hrázký lhrazky at redhat.com
Wed Nov 7 16:07:02 UTC 2018


Instead of having a single red_char_device_write_buffer_get function to
get both client and server buffers and decide by testing client == NULL,
have separate function for a client and for a server. The situation
should always be clear (you're either on the client or on the server
side) and you shouldn't need to parametrize that.

For the server case, add a use_token parameter instead of a separate
red_char_device_write_buffer_get_server_no_token function, as you may
want to parametrize that.

Signed-off-by: Lukáš Hrázký <lhrazky at redhat.com>
---
 server/char-device.c              | 22 ++++++++++++----------
 server/char-device.h              | 12 ++++++++----
 server/red-stream-device.c        |  6 +++---
 server/reds.c                     | 17 +++++++++--------
 server/smartcard-channel-client.c |  3 +--
 server/smartcard.c                |  8 ++++++--
 server/spicevmc.c                 | 12 ++++++------
 7 files changed, 45 insertions(+), 35 deletions(-)

diff --git a/server/char-device.c b/server/char-device.c
index ae538fab..6df04660 100644
--- a/server/char-device.c
+++ b/server/char-device.c
@@ -595,20 +595,22 @@ error:
     return NULL;
 }
 
-RedCharDeviceWriteBuffer *red_char_device_write_buffer_get(RedCharDevice *dev,
-                                                           RedClient *client,
-                                                           int size)
+RedCharDeviceWriteBuffer *red_char_device_write_buffer_get_client(RedCharDevice *dev,
+                                                                  RedClient *client,
+                                                                  int size)
 {
-   return  __red_char_device_write_buffer_get(dev, client, size,
-             client ? WRITE_BUFFER_ORIGIN_CLIENT : WRITE_BUFFER_ORIGIN_SERVER,
-             0);
+    spice_assert(client);
+    return  __red_char_device_write_buffer_get(dev, client, size,
+            WRITE_BUFFER_ORIGIN_CLIENT,
+            0);
 }
 
-RedCharDeviceWriteBuffer *red_char_device_write_buffer_get_server_no_token(
-    RedCharDevice *dev, int size)
+RedCharDeviceWriteBuffer *red_char_device_write_buffer_get_server(RedCharDevice *dev,
+                                                                  int size,
+                                                                  bool use_token)
 {
-   return  __red_char_device_write_buffer_get(dev, NULL, size,
-             WRITE_BUFFER_ORIGIN_SERVER_NO_TOKEN, 0);
+    return  __red_char_device_write_buffer_get(dev, NULL, size,
+            use_token ? WRITE_BUFFER_ORIGIN_SERVER : WRITE_BUFFER_ORIGIN_SERVER_NO_TOKEN, 0);
 }
 
 static RedCharDeviceWriteBuffer *red_char_device_write_buffer_ref(RedCharDeviceWriteBuffer *write_buf)
diff --git a/server/char-device.h b/server/char-device.h
index dccd576d..893d3e4b 100644
--- a/server/char-device.h
+++ b/server/char-device.h
@@ -218,10 +218,14 @@ void red_char_device_send_to_client_tokens_set(RedCharDevice *dev,
                                                uint32_t tokens);
 /** Write to device **/
 
-RedCharDeviceWriteBuffer *red_char_device_write_buffer_get(RedCharDevice *dev,
-                                                           RedClient *client, int size);
-RedCharDeviceWriteBuffer *red_char_device_write_buffer_get_server_no_token(
-    RedCharDevice *dev, int size);
+RedCharDeviceWriteBuffer *red_char_device_write_buffer_get_client(RedCharDevice *dev,
+                                                                  RedClient *client,
+                                                                  int size);
+
+/* Returns NULL if use_token == true and no tokens are available */
+RedCharDeviceWriteBuffer *red_char_device_write_buffer_get_server(RedCharDevice *dev,
+                                                                  int size,
+                                                                  bool use_token);
 
 /* Either add the buffer to the write queue or release it */
 void red_char_device_write_buffer_add(RedCharDevice *dev,
diff --git a/server/red-stream-device.c b/server/red-stream-device.c
index fb036b93..215ddbe7 100644
--- a/server/red-stream-device.c
+++ b/server/red-stream-device.c
@@ -228,7 +228,7 @@ handle_msg_invalid(StreamDevice *dev, SpiceCharDeviceInstance *sin, const char *
 
     RedCharDevice *char_dev = RED_CHAR_DEVICE(dev);
     RedCharDeviceWriteBuffer *buf =
-        red_char_device_write_buffer_get_server_no_token(char_dev, total_size);
+        red_char_device_write_buffer_get_server(char_dev, total_size, false);
     buf->buf_used = total_size;
 
     StreamDevHeader *const hdr = (StreamDevHeader *)buf->buf;
@@ -517,7 +517,7 @@ stream_device_stream_start(void *opaque, StreamMsgStartStop *start,
 
     RedCharDevice *char_dev = RED_CHAR_DEVICE(dev);
     RedCharDeviceWriteBuffer *buf =
-        red_char_device_write_buffer_get_server_no_token(char_dev, total_size);
+        red_char_device_write_buffer_get_server(char_dev, total_size, false);
     buf->buf_used = total_size;
 
     StreamDevHeader *hdr = (StreamDevHeader *)buf->buf;
@@ -662,7 +662,7 @@ send_capabilities(RedCharDevice *char_dev)
     int total_size = sizeof(StreamDevHeader) + msg_size;
 
     RedCharDeviceWriteBuffer *buf =
-        red_char_device_write_buffer_get_server_no_token(char_dev, total_size);
+        red_char_device_write_buffer_get_server(char_dev, total_size, false);
     buf->buf_used = total_size;
 
     StreamDevHeader *const hdr = (StreamDevHeader *)buf->buf;
diff --git a/server/reds.c b/server/reds.c
index 575470e4..b5fc8e1c 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -562,8 +562,8 @@ void reds_client_disconnect(RedsState *reds, RedClient *client)
             uint32_t total_msg_size;
 
             total_msg_size = sizeof(VDIChunkHeader) + sizeof(VDAgentMessage);
-            char_dev_buf = red_char_device_write_buffer_get_server_no_token(
-                               RED_CHAR_DEVICE(reds->agent_dev), total_msg_size);
+            char_dev_buf = red_char_device_write_buffer_get_server(
+                               RED_CHAR_DEVICE(reds->agent_dev), total_msg_size, false);
             char_dev_buf->buf_used = total_msg_size;
             internal_buf = (VDInternalBuf *)char_dev_buf->buf;
             internal_buf->chunk_header.port = VDP_SERVER_PORT;
@@ -936,9 +936,9 @@ void reds_handle_agent_mouse_event(RedsState *reds, const VDAgentMouseState *mou
 
     total_msg_size = sizeof(VDIChunkHeader) + sizeof(VDAgentMessage) +
                      sizeof(VDAgentMouseState);
-    char_dev_buf = red_char_device_write_buffer_get(RED_CHAR_DEVICE(reds->agent_dev),
-                                                    NULL,
-                                                    total_msg_size);
+    char_dev_buf = red_char_device_write_buffer_get_server(RED_CHAR_DEVICE(reds->agent_dev),
+                                                           total_msg_size,
+                                                           true);
 
     if (!char_dev_buf) {
         reds->pending_mouse_event = TRUE;
@@ -1094,9 +1094,10 @@ uint8_t *reds_get_agent_data_buffer(RedsState *reds, MainChannelClient *mcc, siz
 
     spice_assert(dev->priv->recv_from_client_buf == NULL);
     client = red_channel_client_get_client(RED_CHANNEL_CLIENT(mcc));
-    dev->priv->recv_from_client_buf = red_char_device_write_buffer_get(RED_CHAR_DEVICE(dev),
-                                                                       client,
-                                                                       size + sizeof(VDIChunkHeader));
+    dev->priv->recv_from_client_buf =
+        red_char_device_write_buffer_get_client(RED_CHAR_DEVICE(dev),
+                                                client,
+                                                size + sizeof(VDIChunkHeader));
     /* check if buffer was allocated, as flow control is enabled for
      * this device this is a normal condition */
     if (!dev->priv->recv_from_client_buf) {
diff --git a/server/smartcard-channel-client.c b/server/smartcard-channel-client.c
index 1ae3ae3e..3b3fc27c 100644
--- a/server/smartcard-channel-client.c
+++ b/server/smartcard-channel-client.c
@@ -144,8 +144,7 @@ smartcard_channel_client_alloc_msg_rcv_buf(RedChannelClient *rcc,
         spice_assert(smartcard_char_device_get_client(smartcard) || scc->priv->smartcard);
         spice_assert(!scc->priv->write_buf);
         scc->priv->write_buf =
-            red_char_device_write_buffer_get(RED_CHAR_DEVICE(smartcard), client,
-                                             size);
+            red_char_device_write_buffer_get_client(RED_CHAR_DEVICE(smartcard), client, size);
 
         if (!scc->priv->write_buf) {
             spice_error("failed to allocate write buffer");
diff --git a/server/smartcard.c b/server/smartcard.c
index 2cb68e06..5a47814d 100644
--- a/server/smartcard.c
+++ b/server/smartcard.c
@@ -308,7 +308,9 @@ void smartcard_char_device_notify_reader_add(RedCharDeviceSmartcard *dev)
     RedCharDeviceWriteBuffer *write_buf;
     VSCMsgHeader *vheader;
 
-    write_buf = red_char_device_write_buffer_get(RED_CHAR_DEVICE(dev), NULL, sizeof(*vheader));
+    write_buf = red_char_device_write_buffer_get_server(RED_CHAR_DEVICE(dev),
+                                                        sizeof(*vheader),
+                                                        true);
     if (!write_buf) {
         spice_error("failed to allocate write buffer");
         return;
@@ -355,7 +357,9 @@ gboolean smartcard_char_device_notify_reader_remove(RedCharDeviceSmartcard *dev)
         spice_debug("reader add was never sent to the device");
         return FALSE;
     }
-    write_buf = red_char_device_write_buffer_get(RED_CHAR_DEVICE(dev), NULL, sizeof(*vheader));
+    write_buf = red_char_device_write_buffer_get_server(RED_CHAR_DEVICE(dev),
+                                                        sizeof(*vheader),
+                                                        true);
     if (!write_buf) {
         spice_error("failed to allocate write buffer");
         return FALSE;
diff --git a/server/spicevmc.c b/server/spicevmc.c
index c2de5037..c03bc400 100644
--- a/server/spicevmc.c
+++ b/server/spicevmc.c
@@ -510,9 +510,9 @@ static bool handle_compressed_msg(RedVmcChannel *channel, RedChannelClient *rcc,
     int decompressed_size;
     RedCharDeviceWriteBuffer *write_buf;
 
-    write_buf = red_char_device_write_buffer_get(channel->chardev,
-                                                 red_channel_client_get_client(rcc),
-                                                 compressed_data_msg->uncompressed_size);
+    write_buf = red_char_device_write_buffer_get_client(channel->chardev,
+                                                        red_channel_client_get_client(rcc),
+                                                        compressed_data_msg->uncompressed_size);
     if (!write_buf) {
         return FALSE;
     }
@@ -596,9 +596,9 @@ static uint8_t *spicevmc_red_channel_alloc_msg_rcv_buf(RedChannelClient *rcc,
 
         assert(!channel->recv_from_client_buf);
 
-        channel->recv_from_client_buf = red_char_device_write_buffer_get(channel->chardev,
-                                                                         client,
-                                                                         size);
+        channel->recv_from_client_buf = red_char_device_write_buffer_get_client(channel->chardev,
+                                                                                client,
+                                                                                size);
         if (!channel->recv_from_client_buf) {
             spice_error("failed to allocate write buffer");
             return NULL;
-- 
2.19.1



More information about the Spice-devel mailing list