common.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 #include <cstring>
5 #include <optional>
6 #include <stdint.h>
7 #include <string>
8 #include <vector>
9 
10 namespace foxglove {
11 
12 constexpr char SUPPORTED_SUBPROTOCOL[] = "foxglove.websocket.v1";
13 constexpr char CAPABILITY_CLIENT_PUBLISH[] = "clientPublish";
14 constexpr char CAPABILITY_TIME[] = "time";
15 constexpr char CAPABILITY_PARAMETERS[] = "parameters";
16 constexpr char CAPABILITY_PARAMETERS_SUBSCRIBE[] = "parametersSubscribe";
17 constexpr char CAPABILITY_SERVICES[] = "services";
18 constexpr char CAPABILITY_CONNECTION_GRAPH[] = "connectionGraph";
19 
20 constexpr std::array<const char*, 5> DEFAULT_CAPABILITIES = {
23 };
24 
25 using ChannelId = uint32_t;
26 using ClientChannelId = uint32_t;
27 using SubscriptionId = uint32_t;
28 using ServiceId = uint32_t;
29 
30 enum class BinaryOpcode : uint8_t {
31  MESSAGE_DATA = 1,
32  TIME_DATA = 2,
34 };
35 
36 enum class ClientBinaryOpcode : uint8_t {
37  MESSAGE_DATA = 1,
39 };
40 
41 enum class WebSocketLogLevel {
42  Debug,
43  Info,
44  Warn,
45  Error,
46  Critical,
47 };
48 
50  std::string topic;
51  std::string encoding;
52  std::string schemaName;
53  std::string schema;
54  std::optional<std::string> schemaEncoding;
55 
56  bool operator==(const ChannelWithoutId& other) const {
57  return topic == other.topic && encoding == other.encoding && schemaName == other.schemaName &&
58  schema == other.schema && schemaEncoding == other.schemaEncoding;
59  }
60 };
61 
64 
65  Channel() = default; // requirement for json conversions.
67  : ChannelWithoutId(std::move(ch))
68  , id(id) {}
69 
70  bool operator==(const Channel& other) const {
71  return id == other.id && ChannelWithoutId::operator==(other);
72  }
73 };
74 
77  std::string topic;
78  std::string encoding;
79  std::string schemaName;
80  std::vector<uint8_t> schema;
81 };
82 
83 struct ClientMessage {
84  uint64_t logTime;
85  uint64_t publishTime;
86  uint32_t sequence;
88  size_t dataLength;
89  std::vector<uint8_t> data;
90 
91  ClientMessage(uint64_t logTime, uint64_t publishTime, uint32_t sequence,
92  const ClientAdvertisement& advertisement, size_t dataLength, const uint8_t* rawData)
93  : logTime(logTime)
94  , publishTime(publishTime)
95  , sequence(sequence)
96  , advertisement(advertisement)
97  , dataLength(dataLength)
98  , data(dataLength) {
99  std::memcpy(data.data(), rawData, dataLength);
100  }
101 
102  static const size_t MSG_PAYLOAD_OFFSET = 5;
103 
104  const uint8_t* getData() const {
105  return data.data() + MSG_PAYLOAD_OFFSET;
106  }
107  std::size_t getLength() const {
108  return data.size() - MSG_PAYLOAD_OFFSET;
109  }
110 };
111 
113  std::string name;
114  std::string type;
115  std::string requestSchema;
116  std::string responseSchema;
117 };
118 
121 
122  Service() = default;
123  Service(const ServiceWithoutId& s, const ServiceId& id)
124  : ServiceWithoutId(s)
125  , id(id) {}
126 };
127 
130  uint32_t callId;
131  std::string encoding;
132  std::vector<uint8_t> data;
133 
134  size_t size() const {
135  return 4 + 4 + 4 + encoding.size() + data.size();
136  }
137  void read(const uint8_t* data, size_t size);
138  void write(uint8_t* data) const;
139 
140  bool operator==(const ServiceResponse& other) const {
141  return serviceId == other.serviceId && callId == other.callId && encoding == other.encoding &&
142  data == other.data;
143  }
144 };
145 
147 
148 } // namespace foxglove
size_t size() const
Definition: common.hpp:134
ClientAdvertisement advertisement
Definition: common.hpp:87
BinaryOpcode
Definition: common.hpp:30
uint32_t SubscriptionId
Definition: common.hpp:27
bool operator==(const ServiceResponse &other) const
Definition: common.hpp:140
std::optional< std::string > schemaEncoding
Definition: common.hpp:54
constexpr char SUPPORTED_SUBPROTOCOL[]
Definition: common.hpp:12
uint32_t ServiceId
Definition: common.hpp:28
std::string responseSchema
Definition: common.hpp:116
bool operator==(const ChannelWithoutId &other) const
Definition: common.hpp:56
ClientBinaryOpcode
Definition: common.hpp:36
uint32_t ChannelId
Definition: common.hpp:25
std::vector< uint8_t > data
Definition: common.hpp:132
constexpr char CAPABILITY_CLIENT_PUBLISH[]
Definition: common.hpp:13
ChannelId id
Definition: common.hpp:63
constexpr char CAPABILITY_PARAMETERS_SUBSCRIBE[]
Definition: common.hpp:16
uint32_t ClientChannelId
Definition: common.hpp:26
std::size_t getLength() const
Definition: common.hpp:107
ServiceId id
Definition: common.hpp:120
constexpr char CAPABILITY_CONNECTION_GRAPH[]
Definition: common.hpp:18
constexpr char CAPABILITY_TIME[]
Definition: common.hpp:14
bool operator==(const Channel &other) const
Definition: common.hpp:70
std::string schemaName
Definition: common.hpp:52
std::vector< uint8_t > data
Definition: common.hpp:89
constexpr char CAPABILITY_SERVICES[]
Definition: common.hpp:17
ClientMessage(uint64_t logTime, uint64_t publishTime, uint32_t sequence, const ClientAdvertisement &advertisement, size_t dataLength, const uint8_t *rawData)
Definition: common.hpp:91
Service(const ServiceWithoutId &s, const ServiceId &id)
Definition: common.hpp:123
Channel(ChannelId id, ChannelWithoutId ch)
Definition: common.hpp:66
constexpr char CAPABILITY_PARAMETERS[]
Definition: common.hpp:15
std::string requestSchema
Definition: common.hpp:115
const uint8_t * getData() const
Definition: common.hpp:104
ClientChannelId channelId
Definition: common.hpp:76
std::vector< uint8_t > schema
Definition: common.hpp:80
WebSocketLogLevel
Definition: common.hpp:41
constexpr std::array< const char *, 5 > DEFAULT_CAPABILITIES
Definition: common.hpp:20


foxglove_bridge
Author(s): Foxglove
autogenerated on Mon Jul 3 2023 02:12:22