From: Naizhi Li <naizhi@fb.com>
Date: Thu, 2 Apr 2015 00:52:47 +0000 (-0700)
Subject: Add support for writev for AsyncUDPSocket
X-Git-Tag: v0.33.0~7
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=62e15017ed5b871521ad8d89cdb37a91e7401604;p=folly.git

Add support for writev for AsyncUDPSocket

Summary:
I have 2 local buffers and instead of allocating iobufs
on the heap, I can simply use iovec on the stack to send.

Test Plan: Unit tests and turn server.

Reviewed By: davejwatson@fb.com

Subscribers: folly-diffs@, ehrhardt, yfeldblum, chalfant, paramr

FB internal diff: D1956201

Signature: t1:1956201:1427918649:09f00399fd9e8ed3fe62be43b19adbf0a5a3f3b0
---

diff --git a/folly/io/async/AsyncUDPSocket.cpp b/folly/io/async/AsyncUDPSocket.cpp
index 8bea5a5a..5b128126 100644
--- a/folly/io/async/AsyncUDPSocket.cpp
+++ b/folly/io/async/AsyncUDPSocket.cpp
@@ -129,8 +129,6 @@ void AsyncUDPSocket::setFD(int fd, FDOwnership ownership) {
 
 ssize_t AsyncUDPSocket::write(const folly::SocketAddress& address,
                                const std::unique_ptr<folly::IOBuf>& buf) {
-  CHECK_NE(-1, fd_) << "Socket not yet bound";
-
   // UDP's typical MTU size is 1500, so high number of buffers
   //   really do not make sense. Optimze for buffer chains with
   //   buffers less than 16, which is the highest I can think of
@@ -144,13 +142,20 @@ ssize_t AsyncUDPSocket::write(const folly::SocketAddress& address,
     iovec_len = 1;
   }
 
+  return writev(address, vec, iovec_len);
+}
+
+ssize_t AsyncUDPSocket::writev(const folly::SocketAddress& address,
+                               const struct iovec* vec, size_t iovec_len) {
+  CHECK_NE(-1, fd_) << "Socket not yet bound";
+
   sockaddr_storage addrStorage;
   address.getAddress(&addrStorage);
 
   struct msghdr msg;
   msg.msg_name = reinterpret_cast<void*>(&addrStorage);
   msg.msg_namelen = address.getActualSize();
-  msg.msg_iov = vec;
+  msg.msg_iov = const_cast<struct iovec*>(vec);
   msg.msg_iovlen = iovec_len;
   msg.msg_control = nullptr;
   msg.msg_controllen = 0;
diff --git a/folly/io/async/AsyncUDPSocket.h b/folly/io/async/AsyncUDPSocket.h
index a1bca318..02f90080 100644
--- a/folly/io/async/AsyncUDPSocket.h
+++ b/folly/io/async/AsyncUDPSocket.h
@@ -110,11 +110,17 @@ class AsyncUDPSocket : public EventHandler {
 
   /**
    * Send the data in buffer to destination. Returns the return code from
-   * ::sendto.
+   * ::sendmsg.
    */
   ssize_t write(const folly::SocketAddress& address,
                 const std::unique_ptr<folly::IOBuf>& buf);
 
+  /**
+   * Send data in iovec to destination. Returns the return code from sendmsg.
+   */
+  ssize_t writev(const folly::SocketAddress& address,
+                 const struct iovec* vec, size_t veclen);
+
   /**
    * Start reading datagrams
    */