 |
Changeset 3670
- Timestamp:
- 06/23/08 03:25:02
(3 months ago)
- Author:
- kris
- Message:
added a convenient DataOutputPool?
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r3665 |
r3670 |
|
| 20 | 20 | private import tango.net.SocketConduit, |
|---|
| 21 | 21 | tango.net.InternetAddress; |
|---|
| | 22 | |
|---|
| | 23 | private import tango.io.stream.DataStream; |
|---|
| 22 | 24 | |
|---|
| 23 | 25 | /******************************************************************************* |
|---|
| … | … | |
| 60 | 62 | this.factory = factory; |
|---|
| 61 | 63 | this.address = address; |
|---|
| 62 | | } |
|---|
| 63 | | |
|---|
| 64 | | /*********************************************************************** |
|---|
| 65 | | |
|---|
| 66 | | Allocate a Connection from a list rather than creating a |
|---|
| 67 | | new one. Reap old entries as we go. |
|---|
| 68 | | |
|---|
| 69 | | ***********************************************************************/ |
|---|
| 70 | | |
|---|
| 71 | | final synchronized Connection borrow (Time time) |
|---|
| 72 | | { |
|---|
| 73 | | if (freelist) |
|---|
| 74 | | do { |
|---|
| 75 | | auto c = freelist; |
|---|
| 76 | | |
|---|
| 77 | | freelist = c.next; |
|---|
| 78 | | if (freelist && (time - c.time > timeout)) |
|---|
| 79 | | c.close; |
|---|
| 80 | | else |
|---|
| 81 | | return c; |
|---|
| 82 | | } while (true); |
|---|
| 83 | | |
|---|
| 84 | | return new Connection (this); |
|---|
| 85 | 64 | } |
|---|
| 86 | 65 | |
|---|
| … | … | |
| 140 | 119 | for (int attempts=3; attempts--;) |
|---|
| 141 | 120 | try { |
|---|
| 142 | | send (connection.bound); |
|---|
| | 121 | if (send) |
|---|
| | 122 | send (connection.bound); |
|---|
| 143 | 123 | |
|---|
| 144 | 124 | // load the reply. Don't retry on |
|---|
| … | … | |
| 177 | 157 | } |
|---|
| 178 | 158 | |
|---|
| | 159 | /*********************************************************************** |
|---|
| | 160 | |
|---|
| | 161 | Allocate a Connection from a list rather than creating a |
|---|
| | 162 | new one. Reap old entries as we go. |
|---|
| | 163 | |
|---|
| | 164 | ***********************************************************************/ |
|---|
| | 165 | |
|---|
| | 166 | private final synchronized Connection borrow (Time time) |
|---|
| | 167 | { |
|---|
| | 168 | if (freelist) |
|---|
| | 169 | do { |
|---|
| | 170 | auto c = freelist; |
|---|
| | 171 | |
|---|
| | 172 | freelist = c.next; |
|---|
| | 173 | if (freelist && (time - c.time > timeout)) |
|---|
| | 174 | c.close; |
|---|
| | 175 | else |
|---|
| | 176 | return c; |
|---|
| | 177 | } while (true); |
|---|
| | 178 | |
|---|
| | 179 | return new Connection (this); |
|---|
| | 180 | } |
|---|
| | 181 | |
|---|
| 179 | 182 | |
|---|
| 180 | 183 | /*********************************************************************** |
|---|
| … | … | |
| 185 | 188 | ***********************************************************************/ |
|---|
| 186 | 189 | |
|---|
| 187 | | static class Connection |
|---|
| | 190 | private static class Connection |
|---|
| 188 | 191 | { |
|---|
| 189 | 192 | Connection next; |
|---|
| … | … | |
| 266 | 269 | } |
|---|
| 267 | 270 | } |
|---|
| | 271 | } |
|---|
| | 272 | } |
|---|
| | 273 | |
|---|
| | 274 | |
|---|
| | 275 | /******************************************************************************* |
|---|
| | 276 | |
|---|
| | 277 | A pool of connected DataOuptut instances, each of which are thread- |
|---|
| | 278 | safe (each connection has its own private output buffer). |
|---|
| | 279 | |
|---|
| | 280 | Note that the connections will timeout after a period of inactivity, |
|---|
| | 281 | and will subsequently cause a connected host to drop the supporting |
|---|
| | 282 | session. |
|---|
| | 283 | |
|---|
| | 284 | The connections are expected to be reasonably long-lived on the |
|---|
| | 285 | hosting server, since we try to reuse existing sockets |
|---|
| | 286 | |
|---|
| | 287 | *******************************************************************************/ |
|---|
| | 288 | |
|---|
| | 289 | class DataOutputPool : SocketPool!(DataOutput) |
|---|
| | 290 | { |
|---|
| | 291 | private bool flip; |
|---|
| | 292 | private int bufferSize; |
|---|
| | 293 | |
|---|
| | 294 | /*********************************************************************** |
|---|
| | 295 | |
|---|
| | 296 | Create a connection-pool for the specified address |
|---|
| | 297 | |
|---|
| | 298 | ***********************************************************************/ |
|---|
| | 299 | |
|---|
| | 300 | this (InternetAddress addr, int bufferSize=8192, bool flip=false, bool nagle=true) |
|---|
| | 301 | { |
|---|
| | 302 | this.flip = flip; |
|---|
| | 303 | this.bufferSize = bufferSize; |
|---|
| | 304 | super (addr, &factory, nagle); |
|---|
| | 305 | } |
|---|
| | 306 | |
|---|
| | 307 | /*********************************************************************** |
|---|
| | 308 | |
|---|
| | 309 | Wrap the socket with a DataOutput instance |
|---|
| | 310 | |
|---|
| | 311 | ***********************************************************************/ |
|---|
| | 312 | |
|---|
| | 313 | private DataOutput factory (IConduit c) |
|---|
| | 314 | { |
|---|
| | 315 | return new DataOutput (c, bufferSize, flip); |
|---|
| 268 | 316 | } |
|---|
| 269 | 317 | } |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic