aboutsummaryrefslogtreecommitdiff
path: root/src/gatsstream.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2010-08-19 06:28:17 +0000
committerMike Buland <eichlan@xagasoft.com>2010-08-19 06:28:17 +0000
commit11b5a91c5884d496744911f261ed6c2b053b9940 (patch)
tree61ed65f187e0719f141d80d4e1e648aeff311024 /src/gatsstream.cpp
parent9dc8cc535ef5fc4ea78f967fe285fe4424ff4458 (diff)
downloadlibgats-11b5a91c5884d496744911f261ed6c2b053b9940.tar.gz
libgats-11b5a91c5884d496744911f261ed6c2b053b9940.tar.bz2
libgats-11b5a91c5884d496744911f261ed6c2b053b9940.tar.xz
libgats-11b5a91c5884d496744911f261ed6c2b053b9940.zip
Wow, it pretty much all works. the float format is a little funny, I treat it
as a string, with a string header and then string data that is then turned into a float. It's pretty much how it's going to work, unless I come up with something revolutionary.
Diffstat (limited to 'src/gatsstream.cpp')
-rw-r--r--src/gatsstream.cpp85
1 files changed, 83 insertions, 2 deletions
diff --git a/src/gatsstream.cpp b/src/gatsstream.cpp
index 4b9cadf..38adfbb 100644
--- a/src/gatsstream.cpp
+++ b/src/gatsstream.cpp
@@ -1,7 +1,9 @@
1#include "gats/gatsstream.h" 1#include "gats/gatsstream.h"
2#include "gats/object.h" 2#include "gats/object.h"
3 3
4#include <bu/sio.h> 4#include <arpa/inet.h>
5
6// #include <bu/sio.h>
5#include <bu/nullstream.h> 7#include <bu/nullstream.h>
6using namespace Bu; 8using namespace Bu;
7 9
@@ -16,7 +18,59 @@ Gats::GatsStream::~GatsStream()
16 18
17Gats::Object *Gats::GatsStream::readObject() 19Gats::Object *Gats::GatsStream::readObject()
18{ 20{
21 char buf[1500];
22
23 // sio << "Gats::GatsStream::readObject(): Scanning for object header." << sio.nl;
24 do
25 {
26 if( qbRead.getSize() < 5 )
27 {
28 // sio << "Gats::GatsStream::readObject(): reading header data, need 5b, have " << qbRead.getSize() << "b." << sio.nl;
29 int iRead = rStream.read( buf, 5-qbRead.getSize() );
30 qbRead.write( buf, iRead );
31
32 if( qbRead.getSize() < 5 )
33 return NULL;
34 }
35 } while( !skipReadNulls() );
36
37 uint8_t uVer;
38 qbRead.peek( &uVer, 1 );
39 // sio << "Gats::GatsStream::readObject(): Packet version: " << (int)uVer << sio.nl;
40
41 int32_t iSize;
42 qbRead.peek( &iSize, 4, 1 );
43 iSize = ntohl( iSize );
44 // sio << "Gats::GatsStream::readObject(): Header read, looking for " << iSize << "b, we have " << qbRead.getSize() << "b." << sio.nl;
45 while( qbRead.getSize() < iSize )
46 {
47 int32_t iRead = iSize - qbRead.getSize();
48 if( iRead > 1500 )
49 iRead = 1500;
50 // sio << "Gats::GatsStream::readObject(): Attempting to read " << iRead << "b." << sio.nl;
51 int32_t iReal = rStream.read( buf, iRead );
52 // sio << "Gats::GatsStream::readObject(): Read " << iReal << "b." << sio.nl;
53 qbRead.write( buf, iReal );
54 if( iReal < iRead )
55 {
56 // sio << "Gats::GatsStream::readObject(): Insufficient data read in block, bailing on read." << sio.nl;
57 return NULL;
58 }
59 }
60
61 if( qbRead.getSize() < iSize )
62 {
63 // sio << "Gats::GatsStream::readObject(): Somehow, we still don't have enough data, bailing." << sio.nl;
64 return NULL;
65 }
66
67 // sio << "Gats::GatsStream::readObject(): We have " << qbRead.getSize() << "b of " << iSize << "b, time to read the object." << sio.nl;
19 68
69 qbRead.seek( 5 );
70 Gats::Object *pObj = Gats::Object::read( qbRead );
71
72 // sio << "Gats::GatsStream::readObject(): Read completed, there are " << qbRead.getSize() << "b left in the buffer." << sio.nl;
73 return pObj;
20} 74}
21 75
22void Gats::GatsStream::writeObject( Gats::Object *pObject ) 76void Gats::GatsStream::writeObject( Gats::Object *pObject )
@@ -24,6 +78,33 @@ void Gats::GatsStream::writeObject( Gats::Object *pObject )
24 Bu::NullStream ns; 78 Bu::NullStream ns;
25 pObject->write( ns ); 79 pObject->write( ns );
26 80
27 sio << "Object consumed " << ns.tell() << "b." << sio.nl; 81 uint8_t uBuf = 1;
82 int32_t iSize = htonl( ns.tell()+5 );
83 rStream.write( &uBuf, 1 );
84 rStream.write( &iSize, 4 );
85 pObject->write( rStream );
86
87 // sio << "Object consumed " << ns.tell() << "b." << sio.nl;
88}
89
90bool Gats::GatsStream::skipReadNulls()
91{
92 char buf;
93
94 // sio << "Gats::GatsStream::skipReadNulls(): Scanning for nulls, " << qbRead.getSize() << "b." << sio.nl;
95 bool bHaveSeeked = false;
96 for(;;)
97 {
98 if( qbRead.peek( &buf, 1 ) == 0 )
99 return false;
100 if( buf != 0 )
101 return !bHaveSeeked; //true;
102 else
103 {
104 // sio << "Gats::GatsStream::skipReadNulls(): Null byte read, not header yet..." << sio.nl;
105 qbRead.seek( 1 );
106 bHaveSeeked = true;
107 }
108 }
28} 109}
29 110