Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java @ 940:b77d631b9e28
remove scheduleTimeout() and cancelTimeout()
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Tue, 11 Oct 2016 00:13:30 -0600 |
| parents | 65e0fba5861a |
| children | 89fe80dfab2c |
| rev | line source |
|---|---|
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.io.nio; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import java.io.IOException; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import java.io.InterruptedIOException; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 import java.nio.channels.ClosedChannelException; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 import java.nio.channels.SelectableChannel; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 import java.nio.channels.SelectionKey; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 import java.nio.channels.SocketChannel; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 import java.util.Locale; |
| 865 | 28 import java.util.concurrent.RejectedExecutionException; |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 import org.eclipse.jetty.io.AsyncEndPoint; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 import org.eclipse.jetty.io.Buffer; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 import org.eclipse.jetty.io.ConnectedEndPoint; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 import org.eclipse.jetty.io.Connection; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 import org.eclipse.jetty.io.EofException; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 import org.eclipse.jetty.io.nio.SelectorManager.SelectSet; |
|
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
36 import org.slf4j.Logger; |
|
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
37 import org.slf4j.LoggerFactory; |
|
940
b77d631b9e28
remove scheduleTimeout() and cancelTimeout()
Franklin Schmidt <fschmidt@gmail.com>
parents:
915
diff
changeset
|
38 |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 /* ------------------------------------------------------------ */ |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 /** |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 * An Endpoint that can be scheduled by {@link SelectorManager}. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 */ |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
44 public class SelectChannelEndPoint extends ChannelEndPoint implements AsyncEndPoint, ConnectedEndPoint |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 { |
| 865 | 46 public static final Logger LOG=LoggerFactory.getLogger("org.eclipse.jetty.io.nio"); |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
47 |
| 865 | 48 private final SelectorManager.SelectSet _selectSet; |
| 49 private final SelectorManager _manager; | |
| 50 private SelectionKey _key; | |
| 51 private final Runnable _handler = new Runnable() | |
| 52 { | |
| 53 public void run() { handle(); } | |
| 54 }; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
55 |
| 865 | 56 /** The desired value for {@link SelectionKey#interestOps()} */ |
| 57 private int _interestOps; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
58 |
| 865 | 59 /** |
| 60 * The connection instance is the handler for any IO activity on the endpoint. | |
| 61 * There is a different type of connection for HTTP, AJP, WebSocket and | |
| 62 * ProxyConnect. The connection may change for an SCEP as it is upgraded | |
| 63 * from HTTP to proxy connect or websocket. | |
| 64 */ | |
| 65 private volatile AsyncConnection _connection; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
66 |
| 865 | 67 private static final int STATE_NEEDS_DISPATCH=-1; |
| 68 private static final int STATE_UNDISPATCHED=0; | |
| 69 private static final int STATE_DISPATCHED=1; | |
| 70 private static final int STATE_ASYNC=2; | |
| 71 private int _state; | |
| 72 | |
| 73 private boolean _onIdle; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
74 |
| 865 | 75 /** true if the last write operation succeed and wrote all offered bytes */ |
| 76 private volatile boolean _writable = true; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
77 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
78 |
| 865 | 79 /** True if a thread has is blocked in {@link #blockReadable(long)} */ |
| 80 private boolean _readBlocked; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
81 |
| 865 | 82 /** True if a thread has is blocked in {@link #blockWritable(long)} */ |
| 83 private boolean _writeBlocked; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
84 |
| 865 | 85 /** true if {@link SelectSet#destroyEndPoint(SelectChannelEndPoint)} has not been called */ |
| 86 private boolean _open; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
87 |
| 865 | 88 private volatile long _idleTimestamp; |
| 89 private volatile boolean _checkIdle; | |
| 90 | |
| 91 private boolean _interruptable; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
92 |
| 865 | 93 private boolean _ishut; |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
94 |
| 865 | 95 /* ------------------------------------------------------------ */ |
| 96 public SelectChannelEndPoint(SocketChannel channel, SelectSet selectSet, SelectionKey key, int maxIdleTime) | |
| 97 throws IOException | |
| 98 { | |
| 99 super(channel, maxIdleTime); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
100 |
| 865 | 101 _manager = selectSet.getManager(); |
| 102 _selectSet = selectSet; | |
| 103 _state=STATE_UNDISPATCHED; | |
| 104 _onIdle=false; | |
| 105 _open=true; | |
| 106 _key = key; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
107 |
| 865 | 108 setCheckForIdle(true); |
| 109 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
110 |
| 865 | 111 /* ------------------------------------------------------------ */ |
| 112 public SelectionKey getSelectionKey() | |
| 113 { | |
| 114 synchronized (this) | |
| 115 { | |
| 116 return _key; | |
| 117 } | |
| 118 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
119 |
| 865 | 120 /* ------------------------------------------------------------ */ |
| 121 public SelectorManager getSelectManager() | |
| 122 { | |
| 123 return _manager; | |
| 124 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
125 |
| 865 | 126 /* ------------------------------------------------------------ */ |
| 127 public Connection getConnection() | |
| 128 { | |
| 129 return _connection; | |
| 130 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
131 |
| 865 | 132 /* ------------------------------------------------------------ */ |
| 133 public void setConnection(Connection connection) | |
| 134 { | |
| 135 _connection=(AsyncConnection)connection; | |
| 136 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
137 |
| 865 | 138 /* ------------------------------------------------------------ */ |
| 139 public long getIdleTimestamp() | |
| 140 { | |
| 141 return _idleTimestamp; | |
| 142 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
143 |
| 865 | 144 /* ------------------------------------------------------------ */ |
| 145 /** Called by selectSet to schedule handling | |
| 146 * | |
| 147 */ | |
| 148 public void schedule() | |
| 149 { | |
| 150 synchronized (this) | |
| 151 { | |
| 152 // If there is no key, then do nothing | |
| 153 if (_key == null || !_key.isValid()) | |
| 154 { | |
| 155 _readBlocked=false; | |
| 156 _writeBlocked=false; | |
| 157 this.notifyAll(); | |
| 158 return; | |
| 159 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
160 |
| 865 | 161 // If there are threads dispatched reading and writing |
| 162 if (_readBlocked || _writeBlocked) | |
| 163 { | |
| 164 // assert _dispatched; | |
| 165 if (_readBlocked && _key.isReadable()) | |
| 166 _readBlocked=false; | |
| 167 if (_writeBlocked && _key.isWritable()) | |
| 168 _writeBlocked=false; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
169 |
| 865 | 170 // wake them up is as good as a dispatched. |
| 171 this.notifyAll(); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
172 |
| 865 | 173 // we are not interested in further selecting |
| 174 _key.interestOps(0); | |
| 175 if (_state<STATE_DISPATCHED) | |
| 176 updateKey(); | |
| 177 return; | |
| 178 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
179 |
| 865 | 180 // Remove writeable op |
| 181 if ((_key.readyOps() & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE && (_key.interestOps() & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE) | |
| 182 { | |
| 183 // Remove writeable op | |
| 184 _interestOps = _key.interestOps() & ~SelectionKey.OP_WRITE; | |
| 185 _key.interestOps(_interestOps); | |
| 186 _writable = true; // Once writable is in ops, only removed with dispatch. | |
| 187 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
188 |
| 865 | 189 // If dispatched, then deregister interest |
| 190 if (_state>=STATE_DISPATCHED) | |
| 191 _key.interestOps(0); | |
| 192 else | |
| 193 { | |
| 194 // other wise do the dispatch | |
| 195 dispatch(); | |
| 196 } | |
| 197 } | |
| 198 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
199 |
| 865 | 200 /* ------------------------------------------------------------ */ |
| 201 public void asyncDispatch() | |
| 202 { | |
| 203 synchronized(this) | |
| 204 { | |
| 205 switch(_state) | |
| 206 { | |
| 207 case STATE_NEEDS_DISPATCH: | |
| 208 case STATE_UNDISPATCHED: | |
| 209 dispatch(); | |
| 210 break; | |
| 211 | |
| 212 case STATE_DISPATCHED: | |
| 213 case STATE_ASYNC: | |
| 214 _state=STATE_ASYNC; | |
| 215 break; | |
| 216 } | |
| 217 } | |
| 218 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
219 |
| 865 | 220 /* ------------------------------------------------------------ */ |
| 221 public void dispatch() | |
| 222 { | |
| 223 synchronized(this) | |
| 224 { | |
| 225 if (_state<=STATE_UNDISPATCHED) | |
| 226 { | |
| 227 if (_onIdle) | |
| 228 _state = STATE_NEEDS_DISPATCH; | |
| 229 else | |
| 230 { | |
| 231 _state = STATE_DISPATCHED; | |
| 232 try { | |
| 233 _manager.execute(_handler); | |
| 234 } catch(RejectedExecutionException e) { | |
| 235 _state = STATE_NEEDS_DISPATCH; | |
| 236 LOG.warn("Dispatched Failed! "+this+" to "+_manager); | |
| 237 updateKey(); | |
| 238 } | |
| 239 } | |
| 240 } | |
| 241 } | |
| 242 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
243 |
| 865 | 244 /* ------------------------------------------------------------ */ |
| 245 /** | |
| 246 * Called when a dispatched thread is no longer handling the endpoint. | |
| 247 * The selection key operations are updated. | |
| 248 * @return If false is returned, the endpoint has been redispatched and | |
| 249 * thread must keep handling the endpoint. | |
| 250 */ | |
| 251 protected boolean undispatch() | |
| 252 { | |
| 253 synchronized (this) | |
| 254 { | |
| 255 switch(_state) | |
| 256 { | |
| 257 case STATE_ASYNC: | |
| 258 _state=STATE_DISPATCHED; | |
| 259 return false; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
260 |
| 865 | 261 default: |
| 262 _state=STATE_UNDISPATCHED; | |
| 263 updateKey(); | |
| 264 return true; | |
| 265 } | |
| 266 } | |
| 267 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
268 |
| 865 | 269 /* ------------------------------------------------------------ */ |
| 270 public void setCheckForIdle(boolean check) | |
| 271 { | |
| 272 if (check) | |
| 273 { | |
| 274 _idleTimestamp=System.currentTimeMillis(); | |
| 275 _checkIdle=true; | |
| 276 } | |
| 277 else | |
| 278 _checkIdle=false; | |
| 279 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
280 |
| 865 | 281 /* ------------------------------------------------------------ */ |
| 282 public boolean isCheckForIdle() | |
| 283 { | |
| 284 return _checkIdle; | |
| 285 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
286 |
| 865 | 287 /* ------------------------------------------------------------ */ |
| 288 protected void notIdle() | |
| 289 { | |
| 290 _idleTimestamp=System.currentTimeMillis(); | |
| 291 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
292 |
| 865 | 293 /* ------------------------------------------------------------ */ |
| 294 public void checkIdleTimestamp(long now) | |
| 295 { | |
| 296 if (isCheckForIdle() && _maxIdleTime>0) | |
| 297 { | |
| 298 final long idleForMs=now-_idleTimestamp; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
299 |
| 865 | 300 if (idleForMs>_maxIdleTime) |
| 301 { | |
| 302 // Don't idle out again until onIdleExpired task completes. | |
| 303 setCheckForIdle(false); | |
| 304 _manager.execute(new Runnable() | |
| 305 { | |
| 306 public void run() | |
| 307 { | |
| 308 try | |
| 309 { | |
| 310 onIdleExpired(idleForMs); | |
| 311 } | |
| 312 finally | |
| 313 { | |
| 314 setCheckForIdle(true); | |
| 315 } | |
| 316 } | |
| 317 }); | |
| 318 } | |
| 319 } | |
| 320 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
321 |
| 865 | 322 /* ------------------------------------------------------------ */ |
| 323 public void onIdleExpired(long idleForMs) | |
| 324 { | |
| 325 try | |
| 326 { | |
| 327 synchronized (this) | |
| 328 { | |
| 329 _onIdle=true; | |
| 330 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
331 |
| 865 | 332 _connection.onIdleExpired(idleForMs); |
| 333 } | |
| 334 finally | |
| 335 { | |
| 336 synchronized (this) | |
| 337 { | |
| 338 _onIdle=false; | |
| 339 if (_state==STATE_NEEDS_DISPATCH) | |
| 340 dispatch(); | |
| 341 } | |
| 342 } | |
| 343 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
344 |
| 865 | 345 /* ------------------------------------------------------------ */ |
| 346 @Override | |
| 347 public int fill(Buffer buffer) throws IOException | |
| 348 { | |
| 349 int fill=super.fill(buffer); | |
| 350 if (fill>0) | |
| 351 notIdle(); | |
| 352 return fill; | |
| 353 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
354 |
| 865 | 355 /* ------------------------------------------------------------ */ |
| 356 @Override | |
| 357 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException | |
| 358 { | |
| 359 int l = super.flush(header, buffer, trailer); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
360 |
| 865 | 361 // If there was something to write and it wasn't written, then we are not writable. |
| 362 if (l==0 && ( header!=null && header.hasContent() || buffer!=null && buffer.hasContent() || trailer!=null && trailer.hasContent())) | |
| 363 { | |
| 364 synchronized (this) | |
| 365 { | |
| 366 _writable=false; | |
| 367 if (_state<STATE_DISPATCHED) | |
| 368 updateKey(); | |
| 369 } | |
| 370 } | |
| 371 else if (l>0) | |
| 372 { | |
| 373 _writable=true; | |
| 374 notIdle(); | |
| 375 } | |
| 376 return l; | |
| 377 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
378 |
| 865 | 379 /* ------------------------------------------------------------ */ |
| 380 /* | |
| 381 */ | |
| 382 @Override | |
| 383 public int flush(Buffer buffer) throws IOException | |
| 384 { | |
| 385 int l = super.flush(buffer); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
386 |
| 865 | 387 // If there was something to write and it wasn't written, then we are not writable. |
| 388 if (l==0 && buffer!=null && buffer.hasContent()) | |
| 389 { | |
| 390 synchronized (this) | |
| 391 { | |
| 392 _writable=false; | |
| 393 if (_state<STATE_DISPATCHED) | |
| 394 updateKey(); | |
| 395 } | |
| 396 } | |
| 397 else if (l>0) | |
| 398 { | |
| 399 _writable=true; | |
| 400 notIdle(); | |
| 401 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
402 |
| 865 | 403 return l; |
| 404 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
405 |
| 865 | 406 /* ------------------------------------------------------------ */ |
| 407 /* | |
| 408 * Allows thread to block waiting for further events. | |
| 409 */ | |
| 410 @Override | |
| 411 public boolean blockReadable(long timeoutMs) throws IOException | |
| 412 { | |
| 413 synchronized (this) | |
| 414 { | |
| 415 if (isInputShutdown()) | |
| 416 throw new EofException(); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
417 |
| 865 | 418 long now=_selectSet.getNow(); |
| 419 long end=now+timeoutMs; | |
| 420 boolean check=isCheckForIdle(); | |
| 421 setCheckForIdle(true); | |
| 422 try | |
| 423 { | |
| 424 _readBlocked=true; | |
| 425 while (!isInputShutdown() && _readBlocked) | |
| 426 { | |
| 427 try | |
| 428 { | |
| 429 updateKey(); | |
| 430 this.wait(timeoutMs>0?(end-now):10000); | |
| 431 } | |
| 432 catch (final InterruptedException e) | |
| 433 { | |
| 434 LOG.warn("",e); | |
| 435 if (_interruptable) | |
| 436 throw new InterruptedIOException(){{this.initCause(e);}}; | |
| 437 } | |
| 438 finally | |
| 439 { | |
| 440 now=_selectSet.getNow(); | |
| 441 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
442 |
| 865 | 443 if (_readBlocked && timeoutMs>0 && now>=end) |
| 444 return false; | |
| 445 } | |
| 446 } | |
| 447 finally | |
| 448 { | |
| 449 _readBlocked=false; | |
| 450 setCheckForIdle(check); | |
| 451 } | |
| 452 } | |
| 453 return true; | |
| 454 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
455 |
| 865 | 456 /* ------------------------------------------------------------ */ |
| 457 /* | |
| 458 * Allows thread to block waiting for further events. | |
| 459 */ | |
| 460 @Override | |
| 461 public boolean blockWritable(long timeoutMs) throws IOException | |
| 462 { | |
| 463 synchronized (this) | |
| 464 { | |
| 465 if (isOutputShutdown()) | |
| 466 throw new EofException(); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
467 |
| 865 | 468 long now=_selectSet.getNow(); |
| 469 long end=now+timeoutMs; | |
| 470 boolean check=isCheckForIdle(); | |
| 471 setCheckForIdle(true); | |
| 472 try | |
| 473 { | |
| 474 _writeBlocked=true; | |
| 475 while (_writeBlocked && !isOutputShutdown()) | |
| 476 { | |
| 477 try | |
| 478 { | |
| 479 updateKey(); | |
| 480 this.wait(timeoutMs>0?(end-now):10000); | |
| 481 } | |
| 482 catch (final InterruptedException e) | |
| 483 { | |
| 484 LOG.warn("",e); | |
| 485 if (_interruptable) | |
| 486 throw new InterruptedIOException(){{this.initCause(e);}}; | |
| 487 } | |
| 488 finally | |
| 489 { | |
| 490 now=_selectSet.getNow(); | |
| 491 } | |
| 492 if (_writeBlocked && timeoutMs>0 && now>=end) | |
| 493 return false; | |
| 494 } | |
| 495 } | |
| 496 finally | |
| 497 { | |
| 498 _writeBlocked=false; | |
| 499 setCheckForIdle(check); | |
| 500 } | |
| 501 } | |
| 502 return true; | |
| 503 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
504 |
| 865 | 505 /* ------------------------------------------------------------ */ |
| 506 /** Set the interruptable mode of the endpoint. | |
| 507 * If set to false (default), then interrupts are assumed to be spurious | |
| 508 * and blocking operations continue unless the endpoint has been closed. | |
| 509 * If true, then interrupts of blocking operations result in InterruptedIOExceptions | |
| 510 * being thrown. | |
| 511 * @param interupable | |
| 512 */ | |
| 513 public void setInterruptable(boolean interupable) | |
| 514 { | |
| 515 synchronized (this) | |
| 516 { | |
| 517 _interruptable=interupable; | |
| 518 } | |
| 519 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
520 |
| 865 | 521 /* ------------------------------------------------------------ */ |
| 522 public boolean isInterruptable() | |
| 523 { | |
| 524 return _interruptable; | |
| 525 } | |
| 526 | |
| 527 /* ------------------------------------------------------------ */ | |
| 528 /** | |
| 529 * @see org.eclipse.jetty.io.AsyncEndPoint#scheduleWrite() | |
| 530 */ | |
| 531 public void scheduleWrite() | |
| 532 { | |
| 533 if (_writable) | |
| 534 LOG.debug("Required scheduleWrite {}",this); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
535 |
| 865 | 536 _writable=false; |
| 537 updateKey(); | |
| 538 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
539 |
| 865 | 540 /* ------------------------------------------------------------ */ |
| 541 public boolean isWritable() | |
| 542 { | |
| 543 return _writable; | |
| 544 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
545 |
| 865 | 546 /* ------------------------------------------------------------ */ |
| 547 public boolean hasProgressed() | |
| 548 { | |
| 549 return false; | |
| 550 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
551 |
| 865 | 552 /* ------------------------------------------------------------ */ |
| 553 /** | |
| 554 * Updates selection key. Adds operations types to the selection key as needed. No operations | |
| 555 * are removed as this is only done during dispatch. This method records the new key and | |
| 556 * schedules a call to doUpdateKey to do the keyChange | |
| 557 */ | |
| 558 private void updateKey() | |
| 559 { | |
| 560 final boolean changed; | |
| 561 synchronized (this) | |
| 562 { | |
| 563 int current_ops=-1; | |
| 564 if (getChannel().isOpen()) | |
| 565 { | |
| 566 boolean read_interest = _readBlocked || (_state<STATE_DISPATCHED && !_connection.isSuspended()); | |
| 567 boolean write_interest= _writeBlocked || (_state<STATE_DISPATCHED && !_writable); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
568 |
| 865 | 569 _interestOps = |
| 570 ((!_socket.isInputShutdown() && read_interest ) ? SelectionKey.OP_READ : 0) | |
| 571 | ((!_socket.isOutputShutdown()&& write_interest) ? SelectionKey.OP_WRITE : 0); | |
| 572 try | |
| 573 { | |
| 574 current_ops = ((_key!=null && _key.isValid())?_key.interestOps():-1); | |
| 575 } | |
| 576 catch(Exception e) | |
| 577 { | |
| 578 _key=null; | |
| 579 LOG.trace("",e); | |
| 580 } | |
| 581 } | |
| 582 changed=_interestOps!=current_ops; | |
| 583 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
584 |
| 865 | 585 if(changed) |
| 586 { | |
| 587 _selectSet.addChange(this); | |
| 588 _selectSet.wakeup(); | |
| 589 } | |
| 590 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
591 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
592 |
| 865 | 593 /* ------------------------------------------------------------ */ |
| 594 /** | |
| 595 * Synchronize the interestOps with the actual key. Call is scheduled by a call to updateKey | |
| 596 */ | |
| 597 void doUpdateKey() | |
| 598 { | |
| 599 synchronized (this) | |
| 600 { | |
| 601 if (getChannel().isOpen()) | |
| 602 { | |
| 603 if (_interestOps>0) | |
| 604 { | |
| 605 if (_key==null || !_key.isValid()) | |
| 606 { | |
| 607 SelectableChannel sc = (SelectableChannel)getChannel(); | |
| 608 if (sc.isRegistered()) | |
| 609 { | |
| 610 updateKey(); | |
| 611 } | |
| 612 else | |
| 613 { | |
| 614 try | |
| 615 { | |
| 616 _key=((SelectableChannel)getChannel()).register(_selectSet.getSelector(),_interestOps,this); | |
| 617 } | |
| 618 catch (Exception e) | |
| 619 { | |
| 620 LOG.trace("",e); | |
| 621 if (_key!=null && _key.isValid()) | |
| 622 { | |
| 623 _key.cancel(); | |
| 624 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
625 |
| 865 | 626 if (_open) |
| 627 { | |
| 628 _selectSet.destroyEndPoint(this); | |
| 629 } | |
| 630 _open=false; | |
| 631 _key = null; | |
| 632 } | |
| 633 } | |
| 634 } | |
| 635 else | |
| 636 { | |
| 637 _key.interestOps(_interestOps); | |
| 638 } | |
| 639 } | |
| 640 else | |
| 641 { | |
| 642 if (_key!=null && _key.isValid()) | |
| 643 _key.interestOps(0); | |
| 644 else | |
| 645 _key=null; | |
| 646 } | |
| 647 } | |
| 648 else | |
| 649 { | |
| 650 if (_key!=null && _key.isValid()) | |
| 651 _key.cancel(); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
652 |
| 865 | 653 if (_open) |
| 654 { | |
| 655 _open=false; | |
| 656 _selectSet.destroyEndPoint(this); | |
| 657 } | |
| 658 _key = null; | |
| 659 } | |
| 660 } | |
| 661 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
662 |
| 865 | 663 /* ------------------------------------------------------------ */ |
| 664 /* | |
| 665 */ | |
| 666 protected void handle() | |
| 667 { | |
| 668 boolean dispatched=true; | |
| 669 try | |
| 670 { | |
| 671 while(dispatched) | |
| 672 { | |
| 673 try | |
| 674 { | |
| 675 while(true) | |
| 676 { | |
| 677 final AsyncConnection next = (AsyncConnection)_connection.handle(); | |
| 678 if (next!=_connection) | |
| 679 { | |
| 680 LOG.debug("{} replaced {}",next,_connection); | |
| 681 _connection=next; | |
| 682 continue; | |
| 683 } | |
| 684 break; | |
| 685 } | |
| 686 } | |
| 687 catch (ClosedChannelException e) | |
| 688 { | |
| 689 LOG.trace("",e); | |
| 690 } | |
| 691 catch (EofException e) | |
| 692 { | |
| 693 LOG.debug("EOF", e); | |
| 694 try{close();} | |
| 695 catch(IOException e2){LOG.trace("",e2);} | |
| 696 } | |
| 697 catch (IOException e) | |
| 698 { | |
| 699 LOG.warn(e.toString()); | |
| 700 try{close();} | |
| 701 catch(IOException e2){LOG.trace("",e2);} | |
| 702 } | |
| 703 catch (Throwable e) | |
| 704 { | |
| 705 LOG.warn("handle failed", e); | |
| 706 try{close();} | |
| 707 catch(IOException e2){LOG.trace("",e2);} | |
| 708 } | |
| 709 finally | |
| 710 { | |
| 711 if (!_ishut && isInputShutdown() && isOpen()) | |
| 712 { | |
| 713 _ishut=true; | |
| 714 try | |
| 715 { | |
| 716 _connection.onInputShutdown(); | |
| 717 } | |
| 718 catch(Throwable x) | |
| 719 { | |
| 720 LOG.warn("onInputShutdown failed", x); | |
| 721 try{close();} | |
| 722 catch(IOException e2){LOG.trace("",e2);} | |
| 723 } | |
| 724 finally | |
| 725 { | |
| 726 updateKey(); | |
| 727 } | |
| 728 } | |
| 729 dispatched=!undispatch(); | |
| 730 } | |
| 731 } | |
| 732 } | |
| 733 finally | |
| 734 { | |
| 735 if (dispatched) | |
| 736 { | |
| 737 dispatched=!undispatch(); | |
| 738 while (dispatched) | |
| 739 { | |
| 740 LOG.warn("SCEP.run() finally DISPATCHED"); | |
| 741 dispatched=!undispatch(); | |
| 742 } | |
| 743 } | |
| 744 } | |
| 745 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
746 |
| 865 | 747 /* ------------------------------------------------------------ */ |
| 748 /* | |
| 749 * @see org.eclipse.io.nio.ChannelEndPoint#close() | |
| 750 */ | |
| 751 @Override | |
| 752 public void close() throws IOException | |
| 753 { | |
| 754 try | |
| 755 { | |
| 756 super.close(); | |
| 757 } | |
| 758 catch (IOException e) | |
| 759 { | |
| 760 LOG.trace("",e); | |
| 761 } | |
| 762 finally | |
| 763 { | |
| 764 updateKey(); | |
| 765 } | |
| 766 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
767 |
| 865 | 768 /* ------------------------------------------------------------ */ |
| 769 @Override | |
| 770 public String toString() | |
| 771 { | |
| 772 // Do NOT use synchronized (this) | |
| 773 // because it's very easy to deadlock when debugging is enabled. | |
| 774 // We do a best effort to print the right toString() and that's it. | |
| 775 SelectionKey key = _key; | |
| 776 String keyString = ""; | |
| 777 if (key != null) | |
| 778 { | |
| 779 if (key.isValid()) | |
| 780 { | |
| 781 if (key.isReadable()) | |
| 782 keyString += "r"; | |
| 783 if (key.isWritable()) | |
| 784 keyString += "w"; | |
| 785 } | |
| 786 else | |
| 787 { | |
| 788 keyString += "!"; | |
| 789 } | |
| 790 } | |
| 791 else | |
| 792 { | |
| 793 keyString += "-"; | |
| 794 } | |
| 795 return String.format("SCEP@%x{l(%s)<->r(%s),s=%d,open=%b,ishut=%b,oshut=%b,rb=%b,wb=%b,w=%b,i=%d%s}-{%s}", | |
| 796 hashCode(), | |
| 797 _socket.getRemoteSocketAddress(), | |
| 798 _socket.getLocalSocketAddress(), | |
| 799 _state, | |
| 800 isOpen(), | |
| 801 isInputShutdown(), | |
| 802 isOutputShutdown(), | |
| 803 _readBlocked, | |
| 804 _writeBlocked, | |
| 805 _writable, | |
| 806 _interestOps, | |
| 807 keyString, | |
| 808 _connection); | |
| 809 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
810 |
| 865 | 811 /* ------------------------------------------------------------ */ |
| 812 public SelectSet getSelectSet() | |
| 813 { | |
| 814 return _selectSet; | |
| 815 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
816 |
| 865 | 817 /* ------------------------------------------------------------ */ |
| 818 /** | |
| 819 * Don't set the SoTimeout | |
| 820 * @see org.eclipse.jetty.io.nio.ChannelEndPoint#setMaxIdleTime(int) | |
| 821 */ | |
| 822 @Override | |
| 823 public void setMaxIdleTime(int timeMs) throws IOException | |
| 824 { | |
| 825 _maxIdleTime=timeMs; | |
| 826 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
827 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
828 } |
