Sirikata
Classes | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes
Sirikata::Network::ASIOSocketWrapper Class Reference

#include <ASIOSocketWrapper.hpp>

Collaboration diagram for Sirikata::Network::ASIOSocketWrapper:

List of all members.

Classes

class  CheckCRLF
struct  TimestampedChunk

Public Member Functions

 ASIOSocketWrapper (TCPSocket *socket, uint32 queuedBufferSize, uint32 sendBufferSize, const MultiplexedSocketPtr &parent)
 ASIOSocketWrapper (const ASIOSocketWrapper &socket)
 ASIOSocketWrapper (uint32 queuedBufferSize, uint32 sendBufferSize, const MultiplexedSocketPtr &parent)
void bindFunctions (const MultiplexedSocketPtr &parent)
 ~ASIOSocketWrapper ()
ASIOSocketWrapperoperator= (const ASIOSocketWrapper &socket)
const SizedResourceMonitorgetResourceMonitor () const
void clearReadBuffer ()
ASIOReadBuffergetReadBuffer () const
ASIOReadBuffersetReadBuffer (ASIOReadBuffer *arb)
TCPSocketgetSocket ()
const TCPSocketgetSocket () const
void shutdownAndClose ()
 close this socket by disallowing sends, then closing
void createSocket (unsigned int kernelSendBufferSize, unsigned int kernelReceiveBufferSize)
 Creates a lowlevel TCPSocket using the following io service.
void destroySocket ()
 Destroys the lowlevel TCPSocket.
bool rawSend (const MultiplexedSocketPtr &parentMultiSocket, Chunk *chunk, bool force)
 Sends the exact bytes contained within the typedeffed vector.
bool canSend (size_t dataSize) const
void sendControlPacket (const MultiplexedSocketPtr &parentMultiSocket, TCPStream::TCPStreamControlCodes code, const Stream::StreamID &sid)
 Sends a streamID #0 packet with further control data on it.
void sendProtocolHeader (const MultiplexedSocketPtr &parentMultiSocket, const Address &address, const UUID &value, unsigned int numConnections)
 Sends 24 byte header that indicates version of SST, a unique ID and how many TCP connections should be established.
void sendServerProtocolHeader (const MultiplexedSocketPtr &thus, const std::string &origin, const std::string &host, const std::string &port, const std::string &resource_name, const std::string &subprotocol, bool doSendSubProtocol, const std::string &response)
void ioReactorThreadPauseStream (const MultiplexedSocketPtr &parentMultiSocket, Stream::StreamID sid)
void unpauseSendStreams (const MultiplexedSocketPtr &parentMultiSocket)
Address getRemoteEndpoint () const
Address getLocalEndpoint () const
Duration averageSendLatency () const
Duration averageReceiveLatency () const

Static Public Member Functions

static ChunkconstructControlPacket (const MultiplexedSocketPtr &parentMultiSocket, TCPStream::TCPStreamControlCodes code, const Stream::StreamID &sid)
static ChunkconstructPing (const MultiplexedSocketPtr &parentMultiSocket, MemoryReference data, bool isPong)
 Sends a WebSocket ping/pong with the passed data.
static ChunktoBase64ZeroDelim (const MemoryReference &a, const MemoryReference &b, const MemoryReference &c, const MemoryReference *bytesToPrependUnencoded=NULL)
static UUID massageUUID (const UUID &)
 makes sure the UUID only consists of unicode-allowed characters and has no null values inside

Private Types

enum  { ASYNCHRONOUS_SEND_FLAG = (1<<29), QUEUE_CHECK_FLAG = (1<<30) }
typedef boost::system::error_code ErrorCode

Private Member Functions

void finishedSendingChunk (const TimestampedChunk &tc)
 Call this any time a chunk finishes being sent so statistics can be collected.
void finishAsyncSend (const MultiplexedSocketPtr &parentMultiSocket)
 This function sets the QUEUE_CHECK_FLAG and checks the sendQueue for additional packets to send out.
void sendManyDequeItems (const std::tr1::weak_ptr< MultiplexedSocket > &parentMultiSocket, const ErrorCode &error, std::size_t bytes_sent)
 The callback for when a single large Chunk at the front of a chunk deque was sent.
void sendToWire (const MultiplexedSocketPtr &parentMultiSocket, TimestampedChunk toSend)
 When there's a single packet to be sent to the network, mSocket->async_send is simply called upon the Chunk to be sent.
void sendToWire (const MultiplexedSocketPtr &parentMultiSocket, std::deque< TimestampedChunk > &const_toSend)
 This function sends a while queue of packets to the network The function sends each item using a vector of asio::buffers made from the passed in deque.
void retryQueuedSend (const MultiplexedSocketPtr &parentMultiSocket, uint32 current_status)
 If another thread claimed to be sending data asynchronously This function checks to see if the send is still proceeding after the queue push This thread waits until all queue checks are done, then it sees if someone took up the torch of sending the data If no data is being sent, this thread takes up the torch and sets the queue check flag and sees if data is on the queue If no data is on the queue at this point, since it pushed its own data to the queue it assumes someone else successfully sent it off If data is on the queue, it goes ahead and sends the data to the wire by using the appropriate overload of the sendToWire function it then unsets the QUEUE_CHECK_FLAG and if data isn't being set also the ASYNCHRONOUS_SEND_FLAG.

Private Attributes

IOStrandmStrand
TCPSocketmSocket
ASIOReadBuffermReadBuffer
AtomicValue< uint32 > mSendingStatus
 The status of sending threads: odd number indicates asynchronous send...
SizedThreadSafeQueue
< TimestampedChunk
mSendQueue
 The queue of packets to send while an active async_send is doing its job.
EWA< DurationmAverageSendLatency
std::vector< Stream::StreamIDmPausedSendStreams
std::deque< TimestampedChunkmToSend
std::tr1::weak_ptr
< MultiplexedSocket
mParent
std::tr1::shared_ptr
< MultiplexedSocket
mOutstandingDataParent
std::tr1::function< void(const
ErrorCode &error, std::size_t
bytes_sent)> 
mSendManyDequeItems

Member Typedef Documentation

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

Member Enumeration Documentation

anonymous enum [private]
Enumerator:
ASYNCHRONOUS_SEND_FLAG 
QUEUE_CHECK_FLAG 

Constructor & Destructor Documentation

Sirikata::Network::ASIOSocketWrapper::ASIOSocketWrapper ( TCPSocket socket,
uint32  queuedBufferSize,
uint32  sendBufferSize,
const MultiplexedSocketPtr parent 
) [inline]

References bindFunctions().

Sirikata::Network::ASIOSocketWrapper::ASIOSocketWrapper ( const ASIOSocketWrapper socket) [inline]

References bindFunctions(), and mParent.

Sirikata::Network::ASIOSocketWrapper::ASIOSocketWrapper ( uint32  queuedBufferSize,
uint32  sendBufferSize,
const MultiplexedSocketPtr parent 
) [inline]

References bindFunctions().

Sirikata::Network::ASIOSocketWrapper::~ASIOSocketWrapper ( ) [inline]

Member Function Documentation

Duration Sirikata::Network::ASIOSocketWrapper::averageReceiveLatency ( ) const
Duration Sirikata::Network::ASIOSocketWrapper::averageSendLatency ( ) const
void Sirikata::Network::ASIOSocketWrapper::bindFunctions ( const MultiplexedSocketPtr parent)
bool Sirikata::Network::ASIOSocketWrapper::canSend ( size_t  dataSize) const
void Sirikata::Network::ASIOSocketWrapper::clearReadBuffer ( ) [inline]
Chunk * Sirikata::Network::ASIOSocketWrapper::constructControlPacket ( const MultiplexedSocketPtr parentMultiSocket,
TCPStream::TCPStreamControlCodes  code,
const Stream::StreamID sid 
) [static]
Chunk * Sirikata::Network::ASIOSocketWrapper::constructPing ( const MultiplexedSocketPtr parentMultiSocket,
MemoryReference  data,
bool  isPong 
) [static]
void Sirikata::Network::ASIOSocketWrapper::createSocket ( unsigned int  kernelSendBufferSize,
unsigned int  kernelReceiveBufferSize 
)

Creates a lowlevel TCPSocket using the following io service.

References mSocket, mStrand, and Sirikata::Network::IOStrand::service().

void Sirikata::Network::ASIOSocketWrapper::destroySocket ( )

Destroys the lowlevel TCPSocket.

References mSocket.

void Sirikata::Network::ASIOSocketWrapper::finishAsyncSend ( const MultiplexedSocketPtr parentMultiSocket) [private]

This function sets the QUEUE_CHECK_FLAG and checks the sendQueue for additional packets to send out.

If nothing is in the queue then it unsets the ASYNCHRONOUS_SEND_FLAG and QUEUE_CHECK_FLAGS If something is present in the queue it calls sendToWire with the queue

References ASYNCHRONOUS_SEND_FLAG, mSendingStatus, mSendQueue, Sirikata::SizedThreadSafeQueue< T, ResourceMonitor, Superclass >::popAll(), QUEUE_CHECK_FLAG, Sirikata::AtomicValue< T >::read(), sendToWire(), and unpauseSendStreams().

Referenced by sendManyDequeItems().

void Sirikata::Network::ASIOSocketWrapper::finishedSendingChunk ( const TimestampedChunk tc) [private]

Call this any time a chunk finishes being sent so statistics can be collected.

References mAverageSendLatency, Sirikata::EWA< ValType >::sample(), and Sirikata::Network::ASIOSocketWrapper::TimestampedChunk::sinceCreation().

Referenced by sendManyDequeItems().

Address Sirikata::Network::ASIOSocketWrapper::getLocalEndpoint ( ) const
ASIOReadBuffer* Sirikata::Network::ASIOSocketWrapper::getReadBuffer ( ) const [inline]

References mReadBuffer.

Address Sirikata::Network::ASIOSocketWrapper::getRemoteEndpoint ( ) const
const SizedResourceMonitor& Sirikata::Network::ASIOSocketWrapper::getResourceMonitor ( ) const [inline]
TCPSocket& Sirikata::Network::ASIOSocketWrapper::getSocket ( ) [inline]

References mSocket.

const TCPSocket& Sirikata::Network::ASIOSocketWrapper::getSocket ( ) const [inline]

References mSocket.

void Sirikata::Network::ASIOSocketWrapper::ioReactorThreadPauseStream ( const MultiplexedSocketPtr parentMultiSocket,
Stream::StreamID  sid 
)
UUID Sirikata::Network::ASIOSocketWrapper::massageUUID ( const UUID uuid) [static]

makes sure the UUID only consists of unicode-allowed characters and has no null values inside

References Sirikata::UUID::getArray(), and Sirikata::UUID::static_size.

ASIOSocketWrapper& Sirikata::Network::ASIOSocketWrapper::operator= ( const ASIOSocketWrapper socket) [inline]

References mSocket.

bool Sirikata::Network::ASIOSocketWrapper::rawSend ( const MultiplexedSocketPtr parentMultiSocket,
Chunk chunk,
bool  force 
)

Sends the exact bytes contained within the typedeffed vector.

Parameters:
parentMultiSocketthe parent multisocket object this socket belongs to
chunkis the exact bytes to put on the network (including streamID and framing data)
forceif true, force the data to be enqueued even if the queue policy indicates no more space is available.

References ASYNCHRONOUS_SEND_FLAG, mSendingStatus, mSendQueue, Sirikata::SizedThreadSafeQueue< T, ResourceMonitor, Superclass >::push(), retryQueuedSend(), sendToWire(), and TCPSSTLOG.

Referenced by sendControlPacket(), sendProtocolHeader(), and sendServerProtocolHeader().

void Sirikata::Network::ASIOSocketWrapper::retryQueuedSend ( const MultiplexedSocketPtr parentMultiSocket,
uint32  current_status 
) [private]

If another thread claimed to be sending data asynchronously This function checks to see if the send is still proceeding after the queue push This thread waits until all queue checks are done, then it sees if someone took up the torch of sending the data If no data is being sent, this thread takes up the torch and sets the queue check flag and sees if data is on the queue If no data is on the queue at this point, since it pushed its own data to the queue it assumes someone else successfully sent it off If data is on the queue, it goes ahead and sends the data to the wire by using the appropriate overload of the sendToWire function it then unsets the QUEUE_CHECK_FLAG and if data isn't being set also the ASYNCHRONOUS_SEND_FLAG.

References ASYNCHRONOUS_SEND_FLAG, mSendingStatus, mSendQueue, Sirikata::SizedThreadSafeQueue< T, ResourceMonitor, Superclass >::popAll(), QUEUE_CHECK_FLAG, Sirikata::AtomicValue< T >::read(), sendToWire(), and unpauseSendStreams().

Referenced by rawSend().

void Sirikata::Network::ASIOSocketWrapper::sendControlPacket ( const MultiplexedSocketPtr parentMultiSocket,
TCPStream::TCPStreamControlCodes  code,
const Stream::StreamID sid 
) [inline]

Sends a streamID #0 packet with further control data on it.

To start with only stream disconnect and the ack thereof are allowed

References constructControlPacket(), and rawSend().

void Sirikata::Network::ASIOSocketWrapper::sendManyDequeItems ( const std::tr1::weak_ptr< MultiplexedSocket > &  parentMultiSocket,
const ErrorCode error,
std::size_t  bytes_sent 
) [private]

The callback for when a single large Chunk at the front of a chunk deque was sent.

If the whole large Chunk was not sent then the rest of the Chunk is passed back to sendToWire If the whole Chunk was shipped off, the sendToWire function is called with the rest of the queue unless it is empty in which case the finishAsyncSend is called

References Sirikata::Network::BufferPrint(), Sirikata::Logging::error, finishAsyncSend(), finishedSendingChunk(), Sirikata::Logging::insane, mOutstandingDataParent, mToSend, SILOG, TCPSSTLOG, and Sirikata::Network::triggerMultiplexedConnectionError().

Referenced by bindFunctions().

void Sirikata::Network::ASIOSocketWrapper::sendProtocolHeader ( const MultiplexedSocketPtr parentMultiSocket,
const Address address,
const UUID value,
unsigned int  numConnections 
)

Sends 24 byte header that indicates version of SST, a unique ID and how many TCP connections should be established.

References Sirikata::Network::TCPStream::BASE64_ZERODELIM, Sirikata::Network::Address::getHostName(), Sirikata::Network::Address::getService(), rawSend(), Sirikata::Network::TCPStream::RFC_6455, and Sirikata::UUID::toString().

void Sirikata::Network::ASIOSocketWrapper::sendServerProtocolHeader ( const MultiplexedSocketPtr thus,
const std::string &  origin,
const std::string &  host,
const std::string &  port,
const std::string &  resource_name,
const std::string &  subprotocol,
bool  doSendSubProtocol,
const std::string &  response 
)
void Sirikata::Network::ASIOSocketWrapper::sendToWire ( const MultiplexedSocketPtr parentMultiSocket,
TimestampedChunk  toSend 
) [private]

When there's a single packet to be sent to the network, mSocket->async_send is simply called upon the Chunk to be sent.

References Sirikata::Network::BufferPrint(), Sirikata::Network::ASIOSocketWrapper::TimestampedChunk::chunk, mOutstandingDataParent, mSendManyDequeItems, mSocket, mToSend, and Sirikata::Network::ASIOSocketWrapper::TimestampedChunk::size().

Referenced by finishAsyncSend(), rawSend(), and retryQueuedSend().

void Sirikata::Network::ASIOSocketWrapper::sendToWire ( const MultiplexedSocketPtr parentMultiSocket,
std::deque< TimestampedChunk > &  const_toSend 
) [private]

This function sends a while queue of packets to the network The function sends each item using a vector of asio::buffers made from the passed in deque.

References Sirikata::Network::BufferPrint(), mOutstandingDataParent, mSendManyDequeItems, mSocket, and mToSend.

ASIOReadBuffer* Sirikata::Network::ASIOSocketWrapper::setReadBuffer ( ASIOReadBuffer arb) [inline]

References mReadBuffer.

void Sirikata::Network::ASIOSocketWrapper::shutdownAndClose ( )

close this socket by disallowing sends, then closing

References Sirikata::Logging::insane, mSocket, and SILOG.

Chunk * Sirikata::Network::ASIOSocketWrapper::toBase64ZeroDelim ( const MemoryReference a,
const MemoryReference b,
const MemoryReference c,
const MemoryReference bytesToPrependUnencoded = NULL 
) [static]
void Sirikata::Network::ASIOSocketWrapper::unpauseSendStreams ( const MultiplexedSocketPtr parentMultiSocket)

Member Data Documentation

Referenced by ASIOSocketWrapper().

The status of sending threads: odd number indicates asynchronous send...

number >0 indicates other waiting threads number&ASYNCHRONOUS_SEND_FLAG indicates a thread is currently sending data off. number&QUEUE_CHECK_FLAG means queue is currently being swapped away and items added to the queue may not ever be processed if the queue is empty unless a thread takes up the torch and does it

Referenced by canSend(), finishAsyncSend(), rawSend(), and retryQueuedSend().

std::tr1::function<void(const ErrorCode &error, std::size_t bytes_sent)> Sirikata::Network::ASIOSocketWrapper::mSendManyDequeItems [private]

Referenced by bindFunctions(), and sendToWire().

The queue of packets to send while an active async_send is doing its job.

Referenced by canSend(), finishAsyncSend(), getResourceMonitor(), ioReactorThreadPauseStream(), rawSend(), and retryQueuedSend().

Referenced by bindFunctions(), and createSocket().


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