In all paths errors from this function are treated like fatal
error, there's no need to handle all manually potentially
forgetting to handle errors.
Also avoid to deal directly with logging moving the responsibility
to other layers.

Signed-off-by: Frediano Ziglio <fzig...@redhat.com>
---
 src/spice-streaming-agent.cpp | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)

diff --git a/src/spice-streaming-agent.cpp b/src/spice-streaming-agent.cpp
index 1f41a6f..80e3408 100644
--- a/src/spice-streaming-agent.cpp
+++ b/src/spice-streaming-agent.cpp
@@ -86,32 +86,24 @@ static int read_command_from_device(void)
     std::lock_guard<std::mutex> stream_guard(stream_mtx);
     n = read(streamfd, &hdr, sizeof(hdr));
     if (n != sizeof(hdr)) {
-        syslog(LOG_WARNING,
-               "read command from device FAILED -- read %d expected %lu\n",
-               n, sizeof(hdr));
-        return -1;
+        throw std::runtime_error("read command from device FAILED -- read " + 
std::to_string(n) +
+                                 " expected " + std::to_string(sizeof(hdr)));
     }
     if (hdr.protocol_version != STREAM_DEVICE_PROTOCOL) {
-        syslog(LOG_WARNING, "BAD VERSION %d (expected is %d)\n", 
hdr.protocol_version,
-               STREAM_DEVICE_PROTOCOL);
-        return -1;
+        throw std::runtime_error("BAD VERSION " + 
std::to_string(hdr.protocol_version) +
+                                 " (expected is " + 
std::to_string(STREAM_DEVICE_PROTOCOL) + ")");
     }
     if (hdr.type != STREAM_TYPE_START_STOP) {
-        syslog(LOG_WARNING, "UNKNOWN msg of type %d\n", hdr.type);
-        return -1;
+        throw std::runtime_error("UNKNOWN msg of type " + 
std::to_string(hdr.type));
     }
     if (hdr.size >= sizeof(msg)) {
-        syslog(LOG_WARNING,
-               "msg size (%d) is too long (longer than %lu)\n",
-               hdr.size, sizeof(msg));
-        return -1;
+        throw std::runtime_error("msg size (" + std::to_string(hdr.size) + ") 
is too long "
+                                 "(longer than " + std::to_string(sizeof(msg)) 
+ ")");
     }
     n = read(streamfd, &msg, hdr.size);
     if (n != hdr.size) {
-        syslog(LOG_WARNING,
-               "read command from device FAILED -- read %d expected %d\n",
-               n, hdr.size);
-        return -1;
+        throw std::runtime_error("read command from device FAILED -- read " + 
std::to_string(n) +
+                                 " expected " + std::to_string(hdr.size));
     }
     streaming_requested = (msg[0] != 0); /* num_codecs */
     syslog(LOG_INFO, "GOT START_STOP message -- request to %s streaming\n",
-- 
2.14.3

_______________________________________________
Spice-devel mailing list
Spice-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/spice-devel

Reply via email to