From: Michal 'vorner' Vaner Date: Thu, 5 Jan 2012 08:53:57 +0000 (+0100) Subject: Merge branch 'review/sockreq' into scfinal X-Git-Tag: trac2351_base~304^2~15 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f68758a5f5ebb9bc2405202ee5455944f3d95600;p=thirdparty%2Fkea.git Merge branch 'review/sockreq' into scfinal Conflicts: src/lib/server_common/socket_request.cc src/lib/util/io/fd.cc --- f68758a5f5ebb9bc2405202ee5455944f3d95600 diff --cc src/lib/util/io/fd.cc index f9b17a783c,2797015a11..49aac397a1 --- a/src/lib/util/io/fd.cc +++ b/src/lib/util/io/fd.cc @@@ -23,30 -23,20 +23,30 @@@ namespace io bool write_data(const int fd, const void *buffer_v, const size_t length) { - const unsigned char* buffer(static_cast(buffer_v)); - size_t rest(length); + size_t remaining = length; // Amount remaining to be written + + // Just keep writing until all is written - while (rest) { - const int written = write(fd, buffer, rest); + while (remaining > 0) { - ssize_t amount = write(fd, buffer, remaining); - if (amount == -1) { - // Some error. Ignore interrupted system calls otherwise return - // an error indication. - if (errno != EINTR) { - return false; ++ const int written = write(fd, buffer, remaining); + if (written == -1) { + if (errno == EINTR) { // Just keep going + continue; + } else { + return (false); } - } else { // Wrote something - rest -= written; + - } else if (amount > 0) { - // Wrote "amount" bytes from the buffer - remaining -= amount; - buffer += amount; ++ } else if (written > 0) { ++ // Wrote "written" bytes from the buffer ++ remaining -= written; + buffer += written; + + } else { + // Wrote zero bytes from the buffer. We should not get here as any + // error that causes zero bytes to be written should have returned + // -1. However, write(2) can return 0, and in this case we + // interpret it as an error. + return (false); } } return (true); @@@ -54,32 -44,25 +54,29 @@@ ssize_t read_data(const int fd, void *buffer_v, const size_t length) { - - unsigned char *buffer(static_cast(buffer_v)); - size_t rest(length), already(0); - while (rest) { // Stil something to read - const int amount = read(fd, buffer, rest); + unsigned char* buffer(static_cast(buffer_v)); + size_t remaining = length; // Amount remaining to be read + + while (remaining > 0) { - ssize_t amount = read(fd, buffer, remaining); ++ const int amount = read(fd, buffer, remaining); if (amount == -1) { - // Some error. Ignore interrupted system calls otherwise return - // an error indication. - if (errno != EINTR) { - return -1; + if (errno == EINTR) { // Continue on interrupted call + continue; + } else { + return (-1); } - - } else if (amount) { - already += amount; - rest -= amount; + } else if (amount > 0) { + // Read "amount" bytes into the buffer + remaining -= amount; buffer += amount; - - } else { // EOF - return (already); + } else { + // EOF - end the read + break; } } - return (already); + + // Return total number of bytes read + return (static_cast(length - remaining)); } }