YARP
Yet Another Robot Platform
BufferedConnectionWriter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef YARP_OS_IMPL_BUFFEREDCONNECTIONWRITER_H
11 #define YARP_OS_IMPL_BUFFEREDCONNECTIONWRITER_H
12 
14 #include <yarp/os/SizedWriter.h>
15 
16 #include <string>
17 #include <vector>
18 
19 
20 namespace yarp {
21 namespace os {
22 
23 class Bytes;
24 class ManagedBytes;
25 
26 namespace impl {
27 
28 /*
29  * When allocating space to store serialized data, we start off with
30  * a block of this size. It will be resized as necessary.
31  * Data can optionally have a header, serialized separately.
32  */
33 constexpr size_t BUFFERED_CONNECTION_INITIAL_POOL_SIZE = 1024;
34 
50 {
51 public:
64  BufferedConnectionWriter(bool textMode = false,
65  bool bareMode = false);
66 
70  ~BufferedConnectionWriter() override;
71 
77  void reset(bool textMode);
78 
86  void restart();
87 
91  void clear() override;
92 
103  bool addPool(const yarp::os::Bytes& data);
104 
110  void stopPool();
111 
122  void push(const Bytes& data, bool copy);
123 
124  // defined by yarp::os::ConnectionWriter
125  bool isTextMode() const override;
126  bool isBareMode() const override;
127  bool convertTextMode() override;
128  void declareSizes(int argc, int* argv) override; // FIXME Remove?
129  void setReplyHandler(PortReader& reader) override;
130  void appendInt8(std::int8_t data) override;
131  void appendInt16(std::int16_t data) override;
132  void appendInt32(std::int32_t data) override;
133  void appendInt64(std::int64_t data) override;
134  void appendFloat32(yarp::conf::float32_t data) override;
135  void appendFloat64(yarp::conf::float64_t data) override;
136  void appendBlock(const char* data, size_t len) override;
137  void appendText(const std::string& str, const char terminate = '\n') override;
138  void appendExternalBlock(const char* data, size_t len) override;
139 
147  virtual void appendBlock(const yarp::os::Bytes& data);
148 
154  virtual void appendBlockCopy(const Bytes& data);
155 
163  virtual void appendLine(const std::string& data);
164 
165 
166  // defined by yarp::os::SizedWriter
167  size_t length() const override;
168  size_t headerLength() const override;
169  size_t length(size_t index) const override;
170  const char* data(size_t index) const override;
171  bool write(ConnectionWriter& connection) const override;
172  void write(OutputStream& os) override;
173 
180  bool write(PortReader& obj);
181 
186  virtual size_t dataSize() const;
187 
188  size_t bufferCount() const;
189 
190 
191  // defined by yarp::os::SizedWriter
192  PortReader* getReplyHandler() override;
193 
198  void addToHeader();
199 
200 
201  // defined by yarp::os::SizedWriter
202  yarp::os::Portable* getReference() override;
203 
204 
205  // defined by yarp::os::ConnectionWriter
206  void setReference(yarp::os::Portable* obj) override;
207  bool isValid() const override;
208  bool isActive() const override;
209  bool isError() const override;
210  void requestDrop() override;
211 
212  // defined by yarp::os::SizedWriter
213  bool dropRequested() override;
214  void startWrite() const override;
215  void stopWrite() const override;
216 
217  // defined by yarp::os::ConnectionWriter
218  SizedWriter* getBuffer() const override;
219 
226  void setInitialPoolSize(size_t size);
227 
228 
232  std::string toString() const;
233 
234 
235 private:
249  bool applyConvertTextMode();
250  bool applyConvertTextMode() const;
251 
252 
253  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::vector<yarp::os::ManagedBytes*>) lst;
254  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::vector<yarp::os::ManagedBytes*>) header;
255  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::vector<yarp::os::ManagedBytes*>*) target;
256  yarp::os::ManagedBytes* pool;
257  size_t poolIndex;
258  size_t poolCount;
259  size_t poolLength;
260  yarp::os::PortReader* reader;
261  bool textMode;
262  bool bareMode;
263  bool convertTextModePending;
264  yarp::os::Portable* ref;
265  bool shouldDrop;
266  size_t lst_used;
267  size_t header_used;
268  size_t* target_used;
269  size_t initialPoolSize;
270 };
271 
272 
273 } // namespace impl
274 } // namespace os
275 } // namespace yarp
276 
277 #endif // YARP_OS_IMPL_BUFFEREDCONNECTIONWRITER_H
yarp::os::Portable
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:29
yarp::os::impl::BUFFERED_CONNECTION_INITIAL_POOL_SIZE
constexpr size_t BUFFERED_CONNECTION_INITIAL_POOL_SIZE
Definition: BufferedConnectionWriter.h:33
ConnectionWriter.h
yarp::os::Time::isValid
bool isValid()
Check if time is valid (non-zero).
Definition: Time.cpp:317
yarp::os::OutputStream
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:25
YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(x)
Suppress MSVC C4251 warning for the declaration.
Definition: system.h:339
yarp::os::impl::BufferedConnectionWriter
A helper for creating cached object descriptions.
Definition: BufferedConnectionWriter.h:50
yarp::os::ManagedBytes
An abstraction for a block of bytes, with optional responsibility for allocating/destroying that bloc...
Definition: ManagedBytes.h:25
yarp::os::PortReader
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:28
yarp::os::ConnectionWriter
An interface for writing to a network connection.
Definition: ConnectionWriter.h:40
yarp::os::Bytes
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
yarp::conf::float32_t
float float32_t
Definition: numeric.h:50
toString
std::string toString(const T &value)
convert an arbitrary type to string.
Definition: fakeMotionControl.cpp:121
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
YARP_os_impl_API
#define YARP_os_impl_API
Definition: api.h:45
yarp::conf::float64_t
double float64_t
Definition: numeric.h:51
yarp::sig::file::write
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:971
SizedWriter.h
yarp::os::SizedWriter
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:36