YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
ShmemHybridStream.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 "ShmemHybridStream.h"
8#include "ShmemLogComponent.h"
9
10#include <yarp/os/Log.h>
11
12namespace {
13yarp::os::Contact toContact(bool ok, const std::string& host, int port)
14{
15 return ok ? yarp::os::Contact(host, port) : yarp::os::Contact();
16}
17} // namespace
18
20 m_bLinked(false)
21{
22}
23
28
29int ShmemHybridStream::open(const yarp::os::Contact& yarp_address, bool sender)
30{
31 m_bLinked = false;
32
33 if (sender) {
34 return connect(yarp_address);
35 }
36
37 // The receiver listens on any interface
38 if (!m_Acceptor.listen(yarp_address.getPort())) {
39 yCError(SHMEMCARRIER, "ShmemHybridStream can't listen on port %d", yarp_address.getPort());
40 return -1;
41 }
42
43 std::string host;
44 int port = 0;
45 bool ok = m_Acceptor.getLocalAddress(host, port);
46 m_LocalAddress = toContact(ok, host, port);
47 m_RemoteAddress = m_LocalAddress; // finalized in call to accept()
48
49 return 0;
50}
51
53{
54 if (m_bLinked) {
55 return -1;
56 }
57
58 bool ok = m_Acceptor.accept(m_Socket);
59 // Only one connection is accepted
60 m_Acceptor.close();
61
62 if (!ok) {
63 yCError(SHMEMCARRIER, "ShmemHybridStream accept failed");
64 close();
65 return -1;
66 }
67
68 std::string host;
69 int port = 0;
70 ok = m_Socket.getLocalAddress(host, port);
71 m_LocalAddress = toContact(ok, host, port);
72 ok = m_Socket.getRemoteAddress(host, port);
73 m_RemoteAddress = toContact(ok, host, port);
74
75 ShmemPacket_t recv_conn_data;
76 if (!m_Socket.recvAll(&recv_conn_data, sizeof recv_conn_data)) {
77 yCError(SHMEMCARRIER, "ShmemHybridStream socket reading error");
78 close();
79 return -1;
80 }
81
82 if (!in.open(m_RemoteAddress.getPort(), &m_Socket)) {
83 yCError(SHMEMCARRIER, "ShmemHybridStream can't open shared memory");
84 close();
85 return -1;
86 }
87
88 if (!out.open(m_LocalAddress.getPort())) {
89 yCError(SHMEMCARRIER, "ShmemHybridStream can't create shared memory");
90 close();
91 return -1;
92 }
93
94 ShmemPacket_t send_conn_data;
95 send_conn_data.command = ACKNOWLEDGE;
96 send_conn_data.size = 0;
97 if (!m_Socket.sendAll(&send_conn_data, sizeof send_conn_data)) {
98 yCError(SHMEMCARRIER, "ShmemHybridStream socket writing error");
99 close();
100 return -1;
101 }
102
103 m_bLinked = true;
104
105 m_Socket.setNonBlocking();
106
107 return 0;
108}
109
110int ShmemHybridStream::connect(const yarp::os::Contact& address)
111{
112 if (m_bLinked) {
113 return -1;
114 }
115
116 if (!m_Socket.connect(address.getHost(), address.getPort())) {
117 yCError(SHMEMCARRIER, "ShmemHybridStream can't connect to %s:%d", address.getHost().c_str(), address.getPort());
118 close();
119 return -1;
120 }
121
122 std::string host;
123 int port = 0;
124 bool ok = m_Socket.getLocalAddress(host, port);
125 m_LocalAddress = toContact(ok, host, port);
126 ok = m_Socket.getRemoteAddress(host, port);
127 m_RemoteAddress = toContact(ok, host, port);
128
129 if (!out.open(m_LocalAddress.getPort())) {
130 yCError(SHMEMCARRIER, "ShmemHybridStream can't create shared memory");
131 close();
132 return -1;
133 }
134
135 ShmemPacket_t send_conn_data;
136 send_conn_data.command = CONNECT;
137 send_conn_data.size = SHMEM_DEFAULT_SIZE;
138 if (!m_Socket.sendAll(&send_conn_data, sizeof send_conn_data)) {
139 yCError(SHMEMCARRIER, "ShmemHybridStream socket writing error");
140 close();
141 return -1;
142 }
143
144 ShmemPacket_t recv_conn_data;
145 if (!m_Socket.recvAll(&recv_conn_data, sizeof recv_conn_data)) {
146 yCError(SHMEMCARRIER, "ShmemHybridStream socket reading error");
147 close();
148 return -1;
149 }
150
151 if (!in.open(m_RemoteAddress.getPort(), &m_Socket)) {
152 yCError(SHMEMCARRIER, "ShmemHybridStream can't open shared memory");
153 close();
154 return -1;
155 }
156
157 m_bLinked = true;
158
159 m_Socket.setNonBlocking();
160
161 return 0;
162}
163
165{
166 m_bLinked = false;
167 in.close();
168 out.close();
169 m_Socket.close();
170 m_Acceptor.close();
171}
172
174{
175 yCDebug(SHMEMCARRIER, "INTERRUPT");
176 close();
177}
178
180{
181 if (!out.write(b)) {
182 close();
183 }
184}
185
187{
188 yarp::conf::ssize_t ret = in.read(b);
189 if (ret == -1) {
190 close();
191 }
192 return ret;
193}
194
199
204
206{
207 return m_bLinked && in.isOk() && out.isOk();
208}
209
211{
212 yCDebug(SHMEMCARRIER, "RECEIVED RESET COMMAND");
213 close();
214}
215
219
223
225{
226 return m_LocalAddress;
227}
228
230{
231 return m_RemoteAddress;
232}
const yarp::os::LogComponent & SHMEMCARRIER()
#define SHMEM_DEFAULT_SIZE
Definition ShmemTypes.h:10
void write(const yarp::os::Bytes &b) override
Write a block of bytes to the stream.
void reset() override
Reset the stream.
yarp::os::OutputStream & getOutputStream() override
Get an OutputStream to write to.
void interrupt() override
Interrupt the stream.
int open(const yarp::os::Contact &yarp_address, bool sender)
bool isOk() const override
Check if the stream is ok or in an error state.
const yarp::os::Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
void beginPacket() override
Mark the beginning of a logical packet.
const yarp::os::Contact & getLocalAddress() const override
Get the address of the local side of the stream.
void endPacket() override
Mark the end of a logical packet (see beginPacket).
yarp::os::InputStream & getInputStream() override
Get an InputStream to read from.
void close() override
Terminate the stream.
bool open(int port, shmem::Socket *pSock)
yarp::conf::ssize_t read(yarp::os::Bytes &b)
bool write(const yarp::os::Bytes &b)
bool open(int port, int size=4096)
bool recvAll(void *data, size_t len)
Receive exactly len bytes (blocking socket)
bool accept(Socket &stream)
Accept a connection on a listening socket.
bool connect(const std::string &host, int port)
bool listen(int port)
Listen on all the interfaces (port 0 means any free port)
bool sendAll(const void *data, size_t len)
Send exactly len bytes (blocking socket)
bool getRemoteAddress(std::string &host, int &port) const
bool getLocalAddress(std::string &host, int &port) const
A simple abstraction for a block of bytes.
Definition Bytes.h:24
Represents how to reach a part of a YARP network.
Definition Contact.h:33
int getPort() const
Get the port number associated with this Contact for socket communication.
Definition Contact.cpp:239
std::string getHost() const
Get the host name associated with this Contact for socket communication.
Definition Contact.cpp:228
Simple specification of the minimum functions needed from input streams.
Definition InputStream.h:25
virtual int read()
Read and return a single byte.
Simple specification of the minimum functions needed from output streams.
#define yCError(component,...)
#define yCDebug(component,...)
::ssize_t ssize_t
Definition numeric.h:87
The main, catch-all namespace for YARP.
Definition dirs.h:16