aboutsummaryrefslogtreecommitdiff
path: root/c++-qt/src/gatsstream.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'c++-qt/src/gatsstream.cpp')
-rw-r--r--c++-qt/src/gatsstream.cpp116
1 files changed, 116 insertions, 0 deletions
diff --git a/c++-qt/src/gatsstream.cpp b/c++-qt/src/gatsstream.cpp
new file mode 100644
index 0000000..d32b2b2
--- /dev/null
+++ b/c++-qt/src/gatsstream.cpp
@@ -0,0 +1,116 @@
1#include "gats-qt/gatsstream.h"
2#include "gats-qt/object.h"
3
4#ifdef WIN32
5#include <winsock2.h>
6#else
7#include <arpa/inet.h>
8#endif
9
10#include <QBuffer>
11
12Gats::GatsStream::GatsStream( QIODevice &rStream ) :
13 rStream( rStream )
14{
15}
16
17Gats::GatsStream::~GatsStream()
18{
19}
20
21Gats::Object *Gats::GatsStream::readObject()
22{
23 char buf[1500];
24
25 do
26 {
27 if( qbRead.size() < 5 )
28 {
29 int iRead = rStream.read( buf, 5-qbRead.size() );
30 qbRead.append( buf, iRead );
31
32 if( qbRead.size() < 5 )
33 return NULL;
34 }
35 } while( !skipReadNulls() );
36
37 uint8_t uVer;
38 uVer = qbRead[0];
39
40 int32_t iSize;
41 memcpy( &iSize, qbRead.constData()+1, 4 );
42 iSize = ntohl( iSize );
43 while( qbRead.size() < iSize )
44 {
45 int32_t iRead = iSize - qbRead.size();
46 if( iRead > 1500 )
47 iRead = 1500;
48 int32_t iReal = rStream.read( buf, iRead );
49 qbRead.append( buf, iReal );
50 if( iReal < iRead )
51 {
52 return NULL;
53 }
54 }
55
56 if( qbRead.size() < iSize )
57 {
58 return NULL;
59 }
60
61 QBuffer rTmp( &qbRead );
62 rTmp.open( QIODevice::ReadOnly );
63 rTmp.seek( 5 );
64 Gats::Object *pObj = Gats::Object::read( rTmp );
65
66 return pObj;
67}
68
69void Gats::GatsStream::writeObject( Gats::Object *pObject )
70{
71 QIODevice *pTmp;
72 if( rStream.isSequential() )
73 {
74 pTmp = new QBuffer();
75 pTmp->open( QIODevice::WriteOnly );
76 }
77 else
78 {
79 pTmp = &rStream;
80 }
81
82 uint8_t uBuf = 1;
83 uint32_t iSize = 0;
84 pTmp->write( (const char *)&uBuf, 1 );
85 uint64_t iSizePos = pTmp->pos();
86 pTmp->write( (const char *)&iSize, 4 );
87 pObject->write( *pTmp );
88 iSize = htonl( pTmp->pos() );
89 pTmp->seek( iSizePos );
90 pTmp->write( (const char *)&iSize, 4 );
91 pTmp->close();
92
93 if( rStream.isSequential() )
94 {
95 rStream.write( ((QBuffer *)pTmp)->data() );
96 delete pTmp;
97 }
98}
99
100bool Gats::GatsStream::skipReadNulls()
101{
102 bool bHaveSeeked = false;
103 for(;;)
104 {
105 if( qbRead.size() == 0 )
106 return false;
107 if( qbRead.at(0) != 0 )
108 return !bHaveSeeked; //true;
109 else
110 {
111 qbRead.remove( 0, 1 );
112 bHaveSeeked = true;
113 }
114 }
115}
116