[Spice-devel] [PATCH 12/17] Convert message writing from C style to C++ style

Lukáš Hrázký lhrazky at redhat.com
Mon Feb 26 10:05:55 UTC 2018


Get rid of the 'make()' method and the 'msg' member, create the payload
on the stack in 'write()' and write the header and message in two
writes.
---
It does split the write for FormatMessage into two, but makes the code a
bit simpler I think. It reduces the number of combinations the messages
are constructed/written. Untested, but should work...

 src/spice-streaming-agent.cpp | 25 ++++++-------------------
 1 file changed, 6 insertions(+), 19 deletions(-)

diff --git a/src/spice-streaming-agent.cpp b/src/spice-streaming-agent.cpp
index c174ea4..109b683 100644
--- a/src/spice-streaming-agent.cpp
+++ b/src/spice-streaming-agent.cpp
@@ -95,12 +95,10 @@ struct Message
               .padding = 0,     // Workaround GCC bug "sorry: not implemented"
               .type = Info::type,
               .size = (uint32_t) (Info::size(payload...) - sizeof(hdr))
-          }),
-          msg(Info::make(payload...))
+          })
     { }
 
     StreamDevHeader hdr;
-    Payload         msg;
 };
 
 
@@ -112,13 +110,10 @@ struct FormatMessage : Message<StreamMsgFormat, FormatMessage>
     {
         return sizeof(FormatMessage);
     }
-    static StreamMsgFormat make(unsigned w, unsigned h, uint8_t c)
-    {
-        return StreamMsgFormat{ .width = w, .height = h, .codec = c, .padding1 = {} };
-    }
     size_t write(Stream &stream, unsigned w, unsigned h, uint8_t c)
     {
-        return stream.write_all(this, sizeof(*this));
+        StreamMsgFormat msg{ .width = w, .height = h, .codec = c, .padding1 = {} };
+        return stream.write_all(&hdr, sizeof(hdr)) + stream.write_all(&msg, sizeof(msg));
     }
 };
 
@@ -131,10 +126,6 @@ struct FrameMessage : Message<StreamMsgData, FrameMessage>
     {
         return sizeof(FrameMessage) + length;
     }
-    static StreamMsgData make(const void *frame, size_t length)
-    {
-        return StreamMsgData();
-    }
     size_t write(Stream &stream, const void *frame, size_t length)
     {
         return stream.write_all(&hdr, sizeof(hdr)) + stream.write_all(frame, length);
@@ -158,10 +149,9 @@ struct X11CursorMessage : Message<StreamMsgCursorSet, X11CursorMessage>
     {
         return sizeof(X11CursorMessage) + sizeof(uint32_t) * pixel_size(cursor);
     }
-    static StreamMsgCursorSet make(XFixesCursorImage *cursor)
+    size_t write(Stream &stream, XFixesCursorImage *cursor)
     {
-        return StreamMsgCursorSet
-        {
+        StreamMsgCursorSet msg{
             .width = cursor->width,
             .height = cursor->height,
             .hot_spot_x = cursor->xhot,
@@ -170,10 +160,7 @@ struct X11CursorMessage : Message<StreamMsgCursorSet, X11CursorMessage>
             .padding1 = { },
             .data = { }
         };
-    }
-    size_t write(Stream &stream, XFixesCursorImage *cursor)
-    {
-        return stream.write_all(&hdr, sizeof(hdr)) + stream.write_all(pixels.get(), hdr.size);
+        return stream.write_all(&hdr, sizeof(hdr)) + stream.write_all(&msg, sizeof(msg)) + stream.write_all(pixels.get(), hdr.size);
     }
     void fill_pixels(XFixesCursorImage *cursor)
     {
-- 
2.16.1



More information about the Spice-devel mailing list