|
SmartFoxServer 3 C++ Client API 3.1.0-beta
Client API for SmartFoxServer 3
|
A growable buffer of bytes, with a read cursor and a write cursor. More...
#include <ByteBuffer.h>
Public Member Functions | |
| ByteBuffer () | |
| Builds an unbounded buffer, which grows as long as there is memory. | |
| ByteBuffer (size_t capacity) | |
| Builds a bounded buffer. | |
| ByteBuffer (std::vector< uint8_t > bytes) | |
| Builds an unbounded buffer around bytes that already exist. | |
| size_t | readPosition () const |
| void | readPosition (size_t newPos) |
| size_t | writePosition () const |
| void | writePosition (size_t newPos) |
| size_t | capacity () const |
| size_t | remaining () const |
| bool | hasRemaining () const |
| void | rewind () |
| Puts the read cursor back to the start of the buffer. | |
| void | clear () |
| Empties the buffer and puts both cursors back to the start. | |
| BufferView | view () const |
| Returns a view of the whole backing store. | |
| BufferView | remainingView () const |
| Returns a view of the region still to read, from the read cursor to the write cursor. | |
| const std::vector< uint8_t > & | bytes () const |
| Returns the backing store. | |
| uint8_t | read () |
| BufferView | readView (size_t size) |
| Reads a run of bytes without copying them. | |
| std::vector< uint8_t > | read (size_t size) |
| Reads a run of bytes into a new vector. | |
| void | prepend (BufferView header) |
| Inserts bytes in front of the content that is already in the buffer. | |
| void | prepend (const std::vector< uint8_t > &bytes) |
| void | prepend (const uint8_t *bytes, size_t len) |
| std::string | hexDump () const |
| std::string | status () const |
Static Public Member Functions | |
| static std::string | hexDump (BufferView data) |
A growable buffer of bytes, with a read cursor and a write cursor.
Every read and write moves its own cursor forward, so you can fill a buffer and then read it back from the start with ByteBuffer::rewind. All the multi-byte numbers are big-endian, which is the byte order of the SmartFoxServer protocol.
A buffer is either bounded, with a fixed capacity given at construction, or unbounded, in which case it grows as needed.
| sfs3::entities::ByteBuffer::ByteBuffer | ( | size_t | capacity | ) |
Builds a bounded buffer.
| capacity | the fixed size of the buffer, in bytes |
| sfs3::entities::ByteBuffer::ByteBuffer | ( | std::vector< uint8_t > | bytes | ) |
Builds an unbounded buffer around bytes that already exist.
| bytes | the bytes the buffer takes over |
| size_t sfs3::entities::ByteBuffer::readPosition | ( | ) | const |
| void sfs3::entities::ByteBuffer::readPosition | ( | size_t | newPos | ) |
| newPos | the new position of the read cursor |
| size_t sfs3::entities::ByteBuffer::writePosition | ( | ) | const |
| void sfs3::entities::ByteBuffer::writePosition | ( | size_t | newPos | ) |
| newPos | the new position of the write cursor |
| size_t sfs3::entities::ByteBuffer::capacity | ( | ) | const |
| size_t sfs3::entities::ByteBuffer::remaining | ( | ) | const |
| bool sfs3::entities::ByteBuffer::hasRemaining | ( | ) | const |
| BufferView sfs3::entities::ByteBuffer::view | ( | ) | const |
Returns a view of the whole backing store.
NOTE: this covers the full capacity, including the bytes past the write position that were never written. To read only the data that is really in the buffer, bound the view with ByteBuffer::writePosition, or use ByteBuffer::remainingView.
| BufferView sfs3::entities::ByteBuffer::remainingView | ( | ) | const |
Returns a view of the region still to read, from the read cursor to the write cursor.
Use this, and not ByteBuffer::view, on a buffer that is partly consumed: view() always starts at the first byte and would hand back the bytes the reader already walked past.
| const std::vector< uint8_t > & sfs3::entities::ByteBuffer::bytes | ( | ) | const |
Returns the backing store.
NOTE: as with ByteBuffer::view, this is the full capacity, including any bytes past the write position that were never written.
| uint8_t sfs3::entities::ByteBuffer::read | ( | ) |
| BufferView sfs3::entities::ByteBuffer::readView | ( | size_t | size | ) |
Reads a run of bytes without copying them.
| size | how many bytes to read |
| std::vector< uint8_t > sfs3::entities::ByteBuffer::read | ( | size_t | size | ) |
Reads a run of bytes into a new vector.
| size | how many bytes to read |
| void sfs3::entities::ByteBuffer::prepend | ( | BufferView | header | ) |
Inserts bytes in front of the content that is already in the buffer.
This is how the protocol wraps a payload in its headers. The cost grows with the size of the buffer, because the payload is moved forward. Both cursors move with it, so a read that is in progress stays on the same byte.
| header | the bytes to insert at the start |
| void sfs3::entities::ByteBuffer::prepend | ( | const std::vector< uint8_t > & | bytes | ) |
| void sfs3::entities::ByteBuffer::prepend | ( | const uint8_t * | bytes, |
| size_t | len ) |
| std::string sfs3::entities::ByteBuffer::hexDump | ( | ) | const |
|
static |
| std::string sfs3::entities::ByteBuffer::status | ( | ) | const |