summaryrefslogtreecommitdiff
path: root/src/stable/client.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-03-25 20:00:08 +0000
committerMike Buland <eichlan@xagasoft.com>2012-03-25 20:00:08 +0000
commit469bbcf0701e1eb8a6670c23145b0da87357e178 (patch)
treeb5b062a16e46a6c5d3410b4e574cd0cc09057211 /src/stable/client.cpp
parentee1b79396076edc4e30aefb285fada03bb45e80d (diff)
downloadlibbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.gz
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.bz2
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.xz
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.zip
Code is all reorganized. We're about ready to release. I should write up a
little explenation of the arrangement.
Diffstat (limited to 'src/stable/client.cpp')
-rw-r--r--src/stable/client.cpp320
1 files changed, 320 insertions, 0 deletions
diff --git a/src/stable/client.cpp b/src/stable/client.cpp
new file mode 100644
index 0000000..75f6158
--- /dev/null
+++ b/src/stable/client.cpp
@@ -0,0 +1,320 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include "bu/client.h"
9#include "bu/tcpsocket.h"
10#include <stdlib.h>
11#include <errno.h>
12#include "bu/protocol.h"
13#include "bu/clientlink.h"
14#include "bu/clientlinkfactory.h"
15
16/** Read buffer size. */
17#define RBS (2000) // 1500 is the nominal MTU for ethernet, it's a good guess
18
19Bu::Client::Client( Bu::TcpSocket *pSocket,
20 class Bu::ClientLinkFactory *pfLink ) :
21 pTopStream( pSocket ),
22 pSocket( pSocket ),
23 pProto( NULL ),
24 bWantsDisconnect( false ),
25 pfLink( pfLink )
26{
27 lFilts.prepend( pSocket );
28}
29
30Bu::Client::~Client()
31{
32 for( FilterList::iterator i = lFilts.begin(); i; i++ )
33 {
34 delete *i;
35 }
36 pTopStream = pSocket = NULL;
37 delete pfLink;
38}
39
40void Bu::Client::processInput()
41{
42 char buf[RBS];
43 Bu::size nRead, nTotal=0;
44
45 for(;;)
46 {
47 try
48 {
49 nRead = pTopStream->read( buf, RBS );
50
51 if( nRead == 0 )
52 {
53 break;
54 }
55 else
56 {
57 nTotal += nRead;
58 qbRead.write( buf, nRead );
59 if( !pTopStream->canRead() )
60 break;
61 }
62 }
63 catch( Bu::TcpSocketException &e )
64 {
65 pTopStream->close();
66 bWantsDisconnect = true;
67 break;
68 }
69 }
70
71 if( nTotal == 0 )
72 {
73 pTopStream->close();
74 bWantsDisconnect = true;
75 }
76
77 if( pProto && nTotal )
78 {
79 pProto->onNewData( this );
80 }
81}
82
83void Bu::Client::processOutput()
84{
85 char buf[RBS];
86 if( qbWrite.getSize() > 0 )
87 {
88 int nAmnt = RBS;
89 nAmnt = qbWrite.peek( buf, nAmnt );
90 int nReal = pTopStream->write( buf, nAmnt );
91 qbWrite.seek( nReal );
92 pTopStream->flush();
93 }
94}
95
96void Bu::Client::setProtocol( Protocol *pProto )
97{
98 this->pProto = pProto;
99 this->pProto->onNewConnection( this );
100}
101
102Bu::Protocol *Bu::Client::getProtocol()
103{
104 return pProto;
105}
106
107void Bu::Client::clearProtocol()
108{
109 pProto = NULL;
110}
111/*
112Bu::String &Bu::Client::getInput()
113{
114 return sReadBuf;
115}
116
117Bu::String &Bu::Client::getOutput()
118{
119 return sWriteBuf;
120}
121*/
122
123bool Bu::Client::isOpen()
124{
125 if( !pTopStream ) return false;
126 return pTopStream->isOpen();
127}
128
129Bu::size Bu::Client::write( const Bu::String &sData )
130{
131 return qbWrite.write( sData.getStr(), sData.getSize() );
132}
133
134Bu::size Bu::Client::write( const void *pData, Bu::size nBytes )
135{
136 return qbWrite.write( pData, nBytes );
137}
138
139Bu::size Bu::Client::write( int8_t nData )
140{
141 return qbWrite.write( (const char *)&nData, sizeof(nData) );
142}
143
144Bu::size Bu::Client::write( int16_t nData )
145{
146 return qbWrite.write( (const char *)&nData, sizeof(nData) );
147}
148
149Bu::size Bu::Client::write( int32_t nData )
150{
151 return qbWrite.write( (const char *)&nData, sizeof(nData) );
152}
153
154Bu::size Bu::Client::write( int64_t nData )
155{
156 return qbWrite.write( (const char *)&nData, sizeof(nData) );
157}
158
159Bu::size Bu::Client::write( uint8_t nData )
160{
161 return qbWrite.write( (const char *)&nData, sizeof(nData) );
162}
163
164Bu::size Bu::Client::write( uint16_t nData )
165{
166 return qbWrite.write( (const char *)&nData, sizeof(nData) );
167}
168
169Bu::size Bu::Client::write( uint32_t nData )
170{
171 return qbWrite.write( (const char *)&nData, sizeof(nData) );
172}
173
174Bu::size Bu::Client::write( uint64_t nData )
175{
176 return qbWrite.write( (const char *)&nData, sizeof(nData) );
177}
178
179Bu::size Bu::Client::read( void *pData, Bu::size nBytes )
180{
181 return qbRead.read( pData, nBytes );
182}
183
184Bu::size Bu::Client::peek( void *pData, int nBytes, int nOffset )
185{
186 return qbRead.peek( pData, nBytes, nOffset );
187}
188
189Bu::size Bu::Client::getInputSize()
190{
191 return qbRead.getSize();
192}
193
194Bu::size Bu::Client::getOutputSize()
195{
196 return qbWrite.getSize();
197}
198
199const Bu::TcpSocket *Bu::Client::getSocket() const
200{
201 return pSocket;
202}
203
204void Bu::Client::disconnect()
205{
206 bWantsDisconnect = true;
207}
208
209bool Bu::Client::wantsDisconnect()
210{
211 return bWantsDisconnect;
212}
213
214void Bu::Client::close()
215{
216 pTopStream->close();
217}
218
219Bu::ClientLink *Bu::Client::getLink()
220{
221 return pfLink->createLink( this );
222}
223
224void Bu::Client::onMessage( const Bu::String &sMsg )
225{
226 if( pProto )
227 pProto->onMessage( this, sMsg );
228}
229
230void Bu::Client::tick()
231{
232 if( pProto )
233 pProto->onTick( this );
234}
235
236Bu::size Bu::Client::tell()
237{
238 return 0;
239}
240
241void Bu::Client::seek( Bu::size offset )
242{
243 return qbRead.seek( offset );
244}
245
246void Bu::Client::setPos( Bu::size )
247{
248 throw Bu::ExceptionBase();
249}
250
251void Bu::Client::setPosEnd( Bu::size )
252{
253 throw Bu::ExceptionBase();
254}
255
256bool Bu::Client::isEos()
257{
258 return true;
259}
260
261void Bu::Client::flush()
262{
263 processOutput();
264}
265
266bool Bu::Client::canRead()
267{
268 return qbRead.getSize() > 0;
269}
270
271bool Bu::Client::canWrite()
272{
273 return true;
274}
275
276bool Bu::Client::isReadable()
277{
278 return true;
279}
280
281bool Bu::Client::isWritable()
282{
283 return true;
284}
285
286bool Bu::Client::isSeekable()
287{
288 return false;
289}
290
291bool Bu::Client::isBlocking()
292{
293 return false;
294}
295
296void Bu::Client::setBlocking( bool )
297{
298 throw Bu::ExceptionBase();
299}
300
301void Bu::Client::setSize( Bu::size )
302{
303 throw Bu::ExceptionBase();
304}
305
306Bu::size Bu::Client::getSize() const
307{
308 return 0;
309}
310
311Bu::size Bu::Client::getBlockSize() const
312{
313 return pSocket->getBlockSize();
314}
315
316Bu::String Bu::Client::getLocation() const
317{
318 return pSocket->getLocation();
319}
320