25 #define SERIALIZATION_END_FLAG 0x88 45 size_t actuallyRead = Read(Buffer, Count);
49 "(EOF?) Cannot read requested number of bytes from stream");
68 if (Count != Write(Buffer, Count))
75 #if MRPT_IS_BIG_ENDIAN 77 #define IMPLEMENT_CSTREAM_READ_WRITE_SIMPLE_TYPE(T) \ 78 CStream& utils::operator<<(mrpt::utils::CStream& out, const T a) \ 80 mrpt::utils::reverseBytesInPlace(a); \ 81 out.WriteBuffer((void*)&a, sizeof(a)); \ 84 CStream& utils::operator>>(mrpt::utils::CStream& in, T& a) \ 87 in.ReadBuffer((void*)&b, sizeof(a)); \ 88 mrpt::utils::reverseBytesInPlace(b); \ 89 ::memcpy(&a, &b, sizeof(b)); \ 94 #define IMPLEMENT_CSTREAM_READ_WRITE_SIMPLE_TYPE(T) \ 95 CStream& utils::operator<<(mrpt::utils::CStream& out, const T a) \ 97 out.WriteBuffer((void*)&a, sizeof(a)); \ 100 CStream& utils::operator>>(mrpt::utils::CStream& in, T& a) \ 102 in.ReadBuffer((void*)&a, sizeof(a)); \ 119 #ifdef HAVE_LONG_DOUBLE 140 for (it =
a.begin(), it2 =
b.begin(); it !=
a.end(); ++it, ++it2)
165 const char* className;
172 className =
"nullptr";
175 int8_t classNamLen = strlen(className);
176 int8_t classNamLen_mod = classNamLen | 0x80;
178 (*this) << classNamLen_mod;
179 this->WriteBuffer(className, classNamLen);
185 ASSERT_(version >= 0 && version < 255);
188 (*this) << actualVersion;
203 WriteObject(pObj.get());
238 template <
typename VEC>
243 if (
n)
s.WriteBufferFixEndianness(&
v[0],
n);
246 template <
typename VEC>
252 if (
n)
s.ReadBufferFixEndianness(&
v[0],
n);
260 CStream& utils::operator<<(mrpt::utils::CStream& s, const std::vector<float>&
a)
302 if (
n)
s.WriteBufferFixEndianness(&
v[0],
n);
310 if (
n)
s.WriteBufferFixEndianness(&
v[0],
n);
356 if (
n)
s.ReadBufferFixEndianness(&
v[0],
n);
365 if (
n)
s.ReadBufferFixEndianness(&
v[0],
n);
369 #if MRPT_WORD_SIZE != 32 // If it's 32 bit, size_t <=> uint32_t 389 in.ReadBuffer((
void*)&
b[0],
sizeof(
b[0]) *
n);
392 for (it =
a.begin(), it2 =
b.begin(); it !=
a.end(); ++it, ++it2)
403 if (
n)
in.ReadBuffer((
void*)&str[0],
n);
412 if (l)
in.ReadBuffer(
s, l);
418 #define CSTREAM_VERBOSE 0 423 uint8_t lengthReadClassName = 255;
424 char readClassName[260];
425 readClassName[0] = 0;
431 if (
sizeof(lengthReadClassName) !=
433 (
void*)&lengthReadClassName,
sizeof(lengthReadClassName)))
437 if (!(lengthReadClassName & 0x80))
441 if (3 != ReadBuffer(buf, 3))
443 "Cannot read object header from stream! (EOF?)");
444 if (buf[0] || buf[1] || buf[2])
446 "Expecting 0x00 00 00 while parsing old streaming header " 447 "(Perhaps it's a gz-compressed stream? Use a GZ-stream for " 456 lengthReadClassName &= 0x7F;
459 if (lengthReadClassName > 120)
461 "Class name has more than 120 chars. This probably means a " 462 "corrupted binary stream.");
464 if (((
size_t)lengthReadClassName) !=
465 ReadBuffer(readClassName, lengthReadClassName))
468 readClassName[lengthReadClassName] =
'\0';
471 strClassName = readClassName;
478 if (
sizeof(version_old) !=
479 ReadBufferFixEndianness(&version_old, 1 ))
481 "Cannot read object streaming version from stream!");
482 ASSERT_(version_old >= 0 && version_old < 255);
483 version =
int8_t(version_old);
486 strClassName !=
"nullptr" &&
487 sizeof(version) != ReadBuffer((
void*)&version,
sizeof(version)))
490 "Cannot read object streaming version from stream!");
495 cerr <<
"[CStream::ReadObject] readClassName:" << strClassName
496 <<
" version: " << version << endl;
499 catch (std::bad_alloc&)
503 catch (std::exception& e)
505 if (lengthReadClassName == 255)
513 e,
"Exception while parsing typed object '%s' from stream!\n",
532 obj->readFromStream(*
this, (
int)version);
537 if (
sizeof(endFlag) != ReadBuffer((
void*)&endFlag,
sizeof(endFlag)))
539 "Cannot read object streaming version from stream!");
542 "end-flag missing: There is a bug in the deserialization " 543 "method of class: '%s'",
544 strClassName.c_str());
547 catch (std::bad_alloc&)
551 catch (std::exception&)
572 internal_ReadObjectHeader(strClassName, isOldFormat, version);
574 ASSERT_(existingObj && strClassName !=
"nullptr");
575 ASSERT_(strClassName !=
"nullptr");
582 "Stored object has class '%s' which is not registered!",
583 strClassName.c_str());
587 "Stored class does not match with existing object!!:\n Stored: " 591 internal_ReadObject(existingObj, strClassName, isOldFormat, version);
601 if (!fmt)
throw std::runtime_error(
"fmt in CStream::printf cannot be NULL");
603 int result = -1,
length = 1024;
615 if (result >=
length) result = -1;
619 size_t l = strlen(&
buffer[0]);
620 WriteBuffer(&
buffer[0], (
int)l);
632 for (
size_t i = 0; i < N; i++)
s << vec[i];
642 for (
size_t i = 0; i < N; i++) s >> vec[i];
653 unsigned char buf[0x10100];
654 unsigned int nBytesTx = 0;
656 const bool msg_format_is_tiny = msg.
content.size() < 256;
659 buf[nBytesTx++] = msg_format_is_tiny ? 0x69 : 0x79;
660 buf[nBytesTx++] = (
unsigned char)(msg.
type);
662 if (msg_format_is_tiny)
664 buf[nBytesTx++] = (
unsigned char)msg.
content.size();
668 buf[nBytesTx++] = msg.
content.size() & 0xff;
669 buf[nBytesTx++] = (msg.
content.size() >> 8) & 0xff;
674 nBytesTx += (
unsigned char)msg.
content.size();
675 buf[nBytesTx++] = 0x96;
678 WriteBuffer(buf, nBytesTx);
689 std::vector<unsigned char> buf(66000);
690 unsigned int nBytesInFrame = 0;
691 unsigned long nBytesToRx = 0;
692 unsigned char tries = 2;
693 unsigned int payload_len = 0;
694 unsigned int expectedLen = 0;
698 if (nBytesInFrame < 4)
704 payload_len = buf[2];
705 expectedLen = payload_len + 4;
707 else if (buf[0] == 0x79)
711 expectedLen = payload_len + 5;
713 nBytesToRx = expectedLen - nBytesInFrame;
716 unsigned long nBytesRx = 0;
719 nBytesRx = ReadBufferImmediate(&buf[nBytesInFrame], nBytesToRx);
727 if (!nBytesRx)
return false;
729 if (!nBytesInFrame && buf[0] != 0x69 && buf[0] != 0x79)
732 if (!tries--)
return false;
737 nBytesInFrame += nBytesRx;
739 if (nBytesInFrame == expectedLen)
745 if (buf[nBytesInFrame - 1] != 0x96)
757 msg.
content.resize(payload_len);
763 msg.
content.resize(payload_len);
787 size_t N = out_str.size();
788 out_str.resize(N + 1);
789 if (!Read(&out_str[N], 1))
return false;
792 if (out_str[N] ==
'\r')
796 else if (out_str[N] ==
'\n')
size_t ReadBuffer(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
bool getline(std::string &out_str)
Reads from the stream until a ' ' character is found ('' characters are ignored). ...
unsigned __int16 uint16_t
std::vector< uint32_t > vector_uint
std::vector< uint8_t > vector_byte
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
virtual const mrpt::utils::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
The virtual base class which provides a unified interface for all persistent objects in MRPT...
CStream & operator>>(CSerializable::Ptr &pObj)
#define THROW_STACKED_EXCEPTION_CUSTOM_MSG2(e, stuff, param1)
void internal_ReadObjectHeader(std::string &className, bool &isOldFormat, int8_t &version)
Read the object Header.
#define THROW_EXCEPTION(msg)
virtual void writeToStream(mrpt::utils::CStream &out, int *getVersion) const =0
Introduces a pure virtual method responsible for writing to a CStream.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
CStream & readStdVectorToStream(mrpt::utils::CStream &s, VEC &v)
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
const Scalar * const_iterator
CStream & writeStdVectorToStream(mrpt::utils::CStream &s, const VEC &v)
GLsizei GLsizei GLuint * obj
std::vector< int8_t > vector_signed_byte
#define IMPLEMENT_CSTREAM_READ_WRITE_SIMPLE_TYPE(T)
std::vector< bool > vector_bool
A type for passing a vector of bools.
std::vector< int64_t > vector_long
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
CSerializable::Ptr ReadObject()
Reads an object from stream, its class determined at runtime, and returns a smart pointer to the obje...
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
#define THROW_TYPED_EXCEPTION(msg, exceptionClass)
Defines a unified way of reporting exceptions of type different than "std::exception".
void WriteObject(const CSerializable *o)
Writes an object to the stream.
CStream & operator<<(mrpt::utils::CStream &s, const char *a)
GLsizei const GLchar ** string
Used in mrpt::utils::CStream.
std::shared_ptr< CSerializable > Ptr
const TRuntimeClassId * findRegisteredClass(const std::string &className)
Return info about a given class by its name, or nullptr if the class is not registered.
#define SERIALIZATION_END_FLAG
unsigned __int64 uint64_t
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
bool receiveMessage(utils::CMessage &msg)
Tries to receive a message from the device.
void internal_ReadObject(CSerializable *newObj, const std::string &className, bool isOldFormat, int8_t version)
Read the object.
std::vector< uint16_t > vector_word
std::vector< int16_t > vector_signed_word
GLuint GLsizei GLsizei * length
CStream & operator<<(const CSerializable::Ptr &pObj)
Write an object to a stream in the binary MRPT format.
void sendMessage(const utils::CMessage &msg)
Send a message to the device.
std::vector< uint8_t > content
The contents of the message (memory is automatically handled by the std::vector object) ...
std::vector< int32_t > vector_int
CStream & operator>>(mrpt::utils::CStream &in, char *a)
A structure that holds runtime class type information.
unsigned __int32 uint32_t
GLubyte GLubyte GLubyte a
#define MAKEWORD16B(__LOBYTE, __HILOBYTE)
A class that contain generic messages, that can be sent and received from a "CClientTCPSocket" object...
virtual int printf(const char *fmt,...) MRPT_printf_format_check(2
Writes a string to the stream in a textual form.
uint32_t type
An identifier of the message type (only the least-sig byte is typically sent)
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".