proxygen
folly::AsyncReader::ReadCallback Class Referenceabstract

#include <AsyncTransport.h>

Inheritance diagram for folly::AsyncReader::ReadCallback:
BlockingSocket BogoTestClient BogoTestServer fizz::AsyncFizzBase folly::AlpnServer folly::AttachDetachClient folly::BlockingWriteServer folly::ReadCallbackBase folly::RenegotiatingServer folly::SNIServer folly::SSLClient folly::test::MockReadCallback proxygen::HTTPSession ProxyService::ProxyHandler ReadCallback wangle::AsyncSocketHandler wangle::SocketPeeker

Public Member Functions

virtual ~ReadCallback ()=default
 
virtual void getReadBuffer (void **bufReturn, size_t *lenReturn)=0
 
virtual void readDataAvailable (size_t len) noexcept=0
 
virtual bool isBufferMovable () noexcept
 
virtual size_t maxBufferSize () const
 
virtual void readBufferAvailable (std::unique_ptr< IOBuf >) noexcept
 
virtual void readEOF () noexcept=0
 
virtual void readErr (const AsyncSocketException &ex) noexcept=0
 

Detailed Description

Definition at line 484 of file AsyncTransport.h.

Constructor & Destructor Documentation

virtual folly::AsyncReader::ReadCallback::~ReadCallback ( )
virtualdefault

Reimplemented in folly::ReadCallback, and ReadCallback.

Member Function Documentation

virtual void folly::AsyncReader::ReadCallback::getReadBuffer ( void **  bufReturn,
size_t *  lenReturn 
)
pure virtual

When data becomes available, getReadBuffer() will be invoked to get the buffer into which data should be read.

This method allows the ReadCallback to delay buffer allocation until data becomes available. This allows applications to manage large numbers of idle connections, without having to maintain a separate read buffer for each idle connection.

It is possible that in some cases, getReadBuffer() may be called multiple times before readDataAvailable() is invoked. In this case, the data will be written to the buffer returned from the most recent call to readDataAvailable(). If the previous calls to readDataAvailable() returned different buffers, the ReadCallback is responsible for ensuring that they are not leaked.

If getReadBuffer() throws an exception, returns a nullptr buffer, or returns a 0 length, the ReadCallback will be uninstalled and its readError() method will be invoked.

getReadBuffer() is not allowed to change the transport state before it returns. (For example, it should never uninstall the read callback, or set a different read callback.)

Parameters
bufReturngetReadBuffer() should update *bufReturn to contain the address of the read buffer. This parameter will never be nullptr.
lenReturngetReadBuffer() should update *lenReturn to contain the maximum number of bytes that may be written to the read buffer. This parameter will never be nullptr.

Implemented in folly::SSLClient, folly::SNIServer, folly::RenegotiatingServer, folly::AlpnServer, folly::BlockingWriteServer, folly::ReadEOFCallback, folly::ReadErrorCallback, proxygen::HTTPSession, folly::ReadCallback, folly::NoopReadCallback, fizz::AsyncFizzBase, BogoTestClient, folly::AsyncSSLSocket::DefaultOpenSSLAsyncFinishCallback, folly::AttachDetachClient, wangle::AsyncSocketHandler, ProxyService::ProxyHandler, BlockingSocket, ReadCallback, BogoTestServer, and wangle::SocketPeeker.

Referenced by fizz::AsyncFizzBase::deliverAppData(), TestAsyncTransport::fireOneReadEvent(), folly::AsyncPipeReader::handlerReady(), folly::AsyncSSLSocket::prepareReadBuffer(), and folly::AsyncSocket::prepareReadBuffer().

virtual bool folly::AsyncReader::ReadCallback::isBufferMovable ( )
inlinevirtualnoexcept

When data becomes available, isBufferMovable() will be invoked to figure out which API will be used, readBufferAvailable() or readDataAvailable(). If isBufferMovable() returns true, that means ReadCallback supports the IOBuf ownership transfer and readBufferAvailable() will be used. Otherwise, not.

By default, isBufferMovable() always return false. If readBufferAvailable() is implemented and to be invoked, You should overwrite isBufferMovable() and return true in the inherited class.

This method allows the AsyncSocket/AsyncSSLSocket do buffer allocation by itself until data becomes available. Compared with the pre/post buffer allocation in getReadBuffer()/readDataAvailabe(), readBufferAvailable() has two advantages. First, this can avoid memcpy. E.g., in AsyncSSLSocket, the decrypted data was copied from the openssl internal buffer to the readbuf buffer. With the buffer ownership transfer, the internal buffer can be directly "moved" to ReadCallback. Second, the memory allocation can be more precise. The reason is AsyncSocket/AsyncSSLSocket can allocate the memory of precise size because they have more context about the available data than ReadCallback. Think about the getReadBuffer() pre-allocate 4072 bytes buffer, but the available data is always 16KB (max OpenSSL record size).

Reimplemented in proxygen::HTTPSession, fizz::AsyncFizzBase, BogoTestClient, BogoTestServer, wangle::SocketPeeker, and folly::test::MockReadCallback.

Definition at line 561 of file AsyncTransport.h.

Referenced by fizz::AsyncFizzBase::deliverAppData(), folly::AsyncPipeReader::handlerReady(), fizz::test::LocalTransport::setReadCB(), and folly::AsyncSSLSocket::setReadCB().

561  {
562  return false;
563  }
virtual size_t folly::AsyncReader::ReadCallback::maxBufferSize ( ) const
inlinevirtual

Suggested buffer size, allocated for read operations, if callback is movable and supports folly::IOBuf

Definition at line 570 of file AsyncTransport.h.

Referenced by folly::AsyncPipeReader::handlerReady().

570  {
571  return 64 * 1024; // 64K
572  }
virtual void folly::AsyncReader::ReadCallback::readBufferAvailable ( std::unique_ptr< IOBuf )
inlinevirtualnoexcept

readBufferAvailable() will be invoked when data has been successfully read.

Note that only either readBufferAvailable() or readDataAvailable() will be invoked according to the return value of isBufferMovable(). The timing and aftereffect of readBufferAvailable() are the same as readDataAvailable()

Parameters
readBufThe unique pointer of read buffer.

Reimplemented in proxygen::HTTPSession, fizz::AsyncFizzBase, BogoTestClient, BogoTestServer, and folly::test::MockReadCallback.

Definition at line 586 of file AsyncTransport.h.

Referenced by fizz::AsyncFizzBase::deliverAppData(), fizz::test::LocalTransport::deliverData(), folly::AsyncSocket::handleRead(), and folly::AsyncPipeReader::handlerReady().

587  {}
virtual void folly::AsyncReader::ReadCallback::readDataAvailable ( size_t  len)
pure virtualnoexcept

The documentation for this class was generated from the following file: