From: Yang Chi Date: Mon, 31 Jul 2017 20:48:23 +0000 (-0700) Subject: Make sendmsg in AsyncUDPSocket overridable X-Git-Tag: v2017.08.07.00~15 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=53b24a320c88077fe80ff1359e74d59ccbd8d8a9;p=folly.git Make sendmsg in AsyncUDPSocket overridable Summary: Add a protected virtual sendmsg method to AsyncUDPSocket. And make it default to call the system sendmsg function. This enables subclasses of AsyncUDPSocket to be able to override the behavior of sendmseg, also makes AsyncUDPSocket easier to mock and test. Reviewed By: afrind Differential Revision: D5459745 fbshipit-source-id: b0227bf7503b1096cb1f0bfc8b9c784db2f2e45d --- diff --git a/folly/io/async/AsyncUDPSocket.cpp b/folly/io/async/AsyncUDPSocket.cpp index 4d195d36..6f46c011 100644 --- a/folly/io/async/AsyncUDPSocket.cpp +++ b/folly/io/async/AsyncUDPSocket.cpp @@ -176,7 +176,7 @@ ssize_t AsyncUDPSocket::writev(const folly::SocketAddress& address, msg.msg_controllen = 0; msg.msg_flags = 0; - return ::sendmsg(fd_, &msg, 0); + return sendmsg(fd_, &msg, 0); } void AsyncUDPSocket::resumeRead(ReadCallback* cob) { diff --git a/folly/io/async/AsyncUDPSocket.h b/folly/io/async/AsyncUDPSocket.h index 21449420..6d49ed67 100644 --- a/folly/io/async/AsyncUDPSocket.h +++ b/folly/io/async/AsyncUDPSocket.h @@ -162,6 +162,11 @@ class AsyncUDPSocket : public EventHandler { return eventBase_; } + protected: + virtual ssize_t sendmsg(int socket, const struct msghdr* message, int flags) { + return ::sendmsg(socket, message, flags); + } + private: AsyncUDPSocket(const AsyncUDPSocket&) = delete; AsyncUDPSocket& operator=(const AsyncUDPSocket&) = delete; diff --git a/folly/io/async/test/AsyncUDPSocketTest.cpp b/folly/io/async/test/AsyncUDPSocketTest.cpp index 09ebfcc1..372e4c37 100644 --- a/folly/io/async/test/AsyncUDPSocketTest.cpp +++ b/folly/io/async/test/AsyncUDPSocketTest.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -31,6 +32,7 @@ using folly::AsyncTimeout; using folly::EventBase; using folly::SocketAddress; using folly::IOBuf; +using namespace testing; class UDPAcceptor : public AsyncUDPServerSocket::Callback { @@ -289,3 +291,10 @@ TEST(AsyncSocketTest, PingPong) { // Wait for server thread to joib serverThread.join(); } + +class TestAsyncUDPSocket : public AsyncUDPSocket { + public: + explicit TestAsyncUDPSocket(EventBase* evb) : AsyncUDPSocket(evb) {} + + MOCK_METHOD3(sendmsg, ssize_t(int, const struct msghdr*, int)); +};