30 size_t actuallyRead = this->
read(Buffer, Count);
34 "(EOF?) Cannot read requested number of bytes from stream");
53 if (Count != this->
write(Buffer, Count))
60 uint64_t rep = s.time_since_epoch().count();
76 auto n = (uint32_t)a.size();
80 std::vector<uint8_t> b(n);
81 std::vector<bool>::const_iterator it;
82 std::vector<uint8_t>::iterator it2;
83 for (it = a.begin(), it2 = b.begin(); it != a.end(); ++it, ++it2)
85 out.WriteBuffer((
void*)&b[0], (
int)(
sizeof(b[0]) * n));
92 auto n = (uint32_t)str.size();
94 if (n)
out.WriteBuffer(str.c_str(), n);
106 const char* className;
113 className =
"nullptr";
116 int8_t classNamLen = strlen(className);
117 int8_t classNamLen_mod = classNamLen | 0x80;
119 (*this) << classNamLen_mod;
166 namespace serialization
172 template <
typename VEC>
175 const auto n =
static_cast<uint32_t
>(v.size());
180 template <
typename VEC>
195 CArchive& s,
const std::vector<float>& a)
205 CArchive& s,
const std::vector<double>& a)
210 CArchive& s,
const std::vector<int32_t>& a)
215 CArchive& s,
const std::vector<uint32_t>& a)
220 CArchive& s,
const std::vector<uint16_t>& a)
225 CArchive& s,
const std::vector<int16_t>& a)
230 CArchive& s,
const std::vector<int64_t>& a)
235 CArchive& s,
const std::vector<uint8_t>& a)
240 CArchive& s,
const std::vector<int8_t>& a)
288 #if MRPT_WORD_SIZE != 32 // If it's 32 bit, size_t <=> uint32_t
290 CArchive& s,
const std::vector<size_t>& a)
307 std::vector<uint8_t> b(n);
308 in.
ReadBuffer((
void*)&b[0],
sizeof(b[0]) * n);
309 std::vector<uint8_t>::iterator it2;
310 std::vector<bool>::iterator it;
311 for (it = a.begin(), it2 = b.begin(); it != a.end(); ++it, ++it2)
327 #define CARCHIVE_VERBOSE 0
330 std::string& strClassName,
bool& isOldFormat, int8_t& version)
332 uint8_t lengthReadClassName = 255;
333 char readClassName[260];
334 readClassName[0] = 0;
340 if (
sizeof(lengthReadClassName) !=
342 (
void*)&lengthReadClassName,
sizeof(lengthReadClassName)))
346 if (!(lengthReadClassName & 0x80))
352 "Cannot read object header from stream! (EOF?)");
353 if (buf[0] || buf[1] || buf[2])
355 "Expecting 0x00 00 00 while parsing old streaming header "
356 "(Perhaps it's a gz-compressed stream? Use a GZ-stream for "
365 lengthReadClassName &= 0x7F;
368 if (lengthReadClassName > 120)
370 "Class name has more than 120 chars. This probably means a "
371 "corrupted binary stream.");
373 if (((
size_t)lengthReadClassName) !=
374 ReadBuffer(readClassName, lengthReadClassName))
377 readClassName[lengthReadClassName] =
'\0';
380 strClassName = readClassName;
387 if (
sizeof(version_old) !=
390 "Cannot read object streaming version from stream!");
391 ASSERT_(version_old >= 0 && version_old < 255);
392 version = int8_t(version_old);
395 strClassName !=
"nullptr" &&
396 sizeof(version) !=
ReadBuffer((
void*)&version,
sizeof(version)))
399 "Cannot read object streaming version from stream!");
404 cerr <<
"[CArchive::ReadObject] readClassName:" << strClassName
405 <<
" version: " << version << endl;
408 catch (std::bad_alloc&)
414 if (lengthReadClassName == 255)
422 "Exception while parsing typed object '%s' from stream!\n",
429 CSerializable* obj,
const std::string& strClassName,
bool isOldFormat,
442 if (
sizeof(endFlag) !=
ReadBuffer((
void*)&endFlag,
sizeof(endFlag)))
444 "Cannot read object streaming version from stream!");
447 "end-flag missing: There is a bug in the deserialization "
448 "method of class: '%s'",
449 strClassName.c_str());
452 catch (std::bad_alloc&)
463 "Exception while parsing typed object '%s' from stream!\n",
464 strClassName.c_str());
477 std::string strClassName;
478 bool isOldFormat{
false};
483 ASSERT_(existingObj && strClassName !=
"nullptr");
484 ASSERT_(strClassName !=
"nullptr");
491 "Stored object has class '%s' which is not registered!",
492 strClassName.c_str());
495 "Stored class does not match with existing object!!:\n Stored: "
497 id2->className, id->className));
503 CArchive& s,
const std::vector<std::string>& vec)
505 auto N =
static_cast<uint32_t
>(vec.size());
507 for (
size_t i = 0; i < N; i++) s << vec[i];
512 CArchive& s, std::vector<std::string>& vec)
517 for (
size_t i = 0; i < N; i++) s >> vec[i];
525 std::array<uint8_t, 0x10100> buf;
526 unsigned int nBytesTx = 0;
528 const bool msg_format_is_tiny = msg.
content.size() < 256;
531 buf[nBytesTx++] = msg_format_is_tiny ? 0x69 : 0x79;
532 buf[nBytesTx++] = (
unsigned char)(msg.
type);
534 if (msg_format_is_tiny)
536 buf[nBytesTx++] = (
unsigned char)msg.
content.size();
540 buf[nBytesTx++] = msg.
content.size() & 0xff;
541 buf[nBytesTx++] = (msg.
content.size() >> 8) & 0xff;
546 nBytesTx += (
unsigned char)msg.
content.size();
547 buf[nBytesTx++] = 0x96;
558 std::vector<unsigned char> buf(66000);
559 unsigned int nBytesInFrame = 0;
560 unsigned long nBytesToRx = 0;
561 unsigned char tries = 2;
562 unsigned int payload_len = 0;
563 unsigned int expectedLen = 0;
567 if (nBytesInFrame < 4)
573 payload_len = buf[2];
574 expectedLen = payload_len + 4;
576 else if (buf[0] == 0x79)
580 expectedLen = payload_len + 5;
582 nBytesToRx = expectedLen - nBytesInFrame;
585 unsigned long nBytesRx = 0;
588 nBytesRx =
ReadBuffer(&buf[nBytesInFrame], nBytesToRx);
596 if (!nBytesRx)
return false;
598 if (!nBytesInFrame && buf[0] != 0x69 && buf[0] != 0x79)
601 if (!tries--)
return false;
606 nBytesInFrame += nBytesRx;
608 if (nBytesInFrame == expectedLen)
614 if (buf[nBytesInFrame - 1] != 0x96)
625 msg.
content.resize(payload_len);
631 msg.
content.resize(payload_len);