Commit 62e15017 authored by Naizhi Li's avatar Naizhi Li Committed by afrind

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
parent 8cb80904
......@@ -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;
......
......@@ -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
*/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment