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