2 * Copyright 2014 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.
22 * flags given by the application for write* calls
24 enum class WriteFlags : uint32_t {
27 * Whether to delay the output until a subsequent non-corked write.
28 * (Note: may not be supported in all subclasses or on all platforms.)
32 * for a socket that has ACK latency enabled, it will cause the kernel
33 * to fire a TCP ESTATS event when the last byte of the given write call
34 * will be acknowledged.
42 inline WriteFlags operator|(WriteFlags a, WriteFlags b) {
43 return static_cast<WriteFlags>(
44 static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
48 * intersection operator
50 inline WriteFlags operator&(WriteFlags a, WriteFlags b) {
51 return static_cast<WriteFlags>(
52 static_cast<uint32_t>(a) & static_cast<uint32_t>(b));
58 inline WriteFlags operator~(WriteFlags a) {
59 return static_cast<WriteFlags>(~static_cast<uint32_t>(a));
65 inline WriteFlags unSet(WriteFlags a, WriteFlags b) {
72 inline bool isSet(WriteFlags a, WriteFlags b) {
78 * AsyncTransport defines an asynchronous API for streaming I/O.
80 * This class provides an API to for asynchronously waiting for data
81 * on a streaming transport, and for asynchronously sending data.
83 * The APIs for reading and writing are intentionally asymmetric. Waiting for
84 * data to read is a persistent API: a callback is installed, and is notified
85 * whenever new data is available. It continues to be notified of new events
86 * until it is uninstalled.
88 * AsyncTransport does not provide read timeout functionality, because it
89 * typically cannot determine when the timeout should be active. Generally, a
90 * timeout should only be enabled when processing is blocked waiting on data
91 * from the remote endpoint. For server-side applications, the timeout should
92 * not be active if the server is currently processing one or more outstanding
93 * requests on this transport. For client-side applications, the timeout
94 * should not be active if there are no requests pending on the transport.
95 * Additionally, if a client has multiple pending requests, it will ususally
96 * want a separate timeout for each request, rather than a single read timeout.
98 * The write API is fairly intuitive: a user can request to send a block of
99 * data, and a callback will be informed once the entire block has been
100 * transferred to the kernel, or on error. AsyncTransport does provide a send
101 * timeout, since most callers want to give up if the remote end stops
102 * responding and no further progress can be made sending the data.
104 class AsyncTransport : public DelayedDestruction {
106 typedef std::unique_ptr<AsyncTransport, Destructor> UniquePtr;
109 * Close the transport.
111 * This gracefully closes the transport, waiting for all pending write
112 * requests to complete before actually closing the underlying transport.
114 * If a read callback is set, readEOF() will be called immediately. If there
115 * are outstanding write requests, the close will be delayed until all
116 * remaining writes have completed. No new writes may be started after
117 * close() has been called.
119 virtual void close() = 0;
122 * Close the transport immediately.
124 * This closes the transport immediately, dropping any outstanding data
125 * waiting to be written.
127 * If a read callback is set, readEOF() will be called immediately.
128 * If there are outstanding write requests, these requests will be aborted
129 * and writeError() will be invoked immediately on all outstanding write
132 virtual void closeNow() = 0;
135 * Reset the transport immediately.
137 * This closes the transport immediately, sending a reset to the remote peer
138 * if possible to indicate abnormal shutdown.
140 * Note that not all subclasses implement this reset functionality: some
141 * subclasses may treat reset() the same as closeNow(). Subclasses that use
142 * TCP transports should terminate the connection with a TCP reset.
144 virtual void closeWithReset() {
149 * Perform a half-shutdown of the write side of the transport.
151 * The caller should not make any more calls to write() or writev() after
152 * shutdownWrite() is called. Any future write attempts will fail
155 * Not all transport types support half-shutdown. If the underlying
156 * transport does not support half-shutdown, it will fully shutdown both the
157 * read and write sides of the transport. (Fully shutting down the socket is
158 * better than doing nothing at all, since the caller may rely on the
159 * shutdownWrite() call to notify the other end of the connection that no
160 * more data can be read.)
162 * If there is pending data still waiting to be written on the transport,
163 * the actual shutdown will be delayed until the pending data has been
166 * Note: There is no corresponding shutdownRead() equivalent. Simply
167 * uninstall the read callback if you wish to stop reading. (On TCP sockets
168 * at least, shutting down the read side of the socket is a no-op anyway.)
170 virtual void shutdownWrite() = 0;
173 * Perform a half-shutdown of the write side of the transport.
175 * shutdownWriteNow() is identical to shutdownWrite(), except that it
176 * immediately performs the shutdown, rather than waiting for pending writes
177 * to complete. Any pending write requests will be immediately failed when
178 * shutdownWriteNow() is called.
180 virtual void shutdownWriteNow() = 0;
183 * Determine if transport is open and ready to read or write.
185 * Note that this function returns false on EOF; you must also call error()
186 * to distinguish between an EOF and an error.
188 * @return true iff the transport is open and ready, false otherwise.
190 virtual bool good() const = 0;
193 * Determine if the transport is readable or not.
195 * @return true iff the transport is readable, false otherwise.
197 virtual bool readable() const = 0;
200 * Determine if the there is pending data on the transport.
202 * @return true iff the if the there is pending data, false otherwise.
204 virtual bool isPending() const {
208 * Determine if transport is connected to the endpoint
210 * @return false iff the transport is connected, otherwise true
212 virtual bool connecting() const = 0;
215 * Determine if an error has occurred with this transport.
217 * @return true iff an error has occurred (not EOF).
219 virtual bool error() const = 0;
222 * Attach the transport to a EventBase.
224 * This may only be called if the transport is not currently attached to a
225 * EventBase (by an earlier call to detachEventBase()).
227 * This method must be invoked in the EventBase's thread.
229 virtual void attachEventBase(EventBase* eventBase) = 0;
232 * Detach the transport from its EventBase.
234 * This may only be called when the transport is idle and has no reads or
235 * writes pending. Once detached, the transport may not be used again until
236 * it is re-attached to a EventBase by calling attachEventBase().
238 * This method must be called from the current EventBase's thread.
240 virtual void detachEventBase() = 0;
243 * Determine if the transport can be detached.
245 * This method must be called from the current EventBase's thread.
247 virtual bool isDetachable() const = 0;
250 * Get the EventBase used by this transport.
252 * Returns nullptr if this transport is not currently attached to a
255 virtual EventBase* getEventBase() const = 0;
258 * Set the send timeout.
260 * If write requests do not make any progress for more than the specified
261 * number of milliseconds, fail all pending writes and close the transport.
263 * If write requests are currently pending when setSendTimeout() is called,
264 * the timeout interval is immediately restarted using the new value.
266 * @param milliseconds The timeout duration, in milliseconds. If 0, no
267 * timeout will be used.
269 virtual void setSendTimeout(uint32_t milliseconds) = 0;
272 * Get the send timeout.
274 * @return Returns the current send timeout, in milliseconds. A return value
275 * of 0 indicates that no timeout is set.
277 virtual uint32_t getSendTimeout() const = 0;
280 * Get the address of the local endpoint of this transport.
282 * This function may throw AsyncSocketException on error.
284 * @param address The local address will be stored in the specified
287 virtual void getLocalAddress(folly::SocketAddress* address) const = 0;
290 * Get the address of the remote endpoint to which this transport is
293 * This function may throw AsyncSocketException on error.
295 * @param address The remote endpoint's address will be stored in the
296 * specified SocketAddress.
298 virtual void getPeerAddress(folly::SocketAddress* address) const = 0;
301 * @return True iff end of record tracking is enabled
303 virtual bool isEorTrackingEnabled() const = 0;
305 virtual void setEorTracking(bool track) = 0;
307 virtual size_t getAppBytesWritten() const = 0;
308 virtual size_t getRawBytesWritten() const = 0;
309 virtual size_t getAppBytesReceived() const = 0;
310 virtual size_t getRawBytesReceived() const = 0;
313 virtual ~AsyncTransport() {}