25 #define SERIALIZATION_END_FLAG 0x88 50 size_t actuallyRead = Read(Buffer,Count);
53 THROW_EXCEPTION(
"(EOF?) Cannot read requested number of bytes from stream" );
71 if ( Count != Write(Buffer,Count) )
79 #if MRPT_IS_BIG_ENDIAN 81 #define IMPLEMENT_CSTREAM_READ_WRITE_SIMPLE_TYPE( T ) \ 82 CStream& utils::operator<<(mrpt::utils::CStream&out, const T &a) \ 85 ::memcpy(&b,&a,sizeof(b)); \ 86 mrpt::utils::reverseBytesInPlace(b); \ 87 out.WriteBuffer( (void*)&b, sizeof(b) ); \ 90 CStream& utils::operator>>(mrpt::utils::CStream&in, T &a) \ 93 in.ReadBuffer( (void*)&b, sizeof(a) ); \ 94 mrpt::utils::reverseBytesInPlace(b); \ 95 ::memcpy(&a,&b,sizeof(b)); \ 100 #define IMPLEMENT_CSTREAM_READ_WRITE_SIMPLE_TYPE( T ) \ 101 CStream& utils::operator<<(mrpt::utils::CStream&out, const T &a) \ 103 out.WriteBuffer( (void*)&a, sizeof(a) ); \ 106 CStream& utils::operator>>(mrpt::utils::CStream&in, T &a) \ 108 in.ReadBuffer( (void*)&a, sizeof(a) ); \ 125 #ifdef HAVE_LONG_DOUBLE 148 for (it=
a.begin(),it2=
b.begin();it!=
a.end();++it,++it2) *it2 = *it ? 1:0;
174 const char *className;
181 className =
"nullptr";
184 int8_t classNamLen = strlen(className);
185 int8_t classNamLen_mod = classNamLen | 0x80;
187 (*this) << classNamLen_mod;
188 this->WriteBuffer( className, classNamLen);
197 (*this) << actualVersion;
213 WriteObject(pObj.pointer());
249 s <<
n;
if (
n)
s.WriteBufferFixEndianness( &
v[0],
n );
254 v.resize(
n);
if (
n)
s.ReadBufferFixEndianness( &
v[0],
n );
274 s <<
n;
if (
n)
s.WriteBufferFixEndianness( &
v[0],
n );
280 s <<
n;
if (
n)
s.WriteBufferFixEndianness( &
v[0],
n );
298 if (
n)
s.ReadBufferFixEndianness( &
v[0],
n );
305 if (
n)
s.ReadBufferFixEndianness( &
v[0],
n );
309 #if MRPT_WORD_SIZE!=32 // If it's 32 bit, size_t <=> uint32_t 322 in.ReadBuffer( (
void*)&
b[0],
sizeof(
b[0])*
n);
325 for (it=
a.begin(),it2=
b.begin();it!=
a.end();++it,++it2) *it = (*it2!=0);
336 in.ReadBuffer( (
void*)&str[0],
n );
347 in.ReadBuffer(
s, l );
354 #define CSTREAM_VERBOSE 0 356 template <
bool EXISTING_OBJ>
363 uint8_t lengthReadClassName = 255;
364 bool isOldFormat=
false;
365 char readClassName[260];
366 readClassName[0] = 0;
371 if (
sizeof(lengthReadClassName) != ReadBuffer( (
void*)&lengthReadClassName,
sizeof(lengthReadClassName) ) )
375 if (! (lengthReadClassName & 0x80 ))
379 if (3 != ReadBuffer( buf, 3 ) )
THROW_EXCEPTION(
"Cannot read object header from stream! (EOF?)");
380 if (buf[0] || buf[1] || buf[2])
THROW_EXCEPTION(
"Expecting 0x00 00 00 while parsing old streaming header (Perhaps it's a gz-compressed stream? Use a GZ-stream for reading)");
384 lengthReadClassName &= 0x7F;
387 if (lengthReadClassName>120)
388 THROW_EXCEPTION(
"Class name has more than 120 chars. This probably means a corrupted binary stream.");
390 if (((
size_t)lengthReadClassName)!=ReadBuffer( readClassName, lengthReadClassName ))
393 readClassName[lengthReadClassName]=
'\0';
404 if (
sizeof(version_old)!=ReadBufferFixEndianness( &version_old, 1 ))
406 ASSERT_(version_old>=0 && version_old<255);
409 else if (strClassName !=
"nullptr" &&
sizeof(
version)!=ReadBuffer( (
void*)&
version,
sizeof(
version) ))
416 cerr <<
"[CStream::ReadObject] readClassName:" << strClassName <<
" version: " <<
version << endl;
423 if(strClassName !=
"nullptr")
428 if (!id2)
THROW_EXCEPTION_FMT(
"Stored object has class '%s' which is not registered!",strClassName.c_str());
434 else if (strClassName !=
"nullptr")
440 const std::string msg =
format(
"Class '%s' is not registered! Have you called mrpt::registerClass(CLASS)?",readClassName);
441 std::cerr <<
"CStream::ReadObject(): " << msg << std::endl;
445 newObj = CSerializablePtr(
obj);
448 if(strClassName !=
"nullptr")
457 if (
sizeof(endFlag)!=ReadBuffer( (
void*)&endFlag,
sizeof(endFlag) ))
THROW_EXCEPTION(
"Cannot read object streaming version from stream!");
460 ASSERT_(!EXISTING_OBJ || strClassName !=
"nullptr");
462 catch (std::bad_alloc &)
466 catch(std::exception &e)
468 if (lengthReadClassName==255) {
488 CSerializablePtr ret;
489 internal_ReadObject<false>(ret, NULL);
500 CSerializablePtr dummy;
501 internal_ReadObject<true>(dummy,existingObj);
511 if (!fmt)
throw std::runtime_error(
"fmt in CStream::printf cannot be NULL");
513 int result = -1,
length = 1024;
525 if (result>=
length) result=-1;
529 size_t l = strlen(&
buffer[0]);
530 WriteBuffer( &
buffer[0], (
int)l );
537 CStream &utils::operator<<(mrpt::utils::CStream &s,const std::vector<std::string> &vec) {
540 for (
size_t i=0;i<N;i++)
s<<vec[i];
548 for (
size_t i=0;i<N;i++) s>>vec[i];
560 unsigned char buf[0x10100];
561 unsigned int nBytesTx = 0;
563 const bool msg_format_is_tiny = msg.
content.size() < 256;
566 buf[nBytesTx++] = msg_format_is_tiny ? 0x69 : 0x79;
567 buf[nBytesTx++] = (
unsigned char)(msg.
type);
569 if (msg_format_is_tiny) {
570 buf[nBytesTx++] = (
unsigned char)msg.
content.size();
572 buf[nBytesTx++] = msg.
content.size() & 0xff;
573 buf[nBytesTx++] = (msg.
content.size()>>8) & 0xff;
578 nBytesTx += (
unsigned char)msg.
content.size();
579 buf[nBytesTx++] = 0x96;
582 WriteBuffer(buf,nBytesTx);
593 std::vector<unsigned char> buf(66000);
594 unsigned int nBytesInFrame=0;
595 unsigned long nBytesToRx=0;
596 unsigned char tries = 2;
597 unsigned int payload_len = 0;
598 unsigned int expectedLen = 0;
608 payload_len = buf[2];
609 expectedLen = payload_len + 4;
611 else if( buf[0] == 0x79 )
614 expectedLen = payload_len + 5;
616 nBytesToRx = expectedLen - nBytesInFrame;
619 unsigned long nBytesRx = 0;
621 nBytesRx = ReadBufferImmediate(&buf[nBytesInFrame], nBytesToRx);
630 if (!nBytesInFrame && buf[0]!=0x69 && buf[0]!=0x79 )
633 if (!tries--)
return false;
638 nBytesInFrame += nBytesRx;
640 if (nBytesInFrame == expectedLen )
646 if (buf[nBytesInFrame-1]!=0x96)
658 msg.
content.resize(payload_len);
662 if ( buf[0] == 0x79 )
664 msg.
content.resize(payload_len);
687 size_t N = out_str.size();
689 if (! Read(&out_str[N], 1) )
693 if (out_str[N]==
'\r')
697 else if (out_str[N]==
'\n')
void BASE_IMPEXP memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) MRPT_NO_THROWS
An OS and compiler independent version of "memcpy".
size_t ReadBuffer(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer.
CSerializablePtr ReadObject()
Reads an object from stream, its class determined at runtime, and returns a smart pointer to the obje...
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...
The virtual base class which provides a unified interface for all persistent objects in MRPT...
void BASE_IMPEXP registerAllPendingClasses()
Register all pending classes - to be called just before de-serializing an object, for example...
#define THROW_STACKED_EXCEPTION_CUSTOM_MSG2(e, stuff, param1)
#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.
CStream & operator<<(const CSerializablePtr &pObj)
Write an object to a stream in the binary MRPT format.
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)
void internal_ReadObject(CSerializablePtr &newObj, CSerializable *existingObj=NULL)
A common template code for both versions of CStream::ReadObject()
std::vector< bool > vector_bool
A type for passing a vector of bools.
std::vector< int64_t > vector_long
::mrpt::utils::CStream & operator>>(mrpt::utils::CStream &in, CImagePtr &pObj)
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
mrpt::utils::CObject * createObject() const
const TRuntimeClassId BASE_IMPEXP * findRegisteredClass(const std::string &className)
Return info about a given class by its name, or NULL if the class is not registered.
std::string BASE_IMPEXP 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 BASE_IMPEXP & operator<<(mrpt::utils::CStream &s, const char *a)
GLsizei const GLchar ** string
Used in mrpt::utils::CStream.
#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.
CStream & operator>>(CSerializablePtr &pObj)
std::vector< uint16_t > vector_word
std::vector< int16_t > vector_signed_word
GLuint GLsizei GLsizei * length
virtual const mrpt::utils::TRuntimeClassId * GetRuntimeClass() const
Returns information about the class of an object in runtime.
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
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)