Go to the documentation of this file.
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)); \
115 CArchive& out,
const std::vector<bool>&
a)
121 std::vector<uint8_t>
b(
n);
124 for (it =
a.begin(), it2 =
b.begin(); it !=
a.end(); ++it, ++it2)
147 const char* className;
154 className =
"nullptr";
157 int8_t classNamLen = strlen(className);
158 int8_t classNamLen_mod = classNamLen | 0x80;
160 (*this) << classNamLen_mod;
207 namespace serialization
213 template <
typename VEC>
218 if (
n)
s.WriteBufferFixEndianness(&
v[0],
n);
221 template <
typename VEC>
227 if (
n)
s.ReadBufferFixEndianness(&
v[0],
n);
329 #if MRPT_WORD_SIZE != 32 // If it's 32 bit, size_t <=> uint32_t
348 std::vector<uint8_t>
b(
n);
349 in.ReadBuffer((
void*)&
b[0],
sizeof(
b[0]) *
n);
352 for (it =
a.begin(), it2 =
b.begin(); it !=
a.end(); ++it, ++it2)
363 if (
n)
in.ReadBuffer((
void*)&str[0],
n);
372 if (l)
in.ReadBuffer(
s, l);
378 #define CARCHIVE_VERBOSE 0
383 uint8_t lengthReadClassName = 255;
384 char readClassName[260];
385 readClassName[0] = 0;
391 if (
sizeof(lengthReadClassName) !=
393 (
void*)&lengthReadClassName,
sizeof(lengthReadClassName)))
397 if (!(lengthReadClassName & 0x80))
403 "Cannot read object header from stream! (EOF?)");
404 if (buf[0] || buf[1] || buf[2])
406 "Expecting 0x00 00 00 while parsing old streaming header "
407 "(Perhaps it's a gz-compressed stream? Use a GZ-stream for "
416 lengthReadClassName &= 0x7F;
419 if (lengthReadClassName > 120)
421 "Class name has more than 120 chars. This probably means a "
422 "corrupted binary stream.");
424 if (((
size_t)lengthReadClassName) !=
425 ReadBuffer(readClassName, lengthReadClassName))
428 readClassName[lengthReadClassName] =
'\0';
431 strClassName = readClassName;
438 if (
sizeof(version_old) !=
441 "Cannot read object streaming version from stream!");
442 ASSERT_(version_old >= 0 && version_old < 255);
443 version =
int8_t(version_old);
446 strClassName !=
"nullptr" &&
447 sizeof(version) !=
ReadBuffer((
void*)&version,
sizeof(version)))
450 "Cannot read object streaming version from stream!");
455 cerr <<
"[CArchive::ReadObject] readClassName:" << strClassName
456 <<
" version: " << version << endl;
459 catch (std::bad_alloc&)
463 catch (std::exception& e)
465 if (lengthReadClassName == 255)
473 e,
"Exception while parsing typed object '%s' from stream!\n",
492 obj->serializeFrom(*
this, version);
497 if (
sizeof(endFlag) !=
ReadBuffer((
void*)&endFlag,
sizeof(endFlag)))
499 "Cannot read object streaming version from stream!");
502 "end-flag missing: There is a bug in the deserialization "
503 "method of class: '%s'",
504 strClassName.c_str());
507 catch (std::bad_alloc&)
511 catch (std::exception&)
536 ASSERT_(existingObj && strClassName !=
"nullptr");
537 ASSERT_(strClassName !=
"nullptr");
541 if (
id->className != strClassName &&
id->altName != strClassName)
543 "Stored class does not match with existing object!!:\n Stored: "
545 strClassName.c_str(),
id->className));
551 CArchive&
s,
const std::vector<std::string>& vec)
555 for (
size_t i = 0; i < N; i++)
s << vec[i];
560 CArchive&
s, std::vector<std::string>& vec)
565 for (
size_t i = 0; i < N; i++) s >> vec[i];
573 unsigned char buf[0x10100];
574 unsigned int nBytesTx = 0;
576 const bool msg_format_is_tiny = msg.
content.size() < 256;
579 buf[nBytesTx++] = msg_format_is_tiny ? 0x69 : 0x79;
580 buf[nBytesTx++] = (
unsigned char)(msg.
type);
582 if (msg_format_is_tiny)
584 buf[nBytesTx++] = (
unsigned char)msg.
content.size();
588 buf[nBytesTx++] = msg.
content.size() & 0xff;
589 buf[nBytesTx++] = (msg.
content.size() >> 8) & 0xff;
594 nBytesTx += (
unsigned char)msg.
content.size();
595 buf[nBytesTx++] = 0x96;
606 std::vector<unsigned char> buf(66000);
607 unsigned int nBytesInFrame = 0;
608 unsigned long nBytesToRx = 0;
609 unsigned char tries = 2;
610 unsigned int payload_len = 0;
611 unsigned int expectedLen = 0;
615 if (nBytesInFrame < 4)
621 payload_len = buf[2];
622 expectedLen = payload_len + 4;
624 else if (buf[0] == 0x79)
628 expectedLen = payload_len + 5;
630 nBytesToRx = expectedLen - nBytesInFrame;
633 unsigned long nBytesRx = 0;
636 nBytesRx =
ReadBuffer(&buf[nBytesInFrame], nBytesToRx);
644 if (!nBytesRx)
return false;
646 if (!nBytesInFrame && buf[0] != 0x69 && buf[0] != 0x79)
649 if (!tries--)
return false;
654 nBytesInFrame += nBytesRx;
656 if (nBytesInFrame == expectedLen)
662 if (buf[nBytesInFrame - 1] != 0x96)
674 msg.
content.resize(payload_len);
680 msg.
content.resize(payload_len);
virtual void serializeTo(CArchive &out) const =0
Pure virtual method for writing (serializing) to an abstract archive.
const Scalar * const_iterator
A structure that holds runtime class type information.
std::shared_ptr< CSerializable > Ptr
unsigned __int16 uint16_t
#define MAKEWORD16B(__LOBYTE, __HILOBYTE)
virtual size_t read(void *buf, size_t len)=0
Reads a block of bytes.
virtual const mrpt::rtti::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
GLsizei GLsizei GLuint * obj
uint32_t type
An identifier of the message type (only the least-sig byte is typically sent)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
size_t ReadBuffer(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer.
CArchive & operator>>(CArchive &s, mrpt::aligned_std_vector< float > &a)
#define THROW_EXCEPTION(msg)
#define ASSERT_(f)
Defines an assertion mechanism.
Virtual base class for "archives": classes abstracting I/O streams.
#define THROW_STACKED_EXCEPTION_CUSTOM_MSG2(e, stuff, param1)
virtual size_t write(const void *buf, size_t len)=0
Writes a block of bytes.
std::vector< T, mrpt::aligned_allocator_cpp11< T > > aligned_std_vector
A class that contain generic messages, that can be sent and received from a "CClientTCPSocket" object...
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
std::vector< uint8_t > content
The contents of the message (memory is automatically handled by the std::vector object)
bool receiveMessage(CMessage &msg)
Tries to receive a message from the device.
unsigned __int64 uint64_t
The virtual base class which provides a unified interface for all persistent objects in MRPT.
void internal_ReadObject(CSerializable *newObj, const std::string &className, bool isOldFormat, int8_t version)
Read the object.
#define THROW_TYPED_EXCEPTION(msg, exceptionClass)
CArchive & operator<<(CArchive &s, const mrpt::aligned_std_vector< float > &a)
void WriteObject(const CSerializable *o)
Writes an object to the stream.
virtual uint8_t serializeGetVersion() const =0
Must return the current versioning number of the object.
void sendMessage(const CMessage &msg)
Send a message to the device.
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...
const uint8_t SERIALIZATION_END_FLAG
#define IMPLEMENT_CArchive_READ_WRITE_SIMPLE_TYPE(T)
CArchive & readStdVectorToStream(CArchive &s, VEC &v)
Used in mrpt::serialization::CArchive.
CArchive & writeStdVectorToStream(CArchive &s, const VEC &v)
CArchive & operator<<(const CSerializable &obj)
Write a CSerializable object to a stream in the binary MRPT format.
CSerializable::Ptr ReadObject()
Reads an object from stream, its class determined at runtime, and returns a smart pointer to the obje...
GLsizei const GLchar ** string
unsigned __int32 uint32_t
GLubyte GLubyte GLubyte a
void internal_ReadObjectHeader(std::string &className, bool &isOldFormat, int8_t &version)
Read the object Header.
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
CArchive & operator>>(CSerializable &obj)
Reads a CSerializable object from the stream.
Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST | |