Commit 61a8ac33 authored by Orvid King's avatar Orvid King Committed by Facebook Github Bot

Add functions to get the NetworkSockets out of various types of sockets

Summary: As interim steps to codemod to.

Reviewed By: yfeldblum

Differential Revision: D13568657

fbshipit-source-id: b143b5bab0a64c196892358a30fce17037b19b21
parent f7f0026f
...@@ -326,18 +326,30 @@ class AsyncServerSocket : public DelayedDestruction, public AsyncSocketBase { ...@@ -326,18 +326,30 @@ class AsyncServerSocket : public DelayedDestruction, public AsyncSocketBase {
return sockets; return sockets;
} }
std::vector<NetworkSocket> getNetworkSockets() const {
std::vector<NetworkSocket> sockets;
for (auto& handler : sockets_) {
sockets.push_back(handler.socket_);
}
return sockets;
}
/** /**
* Backwards compatible getSocket, warns if > 1 socket * Backwards compatible getSocket, warns if > 1 socket
*/ */
int getSocket() const { int getSocket() const {
return getNetworkSocket().toFd();
}
NetworkSocket getNetworkSocket() const {
if (sockets_.size() > 1) { if (sockets_.size() > 1) {
VLOG(2) << "Warning: getSocket can return multiple fds, " VLOG(2) << "Warning: getSocket can return multiple fds, "
<< "but getSockets was not called, so only returning the first"; << "but getSockets was not called, so only returning the first";
} }
if (sockets_.size() == 0) { if (sockets_.size() == 0) {
return -1; return NetworkSocket();
} else { } else {
return sockets_[0].socket_.toFd(); return sockets_[0].socket_;
} }
} }
......
...@@ -379,7 +379,7 @@ void AsyncSocket::destroy() { ...@@ -379,7 +379,7 @@ void AsyncSocket::destroy() {
DelayedDestruction::destroy(); DelayedDestruction::destroy();
} }
int AsyncSocket::detachFd() { NetworkSocket AsyncSocket::detachNetworkSocket() {
VLOG(6) << "AsyncSocket::detachFd(this=" << this << ", fd=" << fd_ VLOG(6) << "AsyncSocket::detachFd(this=" << this << ", fd=" << fd_
<< ", evb=" << eventBase_ << ", state=" << state_ << ", evb=" << eventBase_ << ", state=" << state_
<< ", events=" << std::hex << eventFlags_ << ")"; << ", events=" << std::hex << eventFlags_ << ")";
...@@ -395,7 +395,7 @@ int AsyncSocket::detachFd() { ...@@ -395,7 +395,7 @@ int AsyncSocket::detachFd() {
// Update the EventHandler to stop using this fd. // Update the EventHandler to stop using this fd.
// This can only be done after closeNow() unregisters the handler. // This can only be done after closeNow() unregisters the handler.
ioHandler_.changeHandlerFD(NetworkSocket()); ioHandler_.changeHandlerFD(NetworkSocket());
return fd.toFd(); return fd;
} }
const folly::SocketAddress& AsyncSocket::anyAddress() { const folly::SocketAddress& AsyncSocket::anyAddress() {
......
...@@ -340,7 +340,11 @@ class AsyncSocket : virtual public AsyncTransportWrapper { ...@@ -340,7 +340,11 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
* Get the file descriptor used by the AsyncSocket. * Get the file descriptor used by the AsyncSocket.
*/ */
virtual int getFd() const { virtual int getFd() const {
return fd_.toFd(); return getNetworkSocket().toFd();
}
virtual NetworkSocket getNetworkSocket() const {
return fd_;
} }
/** /**
...@@ -357,7 +361,11 @@ class AsyncSocket : virtual public AsyncTransportWrapper { ...@@ -357,7 +361,11 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
* Returns the file descriptor. The caller assumes ownership of the * Returns the file descriptor. The caller assumes ownership of the
* descriptor, and it will not be closed when the AsyncSocket is destroyed. * descriptor, and it will not be closed when the AsyncSocket is destroyed.
*/ */
virtual int detachFd(); virtual int detachFd() {
return detachNetworkSocket().toFd();
}
virtual NetworkSocket detachNetworkSocket();
/** /**
* Uniquely identifies a handle to a socket option value. Each * Uniquely identifies a handle to a socket option value. Each
......
...@@ -196,8 +196,12 @@ class AsyncUDPSocket : public EventHandler { ...@@ -196,8 +196,12 @@ 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 {
return getNetworkSocket().toFd();
}
virtual NetworkSocket getNetworkSocket() const {
CHECK_NE(NetworkSocket(), fd_) << "Need to bind before getting FD out"; CHECK_NE(NetworkSocket(), fd_) << "Need to bind before getting FD out";
return fd_.toFd(); return fd_;
} }
/** /**
......
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