Commit 350de5c9 authored by Orvid King's avatar Orvid King Committed by Facebook Github Bot

Add NetworkSocket support to AsyncUDPSocket

Summary: More along the path.

Reviewed By: yfeldblum

Differential Revision: D10847193

fbshipit-source-id: 5d946855c00752d23c012f2c91f07d276760d371
parent b2208566
This diff is collapsed.
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
#include <folly/io/async/AsyncSocketException.h> #include <folly/io/async/AsyncSocketException.h>
#include <folly/io/async/EventBase.h> #include <folly/io/async/EventBase.h>
#include <folly/io/async/EventHandler.h> #include <folly/io/async/EventHandler.h>
#include <folly/net/NetOps.h>
#include <folly/net/NetworkSocket.h>
namespace folly { namespace folly {
...@@ -108,7 +110,7 @@ class AsyncUDPSocket : public EventHandler { ...@@ -108,7 +110,7 @@ class AsyncUDPSocket : public EventHandler {
* Returns the address server is listening on * Returns the address server is listening on
*/ */
virtual const folly::SocketAddress& address() const { virtual const folly::SocketAddress& address() const {
CHECK_NE(-1, fd_) << "Server not yet bound to an address"; CHECK_NE(NetworkSocket(), fd_) << "Server not yet bound to an address";
return localAddress_; return localAddress_;
} }
...@@ -126,7 +128,10 @@ class AsyncUDPSocket : public EventHandler { ...@@ -126,7 +128,10 @@ class AsyncUDPSocket : public EventHandler {
* FDOwnership::SHARED. In case FD is shared, it will not be `close`d in * FDOwnership::SHARED. In case FD is shared, it will not be `close`d in
* destructor. * destructor.
*/ */
virtual void setFD(int fd, FDOwnership ownership); void setFD(int fd, FDOwnership ownership) {
setFD(NetworkSocket::fromFd(fd), ownership);
}
virtual void setFD(NetworkSocket fd, FDOwnership ownership);
/** /**
* Send the data in buffer to destination. Returns the return code from * Send the data in buffer to destination. Returns the return code from
...@@ -183,8 +188,8 @@ class AsyncUDPSocket : public EventHandler { ...@@ -183,8 +188,8 @@ class AsyncUDPSocket : public EventHandler {
* Get internal FD used by this socket * Get internal FD used by this socket
*/ */
virtual int getFD() const { virtual int getFD() const {
CHECK_NE(-1, fd_) << "Need to bind before getting FD out"; CHECK_NE(NetworkSocket(), fd_) << "Need to bind before getting FD out";
return fd_; return fd_.toFd();
} }
/** /**
...@@ -265,7 +270,7 @@ class AsyncUDPSocket : public EventHandler { ...@@ -265,7 +270,7 @@ class AsyncUDPSocket : public EventHandler {
virtual int connect(const folly::SocketAddress& address); virtual int connect(const folly::SocketAddress& address);
virtual bool isBound() const { virtual bool isBound() const {
return fd_ != -1; return fd_ != NetworkSocket();
} }
virtual void detachEventBase(); virtual void detachEventBase();
...@@ -279,8 +284,9 @@ class AsyncUDPSocket : public EventHandler { ...@@ -279,8 +284,9 @@ class AsyncUDPSocket : public EventHandler {
bool setGSO(int val); bool setGSO(int val);
protected: protected:
virtual ssize_t sendmsg(int socket, const struct msghdr* message, int flags) { virtual ssize_t
return ::sendmsg(socket, message, flags); sendmsg(NetworkSocket socket, const struct msghdr* message, int flags) {
return netops::sendmsg(socket, message, flags);
} }
size_t handleErrMessages() noexcept; size_t handleErrMessages() noexcept;
...@@ -303,7 +309,7 @@ class AsyncUDPSocket : public EventHandler { ...@@ -303,7 +309,7 @@ class AsyncUDPSocket : public EventHandler {
EventBase* eventBase_; EventBase* eventBase_;
folly::SocketAddress localAddress_; folly::SocketAddress localAddress_;
int fd_; NetworkSocket fd_;
FDOwnership ownership_; FDOwnership ownership_;
// Temp space to receive client address // Temp space to receive client address
......
...@@ -458,7 +458,9 @@ class TestAsyncUDPSocket : public AsyncUDPSocket { ...@@ -458,7 +458,9 @@ class TestAsyncUDPSocket : public AsyncUDPSocket {
public: public:
explicit TestAsyncUDPSocket(EventBase* evb) : AsyncUDPSocket(evb) {} explicit TestAsyncUDPSocket(EventBase* evb) : AsyncUDPSocket(evb) {}
MOCK_METHOD3(sendmsg, ssize_t(int, const struct msghdr*, int)); MOCK_METHOD3(
sendmsg,
ssize_t(folly::NetworkSocket, const struct msghdr*, int));
}; };
class MockErrMessageCallback : public AsyncUDPSocket::ErrMessageCallback { class MockErrMessageCallback : public AsyncUDPSocket::ErrMessageCallback {
......
...@@ -27,7 +27,7 @@ struct MockAsyncUDPSocket : public AsyncUDPSocket { ...@@ -27,7 +27,7 @@ struct MockAsyncUDPSocket : public AsyncUDPSocket {
MOCK_CONST_METHOD0(address, const SocketAddress&()); MOCK_CONST_METHOD0(address, const SocketAddress&());
MOCK_METHOD1(bind, void(const SocketAddress&)); MOCK_METHOD1(bind, void(const SocketAddress&));
MOCK_METHOD2(setFD, void(int, AsyncUDPSocket::FDOwnership)); MOCK_METHOD2(setFD, void(NetworkSocket, AsyncUDPSocket::FDOwnership));
MOCK_METHOD2( MOCK_METHOD2(
write, write,
ssize_t(const SocketAddress&, const std::unique_ptr<IOBuf>&)); ssize_t(const SocketAddress&, const std::unique_ptr<IOBuf>&));
......
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