Get rid of E_SEND.
[paraslash.git] / net.c
diff --git a/net.c b/net.c
index 2991b6db5c42b58c0028c371890739323662bba7..f01cf5830e84eafa5e88890f5f8d9a68de4d0316 100644 (file)
--- a/net.c
+++ b/net.c
@@ -95,36 +95,28 @@ void init_sockaddr(struct sockaddr_in *addr, int port, const struct hostent *he)
 }
 
 /*
- * send out a buffer, resend on short writes
+ * Send out a buffer, resend on short writes.
  *
- * \param fd the file descriptor
- * \param buf The buffer to be sent
- * \param len The length of \a buf
- *
- * Due to circumstances beyond your control, the kernel might not send all the
- * data out in one chunk, and now, my friend, it's up to us to get the data out
- * there (Beej's Guide to Network Programming).
+ * \param fd The file descriptor.
+ * \param buf The buffer to be sent.
+ * \param len The length of \a buf.
  *
- * \return This function returns 1 on success and \a -E_SEND on errors. The
- * number of bytes actually sent is stored upon successful return in \a len.
+ * \return Standard. In any case, the number of bytes actually sent is stored
+ * in \a len.
  */
 static int sendall(int fd, const char *buf, size_t *len)
 {
-       size_t total = 0, bytesleft = *len; /* how many we have left to send */
-       int n = -1;
-
-       while (total < *len) {
-               n = send(fd, buf + total, bytesleft, 0);
-               if (n == -1)
-                       break;
-               total += n;
-               bytesleft -= n;
-               if (total < *len)
-                       PARA_DEBUG_LOG("short write (%zd byte(s) left)\n",
-                               *len - total);
+       size_t total = *len;
+
+       assert(total);
+       *len = 0;
+       while (*len < total) {
+               int ret = send(fd, buf + *len, total - *len, 0);
+               if (ret == -1)
+                       return -ERRNO_TO_PARA_ERROR(errno);
+               *len += ret;
        }
-       *len = total; /* return number actually sent here */
-       return n == -1? -E_SEND : 1; /* return 1 on success */
+       return 1;
 }
 
 /**
@@ -138,7 +130,7 @@ static int sendall(int fd, const char *buf, size_t *len)
  * out the buffer, encrypted or not, and try to resend the remaing part in case
  * of short writes.
  *
- * \return Positive on success, \p -E_SEND on errors.
+ * \return Standard.
  */
 int send_bin_buffer(int fd, const char *buf, size_t len)
 {
@@ -162,14 +154,14 @@ int send_bin_buffer(int fd, const char *buf, size_t len)
 }
 
 /**
- * encrypt and send null terminated buffer.
+ * Encrypt and send null terminated buffer.
  *
- * \param fd the file descriptor
- * \param buf the null-terminated buffer to be send
+ * \param fd The file descriptor.
+ * \param buf The null-terminated buffer to be send.
  *
  * This is equivalent to send_bin_buffer(fd, buf, strlen(buf)).
  *
- * \return Positive on success, \p -E_SEND on errors.
+ * \return Standard.
  */
 int send_buffer(int fd, const char *buf)
 {
@@ -178,12 +170,12 @@ int send_buffer(int fd, const char *buf)
 
 
 /**
- * send and encrypt a buffer given by a format string
+ * Send and encrypt a buffer given by a format string.
  *
- * \param fd the file descriptor
- * \param fmt a format string
+ * \param fd The file descriptor.
+ * \param fmt A format string.
  *
- * \return Positive on success, \p -E_SEND on errors.
+ * \return Standard.
  */
 __printf_2_3 int send_va_buffer(int fd, const char *fmt, ...)
 {