2 * Copyright 2017 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/Optional.h>
20 #include <folly/SocketAddress.h>
21 #include <folly/detail/SocketFastOpen.h>
22 #include <folly/io/IOBuf.h>
23 #include <folly/io/ShutdownSocketSet.h>
24 #include <folly/io/async/AsyncSocketException.h>
25 #include <folly/io/async/AsyncTimeout.h>
26 #include <folly/io/async/AsyncTransport.h>
27 #include <folly/io/async/DelayedDestruction.h>
28 #include <folly/io/async/EventHandler.h>
29 #include <folly/portability/Sockets.h>
31 #include <sys/types.h>
40 * A class for performing asynchronous I/O on a socket.
42 * AsyncSocket allows users to asynchronously wait for data on a socket, and
43 * to asynchronously send data.
45 * The APIs for reading and writing are intentionally asymmetric. Waiting for
46 * data to read is a persistent API: a callback is installed, and is notified
47 * whenever new data is available. It continues to be notified of new events
48 * until it is uninstalled.
50 * AsyncSocket does not provide read timeout functionality, because it
51 * typically cannot determine when the timeout should be active. Generally, a
52 * timeout should only be enabled when processing is blocked waiting on data
53 * from the remote endpoint. For server sockets, the timeout should not be
54 * active if the server is currently processing one or more outstanding
55 * requests for this socket. For client sockets, the timeout should not be
56 * active if there are no requests pending on the socket. Additionally, if a
57 * client has multiple pending requests, it will ususally want a separate
58 * timeout for each request, rather than a single read timeout.
60 * The write API is fairly intuitive: a user can request to send a block of
61 * data, and a callback will be informed once the entire block has been
62 * transferred to the kernel, or on error. AsyncSocket does provide a send
63 * timeout, since most callers want to give up if the remote end stops
64 * responding and no further progress can be made sending the data.
67 #if defined __linux__ && !defined SO_NO_TRANSPARENT_TLS
68 #define SO_NO_TRANSPARENT_TLS 200
71 #if defined __linux__ && !defined SO_NO_TSOCKS
72 #define SO_NO_TSOCKS 201
76 // We do a dynamic_cast on this, in
77 // AsyncTransportWrapper::getUnderlyingTransport so be safe and
78 // force displacements for it. See:
79 // https://msdn.microsoft.com/en-us/library/7sf3txa8.aspx
80 #pragma vtordisp(push, 2)
82 class AsyncSocket : virtual public AsyncTransportWrapper {
84 typedef std::unique_ptr<AsyncSocket, Destructor> UniquePtr;
86 class ConnectCallback {
88 virtual ~ConnectCallback() = default;
91 * connectSuccess() will be invoked when the connection has been
92 * successfully established.
94 virtual void connectSuccess() noexcept = 0;
97 * connectErr() will be invoked if the connection attempt fails.
99 * @param ex An exception describing the error that occurred.
101 virtual void connectErr(const AsyncSocketException& ex)
105 class EvbChangeCallback {
107 virtual ~EvbChangeCallback() = default;
109 // Called when the socket has been attached to a new EVB
110 // and is called from within that EVB thread
111 virtual void evbAttached(AsyncSocket* socket) = 0;
113 // Called when the socket is detached from an EVB and
114 // is called from the EVB thread being detached
115 virtual void evbDetached(AsyncSocket* socket) = 0;
119 * This interface is implemented only for platforms supporting
120 * per-socket error queues.
122 class ErrMessageCallback {
124 virtual ~ErrMessageCallback() = default;
127 * errMessage() will be invoked when kernel puts a message to
128 * the error queue associated with the socket.
130 * @param cmsg Reference to cmsghdr structure describing
131 * a message read from error queue associated
135 errMessage(const cmsghdr& cmsg) noexcept = 0;
138 * errMessageError() will be invoked if an error occurs reading a message
139 * from the socket error stream.
141 * @param ex An exception describing the error that occurred.
143 virtual void errMessageError(const AsyncSocketException& ex) noexcept = 0;
146 class SendMsgParamsCallback {
148 virtual ~SendMsgParamsCallback() = default;
151 * getFlags() will be invoked to retrieve the desired flags to be passed
152 * to ::sendmsg() system call. This method was intentionally declared
153 * non-virtual, so there is no way to override it. Instead feel free to
154 * override getFlagsImpl(flags, defaultFlags) method instead, and enjoy
155 * the convenience of defaultFlags passed there.
157 * @param flags Write flags requested for the given write operation
159 int getFlags(folly::WriteFlags flags, bool zeroCopyEnabled) noexcept {
160 return getFlagsImpl(flags, getDefaultFlags(flags, zeroCopyEnabled));
164 * getAncillaryData() will be invoked to initialize ancillary data
165 * buffer referred by "msg_control" field of msghdr structure passed to
166 * ::sendmsg() system call. The function assumes that the size of buffer
167 * is not smaller than the value returned by getAncillaryDataSize() method
168 * for the same combination of flags.
170 * @param flags Write flags requested for the given write operation
171 * @param data Pointer to ancillary data buffer to initialize.
173 virtual void getAncillaryData(
174 folly::WriteFlags /*flags*/,
175 void* /*data*/) noexcept {}
178 * getAncillaryDataSize() will be invoked to retrieve the size of
179 * ancillary data buffer which should be passed to ::sendmsg() system call
181 * @param flags Write flags requested for the given write operation
183 virtual uint32_t getAncillaryDataSize(folly::WriteFlags /*flags*/)
188 static const size_t maxAncillaryDataSize{0x5000};
192 * getFlagsImpl() will be invoked by getFlags(folly::WriteFlags flags)
193 * method to retrieve the flags to be passed to ::sendmsg() system call.
194 * SendMsgParamsCallback::getFlags() is calling this method, and returns
195 * its results directly to the caller in AsyncSocket.
196 * Classes inheriting from SendMsgParamsCallback are welcome to override
197 * this method to force SendMsgParamsCallback to return its own set
200 * @param flags Write flags requested for the given write operation
201 * @param defaultflags A set of message flags returned by getDefaultFlags()
202 * method for the given "flags" mask.
204 virtual int getFlagsImpl(folly::WriteFlags /*flags*/, int defaultFlags) {
209 * getDefaultFlags() will be invoked by getFlags(folly::WriteFlags flags)
210 * to retrieve the default set of flags, and pass them to getFlagsImpl(...)
212 * @param flags Write flags requested for the given write operation
214 int getDefaultFlags(folly::WriteFlags flags, bool zeroCopyEnabled) noexcept;
217 explicit AsyncSocket();
219 * Create a new unconnected AsyncSocket.
221 * connect() must later be called on this socket to establish a connection.
223 explicit AsyncSocket(EventBase* evb);
225 void setShutdownSocketSet(const std::weak_ptr<ShutdownSocketSet>& wSS);
228 * Create a new AsyncSocket and begin the connection process.
230 * @param evb EventBase that will manage this socket.
231 * @param address The address to connect to.
232 * @param connectTimeout Optional timeout in milliseconds for the connection
235 AsyncSocket(EventBase* evb,
236 const folly::SocketAddress& address,
237 uint32_t connectTimeout = 0);
240 * Create a new AsyncSocket and begin the connection process.
242 * @param evb EventBase that will manage this socket.
243 * @param ip IP address to connect to (dotted-quad).
244 * @param port Destination port in host byte order.
245 * @param connectTimeout Optional timeout in milliseconds for the connection
248 AsyncSocket(EventBase* evb,
249 const std::string& ip,
251 uint32_t connectTimeout = 0);
254 * Create a AsyncSocket from an already connected socket file descriptor.
256 * Note that while AsyncSocket enables TCP_NODELAY for sockets it creates
257 * when connecting, it does not change the socket options when given an
258 * existing file descriptor. If callers want TCP_NODELAY enabled when using
259 * this version of the constructor, they need to explicitly call
260 * setNoDelay(true) after the constructor returns.
262 * @param evb EventBase that will manage this socket.
263 * @param fd File descriptor to take over (should be a connected socket).
265 AsyncSocket(EventBase* evb, int fd);
268 * Create an AsyncSocket from a different, already connected AsyncSocket.
270 * Similar to AsyncSocket(evb, fd) when fd was previously owned by an
273 explicit AsyncSocket(AsyncSocket::UniquePtr);
276 * Helper function to create a shared_ptr<AsyncSocket>.
278 * This passes in the correct destructor object, since AsyncSocket's
279 * destructor is protected and cannot be invoked directly.
281 static std::shared_ptr<AsyncSocket> newSocket(EventBase* evb) {
282 return std::shared_ptr<AsyncSocket>(new AsyncSocket(evb),
287 * Helper function to create a shared_ptr<AsyncSocket>.
289 static std::shared_ptr<AsyncSocket> newSocket(
291 const folly::SocketAddress& address,
292 uint32_t connectTimeout = 0) {
293 return std::shared_ptr<AsyncSocket>(
294 new AsyncSocket(evb, address, connectTimeout),
299 * Helper function to create a shared_ptr<AsyncSocket>.
301 static std::shared_ptr<AsyncSocket> newSocket(
303 const std::string& ip,
305 uint32_t connectTimeout = 0) {
306 return std::shared_ptr<AsyncSocket>(
307 new AsyncSocket(evb, ip, port, connectTimeout),
312 * Helper function to create a shared_ptr<AsyncSocket>.
314 static std::shared_ptr<AsyncSocket> newSocket(EventBase* evb, int fd) {
315 return std::shared_ptr<AsyncSocket>(new AsyncSocket(evb, fd),
320 * Destroy the socket.
322 * AsyncSocket::destroy() must be called to destroy the socket.
323 * The normal destructor is private, and should not be invoked directly.
324 * This prevents callers from deleting a AsyncSocket while it is invoking a
327 void destroy() override;
330 * Get the EventBase used by this socket.
332 EventBase* getEventBase() const override {
337 * Get the file descriptor used by the AsyncSocket.
339 virtual int getFd() const {
344 * Extract the file descriptor from the AsyncSocket.
346 * This will immediately cause any installed callbacks to be invoked with an
347 * error. The AsyncSocket may no longer be used after the file descriptor
348 * has been extracted.
350 * This method should be used with care as the resulting fd is not guaranteed
351 * to perfectly reflect the state of the AsyncSocket (security state,
352 * pre-received data, etc.).
354 * Returns the file descriptor. The caller assumes ownership of the
355 * descriptor, and it will not be closed when the AsyncSocket is destroyed.
357 virtual int detachFd();
360 * Uniquely identifies a handle to a socket option value. Each
361 * combination of level and option name corresponds to one socket
366 bool operator<(const OptionKey& other) const {
367 if (level == other.level) {
368 return optname < other.optname;
370 return level < other.level;
372 int apply(int fd, int val) const {
373 return setsockopt(fd, level, optname, &val, sizeof(val));
379 // Maps from a socket option key to its value
380 typedef std::map<OptionKey, int> OptionMap;
382 static const OptionMap emptyOptionMap;
383 static const folly::SocketAddress& anyAddress();
386 * Initiate a connection.
388 * @param callback The callback to inform when the connection attempt
390 * @param address The address to connect to.
391 * @param timeout A timeout value, in milliseconds. If the connection
392 * does not succeed within this period,
393 * callback->connectError() will be invoked.
395 virtual void connect(
396 ConnectCallback* callback,
397 const folly::SocketAddress& address,
399 const OptionMap& options = emptyOptionMap,
400 const folly::SocketAddress& bindAddr = anyAddress()) noexcept;
403 ConnectCallback* callback,
404 const std::string& ip,
407 const OptionMap& options = emptyOptionMap) noexcept;
410 * If a connect request is in-flight, cancels it and closes the socket
411 * immediately. Otherwise, this is a no-op.
413 * This does not invoke any connection related callbacks. Call this to
414 * prevent any connect callback while cleaning up, etc.
416 void cancelConnect();
419 * Set the send timeout.
421 * If write requests do not make any progress for more than the specified
422 * number of milliseconds, fail all pending writes and close the socket.
424 * If write requests are currently pending when setSendTimeout() is called,
425 * the timeout interval is immediately restarted using the new value.
427 * (See the comments for AsyncSocket for an explanation of why AsyncSocket
428 * provides setSendTimeout() but not setRecvTimeout().)
430 * @param milliseconds The timeout duration, in milliseconds. If 0, no
431 * timeout will be used.
433 void setSendTimeout(uint32_t milliseconds) override;
436 * Get the send timeout.
438 * @return Returns the current send timeout, in milliseconds. A return value
439 * of 0 indicates that no timeout is set.
441 uint32_t getSendTimeout() const override {
446 * Set the maximum number of reads to execute from the underlying
447 * socket each time the EventBase detects that new ingress data is
448 * available. The default is unlimited, but callers can use this method
449 * to limit the amount of data read from the socket per event loop
452 * @param maxReads Maximum number of reads per data-available event;
453 * a value of zero means unlimited.
455 void setMaxReadsPerEvent(uint16_t maxReads) {
456 maxReadsPerEvent_ = maxReads;
460 * Get the maximum number of reads this object will execute from
461 * the underlying socket each time the EventBase detects that new
462 * ingress data is available.
464 * @returns Maximum number of reads per data-available event; a value
465 * of zero means unlimited.
467 uint16_t getMaxReadsPerEvent() const {
468 return maxReadsPerEvent_;
472 * Set a pointer to ErrMessageCallback implementation which will be
473 * receiving notifications for messages posted to the error queue
474 * associated with the socket.
475 * ErrMessageCallback is implemented only for platforms with
476 * per-socket error message queus support (recvmsg() system call must
480 virtual void setErrMessageCB(ErrMessageCallback* callback);
483 * Get a pointer to ErrMessageCallback implementation currently
484 * registered with this socket.
487 virtual ErrMessageCallback* getErrMessageCallback() const;
490 * Set a pointer to SendMsgParamsCallback implementation which
491 * will be used to form ::sendmsg() system call parameters
494 virtual void setSendMsgParamCB(SendMsgParamsCallback* callback);
497 * Get a pointer to SendMsgParamsCallback implementation currently
498 * registered with this socket.
501 virtual SendMsgParamsCallback* getSendMsgParamsCB() const;
503 // Read and write methods
504 void setReadCB(ReadCallback* callback) override;
505 ReadCallback* getReadCallback() const override;
507 static const size_t kDefaultZeroCopyThreshold = 32768; // 32KB
509 bool setZeroCopy(bool enable);
510 bool getZeroCopy() const {
511 return zeroCopyEnabled_;
514 void setZeroCopyWriteChainThreshold(size_t threshold);
515 size_t getZeroCopyWriteChainThreshold() const {
516 return zeroCopyWriteChainThreshold_;
519 bool isZeroCopyMsg(const cmsghdr& cmsg) const;
520 void processZeroCopyMsg(const cmsghdr& cmsg);
522 void write(WriteCallback* callback, const void* buf, size_t bytes,
523 WriteFlags flags = WriteFlags::NONE) override;
524 void writev(WriteCallback* callback, const iovec* vec, size_t count,
525 WriteFlags flags = WriteFlags::NONE) override;
526 void writeChain(WriteCallback* callback,
527 std::unique_ptr<folly::IOBuf>&& buf,
528 WriteFlags flags = WriteFlags::NONE) override;
531 virtual void writeRequest(WriteRequest* req);
532 void writeRequestReady() {
536 // Methods inherited from AsyncTransport
537 void close() override;
538 void closeNow() override;
539 void closeWithReset() override;
540 void shutdownWrite() override;
541 void shutdownWriteNow() override;
543 bool readable() const override;
544 bool writable() const override;
545 bool isPending() const override;
546 virtual bool hangup() const;
547 bool good() const override;
548 bool error() const override;
549 void attachEventBase(EventBase* eventBase) override;
550 void detachEventBase() override;
551 bool isDetachable() const override;
553 void getLocalAddress(
554 folly::SocketAddress* address) const override;
556 folly::SocketAddress* address) const override;
558 bool isEorTrackingEnabled() const override {
562 void setEorTracking(bool track) override {
566 bool connecting() const override {
567 return (state_ == StateEnum::CONNECTING);
570 virtual bool isClosedByPeer() const {
571 return (state_ == StateEnum::CLOSED &&
572 (readErr_ == READ_EOF || readErr_ == READ_ERROR));
575 virtual bool isClosedBySelf() const {
576 return (state_ == StateEnum::CLOSED &&
577 (readErr_ != READ_EOF && readErr_ != READ_ERROR));
580 size_t getAppBytesWritten() const override {
581 return appBytesWritten_;
584 size_t getRawBytesWritten() const override {
585 return getAppBytesWritten();
588 size_t getAppBytesReceived() const override {
589 return appBytesReceived_;
592 size_t getRawBytesReceived() const override {
593 return getAppBytesReceived();
596 std::chrono::nanoseconds getConnectTime() const {
597 return connectEndTime_ - connectStartTime_;
600 std::chrono::milliseconds getConnectTimeout() const {
601 return connectTimeout_;
604 bool getTFOAttempted() const {
605 return tfoAttempted_;
609 * Returns whether or not the attempt to use TFO
610 * finished successfully. This does not necessarily
611 * mean TFO worked, just that trying to use TFO
614 bool getTFOFinished() const {
619 * Returns whether or not TFO attempt succeded on this
621 * For servers this is pretty straightforward API and can
622 * be invoked right after the connection is accepted. This API
623 * will perform one syscall.
624 * This API is a bit tricky to use for clients, since clients
625 * only know this for sure after the SYN-ACK is returned. So it's
626 * appropriate to call this only after the first application
627 * data is read from the socket when the caller knows that
628 * the SYN has been ACKed by the server.
630 bool getTFOSucceded() const;
632 // Methods controlling socket options
635 * Force writes to be transmitted immediately.
637 * This controls the TCP_NODELAY socket option. When enabled, TCP segments
638 * are sent as soon as possible, even if it is not a full frame of data.
639 * When disabled, the data may be buffered briefly to try and wait for a full
642 * By default, TCP_NODELAY is enabled for AsyncSocket objects.
644 * This method will fail if the socket is not currently open.
646 * @return Returns 0 if the TCP_NODELAY flag was successfully updated,
647 * or a non-zero errno value on error.
649 int setNoDelay(bool noDelay);
653 * Set the FD_CLOEXEC flag so that the socket will be closed if the program
654 * later forks and execs.
656 void setCloseOnExec();
659 * Set the Flavor of Congestion Control to be used for this Socket
660 * Please check '/lib/modules/<kernel>/kernel/net/ipv4' for tcp_*.ko
661 * first to make sure the module is available for plugging in
662 * Alternatively you can choose from net.ipv4.tcp_allowed_congestion_control
664 int setCongestionFlavor(const std::string &cname);
667 * Forces ACKs to be sent immediately
669 * @return Returns 0 if the TCP_QUICKACK flag was successfully updated,
670 * or a non-zero errno value on error.
672 int setQuickAck(bool quickack);
675 * Set the send bufsize
677 int setSendBufSize(size_t bufsize);
680 * Set the recv bufsize
682 int setRecvBufSize(size_t bufsize);
685 * Sets a specific tcp personality
686 * Available only on kernels 3.2 and greater
688 #define SO_SET_NAMESPACE 41
689 int setTCPProfile(int profd);
692 * Generic API for reading a socket option.
694 * @param level same as the "level" parameter in getsockopt().
695 * @param optname same as the "optname" parameter in getsockopt().
696 * @param optval pointer to the variable in which the option value should
698 * @param optlen value-result argument, initially containing the size of
699 * the buffer pointed to by optval, and modified on return
700 * to indicate the actual size of the value returned.
701 * @return same as the return value of getsockopt().
703 template <typename T>
704 int getSockOpt(int level, int optname, T* optval, socklen_t* optlen) {
705 return getsockopt(fd_, level, optname, (void*) optval, optlen);
709 * Generic API for setting a socket option.
711 * @param level same as the "level" parameter in getsockopt().
712 * @param optname same as the "optname" parameter in getsockopt().
713 * @param optval the option value to set.
714 * @return same as the return value of setsockopt().
716 template <typename T>
717 int setSockOpt(int level, int optname, const T *optval) {
718 return setsockopt(fd_, level, optname, optval, sizeof(T));
722 * Virtual method for reading a socket option returning integer
723 * value, which is the most typical case. Convenient for overriding
726 * @param level same as the "level" parameter in getsockopt().
727 * @param optname same as the "optname" parameter in getsockopt().
728 * @param optval same as "optval" parameter in getsockopt().
729 * @param optlen same as "optlen" parameter in getsockopt().
730 * @return same as the return value of getsockopt().
733 getSockOptVirtual(int level, int optname, void* optval, socklen_t* optlen) {
734 return getsockopt(fd_, level, optname, optval, optlen);
738 * Virtual method for setting a socket option accepting integer
739 * value, which is the most typical case. Convenient for overriding
742 * @param level same as the "level" parameter in setsockopt().
743 * @param optname same as the "optname" parameter in setsockopt().
744 * @param optval same as "optval" parameter in setsockopt().
745 * @param optlen same as "optlen" parameter in setsockopt().
746 * @return same as the return value of setsockopt().
748 virtual int setSockOptVirtual(
753 return setsockopt(fd_, level, optname, optval, optlen);
757 * Set pre-received data, to be returned to read callback before any data
760 virtual void setPreReceivedData(std::unique_ptr<IOBuf> data) {
761 if (preReceivedData_) {
762 preReceivedData_->prependChain(std::move(data));
764 preReceivedData_ = std::move(data);
769 * Enables TFO behavior on the AsyncSocket if FOLLY_ALLOW_TFO
773 // No-op if folly does not allow tfo
779 void disableTransparentTls() {
780 noTransparentTls_ = true;
783 void disableTSocks() {
787 enum class StateEnum : uint8_t {
796 void setBufferCallback(BufferCallback* cb);
798 // Callers should set this prior to connecting the socket for the safest
800 void setEvbChangedCallback(std::unique_ptr<EvbChangeCallback> cb) {
801 evbChangeCb_ = std::move(cb);
805 * Attempt to cache the current local and peer addresses (if not already
806 * cached) so that they are available from getPeerAddress() and
807 * getLocalAddress() even after the socket is closed.
809 void cacheAddresses();
812 * writeReturn is the total number of bytes written, or WRITE_ERROR on error.
813 * If no data has been written, 0 is returned.
814 * exception is a more specific exception that cause a write error.
815 * Not all writes have exceptions associated with them thus writeReturn
816 * should be checked to determine whether the operation resulted in an error.
819 explicit WriteResult(ssize_t ret) : writeReturn(ret) {}
821 WriteResult(ssize_t ret, std::unique_ptr<const AsyncSocketException> e)
822 : writeReturn(ret), exception(std::move(e)) {}
825 std::unique_ptr<const AsyncSocketException> exception;
829 * readReturn is the number of bytes read, or READ_EOF on EOF, or
830 * READ_ERROR on error, or READ_BLOCKING if the operation will
832 * exception is a more specific exception that may have caused a read error.
833 * Not all read errors have exceptions associated with them thus readReturn
834 * should be checked to determine whether the operation resulted in an error.
837 explicit ReadResult(ssize_t ret) : readReturn(ret) {}
839 ReadResult(ssize_t ret, std::unique_ptr<const AsyncSocketException> e)
840 : readReturn(ret), exception(std::move(e)) {}
843 std::unique_ptr<const AsyncSocketException> exception;
847 * A WriteRequest object tracks information about a pending write operation.
851 WriteRequest(AsyncSocket* socket, WriteCallback* callback) :
852 socket_(socket), callback_(callback) {}
854 virtual void start() {}
856 virtual void destroy() = 0;
858 virtual WriteResult performWrite() = 0;
860 virtual void consume() = 0;
862 virtual bool isComplete() = 0;
864 WriteRequest* getNext() const {
868 WriteCallback* getCallback() const {
872 uint32_t getTotalBytesWritten() const {
873 return totalBytesWritten_;
876 void append(WriteRequest* next) {
877 assert(next_ == nullptr);
881 void fail(const char* fn, const AsyncSocketException& ex) {
882 socket_->failWrite(fn, ex);
885 void bytesWritten(size_t count) {
886 totalBytesWritten_ += uint32_t(count);
887 socket_->appBytesWritten_ += count;
891 // protected destructor, to ensure callers use destroy()
892 virtual ~WriteRequest() {}
894 AsyncSocket* socket_; ///< parent socket
895 WriteRequest* next_{nullptr}; ///< pointer to next WriteRequest
896 WriteCallback* callback_; ///< completion callback
897 uint32_t totalBytesWritten_{0}; ///< total bytes written
901 enum ReadResultEnum {
908 enum WriteResultEnum {
913 * Protected destructor.
915 * Users of AsyncSocket must never delete it directly. Instead, invoke
916 * destroy() instead. (See the documentation in DelayedDestruction.h for
919 ~AsyncSocket() override;
921 friend std::ostream& operator << (std::ostream& os, const StateEnum& state);
924 /// shutdownWrite() called, but we are still waiting on writes to drain
925 SHUT_WRITE_PENDING = 0x01,
926 /// writes have been completely shut down
929 * Reads have been shutdown.
931 * At the moment we don't distinguish between remote read shutdown
932 * (received EOF from the remote end) and local read shutdown. We can
933 * only receive EOF when a read callback is set, and we immediately inform
934 * it of the EOF. Therefore there doesn't seem to be any reason to have a
935 * separate state of "received EOF but the local side may still want to
938 * We also don't currently provide any API for only shutting down the read
939 * side of a socket. (This is a no-op as far as TCP is concerned, anyway.)
944 class BytesWriteRequest;
946 class WriteTimeout : public AsyncTimeout {
948 WriteTimeout(AsyncSocket* socket, EventBase* eventBase)
949 : AsyncTimeout(eventBase)
952 void timeoutExpired() noexcept override {
953 socket_->timeoutExpired();
957 AsyncSocket* socket_;
960 class IoHandler : public EventHandler {
962 IoHandler(AsyncSocket* socket, EventBase* eventBase)
963 : EventHandler(eventBase, -1)
965 IoHandler(AsyncSocket* socket, EventBase* eventBase, int fd)
966 : EventHandler(eventBase, fd)
969 void handlerReady(uint16_t events) noexcept override {
970 socket_->ioReady(events);
974 AsyncSocket* socket_;
979 class ImmediateReadCB : public folly::EventBase::LoopCallback {
981 explicit ImmediateReadCB(AsyncSocket* socket) : socket_(socket) {}
982 void runLoopCallback() noexcept override {
983 DestructorGuard dg(socket_);
984 socket_->checkForImmediateRead();
987 AsyncSocket* socket_;
991 * Schedule checkForImmediateRead to be executed in the next loop
994 void scheduleImmediateRead() noexcept {
996 eventBase_->runInLoop(&immediateReadHandler_);
1001 * Schedule handleInitalReadWrite to run in the next iteration.
1003 void scheduleInitialReadWrite() noexcept {
1005 DestructorGuard dg(this);
1006 eventBase_->runInLoop([this, dg] {
1008 handleInitialReadWrite();
1014 // event notification methods
1015 void ioReady(uint16_t events) noexcept;
1016 virtual void checkForImmediateRead() noexcept;
1017 virtual void handleInitialReadWrite() noexcept;
1018 virtual void prepareReadBuffer(void** buf, size_t* buflen);
1019 virtual void handleErrMessages() noexcept;
1020 virtual void handleRead() noexcept;
1021 virtual void handleWrite() noexcept;
1022 virtual void handleConnect() noexcept;
1023 void timeoutExpired() noexcept;
1026 * Attempt to read from the socket.
1028 * @param buf The buffer to read data into.
1029 * @param buflen The length of the buffer.
1031 * @return Returns a read result. See read result for details.
1033 virtual ReadResult performRead(void** buf, size_t* buflen, size_t* offset);
1036 * Populate an iovec array from an IOBuf and attempt to write it.
1038 * @param callback Write completion/error callback.
1039 * @param vec Target iovec array; caller retains ownership.
1040 * @param count Number of IOBufs to write, beginning at start of buf.
1041 * @param buf Chain of iovecs.
1042 * @param flags set of flags for the underlying write calls, like cork
1044 void writeChainImpl(WriteCallback* callback, iovec* vec,
1045 size_t count, std::unique_ptr<folly::IOBuf>&& buf,
1049 * Write as much data as possible to the socket without blocking,
1050 * and queue up any leftover data to send when the socket can
1051 * handle writes again.
1053 * @param callback The callback to invoke when the write is completed.
1054 * @param vec Array of buffers to write; this method will make a
1055 * copy of the vector (but not the buffers themselves)
1056 * if the write has to be completed asynchronously.
1057 * @param count Number of elements in vec.
1058 * @param buf The IOBuf that manages the buffers referenced by
1059 * vec, or a pointer to nullptr if the buffers are not
1060 * associated with an IOBuf. Note that ownership of
1061 * the IOBuf is transferred here; upon completion of
1062 * the write, the AsyncSocket deletes the IOBuf.
1063 * @param flags Set of write flags.
1065 void writeImpl(WriteCallback* callback, const iovec* vec, size_t count,
1066 std::unique_ptr<folly::IOBuf>&& buf,
1067 WriteFlags flags = WriteFlags::NONE);
1070 * Attempt to write to the socket.
1072 * @param vec The iovec array pointing to the buffers to write.
1073 * @param count The length of the iovec array.
1074 * @param flags Set of write flags.
1075 * @param countWritten On return, the value pointed to by this parameter
1076 * will contain the number of iovec entries that were
1078 * @param partialWritten On return, the value pointed to by this parameter
1079 * will contain the number of bytes written in the
1080 * partially written iovec entry.
1082 * @return Returns a WriteResult. See WriteResult for more details.
1084 virtual WriteResult performWrite(
1088 uint32_t* countWritten,
1089 uint32_t* partialWritten);
1092 * Sends the message over the socket using sendmsg
1094 * @param msg Message to send
1095 * @param msg_flags Flags to pass to sendmsg
1097 AsyncSocket::WriteResult
1098 sendSocketMessage(int fd, struct msghdr* msg, int msg_flags);
1100 virtual ssize_t tfoSendMsg(int fd, struct msghdr* msg, int msg_flags);
1102 int socketConnect(const struct sockaddr* addr, socklen_t len);
1104 virtual void scheduleConnectTimeout();
1105 void registerForConnectEvents();
1107 bool updateEventRegistration();
1110 * Update event registration.
1112 * @param enable Flags of events to enable. Set it to 0 if no events
1113 * need to be enabled in this call.
1114 * @param disable Flags of events
1115 * to disable. Set it to 0 if no events need to be disabled in this
1118 * @return true iff the update is successful.
1120 bool updateEventRegistration(uint16_t enable, uint16_t disable);
1122 // Actually close the file descriptor and set it to -1 so we don't
1123 // accidentally close it again.
1126 // error handling methods
1129 void finishFail(const AsyncSocketException& ex);
1130 void invokeAllErrors(const AsyncSocketException& ex);
1131 void fail(const char* fn, const AsyncSocketException& ex);
1132 void failConnect(const char* fn, const AsyncSocketException& ex);
1133 void failRead(const char* fn, const AsyncSocketException& ex);
1134 void failErrMessageRead(const char* fn, const AsyncSocketException& ex);
1135 void failWrite(const char* fn, WriteCallback* callback, size_t bytesWritten,
1136 const AsyncSocketException& ex);
1137 void failWrite(const char* fn, const AsyncSocketException& ex);
1138 void failAllWrites(const AsyncSocketException& ex);
1139 virtual void invokeConnectErr(const AsyncSocketException& ex);
1140 virtual void invokeConnectSuccess();
1141 void invalidState(ConnectCallback* callback);
1142 void invalidState(ErrMessageCallback* callback);
1143 void invalidState(ReadCallback* callback);
1144 void invalidState(WriteCallback* callback);
1146 std::string withAddr(const std::string& s);
1148 void cacheLocalAddress() const;
1149 void cachePeerAddress() const;
1151 bool isZeroCopyRequest(WriteFlags flags);
1152 uint32_t getNextZeroCopyBuffId() {
1153 return zeroCopyBuffId_++;
1155 void adjustZeroCopyFlags(folly::IOBuf* buf, folly::WriteFlags& flags);
1156 void adjustZeroCopyFlags(
1159 folly::WriteFlags& flags);
1160 void addZeroCopyBuff(std::unique_ptr<folly::IOBuf>&& buf);
1161 void addZeroCopyBuff(folly::IOBuf* ptr);
1162 void setZeroCopyBuff(std::unique_ptr<folly::IOBuf>&& buf);
1163 bool containsZeroCopyBuff(folly::IOBuf* ptr);
1164 void releaseZeroCopyBuff(uint32_t id);
1166 // a folly::IOBuf can be used in multiple partial requests
1167 // so we keep a map that maps a buffer id to a raw folly::IOBuf ptr
1168 // and one more map that adds a ref count for a folly::IOBuf that is either
1169 // the original ptr or nullptr
1170 uint32_t zeroCopyBuffId_{0};
1171 std::unordered_map<uint32_t, folly::IOBuf*> idZeroCopyBufPtrMap_;
1174 std::pair<uint32_t, std::unique_ptr<folly::IOBuf>>>
1175 idZeroCopyBufPtrToBufMap_;
1177 StateEnum state_; ///< StateEnum describing current state
1178 uint8_t shutdownFlags_; ///< Shutdown state (ShutdownFlags)
1179 uint16_t eventFlags_; ///< EventBase::HandlerFlags settings
1180 int fd_; ///< The socket file descriptor
1181 mutable folly::SocketAddress addr_; ///< The address we tried to connect to
1182 mutable folly::SocketAddress localAddr_;
1183 ///< The address we are connecting from
1184 uint32_t sendTimeout_; ///< The send timeout, in milliseconds
1185 uint16_t maxReadsPerEvent_; ///< Max reads per event loop iteration
1186 EventBase* eventBase_; ///< The EventBase
1187 WriteTimeout writeTimeout_; ///< A timeout for connect and write
1188 IoHandler ioHandler_; ///< A EventHandler to monitor the fd
1189 ImmediateReadCB immediateReadHandler_; ///< LoopCallback for checking read
1191 ConnectCallback* connectCallback_; ///< ConnectCallback
1192 ErrMessageCallback* errMessageCallback_; ///< TimestampCallback
1193 SendMsgParamsCallback* ///< Callback for retrieving
1194 sendMsgParamCallback_; ///< ::sendmsg() parameters
1195 ReadCallback* readCallback_; ///< ReadCallback
1196 WriteRequest* writeReqHead_; ///< Chain of WriteRequests
1197 WriteRequest* writeReqTail_; ///< End of WriteRequest chain
1198 std::weak_ptr<ShutdownSocketSet> wShutdownSocketSet_;
1199 size_t appBytesReceived_; ///< Num of bytes received from socket
1200 size_t appBytesWritten_; ///< Num of bytes written to socket
1201 bool isBufferMovable_{false};
1203 // Pre-received data, to be returned to read callback before any data from the
1205 std::unique_ptr<IOBuf> preReceivedData_;
1207 int8_t readErr_{READ_NO_ERROR}; ///< The read error encountered, if any
1209 std::chrono::steady_clock::time_point connectStartTime_;
1210 std::chrono::steady_clock::time_point connectEndTime_;
1212 std::chrono::milliseconds connectTimeout_{0};
1214 BufferCallback* bufferCallback_{nullptr};
1215 bool tfoEnabled_{false};
1216 bool tfoAttempted_{false};
1217 bool tfoFinished_{false};
1218 bool noTransparentTls_{false};
1219 bool noTSocks_{false};
1220 // Whether to track EOR or not.
1221 bool trackEor_{false};
1222 bool zeroCopyEnabled_{false};
1223 bool zeroCopyVal_{false};
1224 size_t zeroCopyWriteChainThreshold_{kDefaultZeroCopyThreshold};
1226 std::unique_ptr<EvbChangeCallback> evbChangeCb_{nullptr};
1229 #pragma vtordisp(pop)
1232 } // namespace folly