diff options
author | Mike Buland <eichlan@xagasoft.com> | 2007-07-03 00:28:59 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2007-07-03 00:28:59 +0000 |
commit | ac517a2b7625e0aa0862679e961c6349f859ea3b (patch) | |
tree | e3e27a6b9bd5e2be6150088495c91fc91786ad9d /src/client.cpp | |
parent | f8d4301e9fa4f3709258505941e37fab2eadadc6 (diff) | |
parent | bd865cee5f89116c1f054cd0e5c275e97c2d0a9b (diff) | |
download | libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.gz libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.bz2 libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.xz libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.zip |
The reorg is being put in trunk, I think it's ready. Now we just get to find
out how many applications won't work anymore :)
Diffstat (limited to '')
-rw-r--r-- | src/client.cpp | 206 |
1 files changed, 206 insertions, 0 deletions
diff --git a/src/client.cpp b/src/client.cpp new file mode 100644 index 0000000..8077b3d --- /dev/null +++ b/src/client.cpp | |||
@@ -0,0 +1,206 @@ | |||
1 | #include "bu/client.h" | ||
2 | #include "bu/socket.h" | ||
3 | #include <stdlib.h> | ||
4 | #include <errno.h> | ||
5 | #include "bu/exceptions.h" | ||
6 | #include "bu/protocol.h" | ||
7 | |||
8 | /** Read buffer size. */ | ||
9 | #define RBS (1024*2) | ||
10 | |||
11 | Bu::Client::Client( Bu::Socket *pSocket ) : | ||
12 | pSocket( pSocket ), | ||
13 | pProto( NULL ), | ||
14 | nRBOffset( 0 ) | ||
15 | { | ||
16 | } | ||
17 | |||
18 | Bu::Client::~Client() | ||
19 | { | ||
20 | } | ||
21 | |||
22 | void Bu::Client::processInput() | ||
23 | { | ||
24 | char buf[RBS]; | ||
25 | size_t nRead, nTotal=0; | ||
26 | |||
27 | for(;;) | ||
28 | { | ||
29 | nRead = pSocket->read( buf, nRead ); | ||
30 | if( nRead < 0 ) | ||
31 | { | ||
32 | throw Bu::ConnectionException( | ||
33 | excodeReadError, | ||
34 | "Read error: %s", | ||
35 | strerror( errno ) | ||
36 | ); | ||
37 | } | ||
38 | else if( nRead == 0 ) | ||
39 | { | ||
40 | break; | ||
41 | } | ||
42 | else | ||
43 | { | ||
44 | nTotal += nRead; | ||
45 | sReadBuf.append( buf, nRead ); | ||
46 | if( !pSocket->canRead() ) | ||
47 | break; | ||
48 | } | ||
49 | } | ||
50 | |||
51 | if( nTotal == 0 ) | ||
52 | { | ||
53 | pSocket->close(); | ||
54 | } | ||
55 | |||
56 | if( pProto && nTotal ) | ||
57 | { | ||
58 | pProto->onNewData( this ); | ||
59 | } | ||
60 | } | ||
61 | |||
62 | void Bu::Client::processOutput() | ||
63 | { | ||
64 | if( sWriteBuf.getSize() > 0 ) | ||
65 | { | ||
66 | pSocket->write( sWriteBuf.getStr(), sWriteBuf.getSize() ); | ||
67 | sWriteBuf.clear(); | ||
68 | } | ||
69 | } | ||
70 | |||
71 | void Bu::Client::setProtocol( Protocol *pProto ) | ||
72 | { | ||
73 | this->pProto = pProto; | ||
74 | } | ||
75 | |||
76 | Bu::Protocol *Bu::Client::getProtocol() | ||
77 | { | ||
78 | return pProto; | ||
79 | } | ||
80 | |||
81 | void Bu::Client::clearProtocol() | ||
82 | { | ||
83 | pProto = NULL; | ||
84 | } | ||
85 | |||
86 | Bu::FString &Bu::Client::getInput() | ||
87 | { | ||
88 | return sReadBuf; | ||
89 | } | ||
90 | |||
91 | Bu::FString &Bu::Client::getOutput() | ||
92 | { | ||
93 | return sWriteBuf; | ||
94 | } | ||
95 | |||
96 | bool Bu::Client::isOpen() | ||
97 | { | ||
98 | if( !pSocket ) return false; | ||
99 | return pSocket->isOpen(); | ||
100 | } | ||
101 | |||
102 | void Bu::Client::write( const void *pData, int nBytes ) | ||
103 | { | ||
104 | sWriteBuf.append( (const char *)pData, nBytes ); | ||
105 | } | ||
106 | |||
107 | void Bu::Client::write( int8_t nData ) | ||
108 | { | ||
109 | sWriteBuf.append( (const char *)&nData, sizeof(nData) ); | ||
110 | } | ||
111 | |||
112 | void Bu::Client::write( int16_t nData ) | ||
113 | { | ||
114 | sWriteBuf.append( (const char *)&nData, sizeof(nData) ); | ||
115 | } | ||
116 | |||
117 | void Bu::Client::write( int32_t nData ) | ||
118 | { | ||
119 | sWriteBuf.append( (const char *)&nData, sizeof(nData) ); | ||
120 | } | ||
121 | |||
122 | void Bu::Client::write( int64_t nData ) | ||
123 | { | ||
124 | sWriteBuf.append( (const char *)&nData, sizeof(nData) ); | ||
125 | } | ||
126 | |||
127 | void Bu::Client::write( uint8_t nData ) | ||
128 | { | ||
129 | sWriteBuf.append( (const char *)&nData, sizeof(nData) ); | ||
130 | } | ||
131 | |||
132 | void Bu::Client::write( uint16_t nData ) | ||
133 | { | ||
134 | sWriteBuf.append( (const char *)&nData, sizeof(nData) ); | ||
135 | } | ||
136 | |||
137 | void Bu::Client::write( uint32_t nData ) | ||
138 | { | ||
139 | sWriteBuf.append( (const char *)&nData, sizeof(nData) ); | ||
140 | } | ||
141 | |||
142 | void Bu::Client::write( uint64_t nData ) | ||
143 | { | ||
144 | sWriteBuf.append( (const char *)&nData, sizeof(nData) ); | ||
145 | } | ||
146 | |||
147 | void Bu::Client::read( void *pData, int nBytes ) | ||
148 | { | ||
149 | memcpy( pData, sReadBuf.getStr()+nRBOffset, nBytes ); | ||
150 | nRBOffset += nBytes; | ||
151 | if( sReadBuf.getSize()-nRBOffset == 0 ) | ||
152 | { | ||
153 | sReadBuf.clear(); | ||
154 | nRBOffset = 0; | ||
155 | } | ||
156 | // This is an experimental threshold, maybe I'll make this configurable | ||
157 | // later on. | ||
158 | else if( | ||
159 | (sReadBuf.getSize() >= 1024 && nRBOffset >= sReadBuf.getSize()/2) || | ||
160 | (nRBOffset >= sReadBuf.getSize()/4) | ||
161 | ) | ||
162 | { | ||
163 | sReadBuf.trimFront( nRBOffset ); | ||
164 | nRBOffset = 0; | ||
165 | } | ||
166 | } | ||
167 | |||
168 | void Bu::Client::peek( void *pData, int nBytes ) | ||
169 | { | ||
170 | memcpy( pData, sReadBuf.getStr()+nRBOffset, nBytes ); | ||
171 | } | ||
172 | |||
173 | void Bu::Client::seek( int nBytes ) | ||
174 | { | ||
175 | nRBOffset += nBytes; | ||
176 | if( sReadBuf.getSize()-nRBOffset == 0 ) | ||
177 | { | ||
178 | sReadBuf.clear(); | ||
179 | nRBOffset = 0; | ||
180 | } | ||
181 | // This is an experimental threshold, maybe I'll make this configurable | ||
182 | // later on. | ||
183 | else if( | ||
184 | (sReadBuf.getSize() >= 1024 && nRBOffset >= sReadBuf.getSize()/2) || | ||
185 | (nRBOffset >= sReadBuf.getSize()/4) | ||
186 | ) | ||
187 | { | ||
188 | sReadBuf.trimFront( nRBOffset ); | ||
189 | nRBOffset = 0; | ||
190 | } | ||
191 | } | ||
192 | |||
193 | long Bu::Client::getInputSize() | ||
194 | { | ||
195 | return sReadBuf.getSize()-nRBOffset; | ||
196 | } | ||
197 | |||
198 | const Bu::Socket *Bu::Client::getSocket() const | ||
199 | { | ||
200 | return pSocket; | ||
201 | } | ||
202 | |||
203 | void Bu::Client::disconnect() | ||
204 | { | ||
205 | } | ||
206 | |||