Commit abde1c0d authored by Junqi Wang's avatar Junqi Wang Committed by Facebook GitHub Bot

Move connect next to bind

Summary: codecleanupraccoon

Reviewed By: mjoras

Differential Revision: D21845742

fbshipit-source-id: 5adf159ed4ec7a48c76cffdb02d039888ab1686d
parent 7e211332
......@@ -183,6 +183,19 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
}
}
int AsyncUDPSocket::connect(const folly::SocketAddress& address) {
CHECK_NE(NetworkSocket(), fd_) << "Socket not yet bound";
sockaddr_storage addrStorage;
address.getAddress(&addrStorage);
int ret = netops::connect(
fd_, reinterpret_cast<sockaddr*>(&addrStorage), address.getActualSize());
if (ret == 0) {
connected_ = true;
connectedAddress_ = address;
}
return ret;
}
void AsyncUDPSocket::dontFragment(bool df) {
(void)df; // to avoid potential unused variable warning
#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && \
......@@ -650,19 +663,6 @@ void AsyncUDPSocket::failErrMessageRead(const AsyncSocketException& ex) {
}
}
int AsyncUDPSocket::connect(const folly::SocketAddress& address) {
CHECK_NE(NetworkSocket(), fd_) << "Socket not yet bound";
sockaddr_storage addrStorage;
address.getAddress(&addrStorage);
int ret = netops::connect(
fd_, reinterpret_cast<sockaddr*>(&addrStorage), address.getActualSize());
if (ret == 0) {
connected_ = true;
connectedAddress_ = address;
}
return ret;
}
void AsyncUDPSocket::handleRead() noexcept {
void* buf{nullptr};
size_t len{0};
......
......@@ -146,6 +146,26 @@ class AsyncUDPSocket : public EventHandler {
*/
virtual void bind(const folly::SocketAddress& address);
/**
* Connects the UDP socket to a remote destination address provided in
* address. This can speed up UDP writes on linux because it will cache flow
* state on connects.
* Using connect has many quirks, and you should be aware of them before using
* this API:
* 1. This must only be called after binding the socket.
* 2. Normally UDP can use the 2 tuple (src ip, src port) to steer packets
* sent by the peer to the socket, however after connecting the socket, only
* packets destined to the destination address specified in connect() will be
* forwarded and others will be dropped. If the server can send a packet
* from a different destination port / IP then you probably do not want to use
* this API.
* 3. It can be called repeatedly on either the client or server however it's
* normally only useful on the client and not server.
*
* Returns the result of calling the connect syscall.
*/
virtual int connect(const folly::SocketAddress& address);
/**
* Use an already bound file descriptor. You can either transfer ownership
* of this FD by using ownership = FDOwnership::OWNS or share it using
......@@ -314,26 +334,6 @@ class AsyncUDPSocket : public EventHandler {
*/
virtual void setErrMessageCallback(ErrMessageCallback* errMessageCallback);
/**
* Connects the UDP socket to a remote destination address provided in
* address. This can speed up UDP writes on linux because it will cache flow
* state on connects.
* Using connect has many quirks, and you should be aware of them before using
* this API:
* 1. This must only be called after binding the socket.
* 2. Normally UDP can use the 2 tuple (src ip, src port) to steer packets
* sent by the peer to the socket, however after connecting the socket, only
* packets destined to the destination address specified in connect() will be
* forwarded and others will be dropped. If the server can send a packet
* from a different destination port / IP then you probably do not want to use
* this API.
* 3. It can be called repeatedly on either the client or server however it's
* normally only useful on the client and not server.
*
* Returns the result of calling the connect syscall.
*/
virtual int connect(const folly::SocketAddress& address);
virtual bool isBound() const {
return fd_ != NetworkSocket();
}
......
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