15 #ifndef __SHM_PS_LIB_H__
16 #define __SHM_PS_LIB_H__
31 #include <sys/types.h>
62 Publisher(std::string name =
"",
int buffer_num = 3,
PERM perm = DEFAULT_PERM,
bool legacy =
false);
94 Subscriber(std::string name =
"",
bool legacy =
false);
98 bool waitFor(uint64_t timeout_usec);
99 void setDataExpiryTime_us(uint64_t time_us);
102 std::string shm_name;
105 int current_reading_buffer;
106 uint64_t data_expiry_time_us;
131 template <
typename T>
134 , shm_buf_num(buffer_num)
136 , shared_memory(nullptr)
137 , ring_buffer(nullptr)
138 , data_size(sizeof(T))
142 if (!std::is_standard_layout<T>::value)
144 throw std::runtime_error(
"shm::Publisher: Be setted not POD class!");
149 throw std::runtime_error(
"shm::Publisher: Please set name!");
155 shared_memory->connect(RingBuffer::getSize(
sizeof(T), shm_buf_num));
159 shared_memory->connect(
sizeof(T));
161 if (shared_memory->isDisconnected())
163 throw std::runtime_error(
"shm::Publisher: Cannot get memory!");
168 ring_buffer =
new RingBuffer(shared_memory->getPtr(),
sizeof(T), shm_buf_num);
172 data_ptr = shared_memory->getPtr();
183 template <
typename T>
186 if (ring_buffer !=
nullptr)
190 if (shared_memory !=
nullptr)
192 delete shared_memory;
205 template <
typename T>
221 int oldest_buffer = ring_buffer->getOldestBufferNum();
223 (
reinterpret_cast<T *
>(ring_buffer->getDataList()))[oldest_buffer] = data;
226 clock_gettime(CLOCK_MONOTONIC_RAW, &t);
227 ring_buffer->setTimestamp_us(((uint64_t) t.tv_sec * 1000000L) + ((uint64_t) t.tv_nsec / 1000L), oldest_buffer);
229 ring_buffer->signal();
233 memcpy(data_ptr, &data,
sizeof(T));
246 template <
typename T>
249 , shared_memory(nullptr)
250 , ring_buffer(nullptr)
251 , current_reading_buffer(0)
252 , data_expiry_time_us(2000000)
256 if (!std::is_standard_layout<T>::value)
258 throw std::runtime_error(
"shm::Subscriber: Be setted not POD class!");
263 throw std::runtime_error(
"shm::Subscriber: Please set name!");
276 template <
typename T>
279 if (ring_buffer !=
nullptr)
283 if (shared_memory !=
nullptr)
285 delete shared_memory;
299 template <
typename T>
305 if (shared_memory->isDisconnected())
307 if (ring_buffer !=
nullptr)
310 ring_buffer =
nullptr;
312 shared_memory->connect();
313 if (shared_memory->isDisconnected())
318 ring_buffer =
new RingBuffer(shared_memory->getPtr());
319 ring_buffer->setDataExpiryTime_us(data_expiry_time_us);
321 int newest_buffer = ring_buffer->getNewestBufferNum();
322 if (newest_buffer < 0)
325 return (
reinterpret_cast<T*
>(ring_buffer->getDataList()))[current_reading_buffer];
328 current_reading_buffer = newest_buffer;
329 return (
reinterpret_cast<T*
>(ring_buffer->getDataList()))[current_reading_buffer];
333 if (shared_memory->isDisconnected())
335 shared_memory->connect();
336 if (shared_memory->isDisconnected())
341 data_ptr = shared_memory->getPtr();
344 return *
reinterpret_cast<T*
>(data_ptr);
349 template <
typename T>
358 if (shared_memory->isDisconnected())
360 if (ring_buffer !=
nullptr)
363 ring_buffer =
nullptr;
365 shared_memory->connect();
366 if (shared_memory->isDisconnected())
370 ring_buffer =
new RingBuffer(shared_memory->getPtr());
371 ring_buffer->setDataExpiryTime_us(data_expiry_time_us);
374 return ring_buffer->waitFor(timeout_usec);
378 template <
typename T>
380 Subscriber<T>::setDataExpiryTime_us(uint64_t time_us)
382 data_expiry_time_us = time_us;
383 if (ring_buffer !=
nullptr)
385 ring_buffer->setDataExpiryTime_us(data_expiry_time_us);