Gazebo Transport

API Reference

8.5.0
gz/transport/Packet.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #ifndef GZ_TRANSPORT_PACKET_HH_
19 #define GZ_TRANSPORT_PACKET_HH_
20 
21 #include <cstdint>
22 #include <iostream>
23 #include <string>
24 #include <vector>
25 
26 #include "gz/transport/config.hh"
27 #include "gz/transport/Export.hh"
29 
30 // This whole file is deprecated in version 8 of Ignition Transport. Please
31 // remove this file in Version 9.
32 
33 namespace ignition
34 {
35  namespace transport
36  {
37  // Inline bracket to help doxygen filtering.
38  inline namespace IGNITION_TRANSPORT_VERSION_NAMESPACE {
39  //
40  // Message types.
41  static const uint8_t Uninitialized = 0;
42  static const uint8_t AdvType = 1;
43  static const uint8_t SubType = 2;
44  static const uint8_t UnadvType = 3;
45  static const uint8_t HeartbeatType = 4;
46  static const uint8_t ByeType = 5;
47  static const uint8_t NewConnection = 6;
48  static const uint8_t EndConnection = 7;
49 
50  // Flag set when a discovery message is relayed.
51  static const uint16_t FlagRelay = 0b000000000000'0001;
52  // Flag set when we want to avoid to relay a discovery message.
53  // This is used to avoid loops.
54  static const uint16_t FlagNoRelay = 0b000000000000'0010;
55 
58  {
59  "UNINITIALIZED", "ADVERTISE", "SUBSCRIBE", "UNADVERTISE", "HEARTBEAT",
60  "BYE", "NEW_CONNECTION", "END_CONNECTION"
61  };
62 
69  class IGNITION_TRANSPORT_VISIBLE Header
70  {
72  public: IGN_DEPRECATED(8) Header() = default;
73 
79  public: IGN_DEPRECATED(8) Header(const uint16_t _version,
80  const std::string &_pUuid,
81  const uint8_t _type,
82  const uint16_t _flags = 0);
83 
85  public: virtual ~Header() = default;
86 
90  public: uint16_t Version() const;
91 
95  public: std::string PUuid() const;
96 
100  public: uint8_t Type() const;
101 
105  public: uint16_t Flags() const;
106 
110  public: void SetVersion(const uint16_t _version);
111 
115  public: void SetPUuid(const std::string &_pUuid);
116 
120  public: void SetType(const uint8_t _type);
121 
125  public: void SetFlags(const uint16_t _flags);
126 
129  public: int HeaderLength() const;
130 
136  public: size_t Pack(char *_buffer) const;
137 
140  public: size_t Unpack(const char *_buffer);
141 
145  public: friend std::ostream &operator<<(std::ostream &_out,
146  const Header &_header)
147  {
148  _out << "--------------------------------------\n"
149  << "Header:" << std::endl
150  << "\tVersion: " << _header.Version() << "\n"
151  << "\tProcess UUID: " << _header.PUuid() << "\n"
152  << "\tType: " << MsgTypesStr.at(_header.Type()) << "\n"
153  << "\tFlags: " << _header.Flags() << "\n";
154  return _out;
155  }
156 
158  private: uint16_t version = 0;
159 
160 #ifdef _WIN32
161 // Disable warning C4251 which is triggered by
162 // std::string
163 #pragma warning(push)
164 #pragma warning(disable: 4251)
165 #endif
166  private: std::string pUuid = "";
168 #ifdef _WIN32
169 #pragma warning(pop)
170 #endif
171 
173  private: uint8_t type = Uninitialized;
174 
176  private: uint16_t flags = 0;
177  };
178 
184  class IGNITION_TRANSPORT_VISIBLE SubscriptionMsg
185  {
187  public: IGN_DEPRECATED(8) SubscriptionMsg() = default;
188 
192  public: IGN_DEPRECATED(8) SubscriptionMsg(
193  const transport::Header &_header,
194  const std::string &_topic);
195 
199  public: transport::Header Header() const;
200 
204  public: std::string Topic() const;
205 
209  public: void SetHeader(const transport::Header &_header);
210 
214  public: void SetTopic(const std::string &_topic);
215 
218  public: size_t MsgLength() const;
219 
223  public: friend std::ostream &operator<<(std::ostream &_out,
224  const SubscriptionMsg &_msg)
225  {
226  _out << _msg.Header()
227  << "Body:" << std::endl
228  << "\tTopic: [" << _msg.Topic() << "]" << std::endl;
229 
230  return _out;
231  }
232 
236  public: size_t Pack(char *_buffer) const;
237 
241  public: size_t Unpack(const char *_buffer);
242 
244  private: transport::Header header;
245 
246 #ifdef _WIN32
247 // Disable warning C4251 which is triggered by std::string
248 #pragma warning(push)
249 #pragma warning(disable: 4251)
250 #endif
251  private: std::string topic = "";
253 #ifdef _WIN32
254 #pragma warning(pop)
255 #endif
256  };
257 
266  template <class T> class AdvertiseMessage
267  {
269  public: IGN_DEPRECATED(8) AdvertiseMessage() = default;
270 
274  public: IGN_DEPRECATED(8) AdvertiseMessage(const Header &_header,
275  const T &_publisher)
276  : header(_header),
277  publisher(_publisher)
278  {
279  }
280 
284  public: transport::Header Header() const
285  {
286  return this->header;
287  }
288 
292  public: T& Publisher()
293  {
294  return this->publisher;
295  }
296 
300  public: void SetHeader(const transport::Header &_header)
301  {
302  this->header = _header;
303  }
304 
308  public: void SetPublisher(const T &_publisher)
309  {
310  this->publisher = _publisher;
311  }
312 
315  public: size_t MsgLength() const
316  {
317  return this->header.HeaderLength() + this->publisher.MsgLength();
318  }
319 
323  public: size_t Pack(char *_buffer) const
324  {
325  // Pack the common part of any advertise message.
326  size_t len = this->header.Pack(_buffer);
327  if (len == 0)
328  return 0;
329 
330  _buffer += len;
331 
332  // Pack the part of the publisher.
333  if (this->publisher.Pack(_buffer) == 0)
334  return 0;
335 
336  return this->MsgLength();
337  }
338 
342  public: size_t Unpack(const char *_buffer)
343  {
344  // Unpack the message publisher.
345  if (this->publisher.Unpack(_buffer) == 0)
346  return 0;
347 
348  return this->publisher.MsgLength();
349  }
350 
353  public: void SetFromDiscovery(const msgs::Discovery &_msg)
354  {
355  this->publisher.SetFromDiscovery(_msg);
356  }
357 
361  public: friend std::ostream &operator<<(std::ostream &_out,
362  const AdvertiseMessage &_msg)
363  {
364  _out << _msg.header << _msg.publisher;
365  return _out;
366  }
367 
369  private: transport::Header header;
370 
372  private: T publisher;
373  };
374  }
375  }
376 }
377 
378 #endif
void SetHeader(const transport::Header &_header)
Set the header of the message.
Definition: gz/transport/Packet.hh:300
uint16_t Flags() const
Get the message flags.
size_t MsgLength() const
Get the total length of the message.
Definition: gz/transport/Packet.hh:315
size_t MsgLength() const
Get the total length of the message.
Definition: gz/transport/AdvertiseOptions.hh:28
STL class.
T & Publisher()
Get the publisher of this message.
Definition: gz/transport/Packet.hh:292
Header()=default
Constructor.
transport::Header Header() const
Get the message header.
Definition: gz/transport/Packet.hh:284
static const uint8_t UnadvType
Definition: gz/transport/Packet.hh:44
size_t Unpack(const char *_buffer)
Unserialize a stream of bytes into an AdvertiseMessage.
Definition: gz/transport/Packet.hh:342
static const uint8_t AdvType
Definition: gz/transport/Packet.hh:42
static const uint8_t SubType
Definition: gz/transport/Packet.hh:43
static const std::vector< std::string > MsgTypesStr
Used for debugging the message type received/send.
Definition: gz/transport/Packet.hh:57
AdvertiseMessage()=default
Constructor.
Advertise packet used in the discovery protocol to broadcast information about the node advertising a...
Definition: gz/transport/Packet.hh:266
virtual ~Header()=default
Destructor.
static const uint8_t Uninitialized
Definition: gz/transport/Packet.hh:41
void SetVersion(const uint16_t _version)
Set the discovery protocol version.
static const uint16_t FlagRelay
Definition: gz/transport/Packet.hh:51
static const uint8_t ByeType
Definition: gz/transport/Packet.hh:46
transport::Header Header() const
Get the message header.
size_t Unpack(const char *_buffer)
Unserialize a stream of bytes into a Sub.
T at(T... args)
STL class.
void SetHeader(const transport::Header &_header)
Set the header of the message.
static const uint8_t EndConnection
Definition: gz/transport/Packet.hh:48
size_t Pack(char *_buffer) const
Serialize the subscription message.
friend std::ostream & operator<<(std::ostream &_out, const AdvertiseMessage &_msg)
Stream insertion operator.
Definition: gz/transport/Packet.hh:361
void SetType(const uint8_t _type)
Set the message type.
friend std::ostream & operator<<(std::ostream &_out, const SubscriptionMsg &_msg)
Stream insertion operator.
Definition: gz/transport/Packet.hh:223
size_t Pack(char *_buffer) const
Serialize the advertise message.
Definition: gz/transport/Packet.hh:323
void SetTopic(const std::string &_topic)
Set the topic.
T endl(T... args)
int HeaderLength() const
Get the header length.
void SetPublisher(const T &_publisher)
Set the publisher of this message.
Definition: gz/transport/Packet.hh:308
std::string Topic() const
Get the topic.
Header included in each discovery message containing the version of the discovery protocol,...
Definition: gz/transport/Packet.hh:69
static const uint8_t NewConnection
Definition: gz/transport/Packet.hh:47
void SetFlags(const uint16_t _flags)
Set the message flags.
std::string PUuid() const
Get the process uuid.
uint8_t Type() const
Get the message type.
friend std::ostream & operator<<(std::ostream &_out, const Header &_header)
Stream insertion operator.
Definition: gz/transport/Packet.hh:145
static const uint8_t HeartbeatType
Definition: gz/transport/Packet.hh:45
void SetFromDiscovery(const msgs::Discovery &_msg)
Set from discovery message.
Definition: gz/transport/Packet.hh:353
Subscription packet used in the discovery protocol for requesting information about a given topic.
Definition: gz/transport/Packet.hh:184
size_t Unpack(const char *_buffer)
Unserialize the header.
AdvertiseMessage(const Header &_header, const T &_publisher)
Constructor.
Definition: gz/transport/Packet.hh:274
uint16_t Version() const
Get the discovery protocol version.
size_t Pack(char *_buffer) const
Serialize the header. The caller has ownership of the buffer and is responsible for its [de]allocatio...
void SetPUuid(const std::string &_pUuid)
Set the process uuid.
SubscriptionMsg()=default
Constructor.