Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java @ 958:fc521d2f098e
simplify SelectorManager
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 13 Oct 2016 18:48:20 -0600 |
parents | 1094975d013b |
children | 7b94f5b33c64 |
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 */ |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
44 public final class SelectChannelEndPoint extends ChannelEndPoint implements AsyncEndPoint, ConnectedEndPoint |
802
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 |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
67 private static final int STATE_NEEDS_DISPATCH = -1; |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
68 private static final int STATE_UNDISPATCHED = 0; |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
69 private static final int STATE_DISPATCHED = 1; |
865 | 70 private int _state; |
71 | |
72 /** true if the last write operation succeed and wrote all offered bytes */ | |
73 private volatile boolean _writable = true; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
74 |
865 | 75 /** True if a thread has is blocked in {@link #blockReadable(long)} */ |
76 private boolean _readBlocked; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
77 |
865 | 78 /** True if a thread has is blocked in {@link #blockWritable(long)} */ |
79 private boolean _writeBlocked; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
80 |
865 | 81 /** true if {@link SelectSet#destroyEndPoint(SelectChannelEndPoint)} has not been called */ |
82 private boolean _open; | |
83 | |
84 private boolean _ishut; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
85 |
865 | 86 public SelectChannelEndPoint(SocketChannel channel, SelectSet selectSet, SelectionKey key, int maxIdleTime) |
87 throws IOException | |
88 { | |
89 super(channel, maxIdleTime); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
90 |
865 | 91 _manager = selectSet.getManager(); |
92 _selectSet = selectSet; | |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
93 _state = STATE_UNDISPATCHED; |
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
94 _open = true; |
865 | 95 _key = key; |
96 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
97 |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
98 @Override |
865 | 99 public Connection getConnection() |
100 { | |
101 return _connection; | |
102 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
103 |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
104 @Override |
865 | 105 public void setConnection(Connection connection) |
106 { | |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
107 _connection = (AsyncConnection)connection; |
865 | 108 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
109 |
865 | 110 /* ------------------------------------------------------------ */ |
111 /** Called by selectSet to schedule handling | |
112 * | |
113 */ | |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
114 public synchronized void schedule() |
865 | 115 { |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
116 // If there is no key, then do nothing |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
117 if (_key == null || !_key.isValid()) |
865 | 118 { |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
119 _readBlocked = false; |
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
120 _writeBlocked = false; |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
121 this.notifyAll(); |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
122 return; |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
123 } |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
124 |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
125 // If there are threads dispatched reading and writing |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
126 if (_readBlocked || _writeBlocked) |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
127 { |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
128 // assert _dispatched; |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
129 if (_readBlocked && _key.isReadable()) |
865 | 130 _readBlocked=false; |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
131 if (_writeBlocked && _key.isWritable()) |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
132 _writeBlocked = false; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
133 |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
134 // wake them up is as good as a dispatched. |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
135 this.notifyAll(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
136 |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
137 // we are not interested in further selecting |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
138 _key.interestOps(0); |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
139 if (_state<STATE_DISPATCHED) |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
140 updateKey(); |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
141 return; |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
142 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
143 |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
144 // Remove writeable op |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
145 if ((_key.readyOps() & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE && (_key.interestOps() & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE) |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
146 { |
865 | 147 // Remove writeable op |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
148 _interestOps = _key.interestOps() & ~SelectionKey.OP_WRITE; |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
149 _key.interestOps(_interestOps); |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
150 _writable = true; // Once writable is in ops, only removed with dispatch. |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
151 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
152 |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
153 // If dispatched, then deregister interest |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
154 if (_state>=STATE_DISPATCHED) |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
155 _key.interestOps(0); |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
156 else |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
157 { |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
158 // other wise do the dispatch |
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
159 dispatch(); |
865 | 160 } |
161 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
162 |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
163 @Override |
947
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
164 public synchronized void dispatch() |
865 | 165 { |
947
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
166 if (_state<=STATE_UNDISPATCHED) |
865 | 167 { |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
168 _state = STATE_DISPATCHED; |
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
169 try { |
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
170 _manager.execute(_handler); |
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
171 } catch(RejectedExecutionException e) { |
947
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
172 _state = STATE_NEEDS_DISPATCH; |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
173 LOG.warn("Dispatched Failed! "+this+" to "+_manager); |
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
174 updateKey(); |
865 | 175 } |
176 } | |
177 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
178 |
865 | 179 /* ------------------------------------------------------------ */ |
180 /** | |
181 * Called when a dispatched thread is no longer handling the endpoint. | |
182 * The selection key operations are updated. | |
183 * @return If false is returned, the endpoint has been redispatched and | |
184 * thread must keep handling the endpoint. | |
185 */ | |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
186 private synchronized void undispatch() |
865 | 187 { |
947
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
188 _state = STATE_UNDISPATCHED; |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
189 updateKey(); |
865 | 190 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
191 |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
192 @Override |
865 | 193 public int fill(Buffer buffer) throws IOException |
194 { | |
195 int fill=super.fill(buffer); | |
196 return fill; | |
197 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
198 |
865 | 199 @Override |
200 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException | |
201 { | |
202 int l = super.flush(header, buffer, trailer); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
203 |
865 | 204 // If there was something to write and it wasn't written, then we are not writable. |
205 if (l==0 && ( header!=null && header.hasContent() || buffer!=null && buffer.hasContent() || trailer!=null && trailer.hasContent())) | |
206 { | |
207 synchronized (this) | |
208 { | |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
209 _writable = false; |
865 | 210 if (_state<STATE_DISPATCHED) |
211 updateKey(); | |
212 } | |
213 } | |
214 else if (l>0) | |
215 { | |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
216 _writable = true; |
865 | 217 } |
218 return l; | |
219 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
220 |
865 | 221 @Override |
222 public int flush(Buffer buffer) throws IOException | |
223 { | |
224 int l = super.flush(buffer); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
225 |
865 | 226 // If there was something to write and it wasn't written, then we are not writable. |
227 if (l==0 && buffer!=null && buffer.hasContent()) | |
228 { | |
229 synchronized (this) | |
230 { | |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
231 _writable = false; |
865 | 232 if (_state<STATE_DISPATCHED) |
233 updateKey(); | |
234 } | |
235 } | |
236 else if (l>0) | |
237 { | |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
238 _writable = true; |
865 | 239 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
240 |
865 | 241 return l; |
242 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
243 |
865 | 244 /* ------------------------------------------------------------ */ |
245 /* | |
246 * Allows thread to block waiting for further events. | |
247 */ | |
248 @Override | |
249 public boolean blockReadable(long timeoutMs) throws IOException | |
250 { | |
251 synchronized (this) | |
252 { | |
253 if (isInputShutdown()) | |
254 throw new EofException(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
255 |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
256 long now = _selectSet.getNow(); |
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
257 long end = now+timeoutMs; |
865 | 258 try |
259 { | |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
260 _readBlocked = true; |
865 | 261 while (!isInputShutdown() && _readBlocked) |
262 { | |
263 try | |
264 { | |
265 updateKey(); | |
266 this.wait(timeoutMs>0?(end-now):10000); | |
267 } | |
268 catch (final InterruptedException e) | |
269 { | |
270 LOG.warn("",e); | |
271 } | |
272 finally | |
273 { | |
274 now=_selectSet.getNow(); | |
275 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
276 |
865 | 277 if (_readBlocked && timeoutMs>0 && now>=end) |
278 return false; | |
279 } | |
280 } | |
281 finally | |
282 { | |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
283 _readBlocked = false; |
865 | 284 } |
285 } | |
286 return true; | |
287 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
288 |
865 | 289 /* ------------------------------------------------------------ */ |
290 /* | |
291 * Allows thread to block waiting for further events. | |
292 */ | |
293 @Override | |
294 public boolean blockWritable(long timeoutMs) throws IOException | |
295 { | |
296 synchronized (this) | |
297 { | |
298 if (isOutputShutdown()) | |
299 throw new EofException(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
300 |
865 | 301 long now=_selectSet.getNow(); |
302 long end=now+timeoutMs; | |
303 try | |
304 { | |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
305 _writeBlocked = true; |
865 | 306 while (_writeBlocked && !isOutputShutdown()) |
307 { | |
308 try | |
309 { | |
310 updateKey(); | |
311 this.wait(timeoutMs>0?(end-now):10000); | |
312 } | |
313 catch (final InterruptedException e) | |
314 { | |
315 LOG.warn("",e); | |
316 } | |
317 finally | |
318 { | |
319 now=_selectSet.getNow(); | |
320 } | |
321 if (_writeBlocked && timeoutMs>0 && now>=end) | |
322 return false; | |
323 } | |
324 } | |
325 finally | |
326 { | |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
327 _writeBlocked = false; |
865 | 328 } |
329 } | |
330 return true; | |
331 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
332 |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
333 @Override |
865 | 334 public boolean hasProgressed() |
335 { | |
336 return false; | |
337 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
338 |
865 | 339 /* ------------------------------------------------------------ */ |
340 /** | |
341 * Updates selection key. Adds operations types to the selection key as needed. No operations | |
342 * are removed as this is only done during dispatch. This method records the new key and | |
343 * schedules a call to doUpdateKey to do the keyChange | |
344 */ | |
345 private void updateKey() | |
346 { | |
347 final boolean changed; | |
348 synchronized (this) | |
349 { | |
350 int current_ops=-1; | |
351 if (getChannel().isOpen()) | |
352 { | |
353 boolean read_interest = _readBlocked || (_state<STATE_DISPATCHED && !_connection.isSuspended()); | |
354 boolean write_interest= _writeBlocked || (_state<STATE_DISPATCHED && !_writable); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
355 |
865 | 356 _interestOps = |
357 ((!_socket.isInputShutdown() && read_interest ) ? SelectionKey.OP_READ : 0) | |
358 | ((!_socket.isOutputShutdown()&& write_interest) ? SelectionKey.OP_WRITE : 0); | |
359 try | |
360 { | |
361 current_ops = ((_key!=null && _key.isValid())?_key.interestOps():-1); | |
362 } | |
363 catch(Exception e) | |
364 { | |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
365 _key = null; |
865 | 366 LOG.trace("",e); |
367 } | |
368 } | |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
369 changed = _interestOps!=current_ops; |
865 | 370 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
371 |
865 | 372 if(changed) |
373 { | |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
374 doUpdateKey(); |
950 | 375 _selectSet.getSelector().update(); |
865 | 376 } |
377 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
378 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
379 |
865 | 380 /* ------------------------------------------------------------ */ |
381 /** | |
382 * Synchronize the interestOps with the actual key. Call is scheduled by a call to updateKey | |
383 */ | |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
384 synchronized void doUpdateKey() |
865 | 385 { |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
386 if (getChannel().isOpen()) |
865 | 387 { |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
388 if (_interestOps>0) |
865 | 389 { |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
390 if (_key==null || !_key.isValid()) |
865 | 391 { |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
392 SelectableChannel sc = (SelectableChannel)getChannel(); |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
393 if (sc.isRegistered()) |
865 | 394 { |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
395 updateKey(); |
865 | 396 } |
397 else | |
398 { | |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
399 try |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
400 { |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
401 _key = _selectSet.getSelector().register(sc,_interestOps,this); |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
402 } |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
403 catch (Exception e) |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
404 { |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
405 LOG.trace("",e); |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
406 if (_key!=null && _key.isValid()) |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
407 { |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
408 _key.cancel(); |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
409 } |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
410 |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
411 if (_open) |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
412 { |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
413 _selectSet.destroyEndPoint(this); |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
414 } |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
415 _open = false; |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
416 _key = null; |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
417 } |
865 | 418 } |
419 } | |
420 else | |
421 { | |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
422 _key.interestOps(_interestOps); |
865 | 423 } |
424 } | |
425 else | |
426 { | |
427 if (_key!=null && _key.isValid()) | |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
428 _key.interestOps(0); |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
429 else |
953
7db4a488fc82
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
952
diff
changeset
|
430 _key = null; |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
431 } |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
432 } |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
433 else |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
434 { |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
435 if (_key!=null && _key.isValid()) |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
436 _key.cancel(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
437 |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
438 if (_open) |
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
439 { |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
440 _open = false; |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
441 _selectSet.destroyEndPoint(this); |
865 | 442 } |
949
e9088af3787f
remove SelectSet._changes
Franklin Schmidt <fschmidt@gmail.com>
parents:
948
diff
changeset
|
443 _key = null; |
865 | 444 } |
445 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
446 |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
447 private void handle() |
865 | 448 { |
947
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
449 boolean dispatched = true; |
865 | 450 try |
451 { | |
947
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
452 try |
865 | 453 { |
947
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
454 while(true) |
865 | 455 { |
947
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
456 final AsyncConnection next = (AsyncConnection)_connection.handle(); |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
457 if (next!=_connection) |
865 | 458 { |
947
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
459 LOG.debug("{} replaced {}",next,_connection); |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
460 _connection=next; |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
461 continue; |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
462 } |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
463 break; |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
464 } |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
465 } |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
466 catch (ClosedChannelException e) |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
467 { |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
468 LOG.trace("",e); |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
469 } |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
470 catch (EofException e) |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
471 { |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
472 LOG.debug("EOF", e); |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
473 try{close();} |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
474 catch(IOException e2){LOG.trace("",e2);} |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
475 } |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
476 catch (IOException e) |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
477 { |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
478 LOG.warn(e.toString()); |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
479 try{close();} |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
480 catch(IOException e2){LOG.trace("",e2);} |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
481 } |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
482 catch (Throwable e) |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
483 { |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
484 LOG.warn("handle failed", e); |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
485 try{close();} |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
486 catch(IOException e2){LOG.trace("",e2);} |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
487 } |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
488 finally |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
489 { |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
490 if (!_ishut && isInputShutdown() && isOpen()) |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
491 { |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
492 _ishut = true; |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
493 try |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
494 { |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
495 _connection.onInputShutdown(); |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
496 } |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
497 catch(Throwable x) |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
498 { |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
499 LOG.warn("onInputShutdown failed", x); |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
500 try{close();} |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
501 catch(IOException e2){LOG.trace("",e2);} |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
502 } |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
503 finally |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
504 { |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
505 updateKey(); |
865 | 506 } |
507 } | |
947
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
508 undispatch(); |
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
509 dispatched = false; |
865 | 510 } |
511 } | |
512 finally | |
513 { | |
514 if (dispatched) | |
515 { | |
947
64f3d8dae31d
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
946
diff
changeset
|
516 undispatch(); |
865 | 517 } |
518 } | |
519 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
520 |
865 | 521 /* ------------------------------------------------------------ */ |
522 /* | |
523 * @see org.eclipse.io.nio.ChannelEndPoint#close() | |
524 */ | |
525 @Override | |
526 public void close() throws IOException | |
527 { | |
528 try | |
529 { | |
530 super.close(); | |
531 } | |
532 catch (IOException e) | |
533 { | |
534 LOG.trace("",e); | |
535 } | |
536 finally | |
537 { | |
538 updateKey(); | |
539 } | |
540 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
541 |
865 | 542 @Override |
543 public String toString() | |
544 { | |
545 // Do NOT use synchronized (this) | |
546 // because it's very easy to deadlock when debugging is enabled. | |
547 // We do a best effort to print the right toString() and that's it. | |
548 SelectionKey key = _key; | |
549 String keyString = ""; | |
550 if (key != null) | |
551 { | |
552 if (key.isValid()) | |
553 { | |
554 if (key.isReadable()) | |
555 keyString += "r"; | |
556 if (key.isWritable()) | |
557 keyString += "w"; | |
558 } | |
559 else | |
560 { | |
561 keyString += "!"; | |
562 } | |
563 } | |
564 else | |
565 { | |
566 keyString += "-"; | |
567 } | |
568 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}", | |
569 hashCode(), | |
570 _socket.getRemoteSocketAddress(), | |
571 _socket.getLocalSocketAddress(), | |
572 _state, | |
573 isOpen(), | |
574 isInputShutdown(), | |
575 isOutputShutdown(), | |
576 _readBlocked, | |
577 _writeBlocked, | |
578 _writable, | |
579 _interestOps, | |
580 keyString, | |
581 _connection); | |
582 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
583 |
865 | 584 /* ------------------------------------------------------------ */ |
585 /** | |
586 * Don't set the SoTimeout | |
587 * @see org.eclipse.jetty.io.nio.ChannelEndPoint#setMaxIdleTime(int) | |
588 */ | |
589 @Override | |
590 public void setMaxIdleTime(int timeMs) throws IOException | |
591 { | |
955
6f49b8dfffe6
simplify SelectChannelEndPoint
Franklin Schmidt <fschmidt@gmail.com>
parents:
953
diff
changeset
|
592 _maxIdleTime = timeMs; |
865 | 593 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
594 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
595 } |