2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <folly/io/IOBuf.h>
20 #include <folly/ScopeGuard.h>
21 #include <folly/io/async/AsyncSocketException.h>
22 #include <folly/io/async/EventHandler.h>
23 #include <folly/io/async/EventBase.h>
24 #include <folly/SocketAddress.h>
33 class AsyncUDPSocket : public EventHandler {
35 enum class FDOwnership {
43 * Invoked when the socket becomes readable and we want buffer
46 * NOTE: From socket we will end up reading at most `len` bytes
47 * and if there were more bytes in datagram, we will end up
50 virtual void getReadBuffer(void** buf, size_t* len) noexcept = 0;
53 * Invoked when a new datagraom is available on the socket. `len`
54 * is the number of bytes read and `truncated` is true if we had
55 * to drop few bytes because of running out of buffer space.
57 virtual void onDataAvailable(const folly::SocketAddress& client,
59 bool truncated) noexcept = 0;
62 * Invoked when there is an error reading from the socket.
64 * NOTE: Since UDP is connectionless, you can still read from the socket.
65 * But you have to re-register readCallback yourself after
68 virtual void onReadError(const AsyncSocketException& ex)
72 * Invoked when socket is closed and a read callback is registered.
74 virtual void onReadClosed() noexcept = 0;
76 virtual ~ReadCallback() {}
80 * Create a new UDP socket that will run in the
83 explicit AsyncUDPSocket(EventBase* evb);
87 * Returns the address server is listening on
89 const folly::SocketAddress& address() const {
90 CHECK_NE(-1, fd_) << "Server not yet bound to an address";
95 * Bind the socket to the following address. If port is not
96 * set in the `address` an ephemeral port is chosen and you can
97 * use `address()` method above to get it after this method successfully
100 void bind(const folly::SocketAddress& address);
103 * Use an already bound file descriptor. You can either transfer ownership
104 * of this FD by using ownership = FDOwnership::OWNS or share it using
105 * FDOwnership::SHARED. In case FD is shared, it will not be `close`d in
108 void setFD(int fd, FDOwnership ownership);
111 * Send the data in buffer to destination. Returns the return code from
114 ssize_t write(const folly::SocketAddress& address,
115 const std::unique_ptr<folly::IOBuf>& buf);
118 * Start reading datagrams
120 void resumeRead(ReadCallback* cob);
123 * Pause reading datagrams
128 * Stop listening on the socket.
133 * Get internal FD used by this socket
136 CHECK_NE(-1, fd_) << "Need to bind before getting FD out";
141 * Set reuse port mode to call bind() on the same address multiple times
143 void setReusePort(bool reusePort) {
144 reusePort_ = reusePort;
147 AsyncUDPSocket(const AsyncUDPSocket&) = delete;
148 AsyncUDPSocket& operator=(const AsyncUDPSocket&) = delete;
151 void handlerReady(uint16_t events) noexcept;
153 void handleRead() noexcept;
154 bool updateRegistration() noexcept;
156 EventBase* eventBase_;
157 folly::SocketAddress localAddress_;
160 FDOwnership ownership_;
162 // Temp space to receive client address
163 folly::SocketAddress clientAddress_;
165 // Non-null only when we are reading
166 ReadCallback* readCallback_;
168 bool reusePort_{false};