YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
ShmemInputStream.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 "ShmemInputStream.h"
8#include "ShmemLogComponent.h"
9
10#include <yarp/os/Log.h>
11
12#include <cstring>
13
14
16 m_bOpen(false),
17 m_bInterrupt(false),
18 m_ResizeNum(0),
19 m_Port(-1),
20 m_pData(nullptr),
21 m_pHeader(nullptr),
22 m_pSock(nullptr)
23{
24}
25
30
32{
33 return m_bOpen;
34}
35
37{
38 std::lock_guard<std::recursive_mutex> l_guard(m_generalMutex);
39
40 m_pSock = pSock;
41 m_bInterrupt = false;
42
43 m_pData = nullptr;
44 m_pHeader = nullptr;
45 m_ResizeNum = 0;
46
47 m_Port = port;
48
49 // The objects are created by the writer before the handshake. Once they
50 // are opened by the reader, their names are no longer needed, and they
51 // are removed, so that nothing is left behind if a process crashes.
52 const std::string data_name = shmem::objectName("data", port, 0);
53 auto map = std::make_unique<shmem::SharedMemory>();
54 if (!map->open(data_name)) {
55 yCError(SHMEMCARRIER, "ShmemInputStream can't open shared memory");
56 return false;
57 }
59
60 const std::string mtx_name = shmem::objectName("mtx", port);
61 const std::string sem_name = shmem::objectName("sem", port);
62 if (!m_AccessMutex.open(mtx_name) || !m_WaitDataSem.open(sem_name)) {
63 yCError(SHMEMCARRIER, "ShmemInputStream can't open semaphores");
66 return false;
67 }
70
71 m_pMap = std::move(map);
72 m_pHeader = static_cast<ShmemHeader_t*>(m_pMap->data());
73 m_pData = reinterpret_cast<char*>(m_pHeader + 1);
74
76 m_pHeader->attached = true;
78
79 m_bOpen = true;
80
81 return true;
82}
83
85{
86 std::lock_guard<std::recursive_mutex> l_guard(m_generalMutex);
87
88 yCDebug(SHMEMCARRIER, "input stream resize %d to %d", m_ResizeNum + 1, m_pHeader->newsize);
89
90 const std::string data_name = shmem::objectName("data", m_Port, m_ResizeNum + 1);
91 auto pNewMap = std::make_unique<shmem::SharedMemory>();
92 if (!pNewMap->open(data_name)) {
93 yCError(SHMEMCARRIER, "ShmemInputStream can't open shared memory");
94 return false;
95 }
98
99 m_pMap = std::move(pNewMap);
100 m_pHeader = static_cast<ShmemHeader_t*>(m_pMap->data());
101 m_pData = reinterpret_cast<char*>(m_pHeader + 1);
102
103 // Tell the writer that the previous segments can be released
104 m_pHeader->attached = true;
105
106 return true;
107}
108
109int ShmemInputStreamImpl::read(char* data, int len)
110{
111 std::lock_guard<std::recursive_mutex> l_guard(m_generalMutex);
112
113 if (!m_AccessMutex.wait()) {
114 return -1;
115 }
116
117 if (m_pHeader->close) {
119 close();
120 return -1;
121 }
122
123 while (m_pHeader->resize) {
124 if (!Resize()) {
126 close();
127 return -1;
128 }
129 }
130
131 if (m_pHeader->avail < len) {
134 return 0;
135 }
136
137 if (m_pHeader->tail + len > m_pHeader->size) {
138 int first_block_size = m_pHeader->size - m_pHeader->tail;
139
140 memcpy((void*)data, (void*)(m_pData + m_pHeader->tail), first_block_size);
141 memcpy((void*)(data + first_block_size), (void*)m_pData, len - first_block_size);
142 } else {
143 memcpy((void*)data, (void*)(m_pData + m_pHeader->tail), len);
144 }
145
146 m_pHeader->avail -= len;
147 m_pHeader->tail += len;
149
151
152 return len;
153}
154
156{
157 std::lock_guard<std::recursive_mutex> l_guard(m_generalMutex);
158 std::lock_guard<std::mutex> l_serializer(m_ReadSerializerMutex);
159
160 if (!m_bOpen) {
161 return -1;
162 }
163
164 char* data = b.get();
165 size_t len = b.length();
166 if (len == 0) {
167 return 0;
168 }
169
171 while (!(ret = read(data, (int)len))) {
173
174 if (m_bInterrupt) {
175 return -1;
176 }
177
178 // The socket is used only to detect if the other side closed the
179 // connection or died
180 if (m_pSock->isPeerClosed()) {
181 yCDebug(SHMEMCARRIER, "STREAM IS BROKEN");
182 close();
183 return -1;
184 }
185 }
186
187 return ret;
188}
189
191{
192 // Make a pending read give up, otherwise it would hold the lock until new
193 // data is available
194 m_bInterrupt = true;
195
196 std::lock_guard<std::recursive_mutex> l_guard(m_generalMutex);
197
198 if (!m_bOpen) {
199 return;
200 }
201
202 m_bOpen = false;
203
205 while (m_pHeader->waiting > 0) {
208 }
209 m_pHeader->close = true;
211
214
215 m_pMap.reset();
216 m_pHeader = nullptr;
217 m_pData = nullptr;
218}
const yarp::os::LogComponent & SHMEMCARRIER()
std::atomic< bool > m_bInterrupt
bool open(int port, shmem::Socket *pSock)
shmem::Socket * m_pSock
std::recursive_mutex m_generalMutex
yarp::conf::ssize_t read(yarp::os::Bytes &b)
ShmemHeader_t * m_pHeader
shmem::NamedSemaphore m_AccessMutex
shmem::NamedSemaphore m_WaitDataSem
std::unique_ptr< shmem::SharedMemory > m_pMap
std::mutex m_ReadSerializerMutex
bool waitFor(double timeout)
Wait at most timeout seconds. Returns true if the semaphore was acquired.
bool wait()
Wait until the semaphore is available.
static void unlink(const std::string &name)
Remove the name of the semaphore (no-op on Windows)
bool open(const std::string &name)
Open an existing semaphore.
static void unlink(const std::string &name)
Remove the name of the segment (no-op on Windows)
A minimal IPv4 TCP socket.
bool isPeerClosed()
Check, on a non-blocking socket, whether the connection was closed or reset by the peer (e....
A simple abstraction for a block of bytes.
Definition Bytes.h:24
size_t length() const
Definition Bytes.cpp:22
const char * get() const
Definition Bytes.cpp:27
#define yCError(component,...)
#define yCDebug(component,...)
std::string objectName(const char *kind, int port, int index=-1)
Build the name of a named inter-process object.
::ssize_t ssize_t
Definition numeric.h:87