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. More... | |
~BufferedConnectionWriter () override | |
Destructor. More... | |
void | reset (bool textMode) |
Completely clear the writer and start afresh. More... | |
void | restart () |
Tell the writer that we will be serializing a new object, but to keep any cached buffers that already exist. More... | |
void | clear () override |
Clear all cached data. More... | |
bool | addPool (const yarp::os::Bytes &data) |
Add the specified bytes to the current pool buffer. More... | |
void | stopPool () |
Stop adding to the current pool buffer. More... | |
void | push (const Bytes &data, bool copy) |
Add the specified buffer to the list of buffers to be written. More... | |
bool | isTextMode () const override |
Check if the connection is text mode. More... | |
bool | isBareMode () const override |
Check if the connection is bare mode. More... | |
bool | convertTextMode () override |
Converts a standard description in binary into a textual description, if the connection is in text-mode. More... | |
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. More... | |
void | setReplyHandler (PortReader &reader) override |
This sets a handler to deal with replies to the message. More... | |
void | appendInt8 (std::int8_t data) override |
Send a representation of a 8-bit integer to the network connection. More... | |
void | appendInt16 (std::int16_t data) override |
Send a representation of a 16-bit integer to the network connection. More... | |
void | appendInt32 (std::int32_t data) override |
Send a representation of a 32-bit integer to the network connection. More... | |
void | appendInt64 (std::int64_t data) override |
Send a representation of a 64-bit integer to the network connection. More... | |
void | appendFloat32 (yarp::conf::float32_t data) override |
Send a representation of a 32-bit floating point number to the network connection. More... | |
void | appendFloat64 (yarp::conf::float64_t data) override |
Send a representation of a 64-bit floating point number to the network connection. More... | |
void | appendBlock (const char *data, size_t len) override |
Send a block of data to the network connection. More... | |
void | appendText (const std::string &str, const char terminate='\n') override |
Send a terminated string to the network connection. More... | |
void | appendExternalBlock (const char *data, size_t len) override |
Send a block of data to the network connection, without making a copy. More... | |
virtual void | appendBlock (const yarp::os::Bytes &data) |
Add a buffer by recording a reference to it, without copying it. More... | |
virtual void | appendBlockCopy (const Bytes &data) |
Add a buffer by copying its contents. More... | |
virtual void | appendLine (const std::string &data) |
Send a string along with a carriage-return-line-feed sequence. More... | |
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. More... | |
void | write (OutputStream &os) override |
bool | write (PortReader &obj) |
Write message to a receiving object. More... | |
virtual size_t | dataSize () const |
size_t | bufferCount () const |
PortReader * | getReplyHandler () override |
void | addToHeader () |
Switch to storing a header. More... | |
yarp::os::Portable * | getReference () override |
void | setReference (yarp::os::Portable *obj) override |
Stores a direct pointer to the object being sent. More... | |
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. More... | |
bool | dropRequested () override |
void | startWrite () const override |
Call when writing is about to begin. More... | |
void | stopWrite () const override |
Call when all writing is finished. More... | |
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. More... | |
std::string | toString () const |
Public Member Functions inherited from yarp::os::ConnectionWriter | |
virtual | ~ConnectionWriter () |
Destructor. More... | |
virtual void | appendInt (int data) final |
Send a representation of an integer to the network connection. More... | |
virtual void | appendDouble (double data) |
Send a representation of a floating point number to the network connection. More... | |
virtual void | appendString (const char *str, const char terminate='\n') final |
Send a character sequence to the network connection. More... | |
virtual void | appendRawString (const std::string &str) final |
void | appendString (const std::string &str) |
Send a string to the network connection. More... | |
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. More... | |
Public Member Functions inherited from yarp::os::PortWriter | |
virtual | ~PortWriter () |
Destructor. More... | |
virtual void | onCompletion () const |
This is called when the port has finished all writing operations. More... | |
virtual void | onCommencement () const |
This is called when the port is about to begin writing operations. More... | |
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() More... | |
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 47 of file BufferedConnectionWriter.h.
BufferedConnectionWriter::BufferedConnectionWriter | ( | bool | textMode = false , |
bool | bareMode = false |
||
) |
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 33 of file BufferedConnectionWriter.cpp.
|
override |
Destructor.
Definition at line 51 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 97 of file BufferedConnectionWriter.cpp.
void BufferedConnectionWriter::addToHeader | ( | ) |
Switch to storing a header.
Buffers are tracked separately for the header.
Definition at line 411 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
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 269 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 293 of file BufferedConnectionWriter.cpp.
|
virtual |
Add a buffer by copying its contents.
data | the buffer to add |
Definition at line 299 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 288 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 259 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 264 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 244 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 249 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 254 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 239 of file BufferedConnectionWriter.cpp.
|
virtual |
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 304 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 274 of file BufferedConnectionWriter.cpp.
size_t BufferedConnectionWriter::bufferCount | ( | ) | const |
Definition at line 399 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Clear all cached data.
Reimplemented from yarp::os::SizedWriter.
Definition at line 78 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 204 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::SizedWriter.
Definition at line 334 of file BufferedConnectionWriter.cpp.
|
virtual |
Definition at line 384 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
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 212 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::SizedWriter.
Definition at line 448 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::ConnectionWriter.
Definition at line 463 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::SizedWriter.
Definition at line 418 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::SizedWriter.
Definition at line 405 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::SizedWriter.
Definition at line 319 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::ConnectionWriter.
Definition at line 433 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 198 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::ConnectionWriter.
Definition at line 438 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 193 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::ConnectionWriter.
Definition at line 428 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::SizedWriter.
Definition at line 314 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Implements yarp::os::SizedWriter.
Definition at line 324 of file BufferedConnectionWriter.cpp.
void BufferedConnectionWriter::push | ( | const Bytes & | data, |
bool | copy | ||
) |
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 153 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Tag the connection to be dropped after the current message.
Implements yarp::os::ConnectionWriter.
Definition at line 443 of file BufferedConnectionWriter.cpp.
void BufferedConnectionWriter::reset | ( | bool | textMode | ) |
Completely clear the writer and start afresh.
textMode | see parameter to constructor for details |
Definition at line 57 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 66 of file BufferedConnectionWriter.cpp.
void BufferedConnectionWriter::setInitialPoolSize | ( | size_t | size | ) |
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 469 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 423 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 219 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Call when writing is about to begin.
Implements yarp::os::SizedWriter.
Definition at line 453 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 144 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Call when all writing is finished.
Implements yarp::os::SizedWriter.
Definition at line 457 of file BufferedConnectionWriter.cpp.
std::string BufferedConnectionWriter::toString | ( | ) | const |
Definition at line 475 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 344 of file BufferedConnectionWriter.cpp.
|
overridevirtual |
Reimplemented from yarp::os::SizedWriter.
Definition at line 359 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 373 of file BufferedConnectionWriter.cpp.