 |
- Timestamp:
- 08/08/08 17:50:28
(4 months ago)
- Author:
- schveiguy
- Message:
Fixed selector example to work with new TimeSpan? and Selector code, fixed a previously unseen bug.
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r3856 |
r3865 |
|
| 113 | 113 | try |
|---|
| 114 | 114 | { |
|---|
| 115 | | TimeSpan timeout = TimeSpan.seconds(1); |
|---|
| | 115 | TimeSpan timeout = TimeSpan.fromSeconds(1); |
|---|
| 116 | 116 | InternetAddress addr = new InternetAddress(SERVER_ADDR, SERVER_PORT); |
|---|
| 117 | 117 | ServerSocket serverSocket = new ServerSocket(addr, 5); |
|---|
| … | … | |
| 139 | 139 | if (eventCount > 0) |
|---|
| 140 | 140 | { |
|---|
| | 141 | ISelectable[] removeThese; |
|---|
| 141 | 142 | foreach (SelectionKey selectionKey; selector.selectedSet()) |
|---|
| 142 | 143 | { |
|---|
| … | … | |
| 178 | 179 | log.trace(sprint("[{0}] Received {1} from client ({2} bytes)", |
|---|
| 179 | 180 | i, buffer[0..count], count)); |
|---|
| 180 | | selector.reregister(selectionKey.conduit, Event.Write); |
|---|
| | 181 | selector.register(selectionKey.conduit, Event.Write); |
|---|
| 181 | 182 | receiveCount++; |
|---|
| 182 | 183 | } |
|---|
| … | … | |
| 186 | 187 | log.trace(sprint("[{0}] Handle {1} was closed; removing it from Selector", |
|---|
| 187 | 188 | i, cast(int) selectionKey.conduit.fileHandle())); |
|---|
| 188 | | selector.unregister(selectionKey.conduit); |
|---|
| 189 | | (cast(SocketConduit) selectionKey.conduit).close(); |
|---|
| | 189 | // note, we cannot unregister because we are |
|---|
| | 190 | // in the middle of a foreach loop. Delay |
|---|
| | 191 | // unregistering and closing until after the |
|---|
| | 192 | // loop is done. |
|---|
| | 193 | //selector.unregister(selectionKey.conduit); |
|---|
| | 194 | //(cast(SocketConduit) selectionKey.conduit).close(); |
|---|
| | 195 | removeThese ~= selectionKey.conduit; |
|---|
| 190 | 196 | failedReceiveCount++; |
|---|
| 191 | 197 | continue; |
|---|
| … | … | |
| 205 | 211 | log.trace(sprint("[{0}] Sent PONG to client ({1} bytes)", i, count)); |
|---|
| 206 | 212 | |
|---|
| 207 | | selector.reregister(selectionKey.conduit, Event.Read); |
|---|
| | 213 | selector.register(selectionKey.conduit, Event.Read); |
|---|
| 208 | 214 | sendCount++; |
|---|
| 209 | 215 | } |
|---|
| … | … | |
| 213 | 219 | log.trace(sprint("[{0}] Handle {1} was closed; removing it from Selector", |
|---|
| 214 | 220 | i, selectionKey.conduit.fileHandle())); |
|---|
| 215 | | selector.unregister(selectionKey.conduit); |
|---|
| 216 | | (cast(SocketConduit) selectionKey.conduit).close(); |
|---|
| | 221 | // note, see comment above |
|---|
| | 222 | //selector.unregister(selectionKey.conduit); |
|---|
| | 223 | //(cast(SocketConduit) selectionKey.conduit).close(); |
|---|
| | 224 | removeThese ~= selectionKey.conduit; |
|---|
| 217 | 225 | failedSendCount++; |
|---|
| 218 | 226 | continue; |
|---|
| … | … | |
| 246 | 254 | i, cast(int) selectionKey.conduit.fileHandle())); |
|---|
| 247 | 255 | } |
|---|
| 248 | | selector.unregister(selectionKey.conduit); |
|---|
| 249 | | (cast(Conduit) selectionKey.conduit).close(); |
|---|
| | 256 | // note, see comment above |
|---|
| | 257 | //selector.unregister(selectionKey.conduit); |
|---|
| | 258 | //(cast(Conduit) selectionKey.conduit).close(); |
|---|
| | 259 | removeThese ~= selectionKey.conduit; |
|---|
| 250 | 260 | |
|---|
| 251 | 261 | if (selectionKey.conduit !is serverSocket) |
|---|
| … | … | |
| 258 | 268 | } |
|---|
| 259 | 269 | } |
|---|
| | 270 | } |
|---|
| | 271 | foreach(c; removeThese) |
|---|
| | 272 | { |
|---|
| | 273 | selector.unregister(c); |
|---|
| | 274 | (cast(Conduit) c).close(); |
|---|
| 260 | 275 | } |
|---|
| 261 | 276 | } |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic