A helper for creating cached object descriptions. More...
#include <yarp/os/impl/BufferedConnectionWriter.h>
Public Member Functions | |
BufferedConnectionWriter (bool textMode=false, bool bareMode=false) | |
Constructor. | |
~BufferedConnectionWriter () override | |
Destructor. | |
void | reset (bool textMode) |
Completely clear the writer and start afresh. | |
void | restart () |
Tell the writer that we will be serializing a new object, but to keep any cached buffers that already exist. | |
void | clear () override |
Clear all cached data. | |
bool | addPool (const yarp::os::Bytes &data) |
Add the specified bytes to the current pool buffer. | |
void | stopPool () |
Stop adding to the current pool buffer. | |
void | push (const Bytes &data, bool copy) |
Add the specified buffer to the list of buffers to be written. | |
bool | isTextMode () const override |
Check if the connection is text mode. | |
bool | isBareMode () const override |
Check if the connection is bare mode. | |
bool | convertTextMode () override |
Converts a standard description in binary into a textual description, if the connection is in text-mode. | |
void | declareSizes (int argc, int *argv) override |
If you can easily determine how many blocks there are in a message, call this first, before anything else. | |
void | setReplyHandler (PortReader &reader) override |
This sets a handler to deal with replies to the message. | |
void | appendInt8 (std::int8_t data) override |
Send a representation of a 8-bit integer to the network connection. | |
void | appendInt16 (std::int16_t data) override |
Send a representation of a 16-bit integer to the network connection. | |
void | appendInt32 (std::int32_t data) override |
Send a representation of a 32-bit integer to the network connection. | |
void | appendInt64 (std::int64_t data) override |
Send a representation of a 64-bit integer to the network connection. | |
void | appendFloat32 (yarp::conf::float32_t data) override |
Send a representation of a 32-bit floating point number to the network connection. | |
void | appendFloat64 (yarp::conf::float64_t data) override |
Send a representation of a 64-bit floating point number to the network connection. | |
void | appendBlock (const char *data, size_t len) override |
Send a block of data to the network connection. | |
void | appendText (const std::string &str, const char terminate='\n') override |
Send a terminated string to the network connection. | |
void | appendExternalBlock (const char *data, size_t len) override |
Send a block of data to the network connection, without making a copy. | |
virtual void | appendBlock (const yarp::os::Bytes &data) |
Add a buffer by recording a reference to it, without copying it. | |
virtual void | appendBlockCopy (const Bytes &data) |
Add a buffer by copying its contents. | |
virtual void | appendLine (const std::string &data) |
Send a string along with a carriage-return-line-feed sequence. | |
size_t | length () const override |
size_t | headerLength () const override |
size_t | length (size_t index) const override |
const char * | data (size_t index) const override |
bool | write (ConnectionWriter &connection) const override |
Write this object to a network connection. | |
void | write (OutputStream &os) override |
bool | write (PortReader &obj) |
Write message to a receiving object. | |
virtual size_t | dataSize () const |
size_t | bufferCount () const |
PortReader * | getReplyHandler () override |
void | addToHeader () |
Switch to storing a header. | |
yarp::os::Portable * | getReference () override |
void | setReference (yarp::os::Portable *obj) override |
Stores a direct pointer to the object being sent. | |
bool | isValid () const override |
bool | isActive () const override |
bool | isError () const override |
void | requestDrop () override |
Tag the connection to be dropped after the current message. | |
bool | dropRequested () override |
void | startWrite () const override |
Call when writing is about to begin. | |
void | stopWrite () const override |
Call when all writing is finished. | |
SizedWriter * | getBuffer () override |
const SizedWriter * | getBuffer () const override |
void | setInitialPoolSize (size_t size) |
Set a custom initial pool size, which affects the size of buffers created for temporary data storage. | |
std::string | toString () const |
Public Member Functions inherited from yarp::os::ConnectionWriter | |
virtual | ~ConnectionWriter () |
Destructor. | |
void | appendString (const std::string &str) |
Send a string to the network connection. | |
virtual bool | isNull () const |
Public Member Functions inherited from yarp::os::SizedWriter | |
virtual | ~SizedWriter () |
bool | write (ConnectionWriter &connection) const override |
Write this object to a network connection. | |
Public Member Functions inherited from yarp::os::PortWriter | |
virtual | ~PortWriter () |
Destructor. | |
virtual void | onCompletion () const |
This is called when the port has finished all writing operations. | |
virtual void | onCommencement () const |
This is called when the port is about to begin writing operations. | |
virtual yarp::os::Type | getWriteType () const |
Additional Inherited Members | |
Static Public Member Functions inherited from yarp::os::ConnectionWriter | |
static ConnectionWriter * | createBufferedConnectionWriter () |
Create a connection writer implementation that stores to a buffer which can be read later using getBuffer() | |
static bool | writeToStream (PortWriter &portable, OutputStream &os) |
A helper for creating cached object descriptions.
When a object is to be sent from one port to another, and we have multiple connections but don't want to serialize the object multiple times, a BufferedConnectionWriter is useful. It will create buffer space for any parts serialized on the fly, and keep track of any external blocks (e.g. arrays of pixel data) that we want to include verbatim in the message. This class plays no role in management of the lifecycle of external blocks (e.g. when they are created/destroyed, or when they may change in value). If you use external blocks, be sure to pay attention to onCompletion() events on your object.
Definition at line 45 of file BufferedConnectionWriter.h.
Constructor.
textMode | suggest that the object be serialized in a human readable format. BufferedConnectionWriter simply passes this flag along to read/write methods, it takes on action on it. |
bareMode | suggest that the object be serialized with the assumption that all type information is known by recipient. BufferedConnectionWriter simply passes this flag along to read/write methods, it takes on action on it. |
Definition at line 30 of file BufferedConnectionWriter.cpp.
|
override |
Destructor.
Definition at line 48 of file BufferedConnectionWriter.cpp.
bool BufferedConnectionWriter::addPool | ( | const yarp::os::Bytes & | data | ) |
Add the specified bytes to the current pool buffer.
The pool buffer is a place to concatenate small blocks of data that are not being held externally. This requires a memory copy, so for large blocks of data consider adding these separately. A pool buffer will be created if none already exists.
Definition at line 94 of file BufferedConnectionWriter.cpp.
void BufferedConnectionWriter::addToHeader | ( | ) |
Switch to storing a header.
Buffers are tracked separately for the header.
Definition at line 408 of file BufferedConnectionWriter.cpp.
Send a block of data to the network connection.
Since communication may not happen immediately, or may happen multiple times, a copy of this data is made. If you know the block of data will remain valid, and it is a large block of data, please call ConnectionWriter::appendExternalBlock instead.
data | the start of the data block |
len | the length of the data block |
Implements yarp::os::ConnectionWriter.
Definition at line 266 of file BufferedConnectionWriter.cpp.
|
virtual |
Add a buffer by recording a reference to it, without copying it.
Be careful, this is the opposite of what appendBlock(ptr, len) does. Sorry about that.
data | the buffer to add |
Definition at line 290 of file BufferedConnectionWriter.cpp.
Add a buffer by copying its contents.
data | the buffer to add |
Definition at line 296 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Send a block of data to the network connection, without making a copy.
If you are not confident that the block of data will remain valid throughout transmission, call ConnectionWriter::appendBlock instead.
data | the start of the data block |
len | the length of the data block |
Implements yarp::os::ConnectionWriter.
Definition at line 285 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Send a representation of a 32-bit floating point number to the network connection.
data | the floating point number to send |
Implements yarp::os::ConnectionWriter.
Definition at line 256 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Send a representation of a 64-bit floating point number to the network connection.
data | the floating point number to send |
Implements yarp::os::ConnectionWriter.
Definition at line 261 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Send a representation of a 16-bit integer to the network connection.
data | the integer to send |
Implements yarp::os::ConnectionWriter.
Definition at line 241 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Send a representation of a 32-bit integer to the network connection.
data | the integer to send |
Implements yarp::os::ConnectionWriter.
Definition at line 246 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Send a representation of a 64-bit integer to the network connection.
data | the integer to send |
Implements yarp::os::ConnectionWriter.
Definition at line 251 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Send a representation of a 8-bit integer to the network connection.
data | the integer to send |
Implements yarp::os::ConnectionWriter.
Definition at line 236 of file BufferedConnectionWriter.cpp.
Send a string along with a carriage-return-line-feed sequence.
This is a convenience function used by old parts of yarp, for telnet compatibility on sockets.
data | string to write, not including carriage-return-line-feed. |
Definition at line 301 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Send a terminated string to the network connection.
The lenght of string is not specified in advance, therefore the reader should read until the terminating character is found.
str | the string to send |
terminate | the terminating character to use |
Implements yarp::os::ConnectionWriter.
Definition at line 271 of file BufferedConnectionWriter.cpp.
size_t BufferedConnectionWriter::bufferCount | ( | ) | const |
Definition at line 396 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Clear all cached data.
Reimplemented from yarp::os::SizedWriter.
Definition at line 75 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Converts a standard description in binary into a textual description, if the connection is in text-mode.
Useful if you only operate on the binary description usually, and just want to permit text mode for command-line interaction. If isTextMode would return false, no conversion is done.
Implements yarp::os::ConnectionWriter.
Definition at line 201 of file BufferedConnectionWriter.cpp.
Implements yarp::os::SizedWriter.
Definition at line 331 of file BufferedConnectionWriter.cpp.
|
virtual |
Definition at line 381 of file BufferedConnectionWriter.cpp.
If you can easily determine how many blocks there are in a message, call this first, before anything else.
This may improve efficiency in some situations.
argc | Number of blocks |
argv | An array of integers, giving the length of each block |
Implements yarp::os::ConnectionWriter.
Definition at line 209 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::SizedWriter.
Definition at line 445 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::ConnectionWriter.
Definition at line 465 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::ConnectionWriter.
Definition at line 460 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::SizedWriter.
Definition at line 415 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::SizedWriter.
Definition at line 402 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::SizedWriter.
Definition at line 316 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::ConnectionWriter.
Definition at line 430 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Check if the connection is bare mode.
If it is, you are encouraged to omit type information from your serialization.
Implements yarp::os::ConnectionWriter.
Definition at line 195 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::ConnectionWriter.
Definition at line 435 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Check if the connection is text mode.
If it is, you are encouraged (but by no means required) to use a human-readable representation of your data structure.
Implements yarp::os::ConnectionWriter.
Definition at line 190 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::ConnectionWriter.
Definition at line 425 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::SizedWriter.
Definition at line 311 of file BufferedConnectionWriter.cpp.
Implements yarp::os::SizedWriter.
Definition at line 321 of file BufferedConnectionWriter.cpp.
Add the specified buffer to the list of buffers to be written.
If the copy flag is set, the data in the buffer is copied, otherwise a reference to it is kept (be careful to keep the buffer valid until all writes that use it are complete and onComplete() has been called on the object being sent).
data | the buffer to add |
copy | whether the data should be copied, or a reference stored |
Definition at line 150 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Tag the connection to be dropped after the current message.
Implements yarp::os::ConnectionWriter.
Definition at line 440 of file BufferedConnectionWriter.cpp.
Completely clear the writer and start afresh.
textMode | see parameter to constructor for details |
Definition at line 54 of file BufferedConnectionWriter.cpp.
void BufferedConnectionWriter::restart | ( | ) |
Tell the writer that we will be serializing a new object, but to keep any cached buffers that already exist.
If the structure of the new object matches that of what came before, the buffers will be reused without any new memory allocation being necessary. If the structure differs, memory allocation may be needed.
Definition at line 63 of file BufferedConnectionWriter.cpp.
Set a custom initial pool size, which affects the size of buffers created for temporary data storage.
If this method is not called, the default used is BUFFERED_CONNECTION_INITIAL_POOL_SIZE
size | the initial buffer size (in bytes) to use |
Definition at line 470 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Stores a direct pointer to the object being sent.
This is useful for local communication, to bypass serialization.
Implements yarp::os::ConnectionWriter.
Definition at line 420 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
This sets a handler to deal with replies to the message.
The handler will be called once per connection. There will be problems for connections using carriers that don't support replies.
reader | the object that handles replies. |
Implements yarp::os::ConnectionWriter.
Definition at line 216 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Call when writing is about to begin.
Implements yarp::os::SizedWriter.
Definition at line 450 of file BufferedConnectionWriter.cpp.
void BufferedConnectionWriter::stopPool | ( | ) |
Stop adding to the current pool buffer.
Any further calls to addPool() for the current write will result in creation of a new pool.
Definition at line 141 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Call when all writing is finished.
Implements yarp::os::SizedWriter.
Definition at line 454 of file BufferedConnectionWriter.cpp.
std::string BufferedConnectionWriter::toString | ( | ) | const |
Definition at line 476 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Write this object to a network connection.
Override this for your particular class. Be aware that depending on the nature of the connections a port has, and what protocol they use, and how efficient the YARP implementation is, this method may be called once, twice, or many times, as the result of a single call to Port::write
writer | an interface to the network connection for writing |
Implements yarp::os::PortWriter.
Definition at line 341 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Reimplemented from yarp::os::SizedWriter.
Definition at line 356 of file BufferedConnectionWriter.cpp.
bool BufferedConnectionWriter::write | ( | PortReader & | obj | ) |
Write message to a receiving object.
This is to simplify writing tests, YARP does not use this internally.
obj | object to write into |
Definition at line 370 of file BufferedConnectionWriter.cpp.