libcutils: add multi-buffer socket send.
Unix and Windows both have functions to write multiple buffers to a
socket with a single call but they have very different signatures. This
CL creates some cross-platform functions to be able to perform these
operations in a uniform way, which will be required for upcoming
fastboot functionality.
This CL also fixes some inconsistent spacing in the touched files.
Bug: http://b/26558551
Change-Id: I8f14d52d3a1de1f3b464267666d6cd3b54263238
diff --git a/include/cutils/sockets.h b/include/cutils/sockets.h
index 6cffe12..cb9b3ff 100644
--- a/include/cutils/sockets.h
+++ b/include/cutils/sockets.h
@@ -30,12 +30,15 @@
typedef int socklen_t;
typedef SOCKET cutils_socket_t;
+typedef WSABUF cutils_socket_buffer_t;
#else
#include <sys/socket.h>
+#include <sys/uio.h>
typedef int cutils_socket_t;
+typedef struct iovec cutils_socket_buffer_t;
#define INVALID_SOCKET (-1)
#endif
@@ -137,6 +140,28 @@
int socket_get_local_port(cutils_socket_t sock);
/*
+ * Sends to a socket from multiple buffers; wraps writev() on Unix or WSASend()
+ * on Windows. This can give significant speedup compared to calling send()
+ * multiple times.
+ *
+ * Because Unix and Windows use different structs to hold buffers, we also
+ * need a generic function to set up the buffers.
+ *
+ * Example usage:
+ * cutils_socket_buffer_t buffers[2] = {
+ * make_cutils_socket_buffer(data0, len0),
+ * make_cutils_socket_buffer(data1, len1)
+ * };
+ * socket_send_buffers(sock, buffers, 2);
+ *
+ * Returns the number of bytes written or -1 on error.
+ */
+cutils_socket_buffer_t make_cutils_socket_buffer(void* data, size_t length);
+ssize_t socket_send_buffers(cutils_socket_t sock,
+ cutils_socket_buffer_t* buffers,
+ size_t num_buffers);
+
+/*
* socket_peer_is_trusted - Takes a socket which is presumed to be a
* connected local socket (e.g. AF_LOCAL) and returns whether the peer
* (the userid that owns the process on the other end of that socket)