Sirikata
|
#include <ASIOSocketWrapper.hpp>
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 () | |
ASIOSocketWrapper & | operator= (const ASIOSocketWrapper &socket) |
const SizedResourceMonitor & | getResourceMonitor () const |
void | clearReadBuffer () |
ASIOReadBuffer * | getReadBuffer () const |
ASIOReadBuffer * | setReadBuffer (ASIOReadBuffer *arb) |
TCPSocket & | getSocket () |
const TCPSocket & | getSocket () 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 Chunk * | constructControlPacket (const MultiplexedSocketPtr &parentMultiSocket, TCPStream::TCPStreamControlCodes code, const Stream::StreamID &sid) |
static Chunk * | constructPing (const MultiplexedSocketPtr &parentMultiSocket, MemoryReference data, bool isPong) |
Sends a WebSocket ping/pong with the passed data. | |
static Chunk * | toBase64ZeroDelim (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 | |
IOStrand * | mStrand |
TCPSocket * | mSocket |
ASIOReadBuffer * | mReadBuffer |
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< Duration > | mAverageSendLatency |
std::vector< Stream::StreamID > | mPausedSendStreams |
std::deque< TimestampedChunk > | mToSend |
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 |
typedef boost::system::error_code Sirikata::Network::ASIOSocketWrapper::ErrorCode [private] |
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] |
References mOutstandingDataParent, mToSend, and SILOG.
Duration Sirikata::Network::ASIOSocketWrapper::averageReceiveLatency | ( | ) | const |
Duration Sirikata::Network::ASIOSocketWrapper::averageSendLatency | ( | ) | const |
References mAverageSendLatency, and Sirikata::EWA< ValType >::value().
void Sirikata::Network::ASIOSocketWrapper::bindFunctions | ( | const MultiplexedSocketPtr & | parent | ) |
References mSendManyDequeItems, mStrand, sendManyDequeItems(), and Sirikata::Network::IOStrand::wrap().
Referenced by ASIOSocketWrapper().
bool Sirikata::Network::ASIOSocketWrapper::canSend | ( | size_t | dataSize | ) | const |
void Sirikata::Network::ASIOSocketWrapper::clearReadBuffer | ( | ) | [inline] |
References mReadBuffer.
Referenced by Sirikata::Network::ASIOReadBuffer::processError().
Chunk * Sirikata::Network::ASIOSocketWrapper::constructControlPacket | ( | const MultiplexedSocketPtr & | parentMultiSocket, |
TCPStream::TCPStreamControlCodes | code, | ||
const Stream::StreamID & | sid | ||
) | [static] |
References Sirikata::Network::TCPStream::BASE64_ZERODELIM, Sirikata::Network::TCPStream::LENGTH_DELIM, Sirikata::Network::Stream::StreamID::MAX_HEX_SERIALIZED_LENGTH, Sirikata::VariableLength::MAX_SERIALIZED_LENGTH, Sirikata::Network::TCPStream::RFC_6455, Sirikata::VariableLength::serialize(), Sirikata::Network::Stream::StreamID::serializeToHex(), and toBase64ZeroDelim().
Referenced by Sirikata::Network::MultiplexedSocket::closeStream(), and sendControlPacket().
Chunk * Sirikata::Network::ASIOSocketWrapper::constructPing | ( | const MultiplexedSocketPtr & | parentMultiSocket, |
MemoryReference | data, | ||
bool | isPong | ||
) | [static] |
Sends a WebSocket ping/pong with the passed data.
References Sirikata::Network::TCPStream::BASE64_ZERODELIM, Sirikata::DataReference< T >::data(), Sirikata::DataReference< T >::length(), and Sirikata::Network::TCPStream::LENGTH_DELIM.
Referenced by Sirikata::Network::MultiplexedSocket::receivePing().
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 | ( | ) |
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 |
References Sirikata::Network::convertEndpointToAddress(), and mSocket.
ASIOReadBuffer* Sirikata::Network::ASIOSocketWrapper::getReadBuffer | ( | ) | const [inline] |
References mReadBuffer.
Address Sirikata::Network::ASIOSocketWrapper::getRemoteEndpoint | ( | ) | const |
References Sirikata::Network::convertEndpointToAddress(), and mSocket.
const SizedResourceMonitor& Sirikata::Network::ASIOSocketWrapper::getResourceMonitor | ( | ) | const [inline] |
const TCPSocket& Sirikata::Network::ASIOSocketWrapper::getSocket | ( | ) | const [inline] |
References mSocket.
void Sirikata::Network::ASIOSocketWrapper::ioReactorThreadPauseStream | ( | const MultiplexedSocketPtr & | parentMultiSocket, |
Stream::StreamID | sid | ||
) |
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.
parentMultiSocket | the parent multisocket object this socket belongs to |
chunk | is the exact bytes to put on the network (including streamID and framing data) |
force | if 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 | ||
) |
References rawSend(), and Sirikata::Network::TCPStream::RFC_6455.
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 | ) |
References mPausedSendStreams.
Referenced by finishAsyncSend(), ioReactorThreadPauseStream(), and retryQueuedSend().
Referenced by averageSendLatency(), and finishedSendingChunk().
std::tr1::shared_ptr<MultiplexedSocket> Sirikata::Network::ASIOSocketWrapper::mOutstandingDataParent [private] |
Referenced by sendManyDequeItems(), sendToWire(), and ~ASIOSocketWrapper().
std::tr1::weak_ptr<MultiplexedSocket> Sirikata::Network::ASIOSocketWrapper::mParent [private] |
Referenced by ASIOSocketWrapper().
std::vector<Stream::StreamID> Sirikata::Network::ASIOSocketWrapper::mPausedSendStreams [private] |
Referenced by ioReactorThreadPauseStream(), and unpauseSendStreams().
Referenced by clearReadBuffer(), getReadBuffer(), and setReadBuffer().
AtomicValue<uint32> Sirikata::Network::ASIOSocketWrapper::mSendingStatus [private] |
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 createSocket(), destroySocket(), getLocalEndpoint(), getRemoteEndpoint(), getSocket(), operator=(), sendToWire(), and shutdownAndClose().
Referenced by bindFunctions(), and createSocket().
std::deque<TimestampedChunk> Sirikata::Network::ASIOSocketWrapper::mToSend [private] |
Referenced by sendManyDequeItems(), sendToWire(), and ~ASIOSocketWrapper().