YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
TcpFace.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-FileCopyrightText: 2010 Daniel Krieg <krieg@fias.uni-frankfurt.de>
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
9
14
15#include <cstdio>
16
17using namespace yarp::os::impl;
18using namespace yarp::os;
19
20namespace {
21YARP_OS_LOG_COMPONENT(TCPFACE, "yarp.os.impl.TcpFace")
22} // namespace
23
24
25TcpFace::TcpFace() = default;
26
28{
29 closeFace();
30}
31
32
33bool TcpFace::open(const Contact& address)
34{
35 yCDebug(TCPFACE, "opening for address %s", address.toURI().c_str());
36
37 this->address = address;
38#ifdef YARP_HAS_ACE
39 ACE_INET_Addr serverAddr((address.getPort() > 0) ? address.getPort() : 0);
40 int result = peerAcceptor.open(serverAddr, 1);
41 if (address.getPort() <= 0) {
43 peerAcceptor.get_local_addr(localAddr);
44 this->address = address;
45 this->address.setSocket("tcp",
47 localAddr.get_port_number());
48 }
49#else
50 int result = peerAcceptor.open(address);
51 if (address.getPort() <= 0) {
52 this->address = address;
53 this->address.setSocket("tcp",
55 peerAcceptor.get_port_number());
56 }
57#endif
58 yCDebug(TCPFACE, "Opened at address %s", this->address.toURI().c_str());
59
60 return result != -1;
61}
62
64{
65 closeFace();
66}
67
68void TcpFace::closeFace()
69{
70 if (address.isValid()) {
71 peerAcceptor.close();
72
73 address = Contact();
74 }
75}
76
77static void showError()
78{
79 yCError(TCPFACE, "Authentication failed.");
80 yCError(TCPFACE, "Authentication was enabled in the auth.conf file.");
81 yCError(TCPFACE, "If you do not want to use authentication, please");
82 yCError(TCPFACE, "remove this file.");
83 yCError(TCPFACE, "If you do want to set up authentication, check:");
84 yCError(TCPFACE, " http://www.yarp.it/yarp_port_auth.html");
85}
86
91{
92 auto* stream = new SocketTwoWayStream();
93 yCAssert(TCPFACE, stream != nullptr);
94
95 int result = stream->open(peerAcceptor);
96 if (result < 0) {
97 //printf("exception on tcp stream read: %s\n", e.toString().c_str());
98 stream->close();
99 delete stream;
100 stream = nullptr;
101 }
102
103 if (stream != nullptr) {
104 stream->setReadTimeout(2.0);
105 stream->setWriteTimeout(2.0);
106
107 bool success = auth.authSource(&(stream->getInputStream()), &(stream->getOutputStream()));
108 if (!success) {
109 showError();
110 return nullptr;
111 }
112 stream->setReadTimeout(0.);
113 stream->setWriteTimeout(0.);
114
115 return new Protocol(stream);
116 }
117 return nullptr;
118}
119
121{
122 auto* stream = new SocketTwoWayStream();
123 int result = stream->open(address);
124 if (result < 0) {
125 stream->close();
126 delete stream;
127 return nullptr;
128 }
129
130 if (stream != nullptr) {
131 stream->setReadTimeout(2.0);
132 stream->setWriteTimeout(2.0);
133
134 bool success = auth.authDest(&(stream->getInputStream()), &(stream->getOutputStream()));
135 if (!success) {
136 showError();
137 return nullptr;
138 }
139 stream->setReadTimeout(0.);
140 stream->setWriteTimeout(0.);
141
142 return new Protocol(stream);
143 }
144 return nullptr;
145}
146
147
149{
150 return address;
151}
static void showError()
Definition TcpFace.cpp:77
A mini-server for performing network communication in the background.
Represents how to reach a part of a YARP network.
Definition Contact.h:33
bool isValid() const
Checks if a Contact is tagged as valid.
Definition Contact.cpp:298
std::string toURI(bool includeCarrier=true) const
Get a representation of the Contact as a URI.
Definition Contact.cpp:313
int getPort() const
Get the port number associated with this Contact for socket communication.
Definition Contact.cpp:239
void setSocket(const std::string &carrier, const std::string &hostname, int port)
Set information to a Contact about how to reach it using socket communication.
Definition Contact.cpp:288
The input side of an active connection between two ports.
The output side of an active connection between two ports.
bool authDest(yarp::os::InputStream *streamIn, yarp::os::OutputStream *streamOut)
Definition AuthHMAC.cpp:159
bool authSource(yarp::os::InputStream *streamIn, yarp::os::OutputStream *streamOut)
Definition AuthHMAC.cpp:100
static std::string getHostName(bool prefer_loopback=false, const std::string &seed="")
Connection choreographer.
Definition Protocol.h:29
A stream abstraction for socket communication.
InputProtocol * read() override
return nullptr on failure.
Definition TcpFace.cpp:90
Contact getLocalAddress() const override
Get address after open(), if more specific that the address provided to open() - otherwise an invalid...
Definition TcpFace.cpp:148
bool open(const Contact &address) override
Start listening to the given address.
Definition TcpFace.cpp:33
void close() override
Stop listening.
Definition TcpFace.cpp:63
yarp::os::impl::AuthHMAC auth
Definition TcpFace.h:41
OutputProtocol * write(const Contact &address) override
Try to reach out and talk to someone.
Definition TcpFace.cpp:120
#define yCError(component,...)
#define yCAssert(component, x)
#define yCDebug(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.