Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / os_linux.c
index e3dc5e7d616d6b5de099382cb23e431433ea63f0..f48e552ce1b9be5b2f53207d8c3bc97008b6b533 100644 (file)
  * $Id$
  *
  */
+#define _XOPEN_SOURCE  /* make limits.h #define IOV_MAX */
+
 #include "ircd_osdep.h"
+#include "msgq.h"
 
 #include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <limits.h>
 #include <stdio.h>
 #include <netinet/in.h>
 #include <string.h>
@@ -32,6 +36,7 @@
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/times.h>
+#include <sys/uio.h>
 #include <sys/param.h>
 #if 0
 #include <unistd.h>
@@ -253,6 +258,42 @@ IOResult os_send_nonb(int fd, const char* buf, unsigned int length,
   return IO_FAILURE;
 }
 
+/*
+ * os_sendv_nonb - non blocking writev to a connection
+ * returns:
+ *  1  if data was written
+ *    count_out contains amount written
+ *   
+ *  0  if write call blocked, recoverable error
+ *  -1 if an unrecoverable error occurred
+ */
+IOResult os_sendv_nonb(int fd, struct MsgQ* buf, unsigned int* count_in,
+                      unsigned int* count_out)
+{
+  int res;
+  int count;
+  struct iovec iov[IOV_MAX];
+
+  assert(0 != buf);
+  assert(0 != count_in);
+  assert(0 != count_out);
+
+  *count_in = 0;
+  *count_out = 0;
+  errno = 0;
+
+  count = msgq_mapiov(buf, iov, IOV_MAX, count_in);
+
+  if (-1 < (res = writev(fd, iov, count))) {
+    *count_out = (unsigned) res;
+    return IO_SUCCESS;
+  }
+  else if (EAGAIN == errno || ENOMEM == errno || ENOBUFS == errno)
+    return IO_BLOCKED;
+
+  return IO_FAILURE;
+}
+
 
 int os_connect_nonb(int fd, const struct sockaddr_in* sin)
 {