diff options
author | Mike Buland <eichlan@xagasoft.com> | 2010-05-13 21:11:47 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2010-05-13 21:11:47 +0000 |
commit | 7ad392ce0426a040cc55713691bf6fdbf53c3d31 (patch) | |
tree | a50a7cb05f0d8550a7a37aa0cc3f3090250148e7 /src/client.cpp | |
parent | 093ef85e95d1b37e4a87ffc2a51433b2f1ed24e0 (diff) | |
download | libbu++-7ad392ce0426a040cc55713691bf6fdbf53c3d31.tar.gz libbu++-7ad392ce0426a040cc55713691bf6fdbf53c3d31.tar.bz2 libbu++-7ad392ce0426a040cc55713691bf6fdbf53c3d31.tar.xz libbu++-7ad392ce0426a040cc55713691bf6fdbf53c3d31.zip |
QueueBuf is updated, and everything else uses it now, including Client.
Unfortunately this breaks some programs that accessed the client internal
buffer directly. Overall it's much, much more efficient, so it's worth it,
maybe we'll find a good workaround later.
Diffstat (limited to 'src/client.cpp')
-rw-r--r-- | src/client.cpp | 96 |
1 files changed, 28 insertions, 68 deletions
diff --git a/src/client.cpp b/src/client.cpp index 95008f9..88eb49d 100644 --- a/src/client.cpp +++ b/src/client.cpp | |||
@@ -20,7 +20,6 @@ Bu::Client::Client( Bu::Socket *pSocket, class Bu::ClientLinkFactory *pfLink ) : | |||
20 | pTopStream( pSocket ), | 20 | pTopStream( pSocket ), |
21 | pSocket( pSocket ), | 21 | pSocket( pSocket ), |
22 | pProto( NULL ), | 22 | pProto( NULL ), |
23 | nRBOffset( 0 ), | ||
24 | bWantsDisconnect( false ), | 23 | bWantsDisconnect( false ), |
25 | pfLink( pfLink ) | 24 | pfLink( pfLink ) |
26 | { | 25 | { |
@@ -54,7 +53,7 @@ void Bu::Client::processInput() | |||
54 | else | 53 | else |
55 | { | 54 | { |
56 | nTotal += nRead; | 55 | nTotal += nRead; |
57 | sReadBuf.append( buf, nRead ); | 56 | qbRead.write( buf, nRead ); |
58 | if( !pTopStream->canRead() ) | 57 | if( !pTopStream->canRead() ) |
59 | break; | 58 | break; |
60 | } | 59 | } |
@@ -81,13 +80,14 @@ void Bu::Client::processInput() | |||
81 | 80 | ||
82 | void Bu::Client::processOutput() | 81 | void Bu::Client::processOutput() |
83 | { | 82 | { |
84 | if( sWriteBuf.getSize() > 0 ) | 83 | if( qbWrite.getSize() > 0 ) |
85 | { | 84 | { |
86 | int nAmnt = (sWriteBuf.getSize()<2048)?(sWriteBuf.getSize()):(2048); | 85 | int nAmnt = RBS; |
87 | int nReal = pTopStream->write( sWriteBuf.getStr(), nAmnt ); | 86 | char *buf = new char[nAmnt]; |
87 | nAmnt = qbWrite.peek( buf, nAmnt ); | ||
88 | int nReal = pTopStream->write( buf, nAmnt ); | ||
89 | qbWrite.seek( nReal ); | ||
88 | pTopStream->flush(); | 90 | pTopStream->flush(); |
89 | sWriteBuf.trimFront( nReal ); | ||
90 | //sWriteBuf.clear(); | ||
91 | } | 91 | } |
92 | } | 92 | } |
93 | 93 | ||
@@ -106,7 +106,7 @@ void Bu::Client::clearProtocol() | |||
106 | { | 106 | { |
107 | pProto = NULL; | 107 | pProto = NULL; |
108 | } | 108 | } |
109 | 109 | /* | |
110 | Bu::FString &Bu::Client::getInput() | 110 | Bu::FString &Bu::Client::getInput() |
111 | { | 111 | { |
112 | return sReadBuf; | 112 | return sReadBuf; |
@@ -116,6 +116,7 @@ Bu::FString &Bu::Client::getOutput() | |||
116 | { | 116 | { |
117 | return sWriteBuf; | 117 | return sWriteBuf; |
118 | } | 118 | } |
119 | */ | ||
119 | 120 | ||
120 | bool Bu::Client::isOpen() | 121 | bool Bu::Client::isOpen() |
121 | { | 122 | { |
@@ -125,118 +126,77 @@ bool Bu::Client::isOpen() | |||
125 | 126 | ||
126 | void Bu::Client::write( const Bu::FString &sData ) | 127 | void Bu::Client::write( const Bu::FString &sData ) |
127 | { | 128 | { |
128 | sWriteBuf += sData; | 129 | qbWrite.write( sData.getStr(), sData.getSize() ); |
129 | } | 130 | } |
130 | 131 | ||
131 | void Bu::Client::write( const void *pData, int nBytes ) | 132 | void Bu::Client::write( const void *pData, int nBytes ) |
132 | { | 133 | { |
133 | sWriteBuf.append( (const char *)pData, nBytes ); | 134 | qbWrite.write( pData, nBytes ); |
134 | } | 135 | } |
135 | 136 | ||
136 | void Bu::Client::write( int8_t nData ) | 137 | void Bu::Client::write( int8_t nData ) |
137 | { | 138 | { |
138 | sWriteBuf.append( (const char *)&nData, sizeof(nData) ); | 139 | qbWrite.write( (const char *)&nData, sizeof(nData) ); |
139 | } | 140 | } |
140 | 141 | ||
141 | void Bu::Client::write( int16_t nData ) | 142 | void Bu::Client::write( int16_t nData ) |
142 | { | 143 | { |
143 | sWriteBuf.append( (const char *)&nData, sizeof(nData) ); | 144 | qbWrite.write( (const char *)&nData, sizeof(nData) ); |
144 | } | 145 | } |
145 | 146 | ||
146 | void Bu::Client::write( int32_t nData ) | 147 | void Bu::Client::write( int32_t nData ) |
147 | { | 148 | { |
148 | sWriteBuf.append( (const char *)&nData, sizeof(nData) ); | 149 | qbWrite.write( (const char *)&nData, sizeof(nData) ); |
149 | } | 150 | } |
150 | 151 | ||
151 | void Bu::Client::write( int64_t nData ) | 152 | void Bu::Client::write( int64_t nData ) |
152 | { | 153 | { |
153 | sWriteBuf.append( (const char *)&nData, sizeof(nData) ); | 154 | qbWrite.write( (const char *)&nData, sizeof(nData) ); |
154 | } | 155 | } |
155 | 156 | ||
156 | void Bu::Client::write( uint8_t nData ) | 157 | void Bu::Client::write( uint8_t nData ) |
157 | { | 158 | { |
158 | sWriteBuf.append( (const char *)&nData, sizeof(nData) ); | 159 | qbWrite.write( (const char *)&nData, sizeof(nData) ); |
159 | } | 160 | } |
160 | 161 | ||
161 | void Bu::Client::write( uint16_t nData ) | 162 | void Bu::Client::write( uint16_t nData ) |
162 | { | 163 | { |
163 | sWriteBuf.append( (const char *)&nData, sizeof(nData) ); | 164 | qbWrite.write( (const char *)&nData, sizeof(nData) ); |
164 | } | 165 | } |
165 | 166 | ||
166 | void Bu::Client::write( uint32_t nData ) | 167 | void Bu::Client::write( uint32_t nData ) |
167 | { | 168 | { |
168 | sWriteBuf.append( (const char *)&nData, sizeof(nData) ); | 169 | qbWrite.write( (const char *)&nData, sizeof(nData) ); |
169 | } | 170 | } |
170 | 171 | ||
171 | void Bu::Client::write( uint64_t nData ) | 172 | void Bu::Client::write( uint64_t nData ) |
172 | { | 173 | { |
173 | sWriteBuf.append( (const char *)&nData, sizeof(nData) ); | 174 | qbWrite.write( (const char *)&nData, sizeof(nData) ); |
174 | } | 175 | } |
175 | 176 | ||
176 | int Bu::Client::read( void *pData, int nBytes ) | 177 | int Bu::Client::read( void *pData, int nBytes ) |
177 | { | 178 | { |
178 | if( nBytes > sReadBuf.getSize()-nRBOffset ) | 179 | return qbRead.read( pData, nBytes ); |
179 | { | ||
180 | nBytes = sReadBuf.getSize()-nRBOffset; | ||
181 | if( nBytes <= 0 ) | ||
182 | return 0; | ||
183 | } | ||
184 | memcpy( pData, sReadBuf.getStr()+nRBOffset, nBytes ); | ||
185 | nRBOffset += nBytes; | ||
186 | if( sReadBuf.getSize()-nRBOffset == 0 ) | ||
187 | { | ||
188 | sReadBuf.clear(); | ||
189 | nRBOffset = 0; | ||
190 | } | ||
191 | // This is an experimental threshold, maybe I'll make this configurable | ||
192 | // later on. | ||
193 | else if( | ||
194 | (sReadBuf.getSize() >= 1024 && nRBOffset >= sReadBuf.getSize()/2) || | ||
195 | (nRBOffset >= sReadBuf.getSize()/4) | ||
196 | ) | ||
197 | { | ||
198 | sReadBuf.trimFront( nRBOffset ); | ||
199 | nRBOffset = 0; | ||
200 | } | ||
201 | |||
202 | return nBytes; | ||
203 | } | 180 | } |
204 | 181 | ||
205 | int Bu::Client::peek( void *pData, int nBytes, int nOffset ) | 182 | int Bu::Client::peek( void *pData, int nBytes, int nOffset ) |
206 | { | 183 | { |
207 | if( nBytes+nOffset > sReadBuf.getSize()-nRBOffset ) | 184 | return qbRead.peek( pData, nBytes, nOffset ); |
208 | { | ||
209 | nBytes = sReadBuf.getSize()-nRBOffset-nOffset; | ||
210 | if( nBytes <= 0 ) | ||
211 | return 0; | ||
212 | } | ||
213 | memcpy( pData, sReadBuf.getStr()+nRBOffset+nOffset, nBytes ); | ||
214 | return nBytes; | ||
215 | } | 185 | } |
216 | 186 | ||
217 | void Bu::Client::seek( int nBytes ) | 187 | void Bu::Client::seek( int nBytes ) |
218 | { | 188 | { |
219 | nRBOffset += nBytes; | 189 | return qbRead.seek( nBytes ); |
220 | if( sReadBuf.getSize()-nRBOffset == 0 ) | ||
221 | { | ||
222 | sReadBuf.clear(); | ||
223 | nRBOffset = 0; | ||
224 | } | ||
225 | // This is an experimental threshold, maybe I'll make this configurable | ||
226 | // later on. | ||
227 | else if( | ||
228 | (sReadBuf.getSize() >= 1024 && nRBOffset >= sReadBuf.getSize()/2) || | ||
229 | (nRBOffset >= sReadBuf.getSize()/4) | ||
230 | ) | ||
231 | { | ||
232 | sReadBuf.trimFront( nRBOffset ); | ||
233 | nRBOffset = 0; | ||
234 | } | ||
235 | } | 190 | } |
236 | 191 | ||
237 | long Bu::Client::getInputSize() | 192 | long Bu::Client::getInputSize() |
238 | { | 193 | { |
239 | return sReadBuf.getSize()-nRBOffset; | 194 | return qbRead.getSize(); |
195 | } | ||
196 | |||
197 | long Bu::Client::getOutputSize() | ||
198 | { | ||
199 | return qbWrite.getSize(); | ||
240 | } | 200 | } |
241 | 201 | ||
242 | const Bu::Socket *Bu::Client::getSocket() const | 202 | const Bu::Socket *Bu::Client::getSocket() const |