Add support for writev for AsyncUDPSocket
authorNaizhi Li <naizhi@fb.com>
Thu, 2 Apr 2015 00:52:47 +0000 (17:52 -0700)
committerafrind <afrind@fb.com>
Thu, 2 Apr 2015 19:02:25 +0000 (12:02 -0700)
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

folly/io/async/AsyncUDPSocket.cpp
folly/io/async/AsyncUDPSocket.h

index 8bea5a5a809622c384f00f0ee946a2c4fcdde573..5b12812616f60ef84c2c62c3f613d9bc255c2239 100644 (file)
@@ -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;
index a1bca318f07ef5b87b21c0b5ae46b03564491496..02f90080c5eefc5ea1596bc4d77f96df4667e15c 100644 (file)
@@ -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
    */