YARP
Yet Another Robot Platform
BufferedConnectionWriter.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef YARP_OS_IMPL_BUFFEREDCONNECTIONWRITER_H
8 #define YARP_OS_IMPL_BUFFEREDCONNECTIONWRITER_H
9 
11 #include <yarp/os/SizedWriter.h>
12 
13 #include <string>
14 #include <vector>
15 
16 
17 namespace yarp {
18 namespace os {
19 
20 class Bytes;
21 class ManagedBytes;
22 
23 namespace impl {
24 
25 /*
26  * When allocating space to store serialized data, we start off with
27  * a block of this size. It will be resized as necessary.
28  * Data can optionally have a header, serialized separately.
29  */
30 constexpr size_t BUFFERED_CONNECTION_INITIAL_POOL_SIZE = 1024;
31 
47 {
48 public:
61  BufferedConnectionWriter(bool textMode = false,
62  bool bareMode = false);
63 
67  ~BufferedConnectionWriter() override;
68 
74  void reset(bool textMode);
75 
83  void restart();
84 
88  void clear() override;
89 
100  bool addPool(const yarp::os::Bytes& data);
101 
107  void stopPool();
108 
119  void push(const Bytes& data, bool copy);
120 
121  // defined by yarp::os::ConnectionWriter
122  bool isTextMode() const override;
123  bool isBareMode() const override;
124  bool convertTextMode() override;
125  void declareSizes(int argc, int* argv) override; // FIXME Remove?
126  void setReplyHandler(PortReader& reader) override;
127  void appendInt8(std::int8_t data) override;
128  void appendInt16(std::int16_t data) override;
129  void appendInt32(std::int32_t data) override;
130  void appendInt64(std::int64_t data) override;
131  void appendFloat32(yarp::conf::float32_t data) override;
132  void appendFloat64(yarp::conf::float64_t data) override;
133  void appendBlock(const char* data, size_t len) override;
134  void appendText(const std::string& str, const char terminate = '\n') override;
135  void appendExternalBlock(const char* data, size_t len) override;
136 
144  virtual void appendBlock(const yarp::os::Bytes& data);
145 
151  virtual void appendBlockCopy(const Bytes& data);
152 
160  virtual void appendLine(const std::string& data);
161 
162 
163  // defined by yarp::os::SizedWriter
164  size_t length() const override;
165  size_t headerLength() const override;
166  size_t length(size_t index) const override;
167  const char* data(size_t index) const override;
168  bool write(ConnectionWriter& connection) const override;
169  void write(OutputStream& os) override;
170 
177  bool write(PortReader& obj);
178 
183  virtual size_t dataSize() const;
184 
185  size_t bufferCount() const;
186 
187 
188  // defined by yarp::os::SizedWriter
189  PortReader* getReplyHandler() override;
190 
195  void addToHeader();
196 
197 
198  // defined by yarp::os::SizedWriter
199  yarp::os::Portable* getReference() override;
200 
201 
202  // defined by yarp::os::ConnectionWriter
203  void setReference(yarp::os::Portable* obj) override;
204  bool isValid() const override;
205  bool isActive() const override;
206  bool isError() const override;
207  void requestDrop() override;
208 
209  // defined by yarp::os::SizedWriter
210  bool dropRequested() override;
211  void startWrite() const override;
212  void stopWrite() const override;
213 
214  // defined by yarp::os::ConnectionWriter
215  SizedWriter* getBuffer() const override;
216 
223  void setInitialPoolSize(size_t size);
224 
225 
229  std::string toString() const;
230 
231 
232 private:
246  bool applyConvertTextMode();
247  bool applyConvertTextMode() const;
248 
249 
250  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::vector<yarp::os::ManagedBytes*>) lst;
251  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::vector<yarp::os::ManagedBytes*>) header;
252  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::vector<yarp::os::ManagedBytes*>*) target;
253  yarp::os::ManagedBytes* pool;
254  size_t poolIndex;
255  size_t poolCount;
256  size_t poolLength;
257  yarp::os::PortReader* reader;
258  bool textMode;
259  bool bareMode;
260  bool convertTextModePending;
261  yarp::os::Portable* ref;
262  bool shouldDrop;
263  size_t lst_used;
264  size_t header_used;
265  size_t* target_used;
266  size_t initialPoolSize;
267 };
268 
269 
270 } // namespace impl
271 } // namespace os
272 } // namespace yarp
273 
274 #endif // YARP_OS_IMPL_BUFFEREDCONNECTIONWRITER_H
A simple abstraction for a block of bytes.
Definition: Bytes.h:25
An interface for writing to a network connection.
An abstraction for a block of bytes, with optional responsibility for allocating/destroying that bloc...
Definition: ManagedBytes.h:22
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:22
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:25
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:26
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:33
A helper for creating cached object descriptions.
std::string toString(const T &value)
convert an arbitrary type to string.
double float64_t
Definition: numeric.h:77
float float32_t
Definition: numeric.h:76
bool isValid()
Check if time is valid (non-zero).
Definition: Time.cpp:314
constexpr size_t BUFFERED_CONNECTION_INITIAL_POOL_SIZE
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:1098
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(x)
Suppress MSVC C4251 warning for the declaration.
Definition: system.h:336
#define YARP_os_impl_API
Definition: api.h:46