RMW desert 1.0
Loading...
Searching...
No Matches
CBorStream.h
Go to the documentation of this file.
1/****************************************************************************
2 * Copyright (C) 2024 Davide Costa *
3 * *
4 * This file is part of RMW desert. *
5 * *
6 * RMW desert is free software: you can redistribute it and/or modify it *
7 * under the terms of the GNU General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or any *
9 * later version. *
10 * *
11 * RMW desert is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with RMW desert. If not, see <http://www.gnu.org/licenses/>. *
18 ****************************************************************************/
19
34#ifndef CBORSTREAM_H_
35#define CBORSTREAM_H_
36
37#include "TcpDaemon.h"
38#include "TopicsConfig.h"
39
42#include <map>
43#include <queue>
44#include <utility>
45#include <vector>
46#include <string>
47#include <locale>
48#include <codecvt>
49#include <cstdint>
50#include <cstdio>
51#include <mutex>
52
55#include "cbor/encoder.h"
56#include "cbor/ieee754.h"
57#include "cbor/decoder.h"
58#include "cbor/parser.h"
59#include "cbor/helper.h"
60
61#include "half.hpp"
62
63#define PUBLISHER_TYPE 0
64#define SUBSCRIBER_TYPE 1
65#define CLIENT_TYPE 2
66#define SERVICE_TYPE 3
67
68#define MAX_BUFFER_CAPACITY 100
69
70template <typename T, int MaxLen, typename Container=std::deque<T>>
71class CircularQueue : public std::queue<T, Container> {
72 public:
73 void push(const T& value)
74 {
75 if (this->size() == MaxLen)
76 {
77 this->c.pop_front();
78 }
79 std::queue<T, Container>::push(value);
80 }
81};
82
83namespace cbor
84{
85
87{
88 public:
96 TxStream(uint8_t stream_type, std::string stream_name, uint8_t stream_identifier);
97
107 void start_transmission(uint64_t sequence_id);
115 void start_transmission();
122 void end_transmission();
123
128 TxStream & operator<<(const uint64_t n);
133 TxStream & operator<<(const uint32_t n);
138 TxStream & operator<<(const uint16_t n);
143 TxStream & operator<<(const uint8_t n);
148 TxStream & operator<<(const int64_t n);
153 TxStream & operator<<(const int32_t n);
158 TxStream & operator<<(const int16_t n);
163 TxStream & operator<<(const int8_t n);
168 TxStream & operator<<(const char n);
173 TxStream & operator<<(const float f);
178 TxStream & operator<<(const double d);
183 TxStream & operator<<(const std::string s);
188 TxStream & operator<<(const std::u16string s);
193 TxStream & operator<<(const bool b);
194
199 template<typename T>
200 inline TxStream & operator<<(const std::vector<T> v)
201 {
202 *this << static_cast<const uint32_t>(v.size());
203 return serialize_sequence(v.data(), v.size());
204 }
205
210 TxStream & operator<<(const std::vector<bool> v);
211
217 template<typename T>
218 inline TxStream & serialize_sequence(const T * items, size_t size)
219 {
220 for (size_t i = 0; i < size; ++i)
221 {
222 *this << items[i];
223 }
224 return *this;
225 }
226
227 private:
228 uint8_t _stream_type;
229 std::string _stream_name;
230 uint8_t _stream_identifier;
231
232 bool _overflow;
233 uint8_t * _packet;
234 cbor_writer_t * _writer;
235
236 void new_packet();
237 void handle_overrun(cbor_error_t result);
238
239 std::string toUTF8(const std::u16string source);
240
241};
242
244{
245 public:
253 RxStream(uint8_t stream_type, std::string stream_name, uint8_t stream_identifier);
254
264 bool data_available(int64_t sequence_id = 0);
265
272 void clear_buffer();
273
278 RxStream & operator>>(uint64_t & n);
283 RxStream & operator>>(uint32_t & n);
288 RxStream & operator>>(uint16_t & n);
293 RxStream & operator>>(uint8_t & n);
298 RxStream & operator>>(int64_t & n);
303 RxStream & operator>>(int32_t & n);
308 RxStream & operator>>(int16_t & n);
313 RxStream & operator>>(int8_t & n);
314
319 template<typename T>
321
326 RxStream & operator>>(char & n);
331 RxStream & operator>>(float & f);
336 RxStream & operator>>(double & d);
341 RxStream & operator>>(std::string & s);
346 RxStream & operator>>(std::u16string & s);
351 RxStream & operator>>(bool & b);
352
357 template<typename T>
358 inline RxStream & operator>>(std::vector<T> & v)
359 {
360 uint32_t size;
361 *this >> size;
362 v.resize(size);
363
364 return deserialize_sequence(v.data(), size);
365 }
366
371 RxStream & operator>>(std::vector<bool> & v);
372
378 template<typename T>
379 inline RxStream & deserialize_sequence(T * items, size_t size)
380 {
381 for (size_t i = 0; i < size; ++i)
382 {
383 *this >> items[i];
384 }
385 return *this;
386 }
387
395 static void interpret_packets();
396
397 private:
398 uint8_t _stream_type;
399 std::string _stream_name;
400 uint8_t _stream_identifier;
401
402 int _buffered_iterator;
403 std::vector<std::pair<void *, int>> _buffered_packet;
404
405 // <topic, packets <packet <field, field_type>>>
406 static std::map<uint32_t, CircularQueue<std::vector<std::pair<void *, int>>, MAX_BUFFER_CAPACITY>> _interpreted_publications;
407 // <service, packets <packet <field, field_type>>>
408 static std::map<uint32_t, CircularQueue<std::vector<std::pair<void *, int>>, MAX_BUFFER_CAPACITY>> _interpreted_requests;
409 // <service + id, packets <packet <field, field_type>>>
410 static std::map<uint32_t, CircularQueue<std::vector<std::pair<void *, int>>, MAX_BUFFER_CAPACITY>> _interpreted_responses;
411
412 union _cbor_value {
413 int8_t i8;
414 int16_t i16;
415 int32_t i32;
416 int64_t i64;
417 float f32;
418 double f64;
419 uint8_t *bin;
420 char *str;
421 uint8_t str_copy[128];
422 };
423
424 static std::mutex _rx_mutex;
425
426 static std::pair<void *, int> interpret_field(cbor_item_t * items, size_t i, union _cbor_value & val);
427 std::u16string toUTF16(const std::string source);
428};
429
430} // namespace cbor
431
432
433#endif
Class used to send and receive data from the DESERT socket.
Class used to store configurations.
Definition CBorStream.h:71
Definition CBorStream.h:244
RxStream & deserialize_integer(T &n)
Decode a generic integer.
Definition CBorStream.cpp:302
RxStream & operator>>(std::vector< T > &v)
Decode vector.
Definition CBorStream.h:358
static void interpret_packets()
Interpret raw packets and splits them into different communication types.
Definition CBorStream.cpp:386
void clear_buffer()
Clear the currently buffered packet.
Definition CBorStream.cpp:256
bool data_available(int64_t sequence_id=0)
Check if there are data.
Definition CBorStream.cpp:204
RxStream & deserialize_sequence(T *items, size_t size)
Deserialize a sequence of uniform elements.
Definition CBorStream.h:379
RxStream & operator>>(uint64_t &n)
Decode uint64.
Definition CBorStream.cpp:261
Definition CBorStream.h:87
TxStream & operator<<(const uint64_t n)
Encode uint64.
Definition CBorStream.cpp:63
TxStream & serialize_sequence(const T *items, size_t size)
Serialize a sequence of uniform elements.
Definition CBorStream.h:218
TxStream & operator<<(const std::vector< T > v)
Encode vector.
Definition CBorStream.h:200
void end_transmission()
Tell the stream to send down the packet.
Definition CBorStream.cpp:45
void start_transmission()
Tell the stream to create a new packet.
Definition CBorStream.cpp:35