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);
const uint8_t SERIALIZATION_END_FLAG
std::chrono::duration< rep, period > duration
virtual void serializeTo(CArchive &out) const =0
Pure virtual method for writing (serializing) to an abstract archive.
CArchive & readStdVectorToStream(CArchive &s, VEC &v)
#define THROW_TYPED_EXCEPTION(msg, exceptionClass)
std::chrono::time_point< Clock > time_point
#define THROW_EXCEPTION(msg)
std::string std::string format(std::string_view fmt, ARGS &&... args)
virtual void serializeFrom(CArchive &in, uint8_t serial_version)=0
Pure virtual method for reading (deserializing) from an abstract archive.
uint32_t type
An identifier of the message type (only the least-sig byte is typically sent)
void WriteBufferFixEndianness(const T *ptr, size_t ElementCount)
Writes a sequence of elemental datatypes, taking care of reordering their bytes from the running arch...
A structure that holds runtime class type information.
CArchive & writeStdVectorToStream(CArchive &s, const VEC &v)
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
void sendMessage(const CMessage &msg)
Send a message to the device.
std::vector< T, mrpt::aligned_allocator_cpp11< T > > aligned_std_vector
virtual size_t read(void *buf, size_t len)=0
Reads a block of bytes.
CArchive & operator>>(CSerializable &obj)
Reads a CSerializable object from the stream.
void internal_ReadObjectHeader(std::string &className, bool &isOldFormat, int8_t &version)
Read the object Header.
#define ASSERT_(f)
Defines an assertion mechanism.
CArchive & operator<<(const CSerializable &obj)
Write a CSerializable object to a stream in the binary MRPT format.
const TRuntimeClassId * findRegisteredClass(const std::string &className, const bool allow_ignore_namespace=true)
Return info about a given class by its name, or nullptr if the class is not registered.
bool receiveMessage(CMessage &msg)
Tries to receive a message from the device.
virtual size_t write(const void *buf, size_t len)=0
Writes a block of bytes.
CArchive & operator>>(CArchive &s, mrpt::aligned_std_vector< float > &a)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Virtual base class for "archives": classes abstracting I/O streams.
A class that contain generic messages, that can be sent and received from a "CClientTCPSocket" object...
void internal_ReadObject(CSerializable *newObj, const std::string &className, bool isOldFormat, int8_t version)
Read the object.
#define MAKEWORD16B(__LOBYTE, __HILOBYTE)
mrpt::vision::TStereoCalibResults out
CSerializable::Ptr ReadObject()
Reads an object from stream, its class determined at runtime, and returns a smart pointer to the obje...
CArchive & operator<<(CArchive &s, const mrpt::aligned_std_vector< float > &a)
The virtual base class which provides a unified interface for all persistent objects in MRPT...
size_t ReadBuffer(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer.
void WriteObject(const CSerializable *o)
Writes an object to the stream.
std::vector< uint8_t > content
The contents of the message (memory is automatically handled by the std::vector object) ...
virtual uint8_t serializeGetVersion() const =0
Must return the current versioning number of the object.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
size_t ReadBufferFixEndianness(T *ptr, size_t ElementCount)
Reads a sequence of elemental datatypes, taking care of reordering their bytes from the MRPT stream s...
virtual const mrpt::rtti::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
#define THROW_STACKED_EXCEPTION_CUSTOM_MSG2(stuff, param1)
Used in mrpt::serialization::CArchive.
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".