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 | |
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.
-rw-r--r-- | src/client.cpp | 96 | ||||
-rw-r--r-- | src/client.h | 14 | ||||
-rw-r--r-- | src/queuebuf.cpp | 15 | ||||
-rw-r--r-- | src/queuebuf.h | 1 | ||||
-rw-r--r-- | src/tests/itoserver.cpp | 8 | ||||
-rw-r--r-- | src/tests/multiserver.cpp | 31 | ||||
-rw-r--r-- | src/tests/rot13.cpp | 8 |
7 files changed, 80 insertions, 93 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 |
diff --git a/src/client.h b/src/client.h index aecb16c..5a933ca 100644 --- a/src/client.h +++ b/src/client.h | |||
@@ -11,6 +11,7 @@ | |||
11 | #include <stdint.h> | 11 | #include <stdint.h> |
12 | 12 | ||
13 | #include "bu/fstring.h" | 13 | #include "bu/fstring.h" |
14 | #include "bu/queuebuf.h" | ||
14 | 15 | ||
15 | namespace Bu | 16 | namespace Bu |
16 | { | 17 | { |
@@ -31,8 +32,8 @@ namespace Bu | |||
31 | void processInput(); | 32 | void processInput(); |
32 | void processOutput(); | 33 | void processOutput(); |
33 | 34 | ||
34 | Bu::FString &getInput(); | 35 | //Bu::FString &getInput(); |
35 | Bu::FString &getOutput(); | 36 | //Bu::FString &getOutput(); |
36 | void write( const Bu::FString &sData ); | 37 | void write( const Bu::FString &sData ); |
37 | void write( const void *pData, int nBytes ); | 38 | void write( const void *pData, int nBytes ); |
38 | void write( int8_t nData ); | 39 | void write( int8_t nData ); |
@@ -47,6 +48,7 @@ namespace Bu | |||
47 | int peek( void *pData, int nBytes, int nOffset=0 ); | 48 | int peek( void *pData, int nBytes, int nOffset=0 ); |
48 | void seek( int nBytes ); | 49 | void seek( int nBytes ); |
49 | long getInputSize(); | 50 | long getInputSize(); |
51 | long getOutputSize(); | ||
50 | 52 | ||
51 | void setProtocol( Protocol *pProto ); | 53 | void setProtocol( Protocol *pProto ); |
52 | Bu::Protocol *getProtocol(); | 54 | Bu::Protocol *getProtocol(); |
@@ -65,7 +67,8 @@ namespace Bu | |||
65 | 67 | ||
66 | void onMessage( const Bu::FString &sMsg ); | 68 | void onMessage( const Bu::FString &sMsg ); |
67 | 69 | ||
68 | bool hasOutput() { return !sWriteBuf.isEmpty(); } | 70 | bool hasOutput() { return qbWrite.getSize() > 0; } |
71 | bool hasInput() { return qbRead.getSize() > 0; } | ||
69 | 72 | ||
70 | template<typename filter> | 73 | template<typename filter> |
71 | void pushFilter() | 74 | void pushFilter() |
@@ -97,9 +100,8 @@ namespace Bu | |||
97 | Bu::Stream *pTopStream; | 100 | Bu::Stream *pTopStream; |
98 | Bu::Socket *pSocket; | 101 | Bu::Socket *pSocket; |
99 | Bu::Protocol *pProto; | 102 | Bu::Protocol *pProto; |
100 | Bu::FString sReadBuf; | 103 | Bu::QueueBuf qbRead; |
101 | int nRBOffset; | 104 | Bu::QueueBuf qbWrite; |
102 | Bu::FString sWriteBuf; | ||
103 | bool bWantsDisconnect; | 105 | bool bWantsDisconnect; |
104 | class Bu::ClientLinkFactory *pfLink; | 106 | class Bu::ClientLinkFactory *pfLink; |
105 | }; | 107 | }; |
diff --git a/src/queuebuf.cpp b/src/queuebuf.cpp index 9577793..01d92f8 100644 --- a/src/queuebuf.cpp +++ b/src/queuebuf.cpp | |||
@@ -76,7 +76,12 @@ size_t Bu::QueueBuf::read( void *pRawBuf, size_t nBytes ) | |||
76 | return nBytes - iLeft; | 76 | return nBytes - iLeft; |
77 | } | 77 | } |
78 | 78 | ||
79 | size_t Bu::QueueBuf::peek( void *pRawBuf, size_t nBytes ) | 79 | size_t Bu::QueueBuf::peek( void *pBuf, size_t nBytes ) |
80 | { | ||
81 | return peek( pBuf, nBytes, 0 ); | ||
82 | } | ||
83 | |||
84 | size_t Bu::QueueBuf::peek( void *pRawBuf, size_t nBytes, size_t nSkip ) | ||
80 | { | 85 | { |
81 | if( nBytes <= 0 ) | 86 | if( nBytes <= 0 ) |
82 | return 0; | 87 | return 0; |
@@ -87,12 +92,16 @@ size_t Bu::QueueBuf::peek( void *pRawBuf, size_t nBytes ) | |||
87 | size_t iLeft = nBytes; | 92 | size_t iLeft = nBytes; |
88 | char *pBuf = (char *)pRawBuf; | 93 | char *pBuf = (char *)pRawBuf; |
89 | 94 | ||
90 | int iTmpReadOffset = iReadOffset; | 95 | int iTmpReadOffset = iReadOffset + nSkip; |
91 | size_t iTmpRemSize = iTotalSize; | 96 | size_t iTmpRemSize = iTotalSize; |
92 | BlockList::iterator iBlock = lBlocks.begin(); | 97 | BlockList::iterator iBlock = lBlocks.begin(); |
98 | while( iTmpReadOffset > iBlockSize ) | ||
99 | { | ||
100 | iTmpReadOffset -= iBlockSize; | ||
101 | iBlock++; | ||
102 | } | ||
93 | while( iLeft > 0 && iTmpRemSize > 0 ) | 103 | while( iLeft > 0 && iTmpRemSize > 0 ) |
94 | { | 104 | { |
95 | // Switching to use temp variables instead of iReadOffset and iTotalSize | ||
96 | if( iTmpReadOffset == iBlockSize ) | 105 | if( iTmpReadOffset == iBlockSize ) |
97 | { | 106 | { |
98 | iBlock++; | 107 | iBlock++; |
diff --git a/src/queuebuf.h b/src/queuebuf.h index 3591959..382863d 100644 --- a/src/queuebuf.h +++ b/src/queuebuf.h | |||
@@ -29,6 +29,7 @@ namespace Bu | |||
29 | virtual void close(); | 29 | virtual void close(); |
30 | virtual size_t read( void *pBuf, size_t nBytes ); | 30 | virtual size_t read( void *pBuf, size_t nBytes ); |
31 | virtual size_t peek( void *pBuf, size_t nBytes ); | 31 | virtual size_t peek( void *pBuf, size_t nBytes ); |
32 | virtual size_t peek( void *pBuf, size_t nBytes, size_t nSkip ); | ||
32 | virtual size_t write( const void *pBuf, size_t nBytes ); | 33 | virtual size_t write( const void *pBuf, size_t nBytes ); |
33 | virtual long tell(); | 34 | virtual long tell(); |
34 | virtual void seek( long offset ); | 35 | virtual void seek( long offset ); |
diff --git a/src/tests/itoserver.cpp b/src/tests/itoserver.cpp index cf5f6d1..5f6e4fa 100644 --- a/src/tests/itoserver.cpp +++ b/src/tests/itoserver.cpp | |||
@@ -33,8 +33,12 @@ public: | |||
33 | virtual void onNewData( Bu::Client *pClient ) | 33 | virtual void onNewData( Bu::Client *pClient ) |
34 | { | 34 | { |
35 | TRACE(); | 35 | TRACE(); |
36 | pClient->write( pClient->getInput().getStr(), pClient->getInputSize() ); | 36 | char buf[1024]; |
37 | pClient->seek( pClient->getInputSize() ); | 37 | while( pClient->hasInput() ) |
38 | { | ||
39 | int iAmnt = pClient->read( buf, 1024 ); | ||
40 | pClient->write( buf, iAmnt ); | ||
41 | } | ||
38 | } | 42 | } |
39 | }; | 43 | }; |
40 | 44 | ||
diff --git a/src/tests/multiserver.cpp b/src/tests/multiserver.cpp index 22ce94b..85971b5 100644 --- a/src/tests/multiserver.cpp +++ b/src/tests/multiserver.cpp | |||
@@ -19,8 +19,12 @@ public: | |||
19 | 19 | ||
20 | virtual void onNewData( Bu::Client *pClient ) | 20 | virtual void onNewData( Bu::Client *pClient ) |
21 | { | 21 | { |
22 | pClient->write( pClient->getInput() ); | 22 | char buf[1024]; |
23 | pClient->seek( pClient->getInputSize() ); | 23 | while( pClient->hasInput() ) |
24 | { | ||
25 | int iAmnt = pClient->read( buf, 1024 ); | ||
26 | pClient->write( buf, iAmnt ); | ||
27 | } | ||
24 | } | 28 | } |
25 | }; | 29 | }; |
26 | 30 | ||
@@ -34,20 +38,23 @@ public: | |||
34 | 38 | ||
35 | virtual void onNewData( Bu::Client *pClient ) | 39 | virtual void onNewData( Bu::Client *pClient ) |
36 | { | 40 | { |
37 | Bu::FString sTmp = pClient->getInput(); | 41 | while( pClient->hasInput() ) |
38 | for( int j = 0; j < sTmp.getSize(); j++ ) | ||
39 | { | 42 | { |
40 | if( sTmp[j] >= 'a' && sTmp[j] <= 'z' ) | 43 | char sTmp[1024]; |
41 | { | 44 | int iAmnt = pClient->read( sTmp, 1024 ); |
42 | sTmp[j] = ((sTmp[j]-'a'+13)%26) + 'a'; | 45 | for( int j = 0; j < iAmnt; j++ ) |
43 | } | ||
44 | else if( sTmp[j] >= 'A' && sTmp[j] <= 'Z' ) | ||
45 | { | 46 | { |
46 | sTmp[j] = ((sTmp[j]-'A'+13)%26) + 'A'; | 47 | if( sTmp[j] >= 'a' && sTmp[j] <= 'z' ) |
48 | { | ||
49 | sTmp[j] = ((sTmp[j]-'a'+13)%26) + 'a'; | ||
50 | } | ||
51 | else if( sTmp[j] >= 'A' && sTmp[j] <= 'Z' ) | ||
52 | { | ||
53 | sTmp[j] = ((sTmp[j]-'A'+13)%26) + 'A'; | ||
54 | } | ||
47 | } | 55 | } |
56 | pClient->write( sTmp, iAmnt ); | ||
48 | } | 57 | } |
49 | pClient->write( sTmp ); | ||
50 | pClient->seek( pClient->getInputSize() ); | ||
51 | } | 58 | } |
52 | }; | 59 | }; |
53 | 60 | ||
diff --git a/src/tests/rot13.cpp b/src/tests/rot13.cpp index 2326888..03ba385 100644 --- a/src/tests/rot13.cpp +++ b/src/tests/rot13.cpp | |||
@@ -67,8 +67,12 @@ public: | |||
67 | 67 | ||
68 | void onNewData( Bu::Client *pClient ) | 68 | void onNewData( Bu::Client *pClient ) |
69 | { | 69 | { |
70 | pClient->write( pClient->getInput().getStr(), pClient->getInputSize() ); | 70 | char buf[1024]; |
71 | pClient->seek( pClient->getInputSize() ); | 71 | while( pClient->hasInput() ) |
72 | { | ||
73 | int iAmnt = pClient->read( buf, 1024 ); | ||
74 | pClient->write( buf, iAmnt ); | ||
75 | } | ||
72 | } | 76 | } |
73 | }; | 77 | }; |
74 | 78 | ||