[Spice-devel] [PATCH spice-streaming-agent 5/9] Unify the structure of write_all() and read_all()

Frediano Ziglio fziglio at redhat.com
Tue May 15 20:44:35 UTC 2018


A bit more comment would be good. For instance why not unifying the other way
around?

> 
> Signed-off-by: Lukáš Hrázký <lhrazky at redhat.com>
> ---
>  src/stream-port.cpp | 20 +++++++++++---------
>  src/stream-port.hpp |  4 ++--
>  2 files changed, 13 insertions(+), 11 deletions(-)
> 
> diff --git a/src/stream-port.cpp b/src/stream-port.cpp
> index 526c564..cee63ac 100644
> --- a/src/stream-port.cpp
> +++ b/src/stream-port.cpp
> @@ -17,10 +17,10 @@
>  namespace spice {
>  namespace streaming_agent {
>  
> -void read_all(int fd, void *msg, size_t len)
> +void read_all(int fd, void *buf, size_t len)
>  {
>      while (len > 0) {
> -        ssize_t n = read(fd, msg, len);
> +        ssize_t n = read(fd, buf, len);
>  
>          if (n < 0) {
>              if (errno == EINTR) {
> @@ -30,22 +30,24 @@ void read_all(int fd, void *msg, size_t len)
>          }
>  
>          len -= n;
> -        msg = (uint8_t *) msg + n;
> +        buf = (uint8_t *) buf + n;
>      }
>  }
>  
> -void write_all(int fd, const void *buf, const size_t len)
> +void write_all(int fd, const void *buf, size_t len)
>  {
> -    size_t written = 0;
> -    while (written < len) {
> -        int l = write(fd, (const char *) buf + written, len - written);
> -        if (l < 0) {
> +    while (len > 0) {
> +        ssize_t n = write(fd, buf, len);
> +
> +        if (n < 0) {
>              if (errno == EINTR) {
>                  continue;
>              }
>              throw WriteError("Writing message to device failed", errno);
>          }
> -        written += l;
> +
> +        len -= n;
> +        buf = (uint8_t *) buf + n;
>      }
>  }
>  
> diff --git a/src/stream-port.hpp b/src/stream-port.hpp
> index 7780a37..b2d8352 100644
> --- a/src/stream-port.hpp
> +++ b/src/stream-port.hpp
> @@ -13,8 +13,8 @@
>  namespace spice {
>  namespace streaming_agent {
>  
> -void read_all(int fd, void *msg, size_t len);
> -void write_all(int fd, const void *buf, const size_t len);
> +void read_all(int fd, void *buf, size_t len);
> +void write_all(int fd, const void *buf, size_t len);
>  
>  }} // namespace spice::streaming_agent
>  

Frediano


More information about the Spice-devel mailing list