Dune support (#7880)
[hiphop-php.git] / patches / folly-network-socket-backport.patch
blob569418fe988442209594f9d6f8d78da673dc0b2b
1 diff --git a/third-party/folly/src/folly/io/async/AsyncServerSocket.cpp b/third-party/folly/src/folly/io/async/AsyncServerSocket.cpp
2 index 82904d31..b51f3343 100644
3 --- a/third-party/folly/src/folly/io/async/AsyncServerSocket.cpp
4 +++ b/third-party/folly/src/folly/io/async/AsyncServerSocket.cpp
5 @@ -111,7 +111,7 @@ void AsyncServerSocket::RemoteAcceptor::messageAvailable(
6 connectionEventCallback_->onConnectionDequeuedByAcceptorCallback(
7 msg.fd, msg.address);
9 - callback_->connectionAccepted(msg.fd, msg.address);
10 + callback_->connectionAccepted(folly::NetworkSocket::fromFd(msg.fd), msg.address);
11 break;
13 case MessageType::MSG_ERROR: {
14 @@ -928,7 +928,7 @@ void AsyncServerSocket::dispatchSocket(int socket, SocketAddress&& address) {
16 CallbackInfo* info = nextCallback();
17 if (info->eventBase == nullptr || info->eventBase == this->eventBase_) {
18 - info->callback->connectionAccepted(socket, address);
19 + info->callback->connectionAccepted(folly::NetworkSocket::fromFd(socket), address);
20 return;
23 diff --git a/third-party/folly/src/folly/io/async/AsyncServerSocket.h b/third-party/folly/src/folly/io/async/AsyncServerSocket.h
24 index 0edbcafa..eb161224 100644
25 --- a/third-party/folly/src/folly/io/async/AsyncServerSocket.h
26 +++ b/third-party/folly/src/folly/io/async/AsyncServerSocket.h
27 @@ -25,6 +25,7 @@
28 #include <folly/io/async/EventBase.h>
29 #include <folly/io/async/EventHandler.h>
30 #include <folly/io/async/NotificationQueue.h>
31 +#include <folly/net/NetworkSocket.h>
32 #include <folly/portability/Sockets.h>
34 #include <limits.h>
35 @@ -157,7 +157,7 @@ class AsyncServerSocket : public DelayedDestruction, public AsyncSocketBase {
36 * remain valid until connectionAccepted() returns.
38 virtual void connectionAccepted(
39 - int fd,
40 + NetworkSocket fd,
41 const SocketAddress& clientAddr) noexcept = 0;
43 /**
44 @@ -293,6 +294,9 @@ class AsyncServerSocket : public DelayedDestruction, public AsyncSocketBase {
45 * ownership of the file descriptor.
47 void useExistingSocket(int fd);
48 + void useExistingSocket(NetworkSocket fd) {
49 + useExistingSocket(fd.toFd());
50 + }
51 void useExistingSockets(const std::vector<int>& fds);
53 /**
54 @@ -321,6 +325,10 @@ class AsyncServerSocket : public DelayedDestruction, public AsyncSocketBase {
58 + NetworkSocket getNetworkSocket() const {
59 + return NetworkSocket::fromFd(getSocket());
60 + }
62 /* enable zerocopy support for the server sockets - the s = accept sockets
63 * inherit it
65 diff --git a/third-party/folly/src/folly/io/async/EventHandler.h b/third-party/folly/src/folly/io/async/EventHandler.h
66 index 2669a3e8..e8e43b8b 100644
67 --- a/third-party/folly/src/folly/io/async/EventHandler.h
68 +++ b/third-party/folly/src/folly/io/async/EventHandler.h
69 @@ -22,6 +22,7 @@
70 #include <glog/logging.h>
72 #include <folly/io/async/EventUtil.h>
73 +#include <folly/net/NetworkSocket.h>
74 #include <folly/portability/Event.h>
76 namespace folly {
77 @@ -62,6 +63,9 @@ class EventHandler : private boost::noncopyable {
78 * changeHandlerFD() before the handler can be registered.
80 explicit EventHandler(EventBase* eventBase = nullptr, int fd = -1);
81 + explicit EventHandler(EventBase* eventBase, NetworkSocket fd) : EventHandler(eventBase, fd.toFd()) {
83 + }
85 /**
86 * EventHandler destructor.
87 diff --git a/third-party/folly/src/folly/net/NetworkSocket.h b/third-party/folly/src/folly/net/NetworkSocket.h
88 new file mode 100644
89 index 00000000..51d5c571
90 --- /dev/null
91 +++ b/third-party/folly/src/folly/net/NetworkSocket.h
92 @@ -0,0 +1,66 @@
93 +/*
94 + * Copyright 2013-present Facebook, Inc.
95 + *
96 + * Licensed under the Apache License, Version 2.0 (the "License");
97 + * you may not use this file except in compliance with the License.
98 + * You may obtain a copy of the License at
99 + *
100 + * http://www.apache.org/licenses/LICENSE-2.0
102 + * Unless required by applicable law or agreed to in writing, software
103 + * distributed under the License is distributed on an "AS IS" BASIS,
104 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
105 + * See the License for the specific language governing permissions and
106 + * limitations under the License.
107 + */
109 +#pragma once
111 +#include <ostream>
113 +namespace folly {
114 +/**
115 + * This is just a very thin wrapper around either a file descriptor or
116 + * a SOCKET depending on platform, along with a couple of helper methods
117 + * for explicitly converting to/from file descriptors, even on Windows.
118 + */
119 +struct NetworkSocket {
120 + using native_handle_type = int;
121 + static constexpr native_handle_type invalid_handle_value = -1;
123 + native_handle_type data;
125 + constexpr NetworkSocket() : data(invalid_handle_value) {}
126 + constexpr explicit NetworkSocket(native_handle_type d) : data(d) {}
128 + template <typename T>
129 + static NetworkSocket fromFd(T) = delete;
130 + static NetworkSocket fromFd(int fd) {
131 + return NetworkSocket(fd);
134 + int toFd() const {
135 + return data;
138 + friend constexpr bool operator==(
139 + const NetworkSocket& a,
140 + const NetworkSocket& b) noexcept {
141 + return a.data == b.data;
144 + friend constexpr bool operator!=(
145 + const NetworkSocket& a,
146 + const NetworkSocket& b) noexcept {
147 + return !(a == b);
151 +template <class CharT, class Traits>
152 +inline std::basic_ostream<CharT, Traits>& operator<<(
153 + std::basic_ostream<CharT, Traits>& os,
154 + const NetworkSocket& addr) {
155 + os << "folly::NetworkSocket(" << addr.data << ")";
156 + return os;
158 +} // namespace folly
159 diff --git a/third-party/wangle/src/wangle/acceptor/Acceptor.cpp b/third-party/wangle/src/wangle/acceptor/Acceptor.cpp
160 index 51911be..328f731 100644
161 --- a/third-party/wangle/src/wangle/acceptor/Acceptor.cpp
162 +++ b/third-party/wangle/src/wangle/acceptor/Acceptor.cpp
163 @@ -213,7 +213,8 @@ bool Acceptor::canAccept(const SocketAddress& address) {
165 void
166 Acceptor::connectionAccepted(
167 - int fd, const SocketAddress& clientAddr) noexcept {
168 + folly::NetworkSocket fdNetworkSocket, const SocketAddress& clientAddr) noexcept {
169 + int fd = fdNetworkSocket.toFd();
170 namespace fsp = folly::portability::sockets;
171 if (!canAccept(clientAddr)) {
172 // Send a RST to free kernel memory faster
173 diff --git a/third-party/wangle/src/wangle/acceptor/Acceptor.h b/third-party/wangle/src/wangle/acceptor/Acceptor.h
174 index ec005e9..9b84de8 100644
175 --- a/third-party/wangle/src/wangle/acceptor/Acceptor.h
176 +++ b/third-party/wangle/src/wangle/acceptor/Acceptor.h
177 @@ -392,7 +392,7 @@ class Acceptor :
179 // AsyncServerSocket::AcceptCallback methods
180 void connectionAccepted(
181 - int fd,
182 + folly::NetworkSocket fd,
183 const folly::SocketAddress& clientAddr) noexcept override;
184 void acceptError(const std::exception& ex) noexcept override;
185 void acceptStopped() noexcept override;
186 diff --git a/third-party/mcrouter/src/mcrouter/lib/network/AsyncMcServer.cpp b/third-party/mcrouter/src/mcrouter/lib/network/AsyncMcServer.cpp
187 index c3fa836b..ca233196 100644
188 --- a/third-party/mcrouter/src/mcrouter/lib/network/AsyncMcServer.cpp
189 +++ b/third-party/mcrouter/src/mcrouter/lib/network/AsyncMcServer.cpp
190 @@ -271,8 +271,9 @@ class McServerThread {
191 AcceptCallback(McServerThread* mcServerThread, bool secure)
192 : mcServerThread_(mcServerThread), secure_(secure) {}
193 void connectionAccepted(
194 - int fd,
195 + folly::NetworkSocket fdNetworkSocket,
196 const folly::SocketAddress& /* clientAddr */) noexcept final {
197 + int fd = fdNetworkSocket.toFd();
198 if (secure_) {
199 const auto& server = mcServerThread_->server_;
200 auto& opts = server.opts_;