proxygen
ReadCallback Class Reference

#include <AsyncSocketTest.h>

Inheritance diagram for ReadCallback:
folly::AsyncReader::ReadCallback

Classes

class  Buffer
 

Public Member Functions

 ReadCallback (size_t _maxBufferSz=4096)
 
 ~ReadCallback () override
 
void getReadBuffer (void **bufReturn, size_t *lenReturn) override
 
void readDataAvailable (size_t len) noexceptoverride
 
void readEOF () noexceptoverride
 
void readErr (const folly::AsyncSocketException &ex) noexceptoverride
 
void verifyData (const char *expected, size_t expectedLen) const
 
size_t dataRead () const
 
- Public Member Functions inherited from folly::AsyncReader::ReadCallback
virtual bool isBufferMovable () noexcept
 
virtual size_t maxBufferSize () const
 
virtual void readBufferAvailable (std::unique_ptr< IOBuf >) noexcept
 

Public Attributes

StateEnum state
 
folly::AsyncSocketException exception
 
std::vector< Bufferbuffers
 
Buffer currentBuffer
 
VoidCallback dataAvailableCallback
 
const size_t maxBufferSz
 

Detailed Description

Definition at line 88 of file AsyncSocketTest.h.

Constructor & Destructor Documentation

ReadCallback::ReadCallback ( size_t  _maxBufferSz = 4096)
inlineexplicit

Definition at line 90 of file AsyncSocketTest.h.

93  buffers(),
94  maxBufferSz(_maxBufferSz) {}
const size_t maxBufferSz
StateEnum state
folly::AsyncSocketException exception
std::vector< Buffer > buffers
ReadCallback::~ReadCallback ( )
inlineoverridevirtual

Reimplemented from folly::AsyncReader::ReadCallback.

Definition at line 96 of file AsyncSocketTest.h.

96  {
97  for (std::vector<Buffer>::iterator it = buffers.begin();
98  it != buffers.end();
99  ++it) {
100  it->free();
101  }
103  }
Buffer currentBuffer
std::vector< Buffer > buffers

Member Function Documentation

size_t ReadCallback::dataRead ( ) const
inline

Definition at line 143 of file AsyncSocketTest.h.

143  {
144  size_t ret = 0;
145  for (const auto& buf : buffers) {
146  ret += buf.length;
147  }
148  return ret;
149  }
std::vector< Buffer > buffers
void ReadCallback::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 105 of file AsyncSocketTest.h.

105  {
106  if (!currentBuffer.buffer) {
108  }
109  *bufReturn = currentBuffer.buffer;
110  *lenReturn = currentBuffer.length;
111  }
Buffer currentBuffer
const size_t maxBufferSz
void allocate(size_t len)
void ReadCallback::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 113 of file AsyncSocketTest.h.

113  {
114  currentBuffer.length = len;
115  buffers.push_back(currentBuffer);
117  if (dataAvailableCallback) {
119  }
120  }
Buffer currentBuffer
VoidCallback dataAvailableCallback
std::vector< Buffer > buffers
void ReadCallback::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 122 of file AsyncSocketTest.h.

References STATE_SUCCEEDED.

122  {
124  }
state
Definition: http_parser.c:272
void ReadCallback::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 126 of file AsyncSocketTest.h.

References ConnCallback::exception, and STATE_FAILED.

126  {
128  exception = ex;
129  }
folly::AsyncSocketException exception
state
Definition: http_parser.c:272
void ReadCallback::verifyData ( const char *  expected,
size_t  expectedLen 
) const
inline

Definition at line 131 of file AsyncSocketTest.h.

References min.

131  {
132  size_t offset = 0;
133  for (size_t idx = 0; idx < buffers.size(); ++idx) {
134  const auto& buf = buffers[idx];
135  size_t cmpLen = std::min(buf.length, expectedLen - offset);
136  CHECK_EQ(memcmp(buf.buffer, expected + offset, cmpLen), 0);
137  CHECK_EQ(cmpLen, buf.length);
138  offset += cmpLen;
139  }
140  CHECK_EQ(offset, expectedLen);
141  }
LogLevel min
Definition: LogLevel.cpp:30
std::vector< Buffer > buffers

Member Data Documentation

std::vector<Buffer> ReadCallback::buffers

Definition at line 176 of file AsyncSocketTest.h.

Buffer ReadCallback::currentBuffer

Definition at line 177 of file AsyncSocketTest.h.

VoidCallback ReadCallback::dataAvailableCallback

Definition at line 178 of file AsyncSocketTest.h.

folly::AsyncSocketException ReadCallback::exception

Definition at line 175 of file AsyncSocketTest.h.

const size_t ReadCallback::maxBufferSz

Definition at line 179 of file AsyncSocketTest.h.

StateEnum ReadCallback::state

Definition at line 174 of file AsyncSocketTest.h.


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