aboutsummaryrefslogtreecommitdiff
path: root/src/gatsstream.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-11-09 16:25:22 +0000
committerMike Buland <eichlan@xagasoft.com>2012-11-09 16:25:22 +0000
commit74dd68ad611d15abf16a65c36a7cfd3f4492930a (patch)
tree843fed9ba6bb03253a01314afc3b1dfbb2dfd26c /src/gatsstream.cpp
parentd9b407475ae3ebe434b29d9eabdd7d4416e17881 (diff)
downloadlibgats-74dd68ad611d15abf16a65c36a7cfd3f4492930a.tar.gz
libgats-74dd68ad611d15abf16a65c36a7cfd3f4492930a.tar.bz2
libgats-74dd68ad611d15abf16a65c36a7cfd3f4492930a.tar.xz
libgats-74dd68ad611d15abf16a65c36a7cfd3f4492930a.zip
Made the repo less libbu++-centric.
Diffstat (limited to 'src/gatsstream.cpp')
-rw-r--r--src/gatsstream.cpp108
1 files changed, 0 insertions, 108 deletions
diff --git a/src/gatsstream.cpp b/src/gatsstream.cpp
deleted file mode 100644
index d5e3f82..0000000
--- a/src/gatsstream.cpp
+++ /dev/null
@@ -1,108 +0,0 @@
1#include "gats/gatsstream.h"
2#include "gats/object.h"
3
4// #include <bu/sio.h>
5#include <bu/nullstream.h>
6// using namespace Bu;
7
8Gats::GatsStream::GatsStream( Bu::Stream &rStream ) :
9 rStream( rStream )
10{
11}
12
13Gats::GatsStream::~GatsStream()
14{
15}
16
17Gats::Object *Gats::GatsStream::readObject()
18{
19 char buf[1500];
20
21 // sio << "Gats::GatsStream::readObject(): Scanning for object header." << sio.nl;
22 do
23 {
24 if( qbRead.getSize() < 5 )
25 {
26 // sio << "Gats::GatsStream::readObject(): reading header data, need 5b, have " << qbRead.getSize() << "b." << sio.nl;
27 int iRead = rStream.read( buf, 5-qbRead.getSize() );
28 qbRead.write( buf, iRead );
29
30 if( qbRead.getSize() < 5 )
31 return NULL;
32 }
33 } while( !skipReadNulls() );
34
35 uint8_t uVer;
36 qbRead.peek( &uVer, 1 );
37 // sio << "Gats::GatsStream::readObject(): Packet version: " << (int)uVer << sio.nl;
38
39 int32_t iSize;
40 qbRead.peek( &iSize, 4, 1 );
41 iSize = be32toh( iSize );
42 // sio << "Gats::GatsStream::readObject(): Header read, looking for " << iSize << "b, we have " << qbRead.getSize() << "b." << sio.nl;
43 while( qbRead.getSize() < iSize )
44 {
45 int32_t iRead = iSize - qbRead.getSize();
46 if( iRead > 1500 )
47 iRead = 1500;
48 // sio << "Gats::GatsStream::readObject(): Attempting to read " << iRead << "b." << sio.nl;
49 int32_t iReal = rStream.read( buf, iRead );
50 // sio << "Gats::GatsStream::readObject(): Read " << iReal << "b." << sio.nl;
51 qbRead.write( buf, iReal );
52 if( iReal < iRead )
53 {
54 // sio << "Gats::GatsStream::readObject(): Insufficient data read in block, bailing on read." << sio.nl;
55 return NULL;
56 }
57 }
58
59 if( qbRead.getSize() < iSize )
60 {
61 // sio << "Gats::GatsStream::readObject(): Somehow, we still don't have enough data, bailing." << sio.nl;
62 return NULL;
63 }
64
65 // sio << "Gats::GatsStream::readObject(): We have " << qbRead.getSize() << "b of " << iSize << "b, time to read the object." << sio.nl;
66
67 qbRead.seek( 5 );
68 Gats::Object *pObj = Gats::Object::read( qbRead );
69
70 // sio << "Gats::GatsStream::readObject(): Read completed, there are " << qbRead.getSize() << "b left in the buffer." << sio.nl;
71 return pObj;
72}
73
74void Gats::GatsStream::writeObject( Gats::Object *pObject )
75{
76 Bu::NullStream ns;
77 pObject->write( ns );
78
79 uint8_t uBuf = 1;
80 int32_t iSize = htobe32( ns.tell()+5 );
81 rStream.write( &uBuf, 1 );
82 rStream.write( &iSize, 4 );
83 pObject->write( rStream );
84
85 // sio << "Object consumed " << ns.tell() << "b." << sio.nl;
86}
87
88bool Gats::GatsStream::skipReadNulls()
89{
90 char buf;
91
92 // sio << "Gats::GatsStream::skipReadNulls(): Scanning for nulls, " << qbRead.getSize() << "b." << sio.nl;
93 bool bHaveSeeked = false;
94 for(;;)
95 {
96 if( qbRead.peek( &buf, 1 ) == 0 )
97 return false;
98 if( buf != 0 )
99 return !bHaveSeeked; //true;
100 else
101 {
102 // sio << "Gats::GatsStream::skipReadNulls(): Null byte read, not header yet..." << sio.nl;
103 qbRead.seek( 1 );
104 bHaveSeeked = true;
105 }
106 }
107}
108