CDRMessage.hpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. // Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  15. /**
  16. * @file CDRMessage.hpp
  17. *
  18. */
  19. #include <cassert>
  20. #include <cstring>
  21. #include <algorithm>
  22. #include <vector>
  23. #include <fastdds/dds/core/policy/ParameterTypes.hpp>
  24. namespace eprosima {
  25. namespace fastrtps {
  26. namespace rtps {
  27. inline bool CDRMessage::initCDRMsg(
  28. CDRMessage_t* msg,
  29. uint32_t payload_size)
  30. {
  31. if (msg->buffer == nullptr)
  32. {
  33. msg->buffer = (octet*)malloc(payload_size + RTPSMESSAGE_COMMON_RTPS_PAYLOAD_SIZE);
  34. msg->max_size = payload_size + RTPSMESSAGE_COMMON_RTPS_PAYLOAD_SIZE;
  35. }
  36. msg->pos = 0;
  37. msg->length = 0;
  38. #if __BIG_ENDIAN__
  39. msg->msg_endian = BIGEND;
  40. #else
  41. msg->msg_endian = LITTLEEND;
  42. #endif
  43. return true;
  44. }
  45. inline bool CDRMessage::wrapVector(
  46. CDRMessage_t* msg,
  47. std::vector<octet>& vectorToWrap)
  48. {
  49. if (msg->buffer && !msg->wraps)
  50. {
  51. free(msg->buffer);
  52. }
  53. msg->wraps = true;
  54. msg->buffer = vectorToWrap.data();
  55. msg->length = (uint32_t)vectorToWrap.size();
  56. msg->max_size = (uint32_t)vectorToWrap.capacity();
  57. #if __BIG_ENDIAN__
  58. msg->msg_endian = BIGEND;
  59. #else
  60. msg->msg_endian = LITTLEEND;
  61. #endif
  62. return true;
  63. }
  64. inline bool CDRMessage::appendMsg(
  65. CDRMessage_t* first,
  66. CDRMessage_t* second)
  67. {
  68. return(CDRMessage::addData(first, second->buffer, second->length));
  69. }
  70. inline bool CDRMessage::readEntityId(
  71. CDRMessage_t* msg,
  72. EntityId_t* id)
  73. {
  74. if (msg->pos + 4 > msg->length)
  75. {
  76. return false;
  77. }
  78. memcpy(id->value, &msg->buffer[msg->pos], id->size);
  79. msg->pos += 4;
  80. return true;
  81. }
  82. inline bool CDRMessage::readData(
  83. CDRMessage_t* msg,
  84. octet* o,
  85. uint32_t length)
  86. {
  87. if (msg->pos + length > msg->length)
  88. {
  89. return false;
  90. }
  91. memcpy(o, &msg->buffer[msg->pos], length);
  92. msg->pos += length;
  93. return true;
  94. }
  95. inline bool CDRMessage::read_array_with_max_size(
  96. CDRMessage_t* msg,
  97. octet* arr,
  98. size_t max_size)
  99. {
  100. if (msg->pos + 4 > msg->length)
  101. {
  102. return false;
  103. }
  104. uint32_t datasize;
  105. bool valid = CDRMessage::readUInt32(msg, &datasize);
  106. if (max_size < datasize)
  107. {
  108. return false;
  109. }
  110. valid &= CDRMessage::readData(msg, arr, datasize);
  111. msg->pos = (msg->pos + 3) & ~3;
  112. return valid;
  113. }
  114. inline bool CDRMessage::readDataReversed(
  115. CDRMessage_t* msg,
  116. octet* o,
  117. uint32_t length)
  118. {
  119. for (uint32_t i = 0; i < length; i++)
  120. {
  121. *(o + i) = *(msg->buffer + msg->pos + length - 1 - i);
  122. }
  123. msg->pos += length;
  124. return true;
  125. }
  126. inline bool CDRMessage::readInt32(
  127. CDRMessage_t* msg,
  128. int32_t* lo)
  129. {
  130. if (msg->pos + 4 > msg->length)
  131. {
  132. return false;
  133. }
  134. octet* dest = (octet*)lo;
  135. if (msg->msg_endian == DEFAULT_ENDIAN)
  136. {
  137. for (uint8_t i = 0; i < 4; i++)
  138. {
  139. dest[i] = msg->buffer[msg->pos + i];
  140. }
  141. msg->pos += 4;
  142. }
  143. else
  144. {
  145. readDataReversed(msg, dest, 4);
  146. }
  147. return true;
  148. }
  149. inline bool CDRMessage::readUInt32(
  150. CDRMessage_t* msg,
  151. uint32_t* ulo)
  152. {
  153. if (msg->pos + 4 > msg->length)
  154. {
  155. return false;
  156. }
  157. octet* dest = (octet*)ulo;
  158. if (msg->msg_endian == DEFAULT_ENDIAN)
  159. {
  160. for (uint8_t i = 0; i < 4; i++)
  161. {
  162. dest[i] = msg->buffer[msg->pos + i];
  163. }
  164. msg->pos += 4;
  165. }
  166. else
  167. {
  168. readDataReversed(msg, dest, 4);
  169. }
  170. return true;
  171. }
  172. inline bool CDRMessage::readInt64(
  173. CDRMessage_t* msg,
  174. int64_t* lolo)
  175. {
  176. if (msg->pos + 8 > msg->length)
  177. {
  178. return false;
  179. }
  180. octet* dest = (octet*)lolo;
  181. if (msg->msg_endian == DEFAULT_ENDIAN)
  182. {
  183. for (uint8_t i = 0; i < 8; ++i)
  184. {
  185. dest[i] = msg->buffer[msg->pos + i];
  186. }
  187. msg->pos += 8;
  188. }
  189. else
  190. {
  191. readDataReversed(msg, dest, 8);
  192. }
  193. return true;
  194. }
  195. inline bool CDRMessage::readSequenceNumber(
  196. CDRMessage_t* msg,
  197. SequenceNumber_t* sn)
  198. {
  199. if (msg->pos + 8 > msg->length)
  200. {
  201. return false;
  202. }
  203. bool valid = readInt32(msg, &sn->high);
  204. valid &= readUInt32(msg, &sn->low);
  205. return true;
  206. }
  207. inline SequenceNumberSet_t CDRMessage::readSequenceNumberSet(
  208. CDRMessage_t* msg)
  209. {
  210. bool valid = true;
  211. SequenceNumber_t seqNum;
  212. valid &= CDRMessage::readSequenceNumber(msg, &seqNum);
  213. SequenceNumberSet_t sns(seqNum);
  214. uint32_t numBits = 0;
  215. valid &= CDRMessage::readUInt32(msg, &numBits);
  216. uint32_t n_longs = (numBits + 31ul) / 32ul;
  217. uint32_t bitmap[8];
  218. for (uint32_t i = 0; i < n_longs; ++i)
  219. {
  220. valid &= CDRMessage::readUInt32(msg, &bitmap[i]);
  221. }
  222. if (valid)
  223. {
  224. sns.bitmap_set(numBits, bitmap);
  225. }
  226. return sns;
  227. }
  228. inline bool CDRMessage::readFragmentNumberSet(
  229. CDRMessage_t* msg,
  230. FragmentNumberSet_t* fns)
  231. {
  232. bool valid = true;
  233. FragmentNumber_t base = 0ul;
  234. valid &= CDRMessage::readUInt32(msg, &base);
  235. fns->base(base);
  236. uint32_t numBits = 0;
  237. valid &= CDRMessage::readUInt32(msg, &numBits);
  238. uint32_t n_longs = (numBits + 31ul) / 32ul;
  239. uint32_t bitmap[8];
  240. for (uint32_t i = 0; i < n_longs; ++i)
  241. {
  242. valid &= CDRMessage::readUInt32(msg, &bitmap[i]);
  243. }
  244. if (valid)
  245. {
  246. fns->bitmap_set(numBits, bitmap);
  247. }
  248. return valid;
  249. }
  250. inline bool CDRMessage::readTimestamp(
  251. CDRMessage_t* msg,
  252. rtps::Time_t* ts)
  253. {
  254. bool valid = true;
  255. valid &= CDRMessage::readInt32(msg, &ts->seconds());
  256. uint32_t frac(0);
  257. valid &= CDRMessage::readUInt32(msg, &frac);
  258. ts->fraction(frac);
  259. return valid;
  260. }
  261. inline bool CDRMessage::readLocator(
  262. CDRMessage_t* msg,
  263. Locator_t* loc)
  264. {
  265. if (msg->pos + 24 > msg->length)
  266. {
  267. return false;
  268. }
  269. bool valid = readInt32(msg, &loc->kind);
  270. valid &= readUInt32(msg, &loc->port);
  271. valid &= readData(msg, loc->address, 16);
  272. return valid;
  273. }
  274. inline bool CDRMessage::readInt16(
  275. CDRMessage_t* msg,
  276. int16_t* i16)
  277. {
  278. if (msg->pos + 2 > msg->length)
  279. {
  280. return false;
  281. }
  282. octet* o = (octet*)i16;
  283. if (msg->msg_endian == DEFAULT_ENDIAN)
  284. {
  285. *o = msg->buffer[msg->pos];
  286. *(o + 1) = msg->buffer[msg->pos + 1];
  287. }
  288. else
  289. {
  290. *o = msg->buffer[msg->pos + 1];
  291. *(o + 1) = msg->buffer[msg->pos];
  292. }
  293. msg->pos += 2;
  294. return true;
  295. }
  296. inline bool CDRMessage::readUInt16(
  297. CDRMessage_t* msg,
  298. uint16_t* i16)
  299. {
  300. if (msg->pos + 2 > msg->length)
  301. {
  302. return false;
  303. }
  304. octet* o = (octet*)i16;
  305. if (msg->msg_endian == DEFAULT_ENDIAN)
  306. {
  307. *o = msg->buffer[msg->pos];
  308. *(o + 1) = msg->buffer[msg->pos + 1];
  309. }
  310. else
  311. {
  312. *o = msg->buffer[msg->pos + 1];
  313. *(o + 1) = msg->buffer[msg->pos];
  314. }
  315. msg->pos += 2;
  316. return true;
  317. }
  318. inline bool CDRMessage::readOctet(
  319. CDRMessage_t* msg,
  320. octet* o)
  321. {
  322. if (msg->pos + 1 > msg->length)
  323. {
  324. return false;
  325. }
  326. *o = msg->buffer[msg->pos];
  327. msg->pos++;
  328. return true;
  329. }
  330. inline bool CDRMessage::readOctetVector(
  331. CDRMessage_t* msg,
  332. std::vector<octet>* ocvec)
  333. {
  334. if (msg->pos + 4 > msg->length)
  335. {
  336. return false;
  337. }
  338. uint32_t vecsize;
  339. bool valid = CDRMessage::readUInt32(msg, &vecsize);
  340. ocvec->resize(vecsize);
  341. valid &= CDRMessage::readData(msg, ocvec->data(), vecsize);
  342. msg->pos = (msg->pos + 3) & ~3;
  343. return valid;
  344. }
  345. inline bool CDRMessage::readString(
  346. CDRMessage_t* msg,
  347. std::string* stri)
  348. {
  349. uint32_t str_size = 1;
  350. bool valid = true;
  351. valid &= CDRMessage::readUInt32(msg, &str_size);
  352. if (msg->pos + str_size > msg->length)
  353. {
  354. return false;
  355. }
  356. stri->clear();
  357. if (str_size > 1)
  358. {
  359. stri->resize(str_size - 1);
  360. for (uint32_t i = 0; i < str_size - 1; i++)
  361. {
  362. stri->at(i) = msg->buffer[msg->pos + i];
  363. }
  364. }
  365. msg->pos += str_size;
  366. msg->pos = (msg->pos + 3) & ~3;
  367. return valid;
  368. }
  369. inline bool CDRMessage::readString(
  370. CDRMessage_t* msg,
  371. string_255* stri)
  372. {
  373. uint32_t str_size = 1;
  374. bool valid = true;
  375. valid &= CDRMessage::readUInt32(msg, &str_size);
  376. if (msg->pos + str_size > msg->length)
  377. {
  378. return false;
  379. }
  380. *stri = "";
  381. if (str_size > 1)
  382. {
  383. *stri = (const char*) &(msg->buffer[msg->pos]);
  384. }
  385. msg->pos += str_size;
  386. int rest = (str_size) % 4;
  387. rest = rest == 0 ? 0 : 4 - rest;
  388. msg->pos += rest;
  389. return valid;
  390. }
  391. inline bool CDRMessage::addData(
  392. CDRMessage_t* msg,
  393. const octet* data,
  394. const uint32_t length)
  395. {
  396. if (msg->pos + length > msg->max_size)
  397. {
  398. return false;
  399. }
  400. memcpy(&msg->buffer[msg->pos], data, length);
  401. msg->pos += length;
  402. msg->length += length;
  403. return true;
  404. }
  405. inline bool CDRMessage::addDataReversed(
  406. CDRMessage_t* msg,
  407. const octet* data,
  408. const uint32_t length)
  409. {
  410. if (msg->pos + length > msg->max_size)
  411. {
  412. return false;
  413. }
  414. for (uint32_t i = 0; i < length; i++)
  415. {
  416. msg->buffer[msg->pos + i] = *(data + length - 1 - i);
  417. }
  418. msg->pos += length;
  419. msg->length += length;
  420. return true;
  421. }
  422. inline bool CDRMessage::addOctet(
  423. CDRMessage_t* msg,
  424. octet O)
  425. {
  426. if (msg->pos + 1 > msg->max_size)
  427. {
  428. return false;
  429. }
  430. //const void* d = (void*)&O;
  431. msg->buffer[msg->pos] = O;
  432. msg->pos++;
  433. msg->length++;
  434. return true;
  435. }
  436. inline bool CDRMessage::addUInt16(
  437. CDRMessage_t* msg,
  438. uint16_t us)
  439. {
  440. if (msg->pos + 2 > msg->max_size)
  441. {
  442. return false;
  443. }
  444. octet* o = (octet*)&us;
  445. if (msg->msg_endian == DEFAULT_ENDIAN)
  446. {
  447. msg->buffer[msg->pos] = *(o);
  448. msg->buffer[msg->pos + 1] = *(o + 1);
  449. }
  450. else
  451. {
  452. msg->buffer[msg->pos] = *(o + 1);
  453. msg->buffer[msg->pos + 1] = *(o);
  454. }
  455. msg->pos += 2;
  456. msg->length += 2;
  457. return true;
  458. }
  459. inline bool CDRMessage::addInt32(
  460. CDRMessage_t* msg,
  461. int32_t lo)
  462. {
  463. octet* o = (octet*)&lo;
  464. if (msg->pos + 4 > msg->max_size)
  465. {
  466. return false;
  467. }
  468. if (msg->msg_endian == DEFAULT_ENDIAN)
  469. {
  470. for (uint8_t i = 0; i < 4; i++)
  471. {
  472. msg->buffer[msg->pos + i] = *(o + i);
  473. }
  474. }
  475. else
  476. {
  477. for (uint8_t i = 0; i < 4; i++)
  478. {
  479. msg->buffer[msg->pos + i] = *(o + 3 - i);
  480. }
  481. }
  482. msg->pos += 4;
  483. msg->length += 4;
  484. return true;
  485. }
  486. inline bool CDRMessage::addUInt32(
  487. CDRMessage_t* msg,
  488. uint32_t ulo)
  489. {
  490. octet* o = (octet*)&ulo;
  491. if (msg->pos + 4 > msg->max_size)
  492. {
  493. return false;
  494. }
  495. if (msg->msg_endian == DEFAULT_ENDIAN)
  496. {
  497. for (uint8_t i = 0; i < 4; i++)
  498. {
  499. msg->buffer[msg->pos + i] = *(o + i);
  500. }
  501. }
  502. else
  503. {
  504. for (uint8_t i = 0; i < 4; i++)
  505. {
  506. msg->buffer[msg->pos + i] = *(o + 3 - i);
  507. }
  508. }
  509. msg->pos += 4;
  510. msg->length += 4;
  511. return true;
  512. }
  513. inline bool CDRMessage::addInt64(
  514. CDRMessage_t* msg,
  515. int64_t lolo)
  516. {
  517. octet* o = (octet*)&lolo;
  518. if (msg->pos + 8 > msg->max_size)
  519. {
  520. return false;
  521. }
  522. if (msg->msg_endian == DEFAULT_ENDIAN)
  523. {
  524. for (uint8_t i = 0; i < 8; i++)
  525. {
  526. msg->buffer[msg->pos + i] = *(o + i);
  527. }
  528. }
  529. else
  530. {
  531. for (uint8_t i = 0; i < 8; i++)
  532. {
  533. msg->buffer[msg->pos + i] = *(o + 7 - i);
  534. }
  535. }
  536. msg->pos += 8;
  537. msg->length += 8;
  538. return true;
  539. }
  540. inline bool CDRMessage::addOctetVector(
  541. CDRMessage_t* msg,
  542. const std::vector<octet>* ocvec,
  543. bool add_final_padding)
  544. {
  545. // TODO Calculate without padding
  546. auto final_size = msg->pos + ocvec->size();
  547. if (add_final_padding)
  548. {
  549. final_size += 4;
  550. }
  551. if (final_size >= msg->max_size)
  552. {
  553. return false;
  554. }
  555. bool valid = CDRMessage::addUInt32(msg, (uint32_t)ocvec->size());
  556. valid &= CDRMessage::addData(msg, (octet*)ocvec->data(), (uint32_t)ocvec->size());
  557. if (add_final_padding)
  558. {
  559. int rest = ocvec->size() % 4;
  560. if (rest != 0)
  561. {
  562. rest = 4 - rest; //how many you have to add
  563. octet oc = '\0';
  564. for (int i = 0; i < rest; i++)
  565. {
  566. valid &= CDRMessage::addOctet(msg, oc);
  567. }
  568. }
  569. }
  570. return valid;
  571. }
  572. inline bool CDRMessage::addEntityId(
  573. CDRMessage_t* msg,
  574. const EntityId_t* ID)
  575. {
  576. if (msg->pos + 4 >= msg->max_size)
  577. {
  578. return false;
  579. }
  580. memcpy(&msg->buffer[msg->pos], ID->value, ID->size);
  581. msg->pos += 4;
  582. msg->length += 4;
  583. return true;
  584. }
  585. inline bool CDRMessage::addSequenceNumber(
  586. CDRMessage_t* msg,
  587. const SequenceNumber_t* sn)
  588. {
  589. addInt32(msg, sn->high);
  590. addUInt32(msg, sn->low);
  591. return true;
  592. }
  593. inline bool CDRMessage::addSequenceNumberSet(
  594. CDRMessage_t* msg,
  595. const SequenceNumberSet_t* sns)
  596. {
  597. SequenceNumber_t base = sns->base();
  598. CDRMessage::addSequenceNumber(msg, &base);
  599. //Add set
  600. if (sns->empty())
  601. {
  602. addUInt32(msg, 0); //numbits 0
  603. return true;
  604. }
  605. uint32_t numBits;
  606. uint32_t n_longs;
  607. std::array<uint32_t, 8> bitmap;
  608. sns->bitmap_get(numBits, bitmap, n_longs);
  609. addUInt32(msg, numBits);
  610. for (uint32_t i = 0; i < n_longs; i++)
  611. {
  612. addUInt32(msg, bitmap[i]);
  613. }
  614. return true;
  615. }
  616. inline bool CDRMessage::addFragmentNumberSet(
  617. CDRMessage_t* msg,
  618. FragmentNumberSet_t* fns)
  619. {
  620. FragmentNumber_t base = fns->base();
  621. if (base == 0)
  622. {
  623. return false;
  624. }
  625. CDRMessage::addUInt32(msg, base);
  626. //Add set
  627. if (fns->empty())
  628. {
  629. addUInt32(msg, 0); //numbits 0
  630. return true;
  631. }
  632. uint32_t numBits;
  633. uint32_t n_longs;
  634. std::array<uint32_t, 8> bitmap;
  635. fns->bitmap_get(numBits, bitmap, n_longs);
  636. addUInt32(msg, numBits);
  637. for (uint32_t i = 0; i < n_longs; i++)
  638. {
  639. addUInt32(msg, bitmap[i]);
  640. }
  641. return true;
  642. }
  643. inline bool CDRMessage::addLocator(
  644. CDRMessage_t* msg,
  645. const Locator_t& loc)
  646. {
  647. addInt32(msg, loc.kind);
  648. addUInt32(msg, loc.port);
  649. addData(msg, loc.address, 16);
  650. return true;
  651. }
  652. inline bool CDRMessage::add_string(
  653. CDRMessage_t* msg,
  654. const char* in_str)
  655. {
  656. uint32_t str_siz = static_cast<uint32_t>(strlen(in_str) + 1);
  657. bool valid = CDRMessage::addUInt32(msg, str_siz);
  658. valid &= CDRMessage::addData(msg, (unsigned char*) in_str, str_siz);
  659. octet oc = '\0';
  660. for (; str_siz& 3; ++str_siz)
  661. {
  662. valid &= CDRMessage::addOctet(msg, oc);
  663. }
  664. return valid;
  665. }
  666. inline bool CDRMessage::add_string(
  667. CDRMessage_t* msg,
  668. const std::string& in_str)
  669. {
  670. return add_string(msg, in_str.c_str());
  671. }
  672. inline bool CDRMessage::add_string(
  673. CDRMessage_t* msg,
  674. const string_255& in_str)
  675. {
  676. return add_string(msg, in_str.c_str());
  677. }
  678. inline bool CDRMessage::addProperty(
  679. CDRMessage_t* msg,
  680. const Property& property)
  681. {
  682. assert(msg);
  683. if (property.propagate())
  684. {
  685. if (!CDRMessage::add_string(msg, property.name()))
  686. {
  687. return false;
  688. }
  689. if (!CDRMessage::add_string(msg, property.value()))
  690. {
  691. return false;
  692. }
  693. }
  694. return true;
  695. }
  696. inline bool CDRMessage::readProperty(
  697. CDRMessage_t* msg,
  698. Property& property)
  699. {
  700. assert(msg);
  701. if (!CDRMessage::readString(msg, &property.name()))
  702. {
  703. return false;
  704. }
  705. if (!CDRMessage::readString(msg, &property.value()))
  706. {
  707. return false;
  708. }
  709. return true;
  710. }
  711. inline bool CDRMessage::addBinaryProperty(
  712. CDRMessage_t* msg,
  713. const BinaryProperty& binary_property,
  714. bool add_final_padding)
  715. {
  716. assert(msg);
  717. if (binary_property.propagate())
  718. {
  719. if (!CDRMessage::add_string(msg, binary_property.name()))
  720. {
  721. return false;
  722. }
  723. if (!CDRMessage::addOctetVector(msg, &binary_property.value(), add_final_padding))
  724. {
  725. return false;
  726. }
  727. }
  728. return true;
  729. }
  730. inline bool CDRMessage::readBinaryProperty(
  731. CDRMessage_t* msg,
  732. BinaryProperty& binary_property)
  733. {
  734. assert(msg);
  735. if (!CDRMessage::readString(msg, &binary_property.name()))
  736. {
  737. return false;
  738. }
  739. if (!CDRMessage::readOctetVector(msg, &binary_property.value()))
  740. {
  741. return false;
  742. }
  743. binary_property.propagate(true);
  744. return true;
  745. }
  746. inline bool CDRMessage::addPropertySeq(
  747. CDRMessage_t* msg,
  748. const PropertySeq& properties)
  749. {
  750. assert(msg);
  751. bool returnedValue = false;
  752. if (msg->pos + 4 <= msg->max_size)
  753. {
  754. uint32_t number_to_serialize = 0;
  755. for (auto it = properties.begin(); it != properties.end(); ++it)
  756. {
  757. if (it->propagate())
  758. {
  759. ++number_to_serialize;
  760. }
  761. }
  762. if (CDRMessage::addUInt32(msg, number_to_serialize))
  763. {
  764. returnedValue = true;
  765. for (auto it = properties.begin(); returnedValue && it != properties.end(); ++it)
  766. {
  767. if (it->propagate())
  768. {
  769. returnedValue = CDRMessage::addProperty(msg, *it);
  770. }
  771. }
  772. }
  773. }
  774. return returnedValue;
  775. }
  776. inline bool CDRMessage::readPropertySeq(
  777. CDRMessage_t* msg,
  778. PropertySeq& properties)
  779. {
  780. assert(msg);
  781. uint32_t length = 0;
  782. if (!CDRMessage::readUInt32(msg, &length))
  783. {
  784. return false;
  785. }
  786. properties.resize(length);
  787. bool returnedValue = true;
  788. for (uint32_t i = 0; returnedValue && i < length; ++i)
  789. {
  790. returnedValue = CDRMessage::readProperty(msg, properties.at(i));
  791. }
  792. return returnedValue;
  793. }
  794. inline bool CDRMessage::addBinaryPropertySeq(
  795. CDRMessage_t* msg,
  796. const BinaryPropertySeq& binary_properties,
  797. bool add_final_padding)
  798. {
  799. assert(msg);
  800. bool returnedValue = false;
  801. if (msg->pos + 4 <= msg->max_size)
  802. {
  803. uint32_t number_to_serialize = 0;
  804. for (auto it = binary_properties.begin(); it != binary_properties.end(); ++it)
  805. {
  806. if (it->propagate())
  807. {
  808. ++number_to_serialize;
  809. }
  810. }
  811. if (CDRMessage::addUInt32(msg, number_to_serialize))
  812. {
  813. returnedValue = true;
  814. for (auto it = binary_properties.begin(); returnedValue && it != binary_properties.end(); ++it)
  815. {
  816. if (it->propagate())
  817. {
  818. --number_to_serialize;
  819. returnedValue =
  820. CDRMessage::addBinaryProperty(msg, *it,
  821. add_final_padding || (number_to_serialize != 0) );
  822. }
  823. }
  824. }
  825. }
  826. return returnedValue;
  827. }
  828. inline bool CDRMessage::addBinaryPropertySeq(
  829. CDRMessage_t* msg,
  830. const BinaryPropertySeq& binary_properties,
  831. const std::string& name_start,
  832. bool add_final_padding)
  833. {
  834. assert(msg);
  835. bool returnedValue = false;
  836. if (msg->pos + 4 <= msg->max_size)
  837. {
  838. uint32_t number_to_serialize = 0;
  839. for (auto it = binary_properties.begin(); it != binary_properties.end(); ++it)
  840. {
  841. if (it->name().find(name_start) == 0)
  842. {
  843. ++number_to_serialize;
  844. }
  845. }
  846. if (CDRMessage::addUInt32(msg, number_to_serialize))
  847. {
  848. returnedValue = true;
  849. for (auto it = binary_properties.begin(); returnedValue && it != binary_properties.end(); ++it)
  850. {
  851. if (it->name().find(name_start) == 0)
  852. {
  853. --number_to_serialize;
  854. returnedValue =
  855. CDRMessage::addBinaryProperty(msg, *it,
  856. add_final_padding || (number_to_serialize != 0) );
  857. }
  858. }
  859. }
  860. }
  861. return returnedValue;
  862. }
  863. inline bool CDRMessage::readBinaryPropertySeq(
  864. CDRMessage_t* msg,
  865. BinaryPropertySeq& binary_properties)
  866. {
  867. assert(msg);
  868. uint32_t length = 0;
  869. if (!CDRMessage::readUInt32(msg, &length))
  870. {
  871. return false;
  872. }
  873. binary_properties.resize(length);
  874. bool returnedValue = true;
  875. for (uint32_t i = 0; returnedValue && i < length; ++i)
  876. {
  877. returnedValue = CDRMessage::readBinaryProperty(msg, binary_properties.at(i));
  878. }
  879. return returnedValue;
  880. }
  881. inline bool CDRMessage::addDataHolder(
  882. CDRMessage_t* msg,
  883. const DataHolder& data_holder)
  884. {
  885. assert(msg);
  886. if (!CDRMessage::add_string(msg, data_holder.class_id()))
  887. {
  888. return false;
  889. }
  890. if (!CDRMessage::addPropertySeq(msg, data_holder.properties()))
  891. {
  892. return false;
  893. }
  894. if (!CDRMessage::addBinaryPropertySeq(msg, data_holder.binary_properties(), true))
  895. {
  896. return false;
  897. }
  898. return true;
  899. }
  900. inline bool CDRMessage::readDataHolder(
  901. CDRMessage_t* msg,
  902. DataHolder& data_holder)
  903. {
  904. assert(msg);
  905. if (!CDRMessage::readString(msg, &data_holder.class_id()))
  906. {
  907. return false;
  908. }
  909. if (!CDRMessage::readPropertySeq(msg, data_holder.properties()))
  910. {
  911. return false;
  912. }
  913. if (!CDRMessage::readBinaryPropertySeq(msg, data_holder.binary_properties()))
  914. {
  915. return false;
  916. }
  917. return true;
  918. }
  919. inline bool CDRMessage::addDataHolderSeq(
  920. CDRMessage_t* msg,
  921. const DataHolderSeq& data_holders)
  922. {
  923. assert(msg);
  924. bool returnedValue = false;
  925. if (msg->pos + 4 <= msg->max_size)
  926. {
  927. if (CDRMessage::addUInt32(msg, static_cast<uint32_t>(data_holders.size())))
  928. {
  929. returnedValue = true;
  930. for (auto data_holder = data_holders.begin(); returnedValue && data_holder != data_holders.end();
  931. ++data_holder)
  932. {
  933. returnedValue = CDRMessage::addDataHolder(msg, *data_holder);
  934. }
  935. }
  936. }
  937. return returnedValue;
  938. }
  939. inline bool CDRMessage::readDataHolderSeq(
  940. CDRMessage_t* msg,
  941. DataHolderSeq& data_holders)
  942. {
  943. assert(msg);
  944. uint32_t length = 0;
  945. if (!CDRMessage::readUInt32(msg, &length))
  946. {
  947. return false;
  948. }
  949. data_holders.resize(length);
  950. bool returnedValue = true;
  951. for (uint32_t i = 0; returnedValue && i < length; ++i)
  952. {
  953. returnedValue = CDRMessage::readDataHolder(msg, data_holders.at(i));
  954. }
  955. return returnedValue;
  956. }
  957. inline bool CDRMessage::addMessageIdentity(
  958. CDRMessage_t* msg,
  959. const security::MessageIdentity& message_identity)
  960. {
  961. assert(msg);
  962. if (!CDRMessage::addData(msg, message_identity.source_guid().guidPrefix.value, GuidPrefix_t::size))
  963. {
  964. return false;
  965. }
  966. if (!CDRMessage::addData(msg, message_identity.source_guid().entityId.value, EntityId_t::size))
  967. {
  968. return false;
  969. }
  970. if (!CDRMessage::addInt64(msg, message_identity.sequence_number()))
  971. {
  972. return false;
  973. }
  974. return true;
  975. }
  976. inline bool CDRMessage::readMessageIdentity(
  977. CDRMessage_t* msg,
  978. security::MessageIdentity& message_identity)
  979. {
  980. assert(msg);
  981. if (!CDRMessage::readData(msg, message_identity.source_guid().guidPrefix.value, GuidPrefix_t::size))
  982. {
  983. return false;
  984. }
  985. if (!CDRMessage::readData(msg, message_identity.source_guid().entityId.value, EntityId_t::size))
  986. {
  987. return false;
  988. }
  989. if (!CDRMessage::readInt64(msg, &message_identity.sequence_number()))
  990. {
  991. return false;
  992. }
  993. return true;
  994. }
  995. inline bool CDRMessage::addParticipantGenericMessage(
  996. CDRMessage_t* msg,
  997. const security::ParticipantGenericMessage& message)
  998. {
  999. assert(msg);
  1000. if (!CDRMessage::addMessageIdentity(msg, message.message_identity()))
  1001. {
  1002. return false;
  1003. }
  1004. if (!CDRMessage::addMessageIdentity(msg, message.related_message_identity()))
  1005. {
  1006. return false;
  1007. }
  1008. if (!CDRMessage::addData(msg, message.destination_participant_key().guidPrefix.value, GuidPrefix_t::size))
  1009. {
  1010. return false;
  1011. }
  1012. if (!CDRMessage::addData(msg, message.destination_participant_key().entityId.value, EntityId_t::size))
  1013. {
  1014. return false;
  1015. }
  1016. if (!CDRMessage::addData(msg, message.destination_endpoint_key().guidPrefix.value, GuidPrefix_t::size))
  1017. {
  1018. return false;
  1019. }
  1020. if (!CDRMessage::addData(msg, message.destination_endpoint_key().entityId.value, EntityId_t::size))
  1021. {
  1022. return false;
  1023. }
  1024. if (!CDRMessage::addData(msg, message.source_endpoint_key().guidPrefix.value, GuidPrefix_t::size))
  1025. {
  1026. return false;
  1027. }
  1028. if (!CDRMessage::addData(msg, message.source_endpoint_key().entityId.value, EntityId_t::size))
  1029. {
  1030. return false;
  1031. }
  1032. if (!CDRMessage::add_string(msg, message.message_class_id()))
  1033. {
  1034. return false;
  1035. }
  1036. if (!CDRMessage::addDataHolderSeq(msg, message.message_data()))
  1037. {
  1038. return false;
  1039. }
  1040. return true;
  1041. }
  1042. inline bool CDRMessage::readParticipantGenericMessage(
  1043. CDRMessage_t* msg,
  1044. security::ParticipantGenericMessage& message)
  1045. {
  1046. assert(msg);
  1047. if (!CDRMessage::readMessageIdentity(msg, message.message_identity()))
  1048. {
  1049. return false;
  1050. }
  1051. if (!CDRMessage::readMessageIdentity(msg, message.related_message_identity()))
  1052. {
  1053. return false;
  1054. }
  1055. if (!CDRMessage::readData(msg, message.destination_participant_key().guidPrefix.value, GuidPrefix_t::size))
  1056. {
  1057. return false;
  1058. }
  1059. if (!CDRMessage::readData(msg, message.destination_participant_key().entityId.value, EntityId_t::size))
  1060. {
  1061. return false;
  1062. }
  1063. if (!CDRMessage::readData(msg, message.destination_endpoint_key().guidPrefix.value, GuidPrefix_t::size))
  1064. {
  1065. return false;
  1066. }
  1067. if (!CDRMessage::readData(msg, message.destination_endpoint_key().entityId.value, EntityId_t::size))
  1068. {
  1069. return false;
  1070. }
  1071. if (!CDRMessage::readData(msg, message.source_endpoint_key().guidPrefix.value, GuidPrefix_t::size))
  1072. {
  1073. return false;
  1074. }
  1075. if (!CDRMessage::readData(msg, message.source_endpoint_key().entityId.value, EntityId_t::size))
  1076. {
  1077. return false;
  1078. }
  1079. if (!CDRMessage::readString(msg, &message.message_class_id()))
  1080. {
  1081. return false;
  1082. }
  1083. if (!CDRMessage::readDataHolderSeq(msg, message.message_data()))
  1084. {
  1085. return false;
  1086. }
  1087. return true;
  1088. }
  1089. }
  1090. } /* namespace rtps */
  1091. } /* namespace eprosima */
  1092. #endif /* DOXYGEN_SHOULD_SKIP_THIS */