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.
22 #include <folly/io/async/DelayedDestruction.h>
23 #include <folly/io/async/EventBase.h>
24 #include <folly/io/async/AsyncSocketBase.h>
26 #include <openssl/ssl.h>
28 constexpr bool kOpenSslModeMoveBufferOwnership =
29 #ifdef SSL_MODE_MOVE_BUFFER_OWNERSHIP
38 class AsyncSocketException;
44 * flags given by the application for write* calls
46 enum class WriteFlags : uint32_t {
49 * Whether to delay the output until a subsequent non-corked write.
50 * (Note: may not be supported in all subclasses or on all platforms.)
54 * for a socket that has ACK latency enabled, it will cause the kernel
55 * to fire a TCP ESTATS event when the last byte of the given write call
56 * will be acknowledged.
60 * this indicates that only the write side of socket should be shutdown
62 WRITE_SHUTDOWN = 0x04,
68 inline WriteFlags operator|(WriteFlags a, WriteFlags b) {
69 return static_cast<WriteFlags>(
70 static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
74 * intersection operator
76 inline WriteFlags operator&(WriteFlags a, WriteFlags b) {
77 return static_cast<WriteFlags>(
78 static_cast<uint32_t>(a) & static_cast<uint32_t>(b));
84 inline WriteFlags operator~(WriteFlags a) {
85 return static_cast<WriteFlags>(~static_cast<uint32_t>(a));
91 inline WriteFlags unSet(WriteFlags a, WriteFlags b) {
98 inline bool isSet(WriteFlags a, WriteFlags b) {
104 * AsyncTransport defines an asynchronous API for streaming I/O.
106 * This class provides an API to for asynchronously waiting for data
107 * on a streaming transport, and for asynchronously sending data.
109 * The APIs for reading and writing are intentionally asymmetric. Waiting for
110 * data to read is a persistent API: a callback is installed, and is notified
111 * whenever new data is available. It continues to be notified of new events
112 * until it is uninstalled.
114 * AsyncTransport does not provide read timeout functionality, because it
115 * typically cannot determine when the timeout should be active. Generally, a
116 * timeout should only be enabled when processing is blocked waiting on data
117 * from the remote endpoint. For server-side applications, the timeout should
118 * not be active if the server is currently processing one or more outstanding
119 * requests on this transport. For client-side applications, the timeout
120 * should not be active if there are no requests pending on the transport.
121 * Additionally, if a client has multiple pending requests, it will ususally
122 * want a separate timeout for each request, rather than a single read timeout.
124 * The write API is fairly intuitive: a user can request to send a block of
125 * data, and a callback will be informed once the entire block has been
126 * transferred to the kernel, or on error. AsyncTransport does provide a send
127 * timeout, since most callers want to give up if the remote end stops
128 * responding and no further progress can be made sending the data.
130 class AsyncTransport : public DelayedDestruction, public AsyncSocketBase {
132 typedef std::unique_ptr<AsyncTransport, Destructor> UniquePtr;
135 * Close the transport.
137 * This gracefully closes the transport, waiting for all pending write
138 * requests to complete before actually closing the underlying transport.
140 * If a read callback is set, readEOF() will be called immediately. If there
141 * are outstanding write requests, the close will be delayed until all
142 * remaining writes have completed. No new writes may be started after
143 * close() has been called.
145 virtual void close() = 0;
148 * Close the transport immediately.
150 * This closes the transport immediately, dropping any outstanding data
151 * waiting to be written.
153 * If a read callback is set, readEOF() will be called immediately.
154 * If there are outstanding write requests, these requests will be aborted
155 * and writeError() will be invoked immediately on all outstanding write
158 virtual void closeNow() = 0;
161 * Reset the transport immediately.
163 * This closes the transport immediately, sending a reset to the remote peer
164 * if possible to indicate abnormal shutdown.
166 * Note that not all subclasses implement this reset functionality: some
167 * subclasses may treat reset() the same as closeNow(). Subclasses that use
168 * TCP transports should terminate the connection with a TCP reset.
170 virtual void closeWithReset() {
175 * Perform a half-shutdown of the write side of the transport.
177 * The caller should not make any more calls to write() or writev() after
178 * shutdownWrite() is called. Any future write attempts will fail
181 * Not all transport types support half-shutdown. If the underlying
182 * transport does not support half-shutdown, it will fully shutdown both the
183 * read and write sides of the transport. (Fully shutting down the socket is
184 * better than doing nothing at all, since the caller may rely on the
185 * shutdownWrite() call to notify the other end of the connection that no
186 * more data can be read.)
188 * If there is pending data still waiting to be written on the transport,
189 * the actual shutdown will be delayed until the pending data has been
192 * Note: There is no corresponding shutdownRead() equivalent. Simply
193 * uninstall the read callback if you wish to stop reading. (On TCP sockets
194 * at least, shutting down the read side of the socket is a no-op anyway.)
196 virtual void shutdownWrite() = 0;
199 * Perform a half-shutdown of the write side of the transport.
201 * shutdownWriteNow() is identical to shutdownWrite(), except that it
202 * immediately performs the shutdown, rather than waiting for pending writes
203 * to complete. Any pending write requests will be immediately failed when
204 * shutdownWriteNow() is called.
206 virtual void shutdownWriteNow() = 0;
209 * Determine if transport is open and ready to read or write.
211 * Note that this function returns false on EOF; you must also call error()
212 * to distinguish between an EOF and an error.
214 * @return true iff the transport is open and ready, false otherwise.
216 virtual bool good() const = 0;
219 * Determine if the transport is readable or not.
221 * @return true iff the transport is readable, false otherwise.
223 virtual bool readable() const = 0;
226 * Determine if the there is pending data on the transport.
228 * @return true iff the if the there is pending data, false otherwise.
230 virtual bool isPending() const {
235 * Determine if transport is connected to the endpoint
237 * @return false iff the transport is connected, otherwise true
239 virtual bool connecting() const = 0;
242 * Determine if an error has occurred with this transport.
244 * @return true iff an error has occurred (not EOF).
246 virtual bool error() const = 0;
249 * Attach the transport to a EventBase.
251 * This may only be called if the transport is not currently attached to a
252 * EventBase (by an earlier call to detachEventBase()).
254 * This method must be invoked in the EventBase's thread.
256 virtual void attachEventBase(EventBase* eventBase) = 0;
259 * Detach the transport from its EventBase.
261 * This may only be called when the transport is idle and has no reads or
262 * writes pending. Once detached, the transport may not be used again until
263 * it is re-attached to a EventBase by calling attachEventBase().
265 * This method must be called from the current EventBase's thread.
267 virtual void detachEventBase() = 0;
270 * Determine if the transport can be detached.
272 * This method must be called from the current EventBase's thread.
274 virtual bool isDetachable() const = 0;
277 * Set the send timeout.
279 * If write requests do not make any progress for more than the specified
280 * number of milliseconds, fail all pending writes and close the transport.
282 * If write requests are currently pending when setSendTimeout() is called,
283 * the timeout interval is immediately restarted using the new value.
285 * @param milliseconds The timeout duration, in milliseconds. If 0, no
286 * timeout will be used.
288 virtual void setSendTimeout(uint32_t milliseconds) = 0;
291 * Get the send timeout.
293 * @return Returns the current send timeout, in milliseconds. A return value
294 * of 0 indicates that no timeout is set.
296 virtual uint32_t getSendTimeout() const = 0;
299 * Get the address of the local endpoint of this transport.
301 * This function may throw AsyncSocketException on error.
303 * @param address The local address will be stored in the specified
306 virtual void getLocalAddress(SocketAddress* address) const = 0;
308 virtual void getAddress(SocketAddress* address) const {
309 getLocalAddress(address);
313 * Get the address of the remote endpoint to which this transport is
316 * This function may throw AsyncSocketException on error.
318 * @param address The remote endpoint's address will be stored in the
319 * specified SocketAddress.
321 virtual void getPeerAddress(SocketAddress* address) const = 0;
324 * @return True iff end of record tracking is enabled
326 virtual bool isEorTrackingEnabled() const = 0;
328 virtual void setEorTracking(bool track) = 0;
330 virtual size_t getAppBytesWritten() const = 0;
331 virtual size_t getRawBytesWritten() const = 0;
332 virtual size_t getAppBytesReceived() const = 0;
333 virtual size_t getRawBytesReceived() const = 0;
336 virtual ~AsyncTransport() = default;
343 virtual ~ReadCallback() = default;
346 * When data becomes available, getReadBuffer() will be invoked to get the
347 * buffer into which data should be read.
349 * This method allows the ReadCallback to delay buffer allocation until
350 * data becomes available. This allows applications to manage large
351 * numbers of idle connections, without having to maintain a separate read
352 * buffer for each idle connection.
354 * It is possible that in some cases, getReadBuffer() may be called
355 * multiple times before readDataAvailable() is invoked. In this case, the
356 * data will be written to the buffer returned from the most recent call to
357 * readDataAvailable(). If the previous calls to readDataAvailable()
358 * returned different buffers, the ReadCallback is responsible for ensuring
359 * that they are not leaked.
361 * If getReadBuffer() throws an exception, returns a nullptr buffer, or
362 * returns a 0 length, the ReadCallback will be uninstalled and its
363 * readError() method will be invoked.
365 * getReadBuffer() is not allowed to change the transport state before it
366 * returns. (For example, it should never uninstall the read callback, or
367 * set a different read callback.)
369 * @param bufReturn getReadBuffer() should update *bufReturn to contain the
370 * address of the read buffer. This parameter will never
372 * @param lenReturn getReadBuffer() should update *lenReturn to contain the
373 * maximum number of bytes that may be written to the read
374 * buffer. This parameter will never be nullptr.
376 virtual void getReadBuffer(void** bufReturn, size_t* lenReturn) = 0;
379 * readDataAvailable() will be invoked when data has been successfully read
380 * into the buffer returned by the last call to getReadBuffer().
382 * The read callback remains installed after readDataAvailable() returns.
383 * It must be explicitly uninstalled to stop receiving read events.
384 * getReadBuffer() will be called at least once before each call to
385 * readDataAvailable(). getReadBuffer() will also be called before any
388 * @param len The number of bytes placed in the buffer.
391 virtual void readDataAvailable(size_t len) noexcept = 0;
394 * When data becomes available, isBufferMovable() will be invoked to figure
395 * out which API will be used, readBufferAvailable() or
396 * readDataAvailable(). If isBufferMovable() returns true, that means
397 * ReadCallback supports the IOBuf ownership transfer and
398 * readBufferAvailable() will be used. Otherwise, not.
400 * By default, isBufferMovable() always return false. If
401 * readBufferAvailable() is implemented and to be invoked, You should
402 * overwrite isBufferMovable() and return true in the inherited class.
404 * This method allows the AsyncSocket/AsyncSSLSocket do buffer allocation by
405 * itself until data becomes available. Compared with the pre/post buffer
406 * allocation in getReadBuffer()/readDataAvailabe(), readBufferAvailable()
407 * has two advantages. First, this can avoid memcpy. E.g., in
408 * AsyncSSLSocket, the decrypted data was copied from the openssl internal
409 * buffer to the readbuf buffer. With the buffer ownership transfer, the
410 * internal buffer can be directly "moved" to ReadCallback. Second, the
411 * memory allocation can be more precise. The reason is
412 * AsyncSocket/AsyncSSLSocket can allocate the memory of precise size
413 * because they have more context about the available data than
414 * ReadCallback. Think about the getReadBuffer() pre-allocate 4072 bytes
415 * buffer, but the available data is always 16KB (max OpenSSL record size).
418 virtual bool isBufferMovable() noexcept {
423 * readBufferAvailable() will be invoked when data has been successfully
426 * Note that only either readBufferAvailable() or readDataAvailable() will
427 * be invoked according to the return value of isBufferMovable(). The timing
428 * and aftereffect of readBufferAvailable() are the same as
429 * readDataAvailable()
431 * @param readBuf The unique pointer of read buffer.
434 virtual void readBufferAvailable(std::unique_ptr<IOBuf> /*readBuf*/)
438 * readEOF() will be invoked when the transport is closed.
440 * The read callback will be automatically uninstalled immediately before
441 * readEOF() is invoked.
443 virtual void readEOF() noexcept = 0;
446 * readError() will be invoked if an error occurs reading from the
449 * The read callback will be automatically uninstalled immediately before
450 * readError() is invoked.
452 * @param ex An exception describing the error that occurred.
454 virtual void readErr(const AsyncSocketException& ex) noexcept = 0;
457 // Read methods that aren't part of AsyncTransport.
458 virtual void setReadCB(ReadCallback* callback) = 0;
459 virtual ReadCallback* getReadCallback() const = 0;
462 virtual ~AsyncReader() = default;
467 class WriteCallback {
469 virtual ~WriteCallback() = default;
472 * writeSuccess() will be invoked when all of the data has been
473 * successfully written.
475 * Note that this mainly signals that the buffer containing the data to
476 * write is no longer needed and may be freed or re-used. It does not
477 * guarantee that the data has been fully transmitted to the remote
478 * endpoint. For example, on socket-based transports, writeSuccess() only
479 * indicates that the data has been given to the kernel for eventual
482 virtual void writeSuccess() noexcept = 0;
485 * writeError() will be invoked if an error occurs writing the data.
487 * @param bytesWritten The number of bytes that were successfull
488 * @param ex An exception describing the error that occurred.
490 virtual void writeErr(size_t bytesWritten,
491 const AsyncSocketException& ex) noexcept = 0;
494 // Write methods that aren't part of AsyncTransport
495 virtual void write(WriteCallback* callback, const void* buf, size_t bytes,
496 WriteFlags flags = WriteFlags::NONE) = 0;
497 virtual void writev(WriteCallback* callback, const iovec* vec, size_t count,
498 WriteFlags flags = WriteFlags::NONE) = 0;
499 virtual void writeChain(WriteCallback* callback,
500 std::unique_ptr<IOBuf>&& buf,
501 WriteFlags flags = WriteFlags::NONE) = 0;
504 virtual ~AsyncWriter() = default;
507 // Transitional intermediate interface. This is deprecated.
508 // Wrapper around folly::AsyncTransport, that includes read/write callbacks
509 class AsyncTransportWrapper : virtual public AsyncTransport,
510 virtual public AsyncReader,
511 virtual public AsyncWriter {
513 using UniquePtr = std::unique_ptr<AsyncTransportWrapper, Destructor>;
515 // Alias for inherited members from AsyncReader and AsyncWriter
516 // to keep compatibility.
517 using ReadCallback = AsyncReader::ReadCallback;
518 using WriteCallback = AsyncWriter::WriteCallback;
519 virtual void setReadCB(ReadCallback* callback) override = 0;
520 virtual ReadCallback* getReadCallback() const override = 0;
521 virtual void write(WriteCallback* callback, const void* buf, size_t bytes,
522 WriteFlags flags = WriteFlags::NONE) override = 0;
523 virtual void writev(WriteCallback* callback, const iovec* vec, size_t count,
524 WriteFlags flags = WriteFlags::NONE) override = 0;
525 virtual void writeChain(WriteCallback* callback,
526 std::unique_ptr<IOBuf>&& buf,
527 WriteFlags flags = WriteFlags::NONE) override = 0;
529 * The transport wrapper may wrap another transport. This returns the
530 * transport that is wrapped. It returns nullptr if there is no wrapped
533 virtual AsyncTransportWrapper* getWrappedTransport() {