proxygen
wangle::SocketPeeker Class Reference

#include <SocketPeeker.h>

Inheritance diagram for wangle::SocketPeeker:
folly::AsyncReader::ReadCallback folly::DelayedDestruction folly::DelayedDestructionBase

Classes

class  Callback
 

Public Types

using UniquePtr = std::unique_ptr< SocketPeeker, folly::DelayedDestruction::Destructor >
 

Public Member Functions

 SocketPeeker (folly::AsyncSocket &socket, Callback *callback, size_t numBytes)
 
 ~SocketPeeker () override
 
void start ()
 
void getReadBuffer (void **bufReturn, size_t *lenReturn) override
 
void readEOF () noexceptoverride
 
void readErr (const folly::AsyncSocketException &ex) noexceptoverride
 
void readDataAvailable (size_t len) noexceptoverride
 
bool isBufferMovable () noexceptoverride
 
- Public Member Functions inherited from folly::AsyncReader::ReadCallback
virtual ~ReadCallback ()=default
 
virtual size_t maxBufferSize () const
 
virtual void readBufferAvailable (std::unique_ptr< IOBuf >) noexcept
 
- Public Member Functions inherited from folly::DelayedDestruction
virtual void destroy ()
 
bool getDestroyPending () const
 
- Public Member Functions inherited from folly::DelayedDestructionBase
virtual ~DelayedDestructionBase ()=default
 

Private Attributes

folly::AsyncSocketsocket_
 
Callbackcallback_
 
size_t read_ {0}
 
std::vector< uint8_tpeekBytes_
 

Additional Inherited Members

- Protected Member Functions inherited from folly::DelayedDestruction
 ~DelayedDestruction () override=default
 
 DelayedDestruction ()
 
- Protected Member Functions inherited from folly::DelayedDestructionBase
 DelayedDestructionBase ()
 
uint32_t getDestructorGuardCount () const
 

Detailed Description

Definition at line 24 of file SocketPeeker.h.

Member Typedef Documentation

Constructor & Destructor Documentation

wangle::SocketPeeker::SocketPeeker ( folly::AsyncSocket socket,
Callback callback,
size_t  numBytes 
)
inline

Definition at line 37 of file SocketPeeker.h.

38  : socket_(socket), callback_(callback), peekBytes_(numBytes) {}
std::vector< uint8_t > peekBytes_
Definition: SocketPeeker.h:108
folly::AsyncSocket & socket_
Definition: SocketPeeker.h:105
wangle::SocketPeeker::~SocketPeeker ( )
inlineoverride

Definition at line 40 of file SocketPeeker.h.

References folly::AsyncSocket::getReadCallback(), folly::AsyncSocket::setReadCB(), and socket_.

40  {
41  if (socket_.getReadCallback() == this) {
42  socket_.setReadCB(nullptr);
43  }
44  }
folly::AsyncSocket & socket_
Definition: SocketPeeker.h:105
void setReadCB(ReadCallback *callback) override
ReadCallback * getReadCallback() const override

Member Function Documentation

void wangle::SocketPeeker::getReadBuffer ( void **  bufReturn,
size_t *  lenReturn 
)
inlineoverridevirtual

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.

Implements folly::AsyncReader::ReadCallback.

Definition at line 57 of file SocketPeeker.h.

References peekBytes_, and read_.

57  {
58  CHECK_LT(read_, peekBytes_.size());
59  *bufReturn = reinterpret_cast<void*>(peekBytes_.data() + read_);
60  *lenReturn = peekBytes_.size() - read_;
61  }
std::vector< uint8_t > peekBytes_
Definition: SocketPeeker.h:108
bool wangle::SocketPeeker::isBufferMovable ( )
inlineoverridevirtualnoexcept

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 from folly::AsyncReader::ReadCallback.

Definition at line 98 of file SocketPeeker.h.

98  {
99  // Returning false so that we can supply the exact length of the
100  // number of bytes we want to read.
101  return false;
102  }
void wangle::SocketPeeker::readDataAvailable ( size_t  len)
inlineoverridevirtualnoexcept

readDataAvailable() will be invoked when data has been successfully read into the buffer returned by the last call to getReadBuffer().

The read callback remains installed after readDataAvailable() returns. It must be explicitly uninstalled to stop receiving read events. getReadBuffer() will be called at least once before each call to readDataAvailable(). getReadBuffer() will also be called before any call to readEOF().

Parameters
lenThe number of bytes placed in the buffer.

Implements folly::AsyncReader::ReadCallback.

Definition at line 82 of file SocketPeeker.h.

References callback_, folly::IOBuf::copyBuffer(), folly::gen::move, peekBytes_, wangle::SocketPeeker::Callback::peekSuccess(), folly::range(), read_, folly::AsyncSocket::setPreReceivedData(), folly::AsyncSocket::setReadCB(), and socket_.

82  {
84 
85  read_ += len;
86  CHECK_LE(read_, peekBytes_.size());
87 
88  if (read_ == peekBytes_.size()) {
91  socket_.setReadCB(nullptr);
92  auto callback = callback_;
93  callback_ = nullptr;
94  callback->peekSuccess(std::move(peekBytes_));
95  }
96  }
std::vector< uint8_t > peekBytes_
Definition: SocketPeeker.h:108
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::AsyncSocket & socket_
Definition: SocketPeeker.h:105
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
virtual void peekSuccess(std::vector< uint8_t > data) noexcept=0
void setReadCB(ReadCallback *callback) override
virtual void setPreReceivedData(std::unique_ptr< IOBuf > data)
Definition: AsyncSocket.h:770
static std::unique_ptr< IOBuf > copyBuffer(const void *buf, std::size_t size, std::size_t headroom=0, std::size_t minTailroom=0)
Definition: IOBuf.h:1587
void wangle::SocketPeeker::readEOF ( )
inlineoverridevirtualnoexcept

readEOF() will be invoked when the transport is closed.

The read callback will be automatically uninstalled immediately before readEOF() is invoked.

Implements folly::AsyncReader::ReadCallback.

Definition at line 63 of file SocketPeeker.h.

References readErr(), and type.

63  {
65 
66  auto type =
67  folly::AsyncSocketException::AsyncSocketExceptionType::END_OF_FILE;
68  readErr(folly::AsyncSocketException(type, "Unexpected EOF"));
69  }
PskType type
void readErr(const folly::AsyncSocketException &ex) noexceptoverride
Definition: SocketPeeker.h:71
void wangle::SocketPeeker::readErr ( const folly::AsyncSocketException ex)
inlineoverridevirtualnoexcept

readError() will be invoked if an error occurs reading from the transport.

The read callback will be automatically uninstalled immediately before readError() is invoked.

Parameters
exAn exception describing the error that occurred.

Implements folly::AsyncReader::ReadCallback.

Definition at line 71 of file SocketPeeker.h.

References callback_, wangle::SocketPeeker::Callback::peekError(), folly::AsyncSocket::setReadCB(), and socket_.

Referenced by readEOF().

71  {
73 
74  socket_.setReadCB(nullptr);
75  if (callback_) {
76  auto callback = callback_;
77  callback_ = nullptr;
78  callback->peekError(ex);
79  }
80  }
folly::AsyncSocket & socket_
Definition: SocketPeeker.h:105
virtual void peekError(const folly::AsyncSocketException &ex) noexcept=0
void setReadCB(ReadCallback *callback) override
void wangle::SocketPeeker::start ( )
inline

Definition at line 46 of file SocketPeeker.h.

References callback_, folly::gen::move, peekBytes_, wangle::SocketPeeker::Callback::peekSuccess(), folly::AsyncSocket::setReadCB(), and socket_.

46  {
47  if (peekBytes_.size() == 0) {
48  // No peeking necessary.
49  auto callback = callback_;
50  callback_ = nullptr;
51  callback->peekSuccess(std::move(peekBytes_));
52  } else {
53  socket_.setReadCB(this);
54  }
55  }
std::vector< uint8_t > peekBytes_
Definition: SocketPeeker.h:108
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::AsyncSocket & socket_
Definition: SocketPeeker.h:105
virtual void peekSuccess(std::vector< uint8_t > data) noexcept=0
void setReadCB(ReadCallback *callback) override

Member Data Documentation

Callback* wangle::SocketPeeker::callback_
private

Definition at line 106 of file SocketPeeker.h.

Referenced by readDataAvailable(), readErr(), and start().

std::vector<uint8_t> wangle::SocketPeeker::peekBytes_
private

Definition at line 108 of file SocketPeeker.h.

Referenced by getReadBuffer(), readDataAvailable(), and start().

size_t wangle::SocketPeeker::read_ {0}
private

Definition at line 107 of file SocketPeeker.h.

Referenced by getReadBuffer(), and readDataAvailable().

folly::AsyncSocket& wangle::SocketPeeker::socket_
private

Definition at line 105 of file SocketPeeker.h.

Referenced by readDataAvailable(), readErr(), start(), and ~SocketPeeker().


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