YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
StreamConnectionReader.cpp
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#include <yarp/os/Bottle.h>
8#include <yarp/os/Log.h>
9#include <yarp/os/NetType.h>
13
14using namespace yarp::os::impl;
15using namespace yarp::os;
16
19 writer(nullptr),
20 in(nullptr),
21 str(nullptr),
22 protocol(nullptr),
23 messageLen(0),
24 textMode(false),
25 bareMode(false),
26 valid(false),
27 err(false),
28 shouldDrop(false),
29 writePending(false),
30 ref(nullptr),
31 convertedTextMode(false),
32 pushedIntFlag(false),
33 pushedInt(-1),
34 parentConnectionReader(nullptr)
35{
36}
37
39{
40 if (writer != nullptr) {
41 delete writer;
42 writer = nullptr;
43 }
44}
45
47 TwoWayStream* str,
48 const Route& route,
49 size_t len,
50 bool textMode,
51 bool bareMode)
52{
53 this->in = &in;
54 this->str = str;
55 this->route = route;
56 this->messageLen = len;
57 this->textMode = textMode;
58 this->bareMode = bareMode;
59 this->valid = true;
60 ref = nullptr;
61 err = false;
62 convertedTextMode = false;
63 pushedIntFlag = false;
64}
65
67{
68 this->protocol = protocol;
69}
70
72{
73 str = nullptr;
74}
75
77{
78 return shouldDrop;
79}
80
82{
83 if (!isGood()) {
84 return false;
85 }
86 yAssert(in != nullptr);
87 size_t len = b.length();
88 if (len == 0) {
89 return true;
90 }
91 //if (len<0) len = messageLen;
92 if (len > 0) {
94 if (rlen >= 0) {
95 messageLen -= len;
96 return true;
97 }
98 }
99 err = true;
100 return false;
101}
102
104{
105 if (!isGood()) {
106 return {};
107 }
108 yAssert(in != nullptr);
109 bool success = false;
110 std::string result = in->readLine('\n', &success);
111 if (!success) {
112 err = true;
113 return {};
114 }
115 messageLen -= result.length() + 1;
116 return result;
117}
118
120{
121 if (writer != nullptr) {
122 if (writePending) {
123 if (str != nullptr) {
124 if (protocol != nullptr) {
125 protocol->reply(*writer);
126 } else {
127 writer->write(str->getOutputStream());
128 }
129 writer->clear();
130 }
131 }
132 }
133 writePending = false;
134}
135
140
142{
143 reset(*in, str, route, len, textMode, bareMode);
144 return true;
145}
146
148{
149 return messageLen + (pushedIntFlag ? sizeof(yarp::os::NetInt32) : 0);
150}
151
153{
154 if (pushedIntFlag) {
155 return false;
156 }
157 pushedIntFlag = true;
158 pushedInt = x;
159 return true;
160}
161
162template <typename T, typename NetT>
163inline T StreamConnectionReader::expectType()
164{
165 yAssert(in != nullptr);
166
167 NetT x = 0;
168 yarp::os::Bytes b(reinterpret_cast<char*>(&x), sizeof(T));
169 yarp::conf::ssize_t r = in->read(b);
170 if (r < 0 || static_cast<size_t>(r) < b.length()) {
171 err = true;
172 return 0;
173 }
174 messageLen -= b.length();
175
176 return static_cast<T>(x);
177}
178
180{
181 if (!isGood()) {
182 return 0;
183 }
185}
186
188{
189 if (!isGood()) {
190 return 0;
191 }
193}
194
196{
197 if (pushedIntFlag) {
198 pushedIntFlag = false;
199 return pushedInt;
200 }
201 if (!isGood()) {
202 return 0;
203 }
205}
206
208{
209 if (!isGood()) {
210 return 0;
211 }
213}
214
222
230
231bool StreamConnectionReader::expectBlock(char* data, size_t len)
232{
233 yarp::os::Bytes bytes(data, len);
234 return expectBlock(bytes);
235}
236
238{
239 if (!isGood()) {
240 return {};
241 }
242 yAssert(in != nullptr);
243 bool lsuccess = false;
244 std::string result = in->readLine(terminatingChar, &lsuccess);
245 if (lsuccess) {
246 messageLen -= result.length() + 1;
247 }
248 return result;
249}
250
252{
253 return textMode;
254}
255
257{
258 return bareMode;
259}
260
262{
263 if (isTextMode()) {
264 if (!convertedTextMode) {
265 Bottle bot;
266 bot.read(*this);
268 bot.write(writer);
269 std::string s = writer.toString();
270 altStream.reset(s);
271 in = &altStream;
272 convertedTextMode = true;
273 }
274 }
275
276 return true;
277}
278
280{
281 if (str == nullptr) {
282 return nullptr;
283 }
284 if (writer == nullptr) {
286 yAssert(writer != nullptr);
287 }
288 writer->clear();
289 writePending = true;
290 if (protocol != nullptr) {
291 protocol->willReply();
292 }
293 return writer;
294}
295
297{
298 if (str != nullptr) {
299 Contact remote = str->getRemoteAddress();
300 remote.setName(route.getFromName());
301 return remote;
302 }
303 Contact remote = yarp::os::Contact(route.getFromName(), route.getCarrierName());
304 return remote;
305}
306
308{
309 if (str != nullptr) {
310 Contact local = str->getLocalAddress();
311 local.setName(route.getToName());
312 return local;
313 }
314 return yarp::os::Contact();
315}
316
318{
319 return valid;
320}
321
323{
324 if (err) {
325 return true;
326 }
327 return !isActive();
328}
329
331{
332 if (shouldDrop) {
333 return false;
334 }
335 if (!isValid()) {
336 return false;
337 }
338 if (in != nullptr) {
339 if (in->isOk()) {
340 return true;
341 }
342 }
343 return false;
344}
345
350
352{
353 if (protocol != nullptr) {
354 const std::string& env = protocol->getEnvelope();
355 return {const_cast<char*>(env.c_str()), env.length()};
356 }
357 if (parentConnectionReader != nullptr) {
358 return parentConnectionReader->readEnvelope();
359 }
360 return {nullptr, 0};
361}
362
364{
365 shouldDrop = true;
366}
367
369{
370 if (config.size() == 0) {
371 if (protocol != nullptr) {
372 const_cast<Bottle&>(config).fromString(protocol->getSenderSpecifier());
373 }
374 }
375 return config;
376}
377
379{
380 this->parentConnectionReader = parentConnectionReader;
381}
382
383bool StreamConnectionReader::isGood()
384{
385 return isActive() && isValid() && !isError();
386}
#define yAssert(x)
Definition Log.h:388
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:65
size_type size() const
Gets the number of elements in the bottle.
Definition Bottle.cpp:257
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition Bottle.cpp:246
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
Definition Bottle.cpp:236
A mini-server for performing network communication in the background.
A simple abstraction for a block of bytes.
Definition Bytes.h:24
size_t length() const
Definition Bytes.cpp:22
An interface for reading from a network connection.
virtual Bytes readEnvelope()
Read a message envelope, if available.
An interface for writing to a network connection.
Represents how to reach a part of a YARP network.
Definition Contact.h:33
void setName(const std::string &name)
Set the name associated with this Contact.
Definition Contact.cpp:222
Simple specification of the minimum functions needed from input streams.
Definition InputStream.h:25
virtual bool isOk() const =0
Check if the stream is ok or in an error state.
virtual int read()
Read and return a single byte.
yarp::conf::ssize_t readFull(Bytes &b)
Keep reading until buffer is full.
std::string readLine(const char terminal='\n', bool *success=nullptr)
Read a block of text terminated with a specific marker (or EOF).
This is a base class for objects that can be both read from and be written to the YARP network.
Definition Portable.h:25
Information about a connection between two ports.
Definition Route.h:28
const std::string & getToName() const
Get the destination of the route.
Definition Route.cpp:103
const std::string & getCarrierName() const
Get the carrier type of the route.
Definition Route.cpp:123
const std::string & getFromName() const
Get the source of the route.
Definition Route.cpp:93
A base class for nested structures that can be searched.
Definition Searchable.h:31
A stream which can be asked to perform bidirectional communication.
virtual const Contact & getRemoteAddress() const =0
Get the address of the remote side of the stream.
virtual const Contact & getLocalAddress() const =0
Get the address of the local side of the stream.
virtual OutputStream & getOutputStream()=0
Get an OutputStream to write to.
A helper for creating cached object descriptions.
bool write(ConnectionWriter &connection) const override
Write this object to a network connection.
void clear() override
Clear all cached data.
Connection choreographer.
Definition Protocol.h:29
std::string getSenderSpecifier() const override
Extract a name for the sender, if the connection type supports that.
Definition Protocol.cpp:149
const std::string & getEnvelope() const override
Read the envelope associated with the current message.
Definition Protocol.cpp:183
void reply(SizedWriter &writer) override
Reply to a message we have just read.
Definition Protocol.cpp:325
void willReply()
Promise that we'll be making a reply.
Definition Protocol.h:88
std::int64_t expectInt64() override
Read a 64-bit integer from the network connection.
yarp::os::Contact getRemoteContact() const override
Gets information about who is supplying the data being read, if that information is available.
bool convertTextMode() override
Reads in a standard description in text mode, and converts it to a standard description in binary.
void setParentConnectionReader(ConnectionReader *parentConnectionReader) override
Set ConnectionReader to be used for reading the envelope.
yarp::os::ConnectionWriter * getWriter() override
Gets a way to reply to the message, if possible.
bool pushInt(int x) override
Store an integer to return on the next call to expectInt()
bool isBareMode() const override
Check if the connection is bare mode.
std::int16_t expectInt16() override
Read a 16-bit integer from the network connection.
yarp::conf::float64_t expectFloat64() override
Read a 64-bit floating point number from the network connection.
yarp::os::Contact getLocalContact() const override
Gets information about who is receiving the data, if that information is available.
const yarp::os::Searchable & getConnectionModifiers() const override
Access modifiers associated with the connection, if any.
size_t getSize() const override
Checks how much data is available.
std::string expectText(const char terminatingChar) override
Read some text from the network connection.
std::int8_t expectInt8() override
Read a 8-bit integer from the network connection.
yarp::conf::float32_t expectFloat32() override
Read a 32-bit floating point number from the network connection.
yarp::os::Bytes readEnvelope() override
Read a message envelope, if available.
void reset(yarp::os::InputStream &in, TwoWayStream *str, const Route &route, size_t len, bool textMode, bool bareMode=false)
virtual bool expectBlock(yarp::os::Bytes &b)
bool isTextMode() const override
Check if the connection is text mode.
yarp::os::Portable * getReference() const override
Get a direct pointer to the object being sent, if possible.
virtual void setReference(yarp::os::Portable *obj)
std::int32_t expectInt32() override
Read a 32-bit integer from the network connection.
void requestDrop() override
Tag the connection to be dropped after the current message.
::ssize_t ssize_t
Definition numeric.h:87
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.
Definition OSTestMsg.cpp:13
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition NetInt32.h:29