Mercurial Hosting > luan
comparison src/org/eclipse/jetty/io/nio/ChannelEndPoint.java @ 978:bdb6eb0fbf93
simplify ChannelEndPoint
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 16 Oct 2016 14:53:43 -0600 |
parents | 866f2e801618 |
children | 4f2d04c72781 |
comparison
equal
deleted
inserted
replaced
977:d35b0a3a7a4a | 978:bdb6eb0fbf93 |
---|---|
23 import java.net.Socket; | 23 import java.net.Socket; |
24 import java.net.SocketException; | 24 import java.net.SocketException; |
25 import java.nio.ByteBuffer; | 25 import java.nio.ByteBuffer; |
26 import java.nio.channels.ByteChannel; | 26 import java.nio.channels.ByteChannel; |
27 import java.nio.channels.GatheringByteChannel; | 27 import java.nio.channels.GatheringByteChannel; |
28 import java.nio.channels.SelectableChannel; | |
29 import java.nio.channels.SocketChannel; | 28 import java.nio.channels.SocketChannel; |
30 | 29 |
31 import org.eclipse.jetty.io.Buffer; | 30 import org.eclipse.jetty.io.Buffer; |
32 import org.eclipse.jetty.io.EndPoint; | 31 import org.eclipse.jetty.io.EndPoint; |
33 import org.eclipse.jetty.util.StringUtil; | 32 import org.eclipse.jetty.util.StringUtil; |
41 */ | 40 */ |
42 public class ChannelEndPoint implements EndPoint | 41 public class ChannelEndPoint implements EndPoint |
43 { | 42 { |
44 private static final Logger LOG = LoggerFactory.getLogger(ChannelEndPoint.class); | 43 private static final Logger LOG = LoggerFactory.getLogger(ChannelEndPoint.class); |
45 | 44 |
46 protected final ByteChannel _channel; | 45 private final SocketChannel _channel; |
47 protected final ByteBuffer[] _gather2=new ByteBuffer[2]; | 46 private final ByteBuffer[] _gather2 = new ByteBuffer[2]; |
48 protected final Socket _socket; | 47 protected final Socket _socket; |
49 protected final InetSocketAddress _local; | 48 private final InetSocketAddress _local; |
50 protected final InetSocketAddress _remote; | 49 private final InetSocketAddress _remote; |
51 protected volatile int _maxIdleTime; | 50 protected volatile int _maxIdleTime; |
52 private volatile boolean _ishut; | 51 private volatile boolean _ishut = false; |
53 private volatile boolean _oshut; | 52 private volatile boolean _oshut = false; |
54 | 53 |
55 protected ChannelEndPoint(ByteChannel channel, int maxIdleTime) throws IOException | 54 protected ChannelEndPoint(SocketChannel channel, int maxIdleTime) throws IOException |
56 { | 55 { |
57 this._channel = channel; | 56 this._channel = channel; |
58 _maxIdleTime = maxIdleTime; | 57 _maxIdleTime = maxIdleTime; |
59 _socket = (channel instanceof SocketChannel)?((SocketChannel)channel).socket():null; | 58 _socket = channel.socket(); |
60 if (_socket!=null) | 59 _local = (InetSocketAddress)_socket.getLocalSocketAddress(); |
61 { | 60 _remote = (InetSocketAddress)_socket.getRemoteSocketAddress(); |
62 _local = (InetSocketAddress)_socket.getLocalSocketAddress(); | 61 _socket.setSoTimeout(_maxIdleTime); |
63 _remote = (InetSocketAddress)_socket.getRemoteSocketAddress(); | |
64 _socket.setSoTimeout(_maxIdleTime); | |
65 } | |
66 else | |
67 { | |
68 _local = _remote = null; | |
69 } | |
70 } | 62 } |
71 | 63 |
72 @Override | 64 @Override |
73 public final boolean isBlocking() | 65 public final boolean isBlocking() |
74 { | 66 { |
75 return !(_channel instanceof SelectableChannel) || ((SelectableChannel)_channel).isBlocking(); | 67 return _channel.isBlocking(); |
76 } | 68 } |
77 | 69 |
78 @Override | 70 @Override |
79 public boolean blockReadable(long millisecs) throws IOException | 71 public boolean blockReadable(long millisecs) throws IOException |
80 { | 72 { |
98 { | 90 { |
99 LOG.debug("ishut {}", this); | 91 LOG.debug("ishut {}", this); |
100 _ishut = true; | 92 _ishut = true; |
101 if (_channel.isOpen()) | 93 if (_channel.isOpen()) |
102 { | 94 { |
103 if (_socket != null) | 95 try |
104 { | 96 { |
105 try | 97 if (!_socket.isInputShutdown()) |
106 { | 98 { |
107 if (!_socket.isInputShutdown()) | 99 _socket.shutdownInput(); |
108 { | 100 } |
109 _socket.shutdownInput(); | 101 } |
110 } | 102 catch (SocketException e) |
111 } | 103 { |
112 catch (SocketException e) | 104 LOG.debug(e.toString()); |
113 { | 105 LOG.trace("",e); |
114 LOG.debug(e.toString()); | 106 } |
115 LOG.trace("",e); | 107 finally |
116 } | 108 { |
117 finally | 109 if (_oshut) |
118 { | 110 { |
119 if (_oshut) | 111 close(); |
120 { | |
121 close(); | |
122 } | |
123 } | 112 } |
124 } | 113 } |
125 } | 114 } |
126 } | 115 } |
127 | 116 |
130 { | 119 { |
131 LOG.debug("oshut {}",this); | 120 LOG.debug("oshut {}",this); |
132 _oshut = true; | 121 _oshut = true; |
133 if (_channel.isOpen()) | 122 if (_channel.isOpen()) |
134 { | 123 { |
135 if (_socket != null) | 124 try |
136 { | 125 { |
137 try | 126 if (!_socket.isOutputShutdown()) |
138 { | 127 { |
139 if (!_socket.isOutputShutdown()) | 128 _socket.shutdownOutput(); |
140 { | 129 } |
141 _socket.shutdownOutput(); | 130 } |
142 } | 131 catch (SocketException e) |
143 } | 132 { |
144 catch (SocketException e) | 133 LOG.debug(e.toString()); |
145 { | 134 LOG.trace("",e); |
146 LOG.debug(e.toString()); | 135 } |
147 LOG.trace("",e); | 136 finally |
148 } | 137 { |
149 finally | 138 if (_ishut) |
150 { | 139 { |
151 if (_ishut) | 140 close(); |
152 { | |
153 close(); | |
154 } | |
155 } | 141 } |
156 } | 142 } |
157 } | 143 } |
158 } | 144 } |
159 | 145 |
160 @Override | 146 @Override |
161 public final boolean isOutputShutdown() | 147 public final boolean isOutputShutdown() |
162 { | 148 { |
163 return _oshut || !_channel.isOpen() || _socket != null && _socket.isOutputShutdown(); | 149 return _oshut || !_channel.isOpen() || _socket.isOutputShutdown(); |
164 } | 150 } |
165 | 151 |
166 @Override | 152 @Override |
167 public final boolean isInputShutdown() | 153 public final boolean isInputShutdown() |
168 { | 154 { |
169 return _ishut || !_channel.isOpen() || _socket != null && _socket.isInputShutdown(); | 155 return _ishut || !_channel.isOpen() || _socket.isInputShutdown(); |
170 } | 156 } |
171 | 157 |
172 @Override | 158 @Override |
173 public void close() throws IOException | 159 public void close() throws IOException |
174 { | 160 { |
175 LOG.debug("close {}",this); | 161 LOG.debug("close {}",this); |
176 _channel.close(); | 162 _channel.close(); |
177 } | 163 } |
178 | 164 |
179 /* (non-Javadoc) | 165 @Override |
180 * @see org.eclipse.io.EndPoint#fill(org.eclipse.io.Buffer) | |
181 */ | |
182 public int fill(Buffer buffer) throws IOException | 166 public int fill(Buffer buffer) throws IOException |
183 { | 167 { |
184 if (_ishut) | 168 if (_ishut) |
185 return -1; | 169 return -1; |
186 Buffer buf = buffer.buffer(); | 170 Buffer buf = buffer.buffer(); |
239 } | 223 } |
240 | 224 |
241 return len; | 225 return len; |
242 } | 226 } |
243 | 227 |
244 /* (non-Javadoc) | 228 @Override |
245 * @see org.eclipse.io.EndPoint#flush(org.eclipse.io.Buffer) | |
246 */ | |
247 public int flush(Buffer buffer) throws IOException | 229 public int flush(Buffer buffer) throws IOException |
248 { | 230 { |
249 Buffer buf = buffer.buffer(); | 231 Buffer buf = buffer.buffer(); |
250 int len=0; | 232 int len=0; |
251 if (buf instanceof NIOBuffer) | 233 if (buf instanceof NIOBuffer) |
282 throw new IOException("Not Implemented"); | 264 throw new IOException("Not Implemented"); |
283 } | 265 } |
284 return len; | 266 return len; |
285 } | 267 } |
286 | 268 |
287 /* (non-Javadoc) | 269 @Override |
288 * @see org.eclipse.io.EndPoint#flush(org.eclipse.io.Buffer, org.eclipse.io.Buffer, org.eclipse.io.Buffer) | |
289 */ | |
290 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException | 270 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException |
291 { | 271 { |
292 int length=0; | 272 int length=0; |
293 | 273 |
294 Buffer buf0 = header==null?null:header.buffer(); | 274 Buffer buf0 = header==null?null:header.buffer(); |
319 } | 299 } |
320 | 300 |
321 return length; | 301 return length; |
322 } | 302 } |
323 | 303 |
324 protected int gatheringFlush(Buffer header, ByteBuffer bbuf0, Buffer buffer, ByteBuffer bbuf1) throws IOException | 304 private int gatheringFlush(Buffer header, ByteBuffer bbuf0, Buffer buffer, ByteBuffer bbuf1) throws IOException |
325 { | 305 { |
326 int length; | 306 int length; |
327 | 307 |
328 synchronized(this) | 308 synchronized(this) |
329 { | 309 { |
353 } | 333 } |
354 } | 334 } |
355 return length; | 335 return length; |
356 } | 336 } |
357 | 337 |
358 /* ------------------------------------------------------------ */ | 338 public final SocketChannel getChannel() |
359 /** | |
360 * @return Returns the channel. | |
361 */ | |
362 public ByteChannel getChannel() | |
363 { | 339 { |
364 return _channel; | 340 return _channel; |
365 } | 341 } |
366 | 342 |
367 | 343 @Override |
368 /* ------------------------------------------------------------ */ | 344 public final String getLocalAddr() |
369 /* | 345 { |
370 * @see org.eclipse.io.EndPoint#getLocalAddr() | 346 if (_local.getAddress()==null || _local.getAddress().isAnyLocalAddress()) |
371 */ | |
372 public String getLocalAddr() | |
373 { | |
374 if (_socket==null) | |
375 return null; | |
376 if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress()) | |
377 return StringUtil.ALL_INTERFACES; | 347 return StringUtil.ALL_INTERFACES; |
378 return _local.getAddress().getHostAddress(); | 348 return _local.getAddress().getHostAddress(); |
379 } | 349 } |
380 | 350 |
381 /* ------------------------------------------------------------ */ | 351 @Override |
382 /* | 352 public final String getLocalHost() |
383 * @see org.eclipse.io.EndPoint#getLocalHost() | 353 { |
384 */ | 354 if (_local.getAddress()==null || _local.getAddress().isAnyLocalAddress()) |
385 public String getLocalHost() | |
386 { | |
387 if (_socket==null) | |
388 return null; | |
389 if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress()) | |
390 return StringUtil.ALL_INTERFACES; | 355 return StringUtil.ALL_INTERFACES; |
391 return _local.getAddress().getCanonicalHostName(); | 356 return _local.getAddress().getCanonicalHostName(); |
392 } | 357 } |
393 | 358 |
394 /* ------------------------------------------------------------ */ | 359 @Override |
395 /* | 360 public final int getLocalPort() |
396 * @see org.eclipse.io.EndPoint#getLocalPort() | 361 { |
397 */ | |
398 public int getLocalPort() | |
399 { | |
400 if (_socket==null) | |
401 return 0; | |
402 if (_local==null) | |
403 return -1; | |
404 return _local.getPort(); | 362 return _local.getPort(); |
405 } | 363 } |
406 | 364 |
407 /* ------------------------------------------------------------ */ | 365 @Override |
408 /* | 366 public final String getRemoteAddr() |
409 * @see org.eclipse.io.EndPoint#getRemoteAddr() | 367 { |
410 */ | |
411 public String getRemoteAddr() | |
412 { | |
413 if (_socket==null) | |
414 return null; | |
415 if (_remote==null) | |
416 return null; | |
417 return _remote.getAddress().getHostAddress(); | 368 return _remote.getAddress().getHostAddress(); |
418 } | 369 } |
419 | 370 |
420 /* ------------------------------------------------------------ */ | 371 @Override |
421 /* | 372 public final String getRemoteHost() |
422 * @see org.eclipse.io.EndPoint#getRemoteHost() | 373 { |
423 */ | |
424 public String getRemoteHost() | |
425 { | |
426 if (_socket==null) | |
427 return null; | |
428 if (_remote==null) | |
429 return null; | |
430 return _remote.getAddress().getCanonicalHostName(); | 374 return _remote.getAddress().getCanonicalHostName(); |
431 } | 375 } |
432 | 376 |
433 /* ------------------------------------------------------------ */ | 377 @Override |
434 /* | 378 public final int getRemotePort() |
435 * @see org.eclipse.io.EndPoint#getRemotePort() | 379 { |
436 */ | 380 return _remote.getPort(); |
437 public int getRemotePort() | 381 } |
438 { | 382 |
439 if (_socket==null) | 383 @Override |
440 return 0; | |
441 return _remote==null?-1:_remote.getPort(); | |
442 } | |
443 | |
444 public Object getTransport() | |
445 { | |
446 return _channel; | |
447 } | |
448 | |
449 public void flush() | 384 public void flush() |
450 throws IOException | 385 throws IOException |
451 { | 386 { |
452 } | 387 } |
453 | 388 |
454 public int getMaxIdleTime() | 389 @Override |
390 public final int getMaxIdleTime() | |
455 { | 391 { |
456 return _maxIdleTime; | 392 return _maxIdleTime; |
457 } | 393 } |
458 | 394 |
395 @Override | |
459 public void setMaxIdleTime(int timeMs) throws IOException | 396 public void setMaxIdleTime(int timeMs) throws IOException |
460 { | 397 { |
461 if (_socket!=null && timeMs!=_maxIdleTime) | 398 if (timeMs!=_maxIdleTime) |
462 _socket.setSoTimeout(timeMs>0?timeMs:0); | 399 _socket.setSoTimeout(timeMs>0?timeMs:0); |
463 _maxIdleTime=timeMs; | 400 _maxIdleTime=timeMs; |
464 } | 401 } |
465 } | 402 } |