YARP
Yet Another Robot Platform
PortCommand.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 
8 
9 #include <yarp/os/NetType.h>
11 
12 using namespace yarp::os::impl;
13 using namespace yarp::os;
14 
15 
16 namespace {
17 YARP_OS_LOG_COMPONENT(PORTCOMMAND, "yarp.os.impl.PortCommand")
18 } // namespace
19 
21 {
22  yCTrace(PORTCOMMAND, "PortCommand::readBlock");
23  ch = '\0';
24  str = "";
25  if (!reader.isTextMode()) {
26  bool ok = reader.expectBlock(header.get(), header.length());
27  if (!ok) {
28  return false;
29  }
30  char* base = header.get();
31  if (base[4] == '~') {
32  ch = base[5];
33  if (ch == '\0') {
34  //str = reader.expectString(reader.getSize());
35  str = reader.expectText('\0');
36  if (reader.isError()) {
37  return false;
38  }
39  if (str.length() > 0) {
40  ch = str[0];
41  }
42  }
43  } else {
44  return false;
45  }
46  } else {
47  str = reader.expectText();
48  if (reader.isError()) {
49  return false;
50  }
51  if (str.length() > 0) {
52  ch = str[0];
53  }
54  }
55  return true;
56 }
57 
59 {
60  yCTrace(PORTCOMMAND, "PortCommand::writeBlock");
61  yCDebug(PORTCOMMAND, "Writing port command, text mode [%s]\n", writer.isTextMode() ? "true" : "false");
62  if (!writer.isTextMode()) {
63  int len = 0;
64  if (ch == '\0') {
65  len = (int)str.length() + 1;
66  }
67  yCAssert(PORTCOMMAND, header.length() == 8);
68  char* base = header.get();
69  Bytes b(base, 4);
70  NetType::netInt(len, b);
71  base[4] = '~';
72  base[5] = ch;
73  base[6] = 0;
74  base[7] = 1;
75  writer.appendBlock(header.bytes().get(), header.bytes().length());
76  if (ch == '\0') {
77  writer.appendBlock(str.c_str(), str.length() + 1);
78  }
79  } else {
80  if (ch != '\0') {
81  char buf[] = "X";
82  buf[0] = ch;
83  writer.appendText(buf);
84  } else {
85  writer.appendText(str);
86  }
87  }
88  return !writer.isError();
89 }
A simple abstraction for a block of bytes.
Definition: Bytes.h:25
An interface for reading from a network connection.
virtual bool isTextMode() const =0
Check if the connection is text mode.
virtual bool expectBlock(char *data, size_t len)=0
Read a block of data from the network connection.
virtual std::string expectText(const char terminatingChar='\n')=0
Read some text from the network connection.
virtual bool isError() const =0
An interface for writing to a network connection.
virtual bool isError() const =0
virtual bool isTextMode() const =0
Check if the connection is text mode.
virtual void appendText(const std::string &str, const char terminate='\n')=0
Send a terminated string to the network connection.
virtual void appendBlock(const char *data, size_t len)=0
Send a block of data to the network connection.
static int netInt(const yarp::os::Bytes &code)
Definition: NetType.cpp:27
bool write(yarp::os::ConnectionWriter &writer) const override
Write this object to a network connection.
Definition: PortCommand.cpp:58
bool read(yarp::os::ConnectionReader &reader) override
Read this object from a network connection.
Definition: PortCommand.cpp:20
#define yCAssert(component, x)
Definition: LogComponent.h:169
#define yCTrace(component,...)
Definition: LogComponent.h:85
#define yCDebug(component,...)
Definition: LogComponent.h:109
#define YARP_OS_LOG_COMPONENT(name, name_string)
Definition: LogComponent.h:35
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.