summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristophe de Dinechin <dinechin@redhat.com>2017-11-10 16:15:45 +0100
committerFrediano Ziglio <fziglio@redhat.com>2017-11-23 11:39:01 +0000
commitd86a2cfcbb05d92d3a4b08da306cfb267599169b (patch)
tree7f273c7b50a7c8ba17812adf5e5f720c1d818a3f
parent3f3d9d535f48f15bc1a1d4ddf5a6214994ccfaae (diff)
Use RAII to cleanup stream in case of exception or return
This lets us get rid of C-style 'goto done' in do_capture. Signed-off-by: Christophe de Dinechin <dinechin@redhat.com>
-rw-r--r--src/spice-streaming-agent.cpp32
1 files changed, 20 insertions, 12 deletions
diff --git a/src/spice-streaming-agent.cpp b/src/spice-streaming-agent.cpp
index 53ffbf0..97581c4 100644
--- a/src/spice-streaming-agent.cpp
+++ b/src/spice-streaming-agent.cpp
@@ -52,6 +52,23 @@ struct SpiceStreamDataMessage
StreamMsgData msg;
};
+struct Stream
+{
+ Stream(const char *name, int &fd): fd(fd)
+ {
+ fd = open(name, O_RDWR);
+ if (fd < 0)
+ throw std::runtime_error("failed to open streaming device");
+ }
+ ~Stream()
+ {
+ if (fd >= 0)
+ close(fd);
+ fd = -1;
+ }
+ int &fd;
+};
+
static int streaming_requested;
static bool quit;
static int streamfd = -1;
@@ -353,17 +370,14 @@ do_capture(const char *streamport, FILE *f_log)
if (!capture)
throw std::runtime_error("cannot find a suitable capture system");
- streamfd = open(streamport, O_RDWR);
- if (streamfd < 0)
- // TODO was syslog(LOG_ERR, "Failed to open %s: %s\n", streamport, strerror(errno));
- throw std::runtime_error("failed to open streaming device");
+ Stream stream(streamport, streamfd);
unsigned int frame_count = 0;
while (! quit) {
while (!quit && !streaming_requested) {
if (read_command(true) < 0) {
syslog(LOG_ERR, "FAILED to read command\n");
- goto done;
+ return;
}
}
@@ -414,19 +428,13 @@ do_capture(const char *streamport, FILE *f_log)
//usleep(1);
if (read_command(false) < 0) {
syslog(LOG_ERR, "FAILED to read command\n");
- goto done;
+ return;
}
if (!streaming_requested) {
capture->Reset();
}
}
}
-
-done:
- if (streamfd >= 0) {
- close(streamfd);
- streamfd = -1;
- }
}
#define arg_error(...) syslog(LOG_ERR, ## __VA_ARGS__);