Sirikata
Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | Friends
Sirikata::Network::ASIOReadBuffer Class Reference

#include <ASIOReadBuffer.hpp>

Collaboration diagram for Sirikata::Network::ASIOReadBuffer:

List of all members.

Public Types

enum  { sBufferLength = 64*1024, sLowWaterMark = sBufferLength }

Public Member Functions

void ioReactorThreadResumeRead (MultiplexedSocketPtr &thus)
 If read was paused by a user that could not proocess a packet This call attempts to deliver the packet to a user again If the user accepts the packet then the system is asked for future packets This function may be called if the read was not paused.
 ~ASIOReadBuffer ()

Private Types

enum  ReceivedResponse { StreamNotPaused, PausedStream }
enum  ReadStatus { PAUSED_FIXED_BUFFER = 0x0, READING_FIXED_BUFFER = 0x1, PAUSED_NEW_CHUNK = 0x2, READING_NEW_CHUNK = 0x3 }
typedef boost::system::error_code ErrorCode

Private Member Functions

void bindFunctions (IOStrand *strand)
void processError (MultiplexedSocket *parentSocket, const ErrorCode &error)
 This forwards the error message to the MultiplexedSocket so the appropriate action may be taken (including,possibly, disconnecting and shutting down the socket connections and all associated streams.
ReceivedResponse processFullChunk (const MultiplexedSocketPtr &parentSocket, unsigned int whichSocket, const Stream::StreamID &sid, Chunk &newChunk, const Stream::PauseReceiveCallback &pauseReceive)
 This function passes the contents of a chunk to the multiplexed socket for callback handling.
void readIntoFixedBuffer (const MultiplexedSocketPtr &parentSocket)
 This function is called when either 0 information is known about the data to be read (such as size, etc) or if the data is known but the packet is sufficiently small that other packets may be conjoined with it in the buffer This function tells asio to read data from the socket into mBuffer at offset mBufferPos upto the end of the buffer.
void readIntoChunk (const MultiplexedSocketPtr &parentSocket)
 This function is called when a sufficiently large chunk needs to be filled up from a previous readIntoFixedBuffer call.
void processPartialChunk (uint8 *dataBuffer, uint32 packetLength, uint32 &bufferReceived, Chunk &retval)
 Examines a buffer of bytes and converts it into a partially or totally filled chunk and gives back information about unprocessed bytes and the StreamID that sent the chunk Updates mNewChunkId with the StreamID that this chunk was sent from.
void translateFixedBuffer (const MultiplexedSocketPtr &thus)
 Examines the class variable mBuffer from the beginning to mBufferPos and translates all packets contained within to chunks and calls the appropriate callback If the information in the last unprocessed chunk is less than sLowWaterMark that excess information is moved to the front of the buffer and readIntoFixedBuffer is called If the information in the last unprocessed chunk is greater than the sLowWaterMark then a new chunk is made specifically for the remaining data using the processPartialChunk function and readIntoChunk is called.
void asioReadIntoChunk (const ErrorCode &error, std::size_t bytes_read)
 The ASIO callback when ASIO was reading into a singleChunk The function reacts to errors by calling processErrors or a missing MultiplexedSocket by deleting this Otherwise the function reacts to a partially full packet by calling readIntoFixedChunk and a full packet by calling processFullChunk to invoke appropriate callbacks then readIntoFixedBuffer.
void asioReadIntoFixedBuffer (const ErrorCode &error, std::size_t bytes_read)
 The ASIO callback when ASIO was reading into the mBuffer from mBufferPos The function reacts to errors by calling processErrors or a missing MultiplexedSocket by deleting this Otherwise the function farms work off to translateBuffer.
 ASIOReadBuffer (const MultiplexedSocketPtr &parentSocket, unsigned int whichSocket, TCPStream::StreamType streamType)
 ASIOReadBuffer (const ASIOReadBuffer &)
 unimplemented: will fail due to bound function

Private Attributes

enum
Sirikata::Network::ASIOReadBuffer::ReadStatus 
mReadStatus
uint8 mBuffer [sBufferLength]
 A fixed length buffer to read incoming requests when the data is unknown in size or so far small in size.
unsigned int mFixedBufferPos
 Where is ASIO writing to in mBuffer.
unsigned int mChunkBufferPos
std::vector< uint8 > mPartialStreamId
 if a header is delivered in a chunk that's too small to contain it, need to hold the bytes temporarily
bool mFirstFrame
 First frame in a series of continuations (has stream id)
bool mLastFrame
 Last frame in a series of continuations (we actually process)
unsigned int mWhichBuffer
 Which actual low level tcp socket from the mParentSocket is used for communication.
Chunk mNewChunk
 A new chunk being read directly into--usually this member is only used to hold a large packet of information, otherwise the fixed length buffer is used.
ChunkmCachedRejectedChunk
Stream::StreamID mNewChunkID
 The StreamID of a new, partially examined new chunk.
uint8 mDataMask [4]
 WebSocket permits packets to be XORed by a repeating 32-bit value. We need to store it after reading a partial packet.
TCPStream::StreamType mStreamType
 Local copy of the stream type to avoid locking.
std::tr1::weak_ptr
< MultiplexedSocket
mParentSocket
 The shared structure responsible for holding state about the associated TCPStream that this class reads and interprets data from.
std::tr1::function< void(const
ErrorCode &, std::size_t)> 
mAsioReadIntoFixedBuffer
std::tr1::function< void(const
ErrorCode &, std::size_t)> 
mAsioReadIntoChunk

Friends

struct ASIOReadBufferUtil
ASIOReadBufferMakeASIOReadBuffer (const MultiplexedSocketPtr &parentSocket, unsigned int whichSocket, const MemoryReference &strayBytesAfterHeader, TCPStream::StreamType streamType)
 The only public interface to TCPReadBuffer is the constructor-like-function which takes in a MultiplexedSocket and an integer offset.

Member Typedef Documentation

typedef boost::system::error_code Sirikata::Network::ASIOReadBuffer::ErrorCode [private]

Member Enumeration Documentation

anonymous enum
Enumerator:
sBufferLength 

The length of the fixed buffer.

This should only affect the largest chunk of data delivered at once, since async_receive should return as soon as data is available. Therefore we use a relatively large buffer to avoid too much overhead from IO operations.

sLowWaterMark 

The low water mark is the point at which reads are shifted from the fixed sized buffer into a preallocated chunk.

Since the chunk is preallocated, this has to be greater than the longest possible header length. It also must be less than sBufferLength. Since this is only really necessary to handle large packets and avoid extra copying (of the available packet info from end of buffer to front of buffer, then over into the real packet), we set this to be as large as possible, sBufferLength.

Enumerator:
PAUSED_FIXED_BUFFER 
READING_FIXED_BUFFER 
PAUSED_NEW_CHUNK 
READING_NEW_CHUNK 
Enumerator:
StreamNotPaused 
PausedStream 

Constructor & Destructor Documentation

Sirikata::Network::ASIOReadBuffer::ASIOReadBuffer ( const MultiplexedSocketPtr parentSocket,
unsigned int  whichSocket,
TCPStream::StreamType  streamType 
) [private]
Sirikata::Network::ASIOReadBuffer::ASIOReadBuffer ( const ASIOReadBuffer ) [private]

unimplemented: will fail due to bound function

Sirikata::Network::ASIOReadBuffer::~ASIOReadBuffer ( )

Member Function Documentation

void Sirikata::Network::ASIOReadBuffer::asioReadIntoChunk ( const ErrorCode error,
std::size_t  bytes_read 
) [private]

The ASIO callback when ASIO was reading into a singleChunk The function reacts to errors by calling processErrors or a missing MultiplexedSocket by deleting this Otherwise the function reacts to a partially full packet by calling readIntoFixedChunk and a full packet by calling processFullChunk to invoke appropriate callbacks then readIntoFixedBuffer.

References Sirikata::Network::ASIOReadBufferUtil::_pause_receive_callback__status_full(), Sirikata::Network::BufferPrint(), mChunkBufferPos, mFixedBufferPos, mNewChunk, mNewChunkID, mParentSocket, mReadStatus, mWhichBuffer, PAUSED_NEW_CHUNK, processError(), processFullChunk(), readIntoChunk(), readIntoFixedBuffer(), StreamNotPaused, and TCPSSTLOG.

Referenced by bindFunctions().

void Sirikata::Network::ASIOReadBuffer::asioReadIntoFixedBuffer ( const ErrorCode error,
std::size_t  bytes_read 
) [private]

The ASIO callback when ASIO was reading into the mBuffer from mBufferPos The function reacts to errors by calling processErrors or a missing MultiplexedSocket by deleting this Otherwise the function farms work off to translateBuffer.

References Sirikata::Network::BufferPrint(), mBuffer, mFixedBufferPos, mParentSocket, processError(), TCPSSTLOG, and translateFixedBuffer().

Referenced by bindFunctions(), and Sirikata::Network::MakeASIOReadBuffer().

void Sirikata::Network::ASIOReadBuffer::bindFunctions ( IOStrand strand) [private]
void Sirikata::Network::ASIOReadBuffer::ioReactorThreadResumeRead ( MultiplexedSocketPtr thus)

If read was paused by a user that could not proocess a packet This call attempts to deliver the packet to a user again If the user accepts the packet then the system is asked for future packets This function may be called if the read was not paused.

References Sirikata::Network::ASIOReadBufferUtil::_pause_receive_callback_noop(), mFixedBufferPos, mNewChunk, mNewChunkID, mReadStatus, mWhichBuffer, PAUSED_FIXED_BUFFER, PAUSED_NEW_CHUNK, processFullChunk(), READING_FIXED_BUFFER, READING_NEW_CHUNK, readIntoFixedBuffer(), StreamNotPaused, and translateFixedBuffer().

void Sirikata::Network::ASIOReadBuffer::processError ( MultiplexedSocket parentSocket,
const ErrorCode error 
) [private]

This forwards the error message to the MultiplexedSocket so the appropriate action may be taken (including,possibly, disconnecting and shutting down the socket connections and all associated streams.

References Sirikata::Network::ASIOSocketWrapper::clearReadBuffer(), Sirikata::Network::MultiplexedSocket::getASIOSocketWrapper(), Sirikata::Network::MultiplexedSocket::hostDisconnectedCallback(), and mWhichBuffer.

Referenced by asioReadIntoChunk(), and asioReadIntoFixedBuffer().

ASIOReadBuffer::ReceivedResponse Sirikata::Network::ASIOReadBuffer::processFullChunk ( const MultiplexedSocketPtr parentSocket,
unsigned int  whichSocket,
const Stream::StreamID sid,
Chunk newChunk,
const Stream::PauseReceiveCallback pauseReceive 
) [private]

This function passes the contents of a chunk to the multiplexed socket for callback handling.

Parameters:
parentSocketis the MultiplexedSocket responsible for this stream with the relevant callback information
whichSocketis the current ASIO socket responsible for having read the data. It must equal mWhichBuffer
sidis the StreamID that sent the data which made it to this socket and got processed. It will help determine which callback to call
newChunkis the chunk that was sent from the other side to this side and is ready for client processing (or server processing if sid==Stream::StreamID())
pauseReceivecallback which pauses receiving packets on the stream
Returns:
ReceivedResponse indicating whether the stream was paused or data was accepted.

References Sirikata::Network::ASIOReadBufferUtil::_mark_pause_bool_true(), mChunkBufferPos, mDataMask, mLastFrame, mNewChunk, PausedStream, and StreamNotPaused.

Referenced by asioReadIntoChunk(), ioReactorThreadResumeRead(), and translateFixedBuffer().

void Sirikata::Network::ASIOReadBuffer::processPartialChunk ( uint8 *  dataBuffer,
uint32  packetLength,
uint32 &  bufferReceived,
Chunk retval 
) [private]

Examines a buffer of bytes and converts it into a partially or totally filled chunk and gives back information about unprocessed bytes and the StreamID that sent the chunk Updates mNewChunkId with the StreamID that this chunk was sent from.

Parameters:
dataBufferis the buffer to be read and turned into an active Chunk
packetLengthis the length of the to-be-returned Chunk plus the length of that chunk's streamID
bufferReceivedis the length of the dataBuffer, and the value returned in the bufferReceived is number of useful bytes copied to the returned chunk
retvalis the newly allocated chunk sized appropriately to hold all data that will ever be copied to it

References Sirikata::Logging::debug, mChunkBufferPos, mDataMask, mFirstFrame, mLastFrame, mNewChunkID, mPartialStreamId, and SILOG.

Referenced by translateFixedBuffer().

void Sirikata::Network::ASIOReadBuffer::readIntoChunk ( const MultiplexedSocketPtr parentSocket) [private]

This function is called when a sufficiently large chunk needs to be filled up from a previous readIntoFixedBuffer call.

This function will tell ASIO to read directly into the mNewChunk class, offset by the mBufferPos upto the value of mNewChunk.size()

References mAsioReadIntoChunk, mChunkBufferPos, mNewChunk, mReadStatus, mWhichBuffer, and READING_NEW_CHUNK.

Referenced by asioReadIntoChunk(), and translateFixedBuffer().

void Sirikata::Network::ASIOReadBuffer::readIntoFixedBuffer ( const MultiplexedSocketPtr parentSocket) [private]

This function is called when either 0 information is known about the data to be read (such as size, etc) or if the data is known but the packet is sufficiently small that other packets may be conjoined with it in the buffer This function tells asio to read data from the socket into mBuffer at offset mBufferPos upto the end of the buffer.

References mAsioReadIntoFixedBuffer, mBuffer, mFixedBufferPos, mReadStatus, mWhichBuffer, READING_FIXED_BUFFER, and sBufferLength.

Referenced by asioReadIntoChunk(), ioReactorThreadResumeRead(), Sirikata::Network::MakeASIOReadBuffer(), and translateFixedBuffer().

void Sirikata::Network::ASIOReadBuffer::translateFixedBuffer ( const MultiplexedSocketPtr thus) [private]

Examines the class variable mBuffer from the beginning to mBufferPos and translates all packets contained within to chunks and calls the appropriate callback If the information in the last unprocessed chunk is less than sLowWaterMark that excess information is moved to the front of the buffer and readIntoFixedBuffer is called If the information in the last unprocessed chunk is greater than the sLowWaterMark then a new chunk is made specifically for the remaining data using the processPartialChunk function and readIntoChunk is called.

References Sirikata::Network::ASIOReadBufferUtil::_pause_receive_callback__status_full(), mBuffer, mChunkBufferPos, mDataMask, mFirstFrame, mFixedBufferPos, mLastFrame, mNewChunk, mNewChunkID, mReadStatus, mStreamType, mWhichBuffer, PAUSED_FIXED_BUFFER, processFullChunk(), processPartialChunk(), Sirikata::VariableLength::read(), readIntoChunk(), readIntoFixedBuffer(), Sirikata::Network::TCPStream::RFC_6455, sLowWaterMark, StreamNotPaused, and Sirikata::VariableLength::unserialize().

Referenced by asioReadIntoFixedBuffer(), and ioReactorThreadResumeRead().


Friends And Related Function Documentation

friend struct ASIOReadBufferUtil [friend]
ASIOReadBuffer* MakeASIOReadBuffer ( const MultiplexedSocketPtr parentSocket,
unsigned int  whichSocket,
const MemoryReference strayBytesAfterHeader,
TCPStream::StreamType  streamType 
) [friend]

The only public interface to TCPReadBuffer is the constructor-like-function which takes in a MultiplexedSocket and an integer offset.

Parameters:
parentSocketthe MultiplexedSocket which defines the whole connection (if the weak_ptr fails, the connection is bunk
whichSocketindicates which substream this read buffer is for, so the appropriate ASIO socket can be retrieved

Member Data Documentation

std::tr1::function<void(const ErrorCode&,std::size_t)> Sirikata::Network::ASIOReadBuffer::mAsioReadIntoChunk [private]

Referenced by bindFunctions(), and readIntoChunk().

std::tr1::function<void(const ErrorCode&,std::size_t)> Sirikata::Network::ASIOReadBuffer::mAsioReadIntoFixedBuffer [private]
uint8 Sirikata::Network::ASIOReadBuffer::mBuffer[sBufferLength] [private]

A fixed length buffer to read incoming requests when the data is unknown in size or so far small in size.

Referenced by asioReadIntoFixedBuffer(), Sirikata::Network::MakeASIOReadBuffer(), readIntoFixedBuffer(), and translateFixedBuffer().

Referenced by ASIOReadBuffer().

WebSocket permits packets to be XORed by a repeating 32-bit value. We need to store it after reading a partial packet.

Referenced by ASIOReadBuffer(), processFullChunk(), processPartialChunk(), and translateFixedBuffer().

First frame in a series of continuations (has stream id)

Referenced by ASIOReadBuffer(), processPartialChunk(), and translateFixedBuffer().

Last frame in a series of continuations (we actually process)

Referenced by ASIOReadBuffer(), processFullChunk(), processPartialChunk(), and translateFixedBuffer().

A new chunk being read directly into--usually this member is only used to hold a large packet of information, otherwise the fixed length buffer is used.

Referenced by asioReadIntoChunk(), ioReactorThreadResumeRead(), processFullChunk(), readIntoChunk(), and translateFixedBuffer().

The StreamID of a new, partially examined new chunk.

Referenced by asioReadIntoChunk(), ioReactorThreadResumeRead(), processPartialChunk(), and translateFixedBuffer().

The shared structure responsible for holding state about the associated TCPStream that this class reads and interprets data from.

Referenced by asioReadIntoChunk(), and asioReadIntoFixedBuffer().

if a header is delivered in a chunk that's too small to contain it, need to hold the bytes temporarily

Referenced by processPartialChunk().

Local copy of the stream type to avoid locking.

Referenced by ASIOReadBuffer(), and translateFixedBuffer().

Which actual low level tcp socket from the mParentSocket is used for communication.

Referenced by ASIOReadBuffer(), asioReadIntoChunk(), ioReactorThreadResumeRead(), processError(), readIntoChunk(), readIntoFixedBuffer(), and translateFixedBuffer().


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