proxygen
AsyncUDPSocket.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <memory>
20 
21 #include <folly/ScopeGuard.h>
22 #include <folly/SocketAddress.h>
23 #include <folly/io/IOBuf.h>
28 #include <folly/net/NetOps.h>
30 
31 namespace folly {
32 
36 class AsyncUDPSocket : public EventHandler {
37  public:
38  enum class FDOwnership { OWNS, SHARED };
39 
40  class ReadCallback {
41  public:
50  virtual void getReadBuffer(void** buf, size_t* len) noexcept = 0;
51 
57  virtual void onDataAvailable(
58  const folly::SocketAddress& client,
59  size_t len,
60  bool truncated) noexcept = 0;
61 
69  virtual void onReadError(const AsyncSocketException& ex) noexcept = 0;
70 
74  virtual void onReadClosed() noexcept = 0;
75 
76  virtual ~ReadCallback() = default;
77  };
78 
80  public:
81  virtual ~ErrMessageCallback() = default;
82 
91  virtual void errMessage(const cmsghdr& cmsg) noexcept = 0;
92 
99  virtual void errMessageError(const AsyncSocketException& ex) noexcept = 0;
100  };
101 
106  explicit AsyncUDPSocket(EventBase* evb);
107  ~AsyncUDPSocket() override;
108 
112  virtual const folly::SocketAddress& address() const {
113  CHECK_NE(NetworkSocket(), fd_) << "Server not yet bound to an address";
114  return localAddress_;
115  }
116 
123  virtual void bind(const folly::SocketAddress& address);
124 
131  void setFD(int fd, FDOwnership ownership) {
132  setFD(NetworkSocket::fromFd(fd), ownership);
133  }
134  virtual void setFD(NetworkSocket fd, FDOwnership ownership);
135 
140  virtual ssize_t write(
141  const folly::SocketAddress& address,
142  const std::unique_ptr<folly::IOBuf>& buf);
143 
153  virtual ssize_t writeGSO(
154  const folly::SocketAddress& address,
155  const std::unique_ptr<folly::IOBuf>& buf,
156  int gso);
157 
161  virtual ssize_t writev(
162  const folly::SocketAddress& address,
163  const struct iovec* vec,
164  size_t veclen,
165  int gso);
166 
167  virtual ssize_t writev(
168  const folly::SocketAddress& address,
169  const struct iovec* vec,
170  size_t veclen);
171 
175  virtual void resumeRead(ReadCallback* cob);
176 
180  virtual void pauseRead();
181 
185  virtual void close();
186 
190  virtual int getFD() const {
191  CHECK_NE(NetworkSocket(), fd_) << "Need to bind before getting FD out";
192  return fd_.toFd();
193  }
194 
198  virtual void setReusePort(bool reusePort) {
199  reusePort_ = reusePort;
200  }
201 
205  virtual void setReuseAddr(bool reuseAddr) {
206  reuseAddr_ = reuseAddr;
207  }
208 
212  virtual void setRcvBuf(int rcvBuf) {
213  rcvBuf_ = rcvBuf;
214  }
215 
219  virtual void setSndBuf(int sndBuf) {
220  sndBuf_ = sndBuf;
221  }
222 
227  virtual void setBusyPoll(int busyPollUs) {
228  busyPollUs_ = busyPollUs;
229  }
230 
232  return eventBase_;
233  }
234 
245  virtual void dontFragment(bool df);
246 
250  virtual void setErrMessageCallback(ErrMessageCallback* errMessageCallback);
251 
270  virtual int connect(const folly::SocketAddress& address);
271 
272  virtual bool isBound() const {
273  return fd_ != NetworkSocket();
274  }
275 
276  virtual void detachEventBase();
277 
278  virtual void attachEventBase(folly::EventBase* evb);
279 
280  // generic segmentation offload get/set
281  // negative return value means GSO is not available
282  int getGSO();
283 
284  bool setGSO(int val);
285 
286  protected:
287  virtual ssize_t
288  sendmsg(NetworkSocket socket, const struct msghdr* message, int flags) {
289  return netops::sendmsg(socket, message, flags);
290  }
291 
292  size_t handleErrMessages() noexcept;
293 
295 
296  // Non-null only when we are reading
298 
299  private:
300  AsyncUDPSocket(const AsyncUDPSocket&) = delete;
301  AsyncUDPSocket& operator=(const AsyncUDPSocket&) = delete;
302 
303  // EventHandler
304  void handlerReady(uint16_t events) noexcept override;
305 
306  void handleRead() noexcept;
307  bool updateRegistration() noexcept;
308 
311 
314 
315  // Temp space to receive client address
317 
318  bool reuseAddr_{true};
319  bool reusePort_{false};
320  int rcvBuf_{0};
321  int sndBuf_{0};
322  int busyPollUs_{0};
323 
324  // generic segmentation offload value, if available
325  // See https://lwn.net/Articles/188489/ for more details
327 
329 };
330 
331 } // namespace folly
ReadCallback * readCallback_
virtual const folly::SocketAddress & address() const
virtual void setBusyPoll(int busyPollUs)
void setFD(int fd, FDOwnership ownership)
Definition: test.c:42
EventBase * getEventBase() const
flags
Definition: http_parser.h:127
ssize_t sendmsg(NetworkSocket socket, const msghdr *message, int flags)
Definition: NetOps.cpp:328
virtual void pauseRead()
virtual ssize_t writeGSO(const folly::SocketAddress &address, const std::unique_ptr< folly::IOBuf > &buf, int gso)
virtual void resumeRead(ReadCallback *cob)
virtual ssize_t writev(const folly::SocketAddress &address, const struct iovec *vec, size_t veclen, int gso)
double val
Definition: String.cpp:273
virtual void setErrMessageCallback(ErrMessageCallback *errMessageCallback)
void failErrMessageRead(const AsyncSocketException &ex)
virtual void setRcvBuf(int rcvBuf)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
virtual void attachEventBase(folly::EventBase *evb)
virtual ssize_t sendmsg(NetworkSocket socket, const struct msghdr *message, int flags)
virtual ssize_t write(const folly::SocketAddress &address, const std::unique_ptr< folly::IOBuf > &buf)
virtual void detachEventBase()
AsyncUDPSocket(EventBase *evb)
bool updateRegistration() noexcept
Definition: Traits.h:588
size_t handleErrMessages() noexcept
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
virtual void bind(const folly::SocketAddress &address)
folly::Optional< int > gso_
ErrMessageCallback * errMessageCallback_
AsyncUDPSocket & operator=(const AsyncUDPSocket &)=delete
folly::SocketAddress clientAddress_
void handlerReady(uint16_t events) noexceptoverride
virtual int getFD() const
virtual bool isBound() const
void handleRead() noexcept
virtual void dontFragment(bool df)
virtual void setReusePort(bool reusePort)
virtual void setReuseAddr(bool reuseAddr)
folly::SocketAddress localAddress_
static NetworkSocket fromFd(int fd)
Definition: NetworkSocket.h:44
virtual int connect(const folly::SocketAddress &address)
virtual void setSndBuf(int sndBuf)