diff options
Diffstat (limited to 'c++-libbu++/src/gatsstream.cpp')
-rw-r--r-- | c++-libbu++/src/gatsstream.cpp | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/c++-libbu++/src/gatsstream.cpp b/c++-libbu++/src/gatsstream.cpp new file mode 100644 index 0000000..d5e3f82 --- /dev/null +++ b/c++-libbu++/src/gatsstream.cpp | |||
@@ -0,0 +1,108 @@ | |||
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 | |||
8 | Gats::GatsStream::GatsStream( Bu::Stream &rStream ) : | ||
9 | rStream( rStream ) | ||
10 | { | ||
11 | } | ||
12 | |||
13 | Gats::GatsStream::~GatsStream() | ||
14 | { | ||
15 | } | ||
16 | |||
17 | Gats::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 | |||
74 | void 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 | |||
88 | bool 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 | |||