[svn] commit: r3147 - in /branches/vorner-sockcreator/src/bin/sockcreator: sockcreator.cc sockcreator.h
BIND 10 source code commits
bind10-changes at lists.isc.org
Fri Oct 8 18:07:35 UTC 2010
Author: vorner
Date: Fri Oct 8 18:07:34 2010
New Revision: 3147
Log:
Read and write wrappers
To be able to live without short reads. Needed by tests as well.
Modified:
branches/vorner-sockcreator/src/bin/sockcreator/sockcreator.cc
branches/vorner-sockcreator/src/bin/sockcreator/sockcreator.h
Modified: branches/vorner-sockcreator/src/bin/sockcreator/sockcreator.cc
==============================================================================
--- branches/vorner-sockcreator/src/bin/sockcreator/sockcreator.cc (original)
+++ branches/vorner-sockcreator/src/bin/sockcreator/sockcreator.cc Fri Oct 8 18:07:34 2010
@@ -13,6 +13,9 @@
// PERFORMANCE OF THIS SOFTWARE.
#include "sockcreator.h"
+
+#include <unistd.h>
+#include <cerrno>
namespace isc {
namespace socket_creator {
@@ -35,5 +38,47 @@
// TODO Implement
}
+bool
+write_data(const int fd, const char *buffer, const size_t length) {
+ size_t rest(length);
+ // Just keep writing until all is written
+ while(rest) {
+ ssize_t written(write(fd, buffer, rest));
+ if(rest == -1) {
+ if(errno == EINTR) { // Just keep going
+ continue;
+ } else {
+ return false;
+ }
+ } else { // Wrote something
+ rest -= written;
+ buffer += written;
+ }
+ }
+ return true;
+}
+
+ssize_t
+read_data(const int fd, char *buffer, const size_t length) {
+ size_t rest(length), already(0);
+ while(rest) { // Stil something to read
+ ssize_t amount(read(fd, buffer, rest));
+ if(rest == -1) {
+ if(errno == EINTR) { // Continue on interrupted call
+ continue;
+ } else {
+ return -1;
+ }
+ } else if(amount) {
+ already += amount;
+ rest -= amount;
+ buffer += amount;
+ } else { // EOF
+ return already;
+ }
+ }
+ return already;
+}
+
} // End of the namespaces
}
Modified: branches/vorner-sockcreator/src/bin/sockcreator/sockcreator.h
==============================================================================
--- branches/vorner-sockcreator/src/bin/sockcreator/sockcreator.h (original)
+++ branches/vorner-sockcreator/src/bin/sockcreator/sockcreator.h Fri Oct 8 18:07:34 2010
@@ -96,6 +96,38 @@
const get_sock_t get_sock_fun = get_sock,
const send_fd_t send_fd_fun = send_fd);
+/*
+ * \short write() that writes everything.
+ * Wrapper around write(). The difference is, it never writes less data
+ * and looks successfull (eg. it blocks until all data are written).
+ * Retries on signals.
+ *
+ * \return True if sucessfull, false otherwise. The errno variable is left
+ * intact.
+ * \param fd Where to write.
+ * \param data The buffer to write.
+ * \param length How much data is there to write.
+ *
+ * TODO: Shouldn't this be in some kind of library?
+ */
+bool
+write_data(const int fd, const char *data, const size_t length);
+
+/*
+ * \short read() that reads everything.
+ * Wrapper around read(). It does not do short reads, if it returns less,
+ * it means there was EOF. It retries on signals.
+ *
+ * \return Number of bytes read or -1 on error.
+ * \param fd Where to read data from.
+ * \param data Where to put the data.
+ * \param length How many of them.
+ *
+ * TODO: Shouldn't this be in some kind of library?
+ */
+ssize_t
+read_data(const int fd, char *buffer, const size_t length);
+
} // End of the namespaces
}
More information about the bind10-changes
mailing list