OpenDDS  Snapshot(2023/04/28-20:55)
MessageUtils.cpp
Go to the documentation of this file.
1 /*
2  * Distributed under the OpenDDS License.
3  * See: http://www.opendds.org/license.html
4  */
5 
6 #include "MessageUtils.h"
7 
8 #include "MessageTypes.h"
9 
10 #include <dds/DCPS/Time_Helper.h>
11 #include <dds/OpenddsDcpsExtTypeSupportImpl.h>
12 
14 
15 namespace OpenDDS {
16 namespace RTPS {
17 
18 using DCPS::Encoding;
19 
20 namespace {
21  const Encoding encoding_plain_native(Encoding::KIND_XCDR1);
22 }
23 
24 const DCPS::Encoding& get_locators_encoding()
25 {
27  return encoding;
28 }
29 
31  DCPS::LocatorSeq& locators,
32  bool* requires_inline_qos,
33  unsigned int* pBytesRead)
34 {
36  reinterpret_cast<const char*>(blob.get_buffer()),
37  0 /*alloc*/, 0 /*lock*/, ACE_Message_Block::DONT_DELETE, 0 /*db_alloc*/);
38  ACE_Message_Block mb(&db, ACE_Message_Block::DONT_DELETE, 0 /*mb_alloc*/);
39  mb.wr_ptr(mb.space());
40 
42  if (!(ser >> locators)) {
44  ACE_TEXT("(%P|%t) blob_to_locators: ")
45  ACE_TEXT("Failed to deserialize blob's locators\n")),
47  }
48 
49  if (requires_inline_qos) {
50  if (!(ser >> ACE_InputCDR::to_boolean(*requires_inline_qos))) {
52  ACE_TEXT("(%P|%t) blob_to_locators: ")
53  ACE_TEXT("Failed to deserialize blob inline QoS flag\n")),
55  }
56  } else {
57  if (!ser.skip(1)) {
59  ACE_TEXT("(%P|%t) blob_to_locators: ")
60  ACE_TEXT("Failed to skip blob inline QoS flag\n")),
62  }
63  }
64 
65  if (pBytesRead) {
66  *pBytesRead = blob.length() - static_cast<unsigned int>(mb.length());
67  }
68  return DDS::RETCODE_OK;
69 }
70 
71 void locators_to_blob(const DCPS::LocatorSeq& locators,
72  DCPS::TransportBLOB& blob)
73 {
74  using namespace OpenDDS::DCPS;
76  size_t size = 0;
77  serialized_size(encoding, size, locators);
78  ACE_Message_Block mb_locator(size + 1);
79  Serializer ser(&mb_locator, encoding);
80  if (!(ser << locators)) {
81  ACE_ERROR((LM_ERROR, ACE_TEXT("(%P|%t) locators_to_blob: ")
82  ACE_TEXT("Failed to serialize locators to blob\n")));
83  }
84  // Add a bool for 'requires inline qos', see Sedp::set_inline_qos():
85  // if the bool is no longer the last octet of the sequence then that function
86  // must be changed as well.
87  if (!(ser << ACE_OutputCDR::from_boolean(false))) {
88  ACE_ERROR((LM_ERROR, ACE_TEXT("(%P|%t) locators_to_blob: ")
89  ACE_TEXT("Failed to serialize boolean for blob\n")));
90  }
91  message_block_to_sequence(mb_locator, blob);
92 }
93 
96 {
97  DCPS::LocatorSeq locators;
98  blob_to_locators(info.data, locators);
99  return locators;
100 }
101 
103 {
104  if (rtps_duration == DURATION_INFINITE) {
106  }
107 
108  if (version < PROTOCOLVERSION_2_4 && vendor == VENDORID_OPENDDS) {
110  rtps_duration.seconds,
111  static_cast<ACE_UINT32>(rtps_duration.fraction / 1000));
112  } else {
114  rtps_duration.seconds,
116  }
117 }
118 
119 bool bitmapNonEmpty(const SequenceNumberSet& snSet)
120 {
121  const CORBA::ULong num_ulongs = (snSet.numBits + 31) / 32;
122 
123  OPENDDS_ASSERT(num_ulongs <= snSet.bitmap.length());
124 
125  if (num_ulongs == 0) {
126  return false;
127  }
128 
129  const CORBA::ULong last_index = num_ulongs - 1;
130  for (CORBA::ULong i = 0; i < last_index; ++i) {
131  if (snSet.bitmap[i]) {
132  return true;
133  }
134  }
135 
136  const CORBA::ULong mod = snSet.numBits % 32;
137  const CORBA::ULong mask = mod ? (1 + ~(1u << (32 - mod))) : 0xFFFFFFFF;
138  return (bool)(snSet.bitmap[last_index] & mask);
139 }
140 
141 }
142 }
143 
#define ACE_ERROR(X)
ACE_INLINE OpenDDS_Dcps_Export ACE_UINT32 uint32_fractional_seconds_to_microseconds(ACE_UINT32 fraction)
const Duration_t DURATION_INFINITE
Definition: MessageTypes.h:23
size_t length(void) const
bool skip(size_t n, int size=1)
Definition: Serializer.inl:443
const VendorId_t VENDORID_OPENDDS
Definition: MessageTypes.h:26
#define OPENDDS_ASSERT(C)
Definition: Definitions.h:72
sequence< Locator_t > LocatorSeq
void serialized_size(const Encoding &encoding, size_t &size, const SequenceNumber &)
bool bitmapNonEmpty(const SequenceNumberSet &snSet)
DCPS::TimeDuration rtps_duration_to_time_duration(const Duration_t &rtps_duration, const ProtocolVersion_t &version, const VendorId_t &vendor)
ACE_CDR::ULong ULong
Class to serialize and deserialize data for DDS.
Definition: Serializer.h:369
#define OpenDDS_Rtps_Export
Definition: rtps_export.h:23
unsigned long fraction
Definition: RtpsCore.idl:106
void message_block_to_sequence(const ACE_Message_Block &mb_locator, T &out)
Definition: MessageUtils.h:101
char * wr_ptr(void) const
OpenDDS_Rtps_Export DCPS::LocatorSeq transport_locator_to_locator_seq(const DCPS::TransportLocator &info)
ACE_TEXT("TCP_Factory")
size_t space(void) const
const DCPS::Encoding & get_locators_encoding()
const ReturnCode_t RETCODE_ERROR
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
const ReturnCode_t RETCODE_OK
const ProtocolVersion_t PROTOCOLVERSION_2_4
Definition: MessageTypes.h:66
#define ACE_ERROR_RETURN(X, Y)
DDS::ReturnCode_t blob_to_locators(const DCPS::TransportBLOB &blob, DCPS::LocatorSeq &locators, bool *requires_inline_qos, unsigned int *pBytesRead)
const DCPS::Encoding encoding(DCPS::Encoding::KIND_UNALIGNED_CDR, DCPS::ENDIAN_BIG)
DDS::OctetSeq TransportBLOB
LM_ERROR
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
void locators_to_blob(const DCPS::LocatorSeq &locators, DCPS::TransportBLOB &blob)
static const TimeDuration max_value
Definition: TimeDuration.h:32