Re: [PATCH spice-streaming-agent v2 3/4] Use RAII to cleanup stream in case of exception or return

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Frediano Ziglio writes:

>>
>> From: Christophe de Dinechin <dinechin@xxxxxxxxxx>
>>
>> This lets us get rid of C-style 'goto done' in do_capture.
>>
>> Signed-off-by: Christophe de Dinechin <dinechin@xxxxxxxxxx>
>> ---
>>  src/spice-streaming-agent.cpp | 32 ++++++++++++++++++++------------
>>  1 file changed, 20 insertions(+), 12 deletions(-)
>>
>> diff --git a/src/spice-streaming-agent.cpp b/src/spice-streaming-agent.cpp
>> index 1090517..8300cf2 100644
>> --- a/src/spice-streaming-agent.cpp
>> +++ b/src/spice-streaming-agent.cpp
>> @@ -51,6 +51,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;
>> +};
>> +
>
> Maybe you want to disable copy.
> Why you started indenting with 2 spaces instead of 4 ?

A mistake. Apparently, my custom 'ddd' c-style configuration for Emacs is
no longer accepted as a valid value for c-default-style, because some
idiot made it a "radio" type (enum-like) so now only official c-style
values are valid as default.

>
>>  static int streaming_requested;
>>  static bool quit;
>>  static int streamfd = -1;
>> @@ -349,17 +366,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(1) < 0) {
>>                  syslog(LOG_ERR, "FAILED to read command\n");
>> -                goto done;
>> +                return;
>>              }
>>          }
>>
>> @@ -410,19 +424,13 @@ do_capture(const char *streamport, FILE *f_log)
>>              //usleep(1);
>>              if (read_command(0) < 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__);
>
> Frediano
> _______________________________________________
> Spice-devel mailing list
> Spice-devel@xxxxxxxxxxxxxxxxxxxxx
> https://lists.freedesktop.org/mailman/listinfo/spice-devel


--
Cheers,
Christophe de Dinechin (IRC c3d)
_______________________________________________
Spice-devel mailing list
Spice-devel@xxxxxxxxxxxxxxxxxxxxx
https://lists.freedesktop.org/mailman/listinfo/spice-devel




[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]     [Monitors]