3es
0.7
|
A utility class which generates a MtCollatedPacket
message by appending multiple other messages.
More...
#include <3escollatedpacket.h>
Public Member Functions | |
CollatedPacket (bool compress, uint16_t bufferSize=0xff00u) | |
Initialise a collated packet. More... | |
CollatedPacket (unsigned bufferSize, unsigned maxPacketSize) | |
Initialise a collated packet allowing packet sizes large than MaxPacketSize . More... | |
~CollatedPacket () | |
Destructor. | |
bool | compressionEnabled () const |
Is compression enabled. More... | |
unsigned | maxPacketSize () const |
Return the capacity of the collated packet. More... | |
void | reset () |
Reset the collated packet, dropping any existing data. | |
int | add (const PacketWriter &packet) |
Add the packet data in packet to the collation buffer. More... | |
int | add (const uint8_t *buffer, uint16_t byteCount) |
Add bytes to the packet. More... | |
bool | finalise () |
Finalises the collated packet for sending. More... | |
const uint8_t * | buffer (unsigned &byteCount) const |
Access the internal buffer pointer. More... | |
unsigned | collatedBytes () const |
Return the number of bytes that have been collated. More... | |
void | close () override |
Ignored for CollatedPacket . | |
void | setActive (bool active) override |
Enable/disable the connection. More... | |
bool | active () const override |
Check if currently active. More... | |
const char * | address () const override |
Identifies the collated packet. More... | |
uint16_t | port () const override |
Not supported. More... | |
bool | isConnected () const override |
Always connected. More... | |
int | create (const Shape &shape) override |
Collated the create message for shape . More... | |
int | destroy (const Shape &shape) override |
Collated the update message for shape . More... | |
int | update (const Shape &shape) override |
Collated the destroy message for shape . More... | |
int | updateTransfers (unsigned byteLimit) override |
Not supported. More... | |
int | updateFrame (float dt, bool flush=true) override |
Not supported. More... | |
unsigned | referenceResource (const Resource *resource) override |
Not supported. More... | |
unsigned | releaseResource (const Resource *resource) override |
Not supported. More... | |
bool | sendServerInfo (const ServerInfoMessage &info) override |
Collated the create message for shape . More... | |
int | send (const uint8_t *data, int byteCount) override |
Aliased to add() . More... | |
![]() | |
virtual | ~Connection () |
Virtual destructor. | |
int | send (const int8_t *data, int byteCount) |
Static Public Attributes | |
static const size_t | Overhead |
Byte count overhead added by using a CollatedPacket . More... | |
static const unsigned | InitialCursorOffset |
Initial cursor position in the write buffer. More... | |
static const uint16_t | MaxPacketSize |
The default packet size limit for a CollatedPacketMessage . | |
A utility class which generates a MtCollatedPacket
message by appending multiple other messages.
Compression may optionally be applied.
Typical usage:
PacketWriter
.add(const PacketWriter &)
finalise()
on the collated packet.reset()
The CollatedPacket
also extends the Connection
class in order to support multi-threaded packet generation and synchronisation. While the Connection
and Connection
implementations are required to be thread-safe, they cannot guarantee packets area correctly collated by thread. Thus a CollatedPacket
can be used per thread to collate messages for each thread. The packet content can then be sent as a single transaction.
By supporting the Connection
methods, a CollatedPacket
can be used in place of a 'server' argument with various server utility macros and functions.
By default, a CollatedPacket
has is limited to supporting MaxPacketSize
bytes. This allows a single packet with a single PacketHeader
and collated packet message with optional compression. However, when a collated packet is used for transaction collation (as described in the multi-threaded case), it may require collation of larger data sizes. In this case, the CollatedPacket(unsigned, unsigned)
constructor can be used to specify a larger collation buffer limit (the buffer resizes as required). Such large, collated packets are sent using Server::send(const CollatedPacket &)
. Internally, the method may either send the packet as is (if small enough), or extract and reprocess each collated packet.
tes::CollatedPacket::CollatedPacket | ( | bool | compress, |
uint16_t | bufferSize = 0xff00u |
||
) |
Initialise a collated packet.
This sets the initial packet size limited by MaxPacketSize
, and compression options.
compress | True to compress data as written. |
bufferSize | The initial bufferSize |
tes::CollatedPacket::CollatedPacket | ( | unsigned | bufferSize, |
unsigned | maxPacketSize | ||
) |
Initialise a collated packet allowing packet sizes large than MaxPacketSize
.
This is intended for collating messages to be send as a group in a thread-safe fashion. The maximum packet size may exceed the normal send limit. As such compression is not allowed to better support splitting.
bufferSize | The initial bufferSize |
maxPacketSize | The maximum packet size. |
|
overridevirtual |
int tes::CollatedPacket::add | ( | const PacketWriter & | packet | ) |
Add the packet data in packet
to the collation buffer.
The method will fail (return -1) when the maxPacketSize()
has been reached. In this case, the packet should be sent and reset before trying again. The method will also fail if the packet has already been finalised using finalise()
.
packet | The packet data to add. |
packet.packetSize()
on success, or -1 on failure. int tes::CollatedPacket::add | ( | const uint8_t * | buffer, |
uint16_t | byteCount | ||
) |
Add bytes to the packet.
Use with care as the buffer
should always start with a valid PacketHeader
in network byte order.
buffer | The data to add. |
byteCount | The number of bytes in buffer . |
packet.packetSize()
on success, or -1 on failure.
|
overridevirtual |
const uint8_t* tes::CollatedPacket::buffer | ( | unsigned & | byteCount | ) | const |
Access the internal buffer pointer.
[out] | Set | to the number of used bytes in the collated buffer, including the CRC when the packet has been finalised. |
|
inline |
Return the number of bytes that have been collated.
This excludes the PacketHeader
and CollatedPacketMessage
, but will include the CRC once finalised.
|
inline |
Is compression enabled.
Required ZLIB.
|
overridevirtual |
Collated the create message for shape
.
shape | The shape of interest. |
add()
). Implements tes::Connection.
|
overridevirtual |
Collated the update message for shape
.
shape | The shape of interest. |
add()
). Implements tes::Connection.
bool tes::CollatedPacket::finalise | ( | ) |
Finalises the collated packet for sending.
This includes completing compression and calculating the CRC.
|
overridevirtual |
|
inline |
Return the capacity of the collated packet.
This defaults to 64 * 1024 - 1 (the maximum for a 16-bit unsigned integer), when using the constructor: CollatedPacket(bool, unsigned)
. It may be larger when using the CollatedPacket(unsigned, unsigned)
constructor. See that constructor and class notes for details.
|
overridevirtual |
|
overridevirtual |
|
overridevirtual |
|
overridevirtual |
Aliased to add()
.
buffer | The data to add. |
bufferSize | The number of bytes in buffer . |
packet.packetSize()
on success, or -1 on failure. Implements tes::Connection.
|
overridevirtual |
Collated the create message for shape
.
shape | The shape of interest. |
add()
). Implements tes::Connection.
|
overridevirtual |
Enable/disable the connection.
While disabled, messages are ignored.
active | The active state to set. |
Implements tes::Connection.
|
overridevirtual |
Collated the destroy message for shape
.
shape | The shape of interest. |
add()
). Implements tes::Connection.
|
overridevirtual |
|
overridevirtual |
|
static |
Initial cursor position in the write buffer.
This is the sum of PacketHeader
, CollatedPacketMessage
.
|
static |
Byte count overhead added by using a CollatedPacket
.
This is the sum of PacketHeader
, CollatedPacketMessage
and the PacketWriter::CrcType
.