proxygen
|
#include <AsyncUDPSocket.h>
Classes | |
class | ErrMessageCallback |
class | ReadCallback |
Public Types | |
enum | FDOwnership { FDOwnership::OWNS, FDOwnership::SHARED } |
Public Types inherited from folly::EventHandler | |
enum | EventFlags { NONE = 0, READ = EV_READ, WRITE = EV_WRITE, READ_WRITE = (READ | WRITE), PERSIST = EV_PERSIST } |
Public Member Functions | |
AsyncUDPSocket (EventBase *evb) | |
~AsyncUDPSocket () override | |
virtual const folly::SocketAddress & | address () const |
virtual void | bind (const folly::SocketAddress &address) |
void | setFD (int fd, FDOwnership ownership) |
virtual void | setFD (NetworkSocket fd, FDOwnership ownership) |
virtual ssize_t | write (const folly::SocketAddress &address, const std::unique_ptr< folly::IOBuf > &buf) |
virtual ssize_t | writeGSO (const folly::SocketAddress &address, const std::unique_ptr< folly::IOBuf > &buf, int gso) |
virtual ssize_t | writev (const folly::SocketAddress &address, const struct iovec *vec, size_t veclen, int gso) |
virtual ssize_t | writev (const folly::SocketAddress &address, const struct iovec *vec, size_t veclen) |
virtual void | resumeRead (ReadCallback *cob) |
virtual void | pauseRead () |
virtual void | close () |
virtual int | getFD () const |
virtual void | setReusePort (bool reusePort) |
virtual void | setReuseAddr (bool reuseAddr) |
virtual void | setRcvBuf (int rcvBuf) |
virtual void | setSndBuf (int sndBuf) |
virtual void | setBusyPoll (int busyPollUs) |
EventBase * | getEventBase () const |
virtual void | dontFragment (bool df) |
virtual void | setErrMessageCallback (ErrMessageCallback *errMessageCallback) |
virtual int | connect (const folly::SocketAddress &address) |
virtual bool | isBound () const |
virtual void | detachEventBase () |
virtual void | attachEventBase (folly::EventBase *evb) |
int | getGSO () |
bool | setGSO (int val) |
Public Member Functions inherited from folly::EventHandler | |
EventHandler (EventBase *eventBase, int fd) | |
EventHandler (EventBase *eventBase=nullptr, NetworkSocket fd=NetworkSocket()) | |
virtual | ~EventHandler () |
bool | registerHandler (uint16_t events) |
void | unregisterHandler () |
bool | isHandlerRegistered () const |
void | attachEventBase (EventBase *eventBase) |
void | detachEventBase () |
void | changeHandlerFD (int fd) |
void | changeHandlerFD (NetworkSocket fd) |
void | initHandler (EventBase *eventBase, int fd) |
void | initHandler (EventBase *eventBase, NetworkSocket fd) |
uint16_t | getRegisteredEvents () const |
bool | registerInternalHandler (uint16_t events) |
bool | isPending () const |
Protected Member Functions | |
virtual ssize_t | sendmsg (NetworkSocket socket, const struct msghdr *message, int flags) |
size_t | handleErrMessages () noexcept |
void | failErrMessageRead (const AsyncSocketException &ex) |
Protected Attributes | |
ReadCallback * | readCallback_ |
Private Member Functions | |
AsyncUDPSocket (const AsyncUDPSocket &)=delete | |
AsyncUDPSocket & | operator= (const AsyncUDPSocket &)=delete |
void | handlerReady (uint16_t events) noexceptoverride |
void | handleRead () noexcept |
bool | updateRegistration () noexcept |
Private Attributes | |
EventBase * | eventBase_ |
folly::SocketAddress | localAddress_ |
NetworkSocket | fd_ |
FDOwnership | ownership_ |
folly::SocketAddress | clientAddress_ |
bool | reuseAddr_ {true} |
bool | reusePort_ {false} |
int | rcvBuf_ {0} |
int | sndBuf_ {0} |
int | busyPollUs_ {0} |
folly::Optional< int > | gso_ |
ErrMessageCallback * | errMessageCallback_ {nullptr} |
UDP socket
Definition at line 36 of file AsyncUDPSocket.h.
|
strong |
|
explicit |
Create a new UDP socket that will run in the given eventbase
Definition at line 37 of file AsyncUDPSocket.cpp.
References folly::EventBase::dcheckIsInEventBaseThread().
|
override |
Definition at line 45 of file AsyncUDPSocket.cpp.
|
privatedelete |
|
inlinevirtual |
Returns the address server is listening on
Definition at line 112 of file AsyncUDPSocket.h.
References bind(), fd_, and localAddress_.
Referenced by bind(), dontFragment(), setErrMessageCallback(), TEST(), and folly::test::MockAsyncUDPSocket::~MockAsyncUDPSocket().
|
virtual |
Definition at line 546 of file AsyncUDPSocket.cpp.
References folly::EventHandler::attachEventBase(), eventBase_, folly::EventBase::isInEventBaseThread(), and updateRegistration().
Referenced by isBound().
|
virtual |
Bind the socket to the following address. If port is not set in the address
an ephemeral port is chosen and you can use address()
method above to get it after this method successfully returns.
Definition at line 51 of file AsyncUDPSocket.cpp.
References address(), folly::netops::bind(), busyPollUs_, folly::EventHandler::changeHandlerFD(), folly::netops::close(), folly::SocketAddress::describe(), fd_, flag, g(), folly::SocketAddress::getActualSize(), folly::SocketAddress::getAddress(), folly::SocketAddress::getFamily(), folly::SocketAddress::getPort(), localAddress_, folly::makeGuard(), folly::AsyncSocketException::NOT_OPEN, ownership_, OWNS, rcvBuf_, reuseAddr_, reusePort_, folly::netops::set_socket_non_blocking(), folly::SocketAddress::setFromLocalAddress(), folly::netops::setsockopt(), sndBuf_, SO_REUSEPORT, folly::netops::socket(), and folly::value().
Referenced by address(), TEST(), TEST_F(), and folly::test::MockAsyncUDPSocket::~MockAsyncUDPSocket().
|
virtual |
Stop listening on the socket.
Definition at line 329 of file AsyncUDPSocket.cpp.
References folly::netops::close(), folly::EventBase::dcheckIsInEventBaseThread(), eventBase_, fd_, folly::AsyncUDPSocket::ReadCallback::onReadClosed(), ownership_, OWNS, readCallback_, and folly::EventHandler::unregisterHandler().
Referenced by setFD(), ~AsyncUDPSocket(), and folly::test::MockAsyncUDPSocket::~MockAsyncUDPSocket().
|
virtual |
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:
Returns the result of calling the connect syscall.
Definition at line 421 of file AsyncUDPSocket.cpp.
References folly::netops::connect(), fd_, folly::SocketAddress::getActualSize(), and folly::SocketAddress::getAddress().
Referenced by getEventBase(), and folly::test::MockAsyncUDPSocket::~MockAsyncUDPSocket().
|
virtual |
Definition at line 539 of file AsyncUDPSocket.cpp.
References folly::EventHandler::detachEventBase(), eventBase_, folly::EventBase::isInEventBaseThread(), folly::EventHandler::NONE, folly::EventHandler::registerHandler(), and uint16_t.
Referenced by isBound().
|
virtual |
Enable or disable fragmentation on the socket.
On Linux, this sets IP(V6)_MTU_DISCOVER to IP(V6)_PMTUDISC_DO when enabled, and to IP(V6)_PMTUDISC_WANT when disabled. IP(V6)_PMTUDISC_WANT will use per-route setting to set DF bit. It may be more desirable to use IP(V6)_PMTUDISC_PROBE as opposed to IP(V6)_PMTUDISC_DO for apps that has its own PMTU Discovery mechanism. Note this doesn't work on Apple.
Definition at line 174 of file AsyncUDPSocket.cpp.
References address(), fd_, folly::SocketAddress::getFamily(), folly::AsyncSocketException::NOT_OPEN, and folly::netops::setsockopt().
Referenced by getEventBase(), and folly::test::MockAsyncUDPSocket::~MockAsyncUDPSocket().
|
protected |
Definition at line 413 of file AsyncUDPSocket.cpp.
References errMessageCallback_, and folly::AsyncUDPSocket::ErrMessageCallback::errMessageError().
Referenced by handleErrMessages(), and sendmsg().
|
inline |
Definition at line 231 of file AsyncUDPSocket.h.
References connect(), dontFragment(), eventBase_, and setErrMessageCallback().
|
inlinevirtual |
Get internal FD used by this socket
Definition at line 190 of file AsyncUDPSocket.h.
References fd_, and folly::NetworkSocket::toFd().
Referenced by folly::test::MockAsyncUDPSocket::~MockAsyncUDPSocket().
int folly::AsyncUDPSocket::getGSO | ( | ) |
Definition at line 520 of file AsyncUDPSocket.cpp.
References fd_, FOLLY_UNLIKELY, folly::netops::getsockopt(), gso_, folly::Optional< Value >::hasValue(), SOL_UDP, UDP_SEGMENT, and folly::Optional< Value >::value().
Referenced by isBound(), TEST(), and folly::test::MockAsyncUDPSocket::~MockAsyncUDPSocket().
|
protectednoexcept |
Definition at line 356 of file AsyncUDPSocket.cpp.
References folly::data(), folly::AsyncUDPSocket::ErrMessageCallback::errMessage(), errMessageCallback_, failErrMessageRead(), fd_, folly::AsyncSocketException::INTERNAL_ERROR, folly::netops::recvmsg(), and uint8_t.
Referenced by handleRead(), and sendmsg().
|
privatenoexcept |
Definition at line 429 of file AsyncUDPSocket.cpp.
References folly::AsyncSocketException::BAD_ARGS, clientAddress_, fd_, folly::SocketAddress::getFamily(), folly::AsyncUDPSocket::ReadCallback::getReadBuffer(), handleErrMessages(), folly::AsyncSocketException::INTERNAL_ERROR, localAddress_, folly::AsyncUDPSocket::ReadCallback::onDataAvailable(), folly::AsyncUDPSocket::ReadCallback::onReadError(), readCallback_, folly::netops::recvfrom(), folly::SocketAddress::setFromSockaddr(), and updateRegistration().
Referenced by handlerReady().
|
overrideprivatevirtualnoexcept |
handlerReady() is invoked when the handler is ready.
events | A bitset indicating the events that are ready. |
Implements folly::EventHandler.
Definition at line 349 of file AsyncUDPSocket.cpp.
References handleRead(), folly::EventHandler::READ, and readCallback_.
|
inlinevirtual |
Definition at line 272 of file AsyncUDPSocket.h.
References attachEventBase(), detachEventBase(), fd_, getGSO(), setGSO(), and val.
Referenced by TEST_F(), and folly::test::MockAsyncUDPSocket::~MockAsyncUDPSocket().
|
privatedelete |
|
virtual |
Pause reading datagrams
Definition at line 323 of file AsyncUDPSocket.cpp.
References readCallback_, and updateRegistration().
Referenced by setFD(), and folly::test::MockAsyncUDPSocket::~MockAsyncUDPSocket().
|
virtual |
Start reading datagrams
Definition at line 307 of file AsyncUDPSocket.cpp.
References fd_, folly::AsyncSocketException::NOT_OPEN, folly::AsyncUDPSocket::ReadCallback::onReadError(), readCallback_, and updateRegistration().
Referenced by setFD(), and folly::test::MockAsyncUDPSocket::~MockAsyncUDPSocket().
|
inlineprotectedvirtual |
Definition at line 288 of file AsyncUDPSocket.h.
References failErrMessageRead(), handleErrMessages(), folly::pushmi::__adl::noexcept(), and folly::netops::sendmsg().
Referenced by writev().
|
inlinevirtual |
Set SO_BUSY_POLL option on the socket, if not zero. Default is zero. Caution! The feature is not available on Apple's systems.
Definition at line 227 of file AsyncUDPSocket.h.
References busyPollUs_.
|
virtual |
Callback for receiving errors on the UDP sockets
Definition at line 203 of file AsyncUDPSocket.cpp.
References address(), errMessageCallback_, fd_, folly::SocketAddress::getFamily(), folly::AsyncSocketException::NOT_OPEN, and folly::netops::setsockopt().
Referenced by getEventBase(), and folly::test::MockAsyncUDPSocket::~MockAsyncUDPSocket().
|
inline |
Use an already bound file descriptor. You can either transfer ownership of this FD by using ownership = FDOwnership::OWNS or share it using FDOwnership::SHARED. In case FD is shared, it will not be close
d in destructor.
Definition at line 131 of file AsyncUDPSocket.h.
References close(), folly::NetworkSocket::fromFd(), pauseRead(), resumeRead(), write(), writeGSO(), and writev().
Referenced by folly::test::MockAsyncUDPSocket::~MockAsyncUDPSocket().
|
virtual |
Definition at line 224 of file AsyncUDPSocket.cpp.
References folly::EventHandler::changeHandlerFD(), fd_, localAddress_, ownership_, and folly::SocketAddress::setFromLocalAddress().
bool folly::AsyncUDPSocket::setGSO | ( | int | val | ) |
Definition at line 507 of file AsyncUDPSocket.cpp.
References fd_, gso_, folly::netops::setsockopt(), SOL_UDP, UDP_SEGMENT, and val.
Referenced by isBound(), and folly::test::MockAsyncUDPSocket::~MockAsyncUDPSocket().
|
inlinevirtual |
Set SO_RCVBUF option on the socket, if not zero. Default is zero.
Definition at line 212 of file AsyncUDPSocket.h.
References rcvBuf_.
|
inlinevirtual |
Set SO_REUSEADDR flag on the socket. Default is ON.
Definition at line 205 of file AsyncUDPSocket.h.
References reuseAddr_.
Referenced by folly::test::MockAsyncUDPSocket::~MockAsyncUDPSocket().
|
inlinevirtual |
Set reuse port mode to call bind() on the same address multiple times
Definition at line 198 of file AsyncUDPSocket.h.
References reusePort_.
Referenced by folly::test::MockAsyncUDPSocket::~MockAsyncUDPSocket().
|
inlinevirtual |
Set SO_SNDBUG option on the socket, if not zero. Default is zero.
Definition at line 219 of file AsyncUDPSocket.h.
References sndBuf_.
|
privatenoexcept |
Definition at line 497 of file AsyncUDPSocket.cpp.
References folly::EventHandler::NONE, folly::EventHandler::PERSIST, folly::EventHandler::READ, readCallback_, folly::EventHandler::registerHandler(), and uint16_t.
Referenced by attachEventBase(), handleRead(), pauseRead(), and resumeRead().
|
virtual |
Send the data in buffer to destination. Returns the return code from ::sendmsg.
Definition at line 254 of file AsyncUDPSocket.cpp.
References writeGSO().
Referenced by setFD(), TEST(), and folly::test::MockAsyncUDPSocket::~MockAsyncUDPSocket().
|
virtual |
Send the data in buffer to destination. Returns the return code from ::sendmsg. gso is the generic segmentation offload value writeGSO will return -1 if buf->computeChainDataLength() <= gso Before calling writeGSO with a positive value verify GSO is supported on this platform by calling getGSO
Definition at line 234 of file AsyncUDPSocket.cpp.
References folly::IOBuf::coalesce(), folly::IOBuf::data(), folly::IOBuf::fillIov(), folly::IOBuf::length(), uint8_t, UNLIKELY, and writev().
Referenced by GSOSendTest::GSOSendTest(), setFD(), write(), and folly::test::MockAsyncUDPSocket::~MockAsyncUDPSocket().
|
virtual |
Send data in iovec to destination. Returns the return code from sendmsg.
Definition at line 260 of file AsyncUDPSocket.cpp.
References fd_, folly::SocketAddress::getActualSize(), folly::SocketAddress::getAddress(), sendmsg(), SOL_UDP, UDP_SEGMENT, and uint16_t.
Referenced by setFD(), writeGSO(), writev(), and folly::test::MockAsyncUDPSocket::~MockAsyncUDPSocket().
|
virtual |
Definition at line 301 of file AsyncUDPSocket.cpp.
References writev().
|
private |
Definition at line 322 of file AsyncUDPSocket.h.
Referenced by bind(), and setBusyPoll().
|
private |
Definition at line 316 of file AsyncUDPSocket.h.
Referenced by handleRead().
|
private |
Definition at line 328 of file AsyncUDPSocket.h.
Referenced by failErrMessageRead(), handleErrMessages(), and setErrMessageCallback().
|
private |
Definition at line 309 of file AsyncUDPSocket.h.
Referenced by attachEventBase(), close(), detachEventBase(), and getEventBase().
|
private |
Definition at line 312 of file AsyncUDPSocket.h.
Referenced by address(), bind(), close(), connect(), dontFragment(), getFD(), getGSO(), handleErrMessages(), handleRead(), isBound(), resumeRead(), setErrMessageCallback(), setFD(), setGSO(), writev(), and ~AsyncUDPSocket().
|
private |
Definition at line 326 of file AsyncUDPSocket.h.
|
private |
Definition at line 310 of file AsyncUDPSocket.h.
Referenced by address(), bind(), handleRead(), and setFD().
|
private |
Definition at line 313 of file AsyncUDPSocket.h.
|
private |
Definition at line 320 of file AsyncUDPSocket.h.
Referenced by bind(), and setRcvBuf().
|
protected |
Definition at line 297 of file AsyncUDPSocket.h.
Referenced by close(), handleRead(), handlerReady(), pauseRead(), resumeRead(), and updateRegistration().
|
private |
Definition at line 318 of file AsyncUDPSocket.h.
Referenced by bind(), and setReuseAddr().
|
private |
Definition at line 319 of file AsyncUDPSocket.h.
Referenced by bind(), and setReusePort().
|
private |
Definition at line 321 of file AsyncUDPSocket.h.
Referenced by bind(), and setSndBuf().