diff options
author | Mike Buland <eichlan@xagasoft.com> | 2012-11-09 17:20:11 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2012-11-09 17:20:11 +0000 |
commit | d534a56d95bca7bdd812be024d9eacba4734e2b7 (patch) | |
tree | f9b98ee2b80e645a7b54e7934882be6c9f73c165 /c++-libbu++/src/gatsstream.cpp | |
parent | 61ccc86fdf06f12cb72a8b7e65286f812cf62154 (diff) | |
download | libgats-d534a56d95bca7bdd812be024d9eacba4734e2b7.tar.gz libgats-d534a56d95bca7bdd812be024d9eacba4734e2b7.tar.bz2 libgats-d534a56d95bca7bdd812be024d9eacba4734e2b7.tar.xz libgats-d534a56d95bca7bdd812be024d9eacba4734e2b7.zip |
Many changes: tabconv'd the C++ code, added a license, BSD, and docs.
Diffstat (limited to '')
-rw-r--r-- | c++-libbu++/src/gatsstream.cpp | 165 |
1 files changed, 86 insertions, 79 deletions
diff --git a/c++-libbu++/src/gatsstream.cpp b/c++-libbu++/src/gatsstream.cpp index d5e3f82..05fbd84 100644 --- a/c++-libbu++/src/gatsstream.cpp +++ b/c++-libbu++/src/gatsstream.cpp | |||
@@ -1,3 +1,10 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2012 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libgats library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
1 | #include "gats/gatsstream.h" | 8 | #include "gats/gatsstream.h" |
2 | #include "gats/object.h" | 9 | #include "gats/object.h" |
3 | 10 | ||
@@ -6,7 +13,7 @@ | |||
6 | // using namespace Bu; | 13 | // using namespace Bu; |
7 | 14 | ||
8 | Gats::GatsStream::GatsStream( Bu::Stream &rStream ) : | 15 | Gats::GatsStream::GatsStream( Bu::Stream &rStream ) : |
9 | rStream( rStream ) | 16 | rStream( rStream ) |
10 | { | 17 | { |
11 | } | 18 | } |
12 | 19 | ||
@@ -16,93 +23,93 @@ Gats::GatsStream::~GatsStream() | |||
16 | 23 | ||
17 | Gats::Object *Gats::GatsStream::readObject() | 24 | Gats::Object *Gats::GatsStream::readObject() |
18 | { | 25 | { |
19 | char buf[1500]; | 26 | char buf[1500]; |
20 | 27 | ||
21 | // sio << "Gats::GatsStream::readObject(): Scanning for object header." << sio.nl; | 28 | // sio << "Gats::GatsStream::readObject(): Scanning for object header." << sio.nl; |
22 | do | 29 | do |
23 | { | 30 | { |
24 | if( qbRead.getSize() < 5 ) | 31 | if( qbRead.getSize() < 5 ) |
25 | { | 32 | { |
26 | // sio << "Gats::GatsStream::readObject(): reading header data, need 5b, have " << qbRead.getSize() << "b." << sio.nl; | 33 | // sio << "Gats::GatsStream::readObject(): reading header data, need 5b, have " << qbRead.getSize() << "b." << sio.nl; |
27 | int iRead = rStream.read( buf, 5-qbRead.getSize() ); | 34 | int iRead = rStream.read( buf, 5-qbRead.getSize() ); |
28 | qbRead.write( buf, iRead ); | 35 | qbRead.write( buf, iRead ); |
29 | 36 | ||
30 | if( qbRead.getSize() < 5 ) | 37 | if( qbRead.getSize() < 5 ) |
31 | return NULL; | 38 | return NULL; |
32 | } | 39 | } |
33 | } while( !skipReadNulls() ); | 40 | } while( !skipReadNulls() ); |
34 | 41 | ||
35 | uint8_t uVer; | 42 | uint8_t uVer; |
36 | qbRead.peek( &uVer, 1 ); | 43 | qbRead.peek( &uVer, 1 ); |
37 | // sio << "Gats::GatsStream::readObject(): Packet version: " << (int)uVer << sio.nl; | 44 | // sio << "Gats::GatsStream::readObject(): Packet version: " << (int)uVer << sio.nl; |
38 | 45 | ||
39 | int32_t iSize; | 46 | int32_t iSize; |
40 | qbRead.peek( &iSize, 4, 1 ); | 47 | qbRead.peek( &iSize, 4, 1 ); |
41 | iSize = be32toh( iSize ); | 48 | iSize = be32toh( iSize ); |
42 | // sio << "Gats::GatsStream::readObject(): Header read, looking for " << iSize << "b, we have " << qbRead.getSize() << "b." << sio.nl; | 49 | // sio << "Gats::GatsStream::readObject(): Header read, looking for " << iSize << "b, we have " << qbRead.getSize() << "b." << sio.nl; |
43 | while( qbRead.getSize() < iSize ) | 50 | while( qbRead.getSize() < iSize ) |
44 | { | 51 | { |
45 | int32_t iRead = iSize - qbRead.getSize(); | 52 | int32_t iRead = iSize - qbRead.getSize(); |
46 | if( iRead > 1500 ) | 53 | if( iRead > 1500 ) |
47 | iRead = 1500; | 54 | iRead = 1500; |
48 | // sio << "Gats::GatsStream::readObject(): Attempting to read " << iRead << "b." << sio.nl; | 55 | // sio << "Gats::GatsStream::readObject(): Attempting to read " << iRead << "b." << sio.nl; |
49 | int32_t iReal = rStream.read( buf, iRead ); | 56 | int32_t iReal = rStream.read( buf, iRead ); |
50 | // sio << "Gats::GatsStream::readObject(): Read " << iReal << "b." << sio.nl; | 57 | // sio << "Gats::GatsStream::readObject(): Read " << iReal << "b." << sio.nl; |
51 | qbRead.write( buf, iReal ); | 58 | qbRead.write( buf, iReal ); |
52 | if( iReal < iRead ) | 59 | if( iReal < iRead ) |
53 | { | 60 | { |
54 | // sio << "Gats::GatsStream::readObject(): Insufficient data read in block, bailing on read." << sio.nl; | 61 | // sio << "Gats::GatsStream::readObject(): Insufficient data read in block, bailing on read." << sio.nl; |
55 | return NULL; | 62 | return NULL; |
56 | } | 63 | } |
57 | } | 64 | } |
58 | 65 | ||
59 | if( qbRead.getSize() < iSize ) | 66 | if( qbRead.getSize() < iSize ) |
60 | { | 67 | { |
61 | // sio << "Gats::GatsStream::readObject(): Somehow, we still don't have enough data, bailing." << sio.nl; | 68 | // sio << "Gats::GatsStream::readObject(): Somehow, we still don't have enough data, bailing." << sio.nl; |
62 | return NULL; | 69 | return NULL; |
63 | } | 70 | } |
64 | 71 | ||
65 | // sio << "Gats::GatsStream::readObject(): We have " << qbRead.getSize() << "b of " << iSize << "b, time to read the object." << sio.nl; | 72 | // sio << "Gats::GatsStream::readObject(): We have " << qbRead.getSize() << "b of " << iSize << "b, time to read the object." << sio.nl; |
66 | 73 | ||
67 | qbRead.seek( 5 ); | 74 | qbRead.seek( 5 ); |
68 | Gats::Object *pObj = Gats::Object::read( qbRead ); | 75 | Gats::Object *pObj = Gats::Object::read( qbRead ); |
69 | 76 | ||
70 | // sio << "Gats::GatsStream::readObject(): Read completed, there are " << qbRead.getSize() << "b left in the buffer." << sio.nl; | 77 | // sio << "Gats::GatsStream::readObject(): Read completed, there are " << qbRead.getSize() << "b left in the buffer." << sio.nl; |
71 | return pObj; | 78 | return pObj; |
72 | } | 79 | } |
73 | 80 | ||
74 | void Gats::GatsStream::writeObject( Gats::Object *pObject ) | 81 | void Gats::GatsStream::writeObject( Gats::Object *pObject ) |
75 | { | 82 | { |
76 | Bu::NullStream ns; | 83 | Bu::NullStream ns; |
77 | pObject->write( ns ); | 84 | pObject->write( ns ); |
78 | 85 | ||
79 | uint8_t uBuf = 1; | 86 | uint8_t uBuf = 1; |
80 | int32_t iSize = htobe32( ns.tell()+5 ); | 87 | int32_t iSize = htobe32( ns.tell()+5 ); |
81 | rStream.write( &uBuf, 1 ); | 88 | rStream.write( &uBuf, 1 ); |
82 | rStream.write( &iSize, 4 ); | 89 | rStream.write( &iSize, 4 ); |
83 | pObject->write( rStream ); | 90 | pObject->write( rStream ); |
84 | 91 | ||
85 | // sio << "Object consumed " << ns.tell() << "b." << sio.nl; | 92 | // sio << "Object consumed " << ns.tell() << "b." << sio.nl; |
86 | } | 93 | } |
87 | 94 | ||
88 | bool Gats::GatsStream::skipReadNulls() | 95 | bool Gats::GatsStream::skipReadNulls() |
89 | { | 96 | { |
90 | char buf; | 97 | char buf; |
91 | 98 | ||
92 | // sio << "Gats::GatsStream::skipReadNulls(): Scanning for nulls, " << qbRead.getSize() << "b." << sio.nl; | 99 | // sio << "Gats::GatsStream::skipReadNulls(): Scanning for nulls, " << qbRead.getSize() << "b." << sio.nl; |
93 | bool bHaveSeeked = false; | 100 | bool bHaveSeeked = false; |
94 | for(;;) | 101 | for(;;) |
95 | { | 102 | { |
96 | if( qbRead.peek( &buf, 1 ) == 0 ) | 103 | if( qbRead.peek( &buf, 1 ) == 0 ) |
97 | return false; | 104 | return false; |
98 | if( buf != 0 ) | 105 | if( buf != 0 ) |
99 | return !bHaveSeeked; //true; | 106 | return !bHaveSeeked; //true; |
100 | else | 107 | else |
101 | { | 108 | { |
102 | // sio << "Gats::GatsStream::skipReadNulls(): Null byte read, not header yet..." << sio.nl; | 109 | // sio << "Gats::GatsStream::skipReadNulls(): Null byte read, not header yet..." << sio.nl; |
103 | qbRead.seek( 1 ); | 110 | qbRead.seek( 1 ); |
104 | bHaveSeeked = true; | 111 | bHaveSeeked = true; |
105 | } | 112 | } |
106 | } | 113 | } |
107 | } | 114 | } |
108 | 115 | ||