29 size_t actuallyRead = this->
read(Buffer, Count);
33 "(EOF?) Cannot read requested number of bytes from stream");
52 if (Count != this->
write(Buffer, Count))
59 #if MRPT_IS_BIG_ENDIAN 61 #define IMPLEMENT_CArchive_READ_WRITE_SIMPLE_TYPE(T) \ 62 CArchive& mrpt::serialization::operator<<(CArchive& out, const T a) \ 64 mrpt::reverseBytesInPlace(a); \ 65 out.WriteBuffer((void*)&a, sizeof(a)); \ 68 CArchive& mrpt::serialization::operator>>(CArchive& in, T& a) \ 71 in.ReadBuffer((void*)&b, sizeof(a)); \ 72 mrpt::reverseBytesInPlace(b); \ 73 ::memcpy(&a, &b, sizeof(b)); \ 78 #define IMPLEMENT_CArchive_READ_WRITE_SIMPLE_TYPE(T) \ 79 CArchive& mrpt::serialization::operator<<(CArchive& out, const T a) \ 81 out.WriteBuffer((void*)&a, sizeof(a)); \ 84 CArchive& mrpt::serialization::operator>>(CArchive& in, T& a) \ 86 in.ReadBuffer((void*)&a, sizeof(a)); \ 106 uint64_t rep =
s.time_since_epoch().count();
127 CArchive& out,
const std::vector<bool>&
a)
133 std::vector<uint8_t>
b(
n);
136 for (it =
a.begin(), it2 =
b.begin(); it !=
a.end(); ++it, ++it2)
159 const char* className;
166 className =
"nullptr";
169 int8_t classNamLen = strlen(className);
170 int8_t classNamLen_mod = classNamLen | 0x80;
172 (*this) << classNamLen_mod;
219 namespace serialization
225 template <
typename VEC>
230 if (
n)
s.WriteBufferFixEndianness(&
v[0],
n);
233 template <
typename VEC>
239 if (
n)
s.ReadBufferFixEndianness(&
v[0],
n);
341 #if MRPT_WORD_SIZE != 32 // If it's 32 bit, size_t <=> uint32_t 360 std::vector<uint8_t>
b(
n);
361 in.ReadBuffer((
void*)&
b[0],
sizeof(
b[0]) *
n);
364 for (it =
a.begin(), it2 =
b.begin(); it !=
a.end(); ++it, ++it2)
375 if (
n)
in.ReadBuffer((
void*)&str[0],
n);
384 if (l)
in.ReadBuffer(
s, l);
390 #define CARCHIVE_VERBOSE 0 395 uint8_t lengthReadClassName = 255;
396 char readClassName[260];
397 readClassName[0] = 0;
403 if (
sizeof(lengthReadClassName) !=
405 (
void*)&lengthReadClassName,
sizeof(lengthReadClassName)))
409 if (!(lengthReadClassName & 0x80))
415 "Cannot read object header from stream! (EOF?)");
416 if (buf[0] || buf[1] || buf[2])
418 "Expecting 0x00 00 00 while parsing old streaming header " 419 "(Perhaps it's a gz-compressed stream? Use a GZ-stream for " 428 lengthReadClassName &= 0x7F;
431 if (lengthReadClassName > 120)
433 "Class name has more than 120 chars. This probably means a " 434 "corrupted binary stream.");
436 if (((
size_t)lengthReadClassName) !=
437 ReadBuffer(readClassName, lengthReadClassName))
440 readClassName[lengthReadClassName] =
'\0';
443 strClassName = readClassName;
450 if (
sizeof(version_old) !=
453 "Cannot read object streaming version from stream!");
454 ASSERT_(version_old >= 0 && version_old < 255);
455 version =
int8_t(version_old);
458 strClassName !=
"nullptr" &&
459 sizeof(version) !=
ReadBuffer((
void*)&version,
sizeof(version)))
462 "Cannot read object streaming version from stream!");
467 cerr <<
"[CArchive::ReadObject] readClassName:" << strClassName
468 <<
" version: " << version << endl;
471 catch (std::bad_alloc&)
475 catch (std::exception& e)
477 if (lengthReadClassName == 255)
485 e,
"Exception while parsing typed object '%s' from stream!\n",
504 obj->serializeFrom(*
this, version);
509 if (
sizeof(endFlag) !=
ReadBuffer((
void*)&endFlag,
sizeof(endFlag)))
511 "Cannot read object streaming version from stream!");
514 "end-flag missing: There is a bug in the deserialization " 515 "method of class: '%s'",
516 strClassName.c_str());
519 catch (std::bad_alloc&)
523 catch (std::exception&)
548 ASSERT_(existingObj && strClassName !=
"nullptr");
549 ASSERT_(strClassName !=
"nullptr");
556 "Stored object has class '%s' which is not registered!",
557 strClassName.c_str());
560 "Stored class does not match with existing object!!:\n Stored: " 562 id2->className,
id->className));
568 CArchive&
s,
const std::vector<std::string>& vec)
572 for (
size_t i = 0; i < N; i++)
s << vec[i];
577 CArchive&
s, std::vector<std::string>& vec)
582 for (
size_t i = 0; i < N; i++) s >> vec[i];
590 unsigned char buf[0x10100];
591 unsigned int nBytesTx = 0;
593 const bool msg_format_is_tiny = msg.
content.size() < 256;
596 buf[nBytesTx++] = msg_format_is_tiny ? 0x69 : 0x79;
597 buf[nBytesTx++] = (
unsigned char)(msg.
type);
599 if (msg_format_is_tiny)
601 buf[nBytesTx++] = (
unsigned char)msg.
content.size();
605 buf[nBytesTx++] = msg.
content.size() & 0xff;
606 buf[nBytesTx++] = (msg.
content.size() >> 8) & 0xff;
611 nBytesTx += (
unsigned char)msg.
content.size();
612 buf[nBytesTx++] = 0x96;
623 std::vector<unsigned char> buf(66000);
624 unsigned int nBytesInFrame = 0;
625 unsigned long nBytesToRx = 0;
626 unsigned char tries = 2;
627 unsigned int payload_len = 0;
628 unsigned int expectedLen = 0;
632 if (nBytesInFrame < 4)
638 payload_len = buf[2];
639 expectedLen = payload_len + 4;
641 else if (buf[0] == 0x79)
645 expectedLen = payload_len + 5;
647 nBytesToRx = expectedLen - nBytesInFrame;
650 unsigned long nBytesRx = 0;
653 nBytesRx =
ReadBuffer(&buf[nBytesInFrame], nBytesToRx);
661 if (!nBytesRx)
return false;
663 if (!nBytesInFrame && buf[0] != 0x69 && buf[0] != 0x79)
666 if (!tries--)
return false;
671 nBytesInFrame += nBytesRx;
673 if (nBytesInFrame == expectedLen)
679 if (buf[nBytesInFrame - 1] != 0x96)
691 msg.
content.resize(payload_len);
697 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_STACKED_EXCEPTION_CUSTOM_MSG2(e, stuff, param1)
unsigned __int16 uint16_t
#define THROW_TYPED_EXCEPTION(msg, exceptionClass)
std::chrono::time_point< Clock > time_point
#define THROW_EXCEPTION(msg)
uint32_t type
An identifier of the message type (only the least-sig byte is typically sent)
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
GLsizei GLsizei GLuint * obj
virtual size_t read(void *buf, size_t len)=0
Reads a block of bytes.
#define IMPLEMENT_CArchive_READ_WRITE_SIMPLE_TYPE(T)
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.
GLsizei const GLchar ** string
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.
const TRuntimeClassId * findRegisteredClass(const std::string &className)
Return info about a given class by its name, or nullptr if the class is not registered.
CArchive & operator>>(CArchive &s, mrpt::aligned_std_vector< float > &a)
unsigned __int64 uint64_t
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)
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
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,...)
unsigned __int32 uint32_t
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...
GLubyte GLubyte GLubyte a
virtual const mrpt::rtti::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
const Scalar * const_iterator
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".