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.
68 // We do a dynamic_cast on this, in
69 // AsyncTransportWrapper::getUnderlyingTransport so be safe and
70 // force displacements for it. See:
71 // https://msdn.microsoft.com/en-us/library/7sf3txa8.aspx
72 #pragma vtordisp(push, 2)
74 class AsyncSocket : virtual public AsyncTransportWrapper {
76 typedef std::unique_ptr<AsyncSocket, Destructor> UniquePtr;
78 class ConnectCallback {
80 virtual ~ConnectCallback() = default;
83 * connectSuccess() will be invoked when the connection has been
84 * successfully established.
86 virtual void connectSuccess() noexcept = 0;
89 * connectErr() will be invoked if the connection attempt fails.
91 * @param ex An exception describing the error that occurred.
93 virtual void connectErr(const AsyncSocketException& ex)
97 class EvbChangeCallback {
99 virtual ~EvbChangeCallback() = default;
101 // Called when the socket has been attached to a new EVB
102 // and is called from within that EVB thread
103 virtual void evbAttached(AsyncSocket* socket) = 0;
105 // Called when the socket is detached from an EVB and
106 // is called from the EVB thread being detached
107 virtual void evbDetached(AsyncSocket* socket) = 0;
110 explicit AsyncSocket();
112 * Create a new unconnected AsyncSocket.
114 * connect() must later be called on this socket to establish a connection.
116 explicit AsyncSocket(EventBase* evb);
118 void setShutdownSocketSet(ShutdownSocketSet* ss);
121 * Create a new AsyncSocket and begin the connection process.
123 * @param evb EventBase that will manage this socket.
124 * @param address The address to connect to.
125 * @param connectTimeout Optional timeout in milliseconds for the connection
128 AsyncSocket(EventBase* evb,
129 const folly::SocketAddress& address,
130 uint32_t connectTimeout = 0);
133 * Create a new AsyncSocket and begin the connection process.
135 * @param evb EventBase that will manage this socket.
136 * @param ip IP address to connect to (dotted-quad).
137 * @param port Destination port in host byte order.
138 * @param connectTimeout Optional timeout in milliseconds for the connection
141 AsyncSocket(EventBase* evb,
142 const std::string& ip,
144 uint32_t connectTimeout = 0);
147 * Create a AsyncSocket from an already connected socket file descriptor.
149 * Note that while AsyncSocket enables TCP_NODELAY for sockets it creates
150 * when connecting, it does not change the socket options when given an
151 * existing file descriptor. If callers want TCP_NODELAY enabled when using
152 * this version of the constructor, they need to explicitly call
153 * setNoDelay(true) after the constructor returns.
155 * @param evb EventBase that will manage this socket.
156 * @param fd File descriptor to take over (should be a connected socket).
158 AsyncSocket(EventBase* evb, int fd);
161 * Helper function to create a shared_ptr<AsyncSocket>.
163 * This passes in the correct destructor object, since AsyncSocket's
164 * destructor is protected and cannot be invoked directly.
166 static std::shared_ptr<AsyncSocket> newSocket(EventBase* evb) {
167 return std::shared_ptr<AsyncSocket>(new AsyncSocket(evb),
172 * Helper function to create a shared_ptr<AsyncSocket>.
174 static std::shared_ptr<AsyncSocket> newSocket(
176 const folly::SocketAddress& address,
177 uint32_t connectTimeout = 0) {
178 return std::shared_ptr<AsyncSocket>(
179 new AsyncSocket(evb, address, connectTimeout),
184 * Helper function to create a shared_ptr<AsyncSocket>.
186 static std::shared_ptr<AsyncSocket> newSocket(
188 const std::string& ip,
190 uint32_t connectTimeout = 0) {
191 return std::shared_ptr<AsyncSocket>(
192 new AsyncSocket(evb, ip, port, connectTimeout),
197 * Helper function to create a shared_ptr<AsyncSocket>.
199 static std::shared_ptr<AsyncSocket> newSocket(EventBase* evb, int fd) {
200 return std::shared_ptr<AsyncSocket>(new AsyncSocket(evb, fd),
205 * Destroy the socket.
207 * AsyncSocket::destroy() must be called to destroy the socket.
208 * The normal destructor is private, and should not be invoked directly.
209 * This prevents callers from deleting a AsyncSocket while it is invoking a
212 virtual void destroy() override;
215 * Get the EventBase used by this socket.
217 EventBase* getEventBase() const override {
222 * Get the file descriptor used by the AsyncSocket.
224 virtual int getFd() const {
229 * Extract the file descriptor from the AsyncSocket.
231 * This will immediately cause any installed callbacks to be invoked with an
232 * error. The AsyncSocket may no longer be used after the file descriptor
233 * has been extracted.
235 * Returns the file descriptor. The caller assumes ownership of the
236 * descriptor, and it will not be closed when the AsyncSocket is destroyed.
238 virtual int detachFd();
241 * Uniquely identifies a handle to a socket option value. Each
242 * combination of level and option name corresponds to one socket
247 bool operator<(const OptionKey& other) const {
248 if (level == other.level) {
249 return optname < other.optname;
251 return level < other.level;
253 int apply(int fd, int val) const {
254 return setsockopt(fd, level, optname, &val, sizeof(val));
260 // Maps from a socket option key to its value
261 typedef std::map<OptionKey, int> OptionMap;
263 static const OptionMap emptyOptionMap;
264 static const folly::SocketAddress& anyAddress();
267 * Initiate a connection.
269 * @param callback The callback to inform when the connection attempt
271 * @param address The address to connect to.
272 * @param timeout A timeout value, in milliseconds. If the connection
273 * does not succeed within this period,
274 * callback->connectError() will be invoked.
276 virtual void connect(
277 ConnectCallback* callback,
278 const folly::SocketAddress& address,
280 const OptionMap& options = emptyOptionMap,
281 const folly::SocketAddress& bindAddr = anyAddress()) noexcept;
284 ConnectCallback* callback,
285 const std::string& ip,
288 const OptionMap& options = emptyOptionMap) noexcept;
291 * If a connect request is in-flight, cancels it and closes the socket
292 * immediately. Otherwise, this is a no-op.
294 * This does not invoke any connection related callbacks. Call this to
295 * prevent any connect callback while cleaning up, etc.
297 void cancelConnect();
300 * Set the send timeout.
302 * If write requests do not make any progress for more than the specified
303 * number of milliseconds, fail all pending writes and close the socket.
305 * If write requests are currently pending when setSendTimeout() is called,
306 * the timeout interval is immediately restarted using the new value.
308 * (See the comments for AsyncSocket for an explanation of why AsyncSocket
309 * provides setSendTimeout() but not setRecvTimeout().)
311 * @param milliseconds The timeout duration, in milliseconds. If 0, no
312 * timeout will be used.
314 void setSendTimeout(uint32_t milliseconds) override;
317 * Get the send timeout.
319 * @return Returns the current send timeout, in milliseconds. A return value
320 * of 0 indicates that no timeout is set.
322 uint32_t getSendTimeout() const override {
327 * Set the maximum number of reads to execute from the underlying
328 * socket each time the EventBase detects that new ingress data is
329 * available. The default is unlimited, but callers can use this method
330 * to limit the amount of data read from the socket per event loop
333 * @param maxReads Maximum number of reads per data-available event;
334 * a value of zero means unlimited.
336 void setMaxReadsPerEvent(uint16_t maxReads) {
337 maxReadsPerEvent_ = maxReads;
341 * Get the maximum number of reads this object will execute from
342 * the underlying socket each time the EventBase detects that new
343 * ingress data is available.
345 * @returns Maximum number of reads per data-available event; a value
346 * of zero means unlimited.
348 uint16_t getMaxReadsPerEvent() const {
349 return maxReadsPerEvent_;
352 // Read and write methods
353 void setReadCB(ReadCallback* callback) override;
354 ReadCallback* getReadCallback() const override;
356 void write(WriteCallback* callback, const void* buf, size_t bytes,
357 WriteFlags flags = WriteFlags::NONE) override;
358 void writev(WriteCallback* callback, const iovec* vec, size_t count,
359 WriteFlags flags = WriteFlags::NONE) override;
360 void writeChain(WriteCallback* callback,
361 std::unique_ptr<folly::IOBuf>&& buf,
362 WriteFlags flags = WriteFlags::NONE) override;
365 virtual void writeRequest(WriteRequest* req);
366 void writeRequestReady() {
370 // Methods inherited from AsyncTransport
371 void close() override;
372 void closeNow() override;
373 void closeWithReset() override;
374 void shutdownWrite() override;
375 void shutdownWriteNow() override;
377 bool readable() const override;
378 bool isPending() const override;
379 virtual bool hangup() const;
380 bool good() const override;
381 bool error() const override;
382 void attachEventBase(EventBase* eventBase) override;
383 void detachEventBase() override;
384 bool isDetachable() const override;
386 void getLocalAddress(
387 folly::SocketAddress* address) const override;
389 folly::SocketAddress* address) const override;
391 bool isEorTrackingEnabled() const override { return false; }
393 void setEorTracking(bool /*track*/) override {}
395 bool connecting() const override {
396 return (state_ == StateEnum::CONNECTING);
399 virtual bool isClosedByPeer() const {
400 return (state_ == StateEnum::CLOSED &&
401 (readErr_ == READ_EOF || readErr_ == READ_ERROR));
404 virtual bool isClosedBySelf() const {
405 return (state_ == StateEnum::CLOSED &&
406 (readErr_ != READ_EOF && readErr_ != READ_ERROR));
409 size_t getAppBytesWritten() const override {
410 return appBytesWritten_;
413 size_t getRawBytesWritten() const override {
414 return getAppBytesWritten();
417 size_t getAppBytesReceived() const override {
418 return appBytesReceived_;
421 size_t getRawBytesReceived() const override {
422 return getAppBytesReceived();
425 std::chrono::nanoseconds getConnectTime() const {
426 return connectEndTime_ - connectStartTime_;
429 std::chrono::milliseconds getConnectTimeout() const {
430 return connectTimeout_;
433 bool getTFOAttempted() const {
434 return tfoAttempted_;
438 * Returns whether or not the attempt to use TFO
439 * finished successfully. This does not necessarily
440 * mean TFO worked, just that trying to use TFO
443 bool getTFOFinished() const {
448 * Returns whether or not TFO attempt succeded on this
450 * For servers this is pretty straightforward API and can
451 * be invoked right after the connection is accepted. This API
452 * will perform one syscall.
453 * This API is a bit tricky to use for clients, since clients
454 * only know this for sure after the SYN-ACK is returned. So it's
455 * appropriate to call this only after the first application
456 * data is read from the socket when the caller knows that
457 * the SYN has been ACKed by the server.
459 bool getTFOSucceded() const;
461 // Methods controlling socket options
464 * Force writes to be transmitted immediately.
466 * This controls the TCP_NODELAY socket option. When enabled, TCP segments
467 * are sent as soon as possible, even if it is not a full frame of data.
468 * When disabled, the data may be buffered briefly to try and wait for a full
471 * By default, TCP_NODELAY is enabled for AsyncSocket objects.
473 * This method will fail if the socket is not currently open.
475 * @return Returns 0 if the TCP_NODELAY flag was successfully updated,
476 * or a non-zero errno value on error.
478 int setNoDelay(bool noDelay);
482 * Set the FD_CLOEXEC flag so that the socket will be closed if the program
483 * later forks and execs.
485 void setCloseOnExec();
488 * Set the Flavor of Congestion Control to be used for this Socket
489 * Please check '/lib/modules/<kernel>/kernel/net/ipv4' for tcp_*.ko
490 * first to make sure the module is available for plugging in
491 * Alternatively you can choose from net.ipv4.tcp_allowed_congestion_control
493 int setCongestionFlavor(const std::string &cname);
496 * Forces ACKs to be sent immediately
498 * @return Returns 0 if the TCP_QUICKACK flag was successfully updated,
499 * or a non-zero errno value on error.
501 int setQuickAck(bool quickack);
504 * Set the send bufsize
506 int setSendBufSize(size_t bufsize);
509 * Set the recv bufsize
511 int setRecvBufSize(size_t bufsize);
514 * Sets a specific tcp personality
515 * Available only on kernels 3.2 and greater
517 #define SO_SET_NAMESPACE 41
518 int setTCPProfile(int profd);
521 * Generic API for reading a socket option.
523 * @param level same as the "level" parameter in getsockopt().
524 * @param optname same as the "optname" parameter in getsockopt().
525 * @param optval pointer to the variable in which the option value should
527 * @param optlen value-result argument, initially containing the size of
528 * the buffer pointed to by optval, and modified on return
529 * to indicate the actual size of the value returned.
530 * @return same as the return value of getsockopt().
532 template <typename T>
533 int getSockOpt(int level, int optname, T* optval, socklen_t* optlen) {
534 return getsockopt(fd_, level, optname, (void*) optval, optlen);
538 * Generic API for setting a socket option.
540 * @param level same as the "level" parameter in getsockopt().
541 * @param optname same as the "optname" parameter in getsockopt().
542 * @param optval the option value to set.
543 * @return same as the return value of setsockopt().
545 template <typename T>
546 int setSockOpt(int level, int optname, const T *optval) {
547 return setsockopt(fd_, level, optname, optval, sizeof(T));
550 virtual void setPeek(bool peek) {
555 * Enables TFO behavior on the AsyncSocket if FOLLY_ALLOW_TFO
559 // No-op if folly does not allow tfo
565 enum class StateEnum : uint8_t {
574 void setBufferCallback(BufferCallback* cb);
576 // Callers should set this prior to connecting the socket for the safest
578 void setEvbChangedCallback(std::unique_ptr<EvbChangeCallback> cb) {
579 evbChangeCb_ = std::move(cb);
583 * writeReturn is the total number of bytes written, or WRITE_ERROR on error.
584 * If no data has been written, 0 is returned.
585 * exception is a more specific exception that cause a write error.
586 * Not all writes have exceptions associated with them thus writeReturn
587 * should be checked to determine whether the operation resulted in an error.
590 explicit WriteResult(ssize_t ret) : writeReturn(ret) {}
592 WriteResult(ssize_t ret, std::unique_ptr<const AsyncSocketException> e)
593 : writeReturn(ret), exception(std::move(e)) {}
596 std::unique_ptr<const AsyncSocketException> exception;
600 * readReturn is the number of bytes read, or READ_EOF on EOF, or
601 * READ_ERROR on error, or READ_BLOCKING if the operation will
603 * exception is a more specific exception that may have caused a read error.
604 * Not all read errors have exceptions associated with them thus readReturn
605 * should be checked to determine whether the operation resulted in an error.
608 explicit ReadResult(ssize_t ret) : readReturn(ret) {}
610 ReadResult(ssize_t ret, std::unique_ptr<const AsyncSocketException> e)
611 : readReturn(ret), exception(std::move(e)) {}
614 std::unique_ptr<const AsyncSocketException> exception;
618 * A WriteRequest object tracks information about a pending write operation.
622 WriteRequest(AsyncSocket* socket, WriteCallback* callback) :
623 socket_(socket), callback_(callback) {}
625 virtual void start() {}
627 virtual void destroy() = 0;
629 virtual WriteResult performWrite() = 0;
631 virtual void consume() = 0;
633 virtual bool isComplete() = 0;
635 WriteRequest* getNext() const {
639 WriteCallback* getCallback() const {
643 uint32_t getTotalBytesWritten() const {
644 return totalBytesWritten_;
647 void append(WriteRequest* next) {
648 assert(next_ == nullptr);
652 void fail(const char* fn, const AsyncSocketException& ex) {
653 socket_->failWrite(fn, ex);
656 void bytesWritten(size_t count) {
657 totalBytesWritten_ += uint32_t(count);
658 socket_->appBytesWritten_ += count;
662 // protected destructor, to ensure callers use destroy()
663 virtual ~WriteRequest() {}
665 AsyncSocket* socket_; ///< parent socket
666 WriteRequest* next_{nullptr}; ///< pointer to next WriteRequest
667 WriteCallback* callback_; ///< completion callback
668 uint32_t totalBytesWritten_{0}; ///< total bytes written
672 enum ReadResultEnum {
679 enum WriteResultEnum {
684 * Protected destructor.
686 * Users of AsyncSocket must never delete it directly. Instead, invoke
687 * destroy() instead. (See the documentation in DelayedDestruction.h for
692 friend std::ostream& operator << (std::ostream& os, const StateEnum& state);
695 /// shutdownWrite() called, but we are still waiting on writes to drain
696 SHUT_WRITE_PENDING = 0x01,
697 /// writes have been completely shut down
700 * Reads have been shutdown.
702 * At the moment we don't distinguish between remote read shutdown
703 * (received EOF from the remote end) and local read shutdown. We can
704 * only receive EOF when a read callback is set, and we immediately inform
705 * it of the EOF. Therefore there doesn't seem to be any reason to have a
706 * separate state of "received EOF but the local side may still want to
709 * We also don't currently provide any API for only shutting down the read
710 * side of a socket. (This is a no-op as far as TCP is concerned, anyway.)
715 class BytesWriteRequest;
717 class WriteTimeout : public AsyncTimeout {
719 WriteTimeout(AsyncSocket* socket, EventBase* eventBase)
720 : AsyncTimeout(eventBase)
723 virtual void timeoutExpired() noexcept {
724 socket_->timeoutExpired();
728 AsyncSocket* socket_;
731 class IoHandler : public EventHandler {
733 IoHandler(AsyncSocket* socket, EventBase* eventBase)
734 : EventHandler(eventBase, -1)
736 IoHandler(AsyncSocket* socket, EventBase* eventBase, int fd)
737 : EventHandler(eventBase, fd)
740 virtual void handlerReady(uint16_t events) noexcept {
741 socket_->ioReady(events);
745 AsyncSocket* socket_;
750 class ImmediateReadCB : public folly::EventBase::LoopCallback {
752 explicit ImmediateReadCB(AsyncSocket* socket) : socket_(socket) {}
753 void runLoopCallback() noexcept override {
754 DestructorGuard dg(socket_);
755 socket_->checkForImmediateRead();
758 AsyncSocket* socket_;
762 * Schedule checkForImmediateRead to be executed in the next loop
765 void scheduleImmediateRead() noexcept {
767 eventBase_->runInLoop(&immediateReadHandler_);
772 * Schedule handleInitalReadWrite to run in the next iteration.
774 void scheduleInitialReadWrite() noexcept {
776 DestructorGuard dg(this);
777 eventBase_->runInLoop([this, dg] {
779 handleInitialReadWrite();
785 // event notification methods
786 void ioReady(uint16_t events) noexcept;
787 virtual void checkForImmediateRead() noexcept;
788 virtual void handleInitialReadWrite() noexcept;
789 virtual void prepareReadBuffer(void** buf, size_t* buflen);
790 virtual void handleRead() noexcept;
791 virtual void handleWrite() noexcept;
792 virtual void handleConnect() noexcept;
793 void timeoutExpired() noexcept;
796 * Attempt to read from the socket.
798 * @param buf The buffer to read data into.
799 * @param buflen The length of the buffer.
801 * @return Returns a read result. See read result for details.
803 virtual ReadResult performRead(void** buf, size_t* buflen, size_t* offset);
806 * Populate an iovec array from an IOBuf and attempt to write it.
808 * @param callback Write completion/error callback.
809 * @param vec Target iovec array; caller retains ownership.
810 * @param count Number of IOBufs to write, beginning at start of buf.
811 * @param buf Chain of iovecs.
812 * @param flags set of flags for the underlying write calls, like cork
814 void writeChainImpl(WriteCallback* callback, iovec* vec,
815 size_t count, std::unique_ptr<folly::IOBuf>&& buf,
819 * Write as much data as possible to the socket without blocking,
820 * and queue up any leftover data to send when the socket can
821 * handle writes again.
823 * @param callback The callback to invoke when the write is completed.
824 * @param vec Array of buffers to write; this method will make a
825 * copy of the vector (but not the buffers themselves)
826 * if the write has to be completed asynchronously.
827 * @param count Number of elements in vec.
828 * @param buf The IOBuf that manages the buffers referenced by
829 * vec, or a pointer to nullptr if the buffers are not
830 * associated with an IOBuf. Note that ownership of
831 * the IOBuf is transferred here; upon completion of
832 * the write, the AsyncSocket deletes the IOBuf.
833 * @param flags Set of write flags.
835 void writeImpl(WriteCallback* callback, const iovec* vec, size_t count,
836 std::unique_ptr<folly::IOBuf>&& buf,
837 WriteFlags flags = WriteFlags::NONE);
840 * Attempt to write to the socket.
842 * @param vec The iovec array pointing to the buffers to write.
843 * @param count The length of the iovec array.
844 * @param flags Set of write flags.
845 * @param countWritten On return, the value pointed to by this parameter
846 * will contain the number of iovec entries that were
848 * @param partialWritten On return, the value pointed to by this parameter
849 * will contain the number of bytes written in the
850 * partially written iovec entry.
852 * @return Returns a WriteResult. See WriteResult for more details.
854 virtual WriteResult performWrite(
858 uint32_t* countWritten,
859 uint32_t* partialWritten);
862 * Sends the message over the socket using sendmsg
864 * @param msg Message to send
865 * @param msg_flags Flags to pass to sendmsg
867 AsyncSocket::WriteResult
868 sendSocketMessage(int fd, struct msghdr* msg, int msg_flags);
870 virtual ssize_t tfoSendMsg(int fd, struct msghdr* msg, int msg_flags);
872 int socketConnect(const struct sockaddr* addr, socklen_t len);
874 virtual void scheduleConnectTimeout();
875 void registerForConnectEvents();
877 bool updateEventRegistration();
880 * Update event registration.
882 * @param enable Flags of events to enable. Set it to 0 if no events
883 * need to be enabled in this call.
884 * @param disable Flags of events
885 * to disable. Set it to 0 if no events need to be disabled in this
888 * @return true iff the update is successful.
890 bool updateEventRegistration(uint16_t enable, uint16_t disable);
892 // Actually close the file descriptor and set it to -1 so we don't
893 // accidentally close it again.
896 // error handling methods
899 void finishFail(const AsyncSocketException& ex);
900 void invokeAllErrors(const AsyncSocketException& ex);
901 void fail(const char* fn, const AsyncSocketException& ex);
902 void failConnect(const char* fn, const AsyncSocketException& ex);
903 void failRead(const char* fn, const AsyncSocketException& ex);
904 void failWrite(const char* fn, WriteCallback* callback, size_t bytesWritten,
905 const AsyncSocketException& ex);
906 void failWrite(const char* fn, const AsyncSocketException& ex);
907 void failAllWrites(const AsyncSocketException& ex);
908 virtual void invokeConnectErr(const AsyncSocketException& ex);
909 virtual void invokeConnectSuccess();
910 void invalidState(ConnectCallback* callback);
911 void invalidState(ReadCallback* callback);
912 void invalidState(WriteCallback* callback);
914 std::string withAddr(const std::string& s);
916 StateEnum state_; ///< StateEnum describing current state
917 uint8_t shutdownFlags_; ///< Shutdown state (ShutdownFlags)
918 uint16_t eventFlags_; ///< EventBase::HandlerFlags settings
919 int fd_; ///< The socket file descriptor
920 mutable folly::SocketAddress addr_; ///< The address we tried to connect to
921 mutable folly::SocketAddress localAddr_;
922 ///< The address we are connecting from
923 uint32_t sendTimeout_; ///< The send timeout, in milliseconds
924 uint16_t maxReadsPerEvent_; ///< Max reads per event loop iteration
925 EventBase* eventBase_; ///< The EventBase
926 WriteTimeout writeTimeout_; ///< A timeout for connect and write
927 IoHandler ioHandler_; ///< A EventHandler to monitor the fd
928 ImmediateReadCB immediateReadHandler_; ///< LoopCallback for checking read
930 ConnectCallback* connectCallback_; ///< ConnectCallback
931 ReadCallback* readCallback_; ///< ReadCallback
932 WriteRequest* writeReqHead_; ///< Chain of WriteRequests
933 WriteRequest* writeReqTail_; ///< End of WriteRequest chain
934 ShutdownSocketSet* shutdownSocketSet_;
935 size_t appBytesReceived_; ///< Num of bytes received from socket
936 size_t appBytesWritten_; ///< Num of bytes written to socket
937 bool isBufferMovable_{false};
939 bool peek_{false}; // Peek bytes.
941 int8_t readErr_{READ_NO_ERROR}; ///< The read error encountered, if any.
943 std::chrono::steady_clock::time_point connectStartTime_;
944 std::chrono::steady_clock::time_point connectEndTime_;
946 std::chrono::milliseconds connectTimeout_{0};
948 BufferCallback* bufferCallback_{nullptr};
949 bool tfoEnabled_{false};
950 bool tfoAttempted_{false};
951 bool tfoFinished_{false};
953 std::unique_ptr<EvbChangeCallback> evbChangeCb_{nullptr};
956 #pragma vtordisp(pop)