Mercurial Hosting > luan
changeset 978:bdb6eb0fbf93
simplify ChannelEndPoint
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 16 Oct 2016 14:53:43 -0600 |
parents | d35b0a3a7a4a |
children | c8cd3e96db5f |
files | src/org/eclipse/jetty/io/EndPoint.java src/org/eclipse/jetty/io/nio/ChannelEndPoint.java src/org/eclipse/jetty/io/nio/SslConnection.java src/org/eclipse/jetty/server/AsyncHttpConnection.java src/org/eclipse/jetty/server/nio/BlockingChannelConnector.java |
diffstat | 5 files changed, 186 insertions(+), 255 deletions(-) [+] |
line wrap: on
line diff
diff -r d35b0a3a7a4a -r bdb6eb0fbf93 src/org/eclipse/jetty/io/EndPoint.java --- a/src/org/eclipse/jetty/io/EndPoint.java Sun Oct 16 01:31:05 2016 -0600 +++ b/src/org/eclipse/jetty/io/EndPoint.java Sun Oct 16 14:53:43 2016 -0600 @@ -19,6 +19,7 @@ package org.eclipse.jetty.io; import java.io.IOException; +import java.nio.channels.SocketChannel; /** @@ -27,148 +28,140 @@ */ public interface EndPoint { - /** - * Shutdown any backing output stream associated with the endpoint - */ - void shutdownOutput() throws IOException; + /** + * Shutdown any backing output stream associated with the endpoint + */ + void shutdownOutput() throws IOException; - boolean isOutputShutdown(); + boolean isOutputShutdown(); - /** - * Shutdown any backing input stream associated with the endpoint - */ - void shutdownInput() throws IOException; + /** + * Shutdown any backing input stream associated with the endpoint + */ + void shutdownInput() throws IOException; - boolean isInputShutdown(); + boolean isInputShutdown(); - /** - * Close any backing stream associated with the endpoint - */ - void close() throws IOException; + /** + * Close any backing stream associated with the endpoint + */ + void close() throws IOException; - /** - * Fill the buffer from the current putIndex to it's capacity from whatever - * byte source is backing the buffer. The putIndex is increased if bytes filled. - * The buffer may chose to do a compact before filling. - * @return an <code>int</code> value indicating the number of bytes - * filled or -1 if EOF is reached. - * @throws EofException If input is shutdown or the endpoint is closed. - */ - int fill(Buffer buffer) throws IOException; + /** + * Fill the buffer from the current putIndex to it's capacity from whatever + * byte source is backing the buffer. The putIndex is increased if bytes filled. + * The buffer may chose to do a compact before filling. + * @return an <code>int</code> value indicating the number of bytes + * filled or -1 if EOF is reached. + * @throws EofException If input is shutdown or the endpoint is closed. + */ + int fill(Buffer buffer) throws IOException; - /** - * Flush the buffer from the current getIndex to it's putIndex using whatever byte - * sink is backing the buffer. The getIndex is updated with the number of bytes flushed. - * Any mark set is cleared. - * If the entire contents of the buffer are flushed, then an implicit empty() is done. - * - * @param buffer The buffer to flush. This buffers getIndex is updated. - * @return the number of bytes written - * @throws EofException If the endpoint is closed or output is shutdown. - */ - int flush(Buffer buffer) throws IOException; + /** + * Flush the buffer from the current getIndex to it's putIndex using whatever byte + * sink is backing the buffer. The getIndex is updated with the number of bytes flushed. + * Any mark set is cleared. + * If the entire contents of the buffer are flushed, then an implicit empty() is done. + * + * @param buffer The buffer to flush. This buffers getIndex is updated. + * @return the number of bytes written + * @throws EofException If the endpoint is closed or output is shutdown. + */ + int flush(Buffer buffer) throws IOException; - /** - * Flush the buffer from the current getIndex to it's putIndex using whatever byte - * sink is backing the buffer. The getIndex is updated with the number of bytes flushed. - * Any mark set is cleared. - * If the entire contents of the buffer are flushed, then an implicit empty() is done. - * The passed header/trailer buffers are written before/after the contents of this buffer. This may be done - * either as gather writes, as a poke into this buffer or as several writes. The implementation is free to - * select the optimal mechanism. - * @param header A buffer to write before flushing this buffer. This buffers getIndex is updated. - * @param buffer The buffer to flush. This buffers getIndex is updated. - * @param trailer A buffer to write after flushing this buffer. This buffers getIndex is updated. - * @return the total number of bytes written. - */ - int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException; + /** + * Flush the buffer from the current getIndex to it's putIndex using whatever byte + * sink is backing the buffer. The getIndex is updated with the number of bytes flushed. + * Any mark set is cleared. + * If the entire contents of the buffer are flushed, then an implicit empty() is done. + * The passed header/trailer buffers are written before/after the contents of this buffer. This may be done + * either as gather writes, as a poke into this buffer or as several writes. The implementation is free to + * select the optimal mechanism. + * @param header A buffer to write before flushing this buffer. This buffers getIndex is updated. + * @param buffer The buffer to flush. This buffers getIndex is updated. + * @param trailer A buffer to write after flushing this buffer. This buffers getIndex is updated. + * @return the total number of bytes written. + */ + int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException; - /* ------------------------------------------------------------ */ - /** - * @return The local IP address to which this <code>EndPoint</code> is bound, or <code>null</code> - * if this <code>EndPoint</code> does not represent a network connection. - */ - public String getLocalAddr(); + /* ------------------------------------------------------------ */ + /** + * @return The local IP address to which this <code>EndPoint</code> is bound, or <code>null</code> + * if this <code>EndPoint</code> does not represent a network connection. + */ + public String getLocalAddr(); - /* ------------------------------------------------------------ */ - /** - * @return The local host name to which this <code>EndPoint</code> is bound, or <code>null</code> - * if this <code>EndPoint</code> does not represent a network connection. - */ - public String getLocalHost(); - - /* ------------------------------------------------------------ */ - /** - * @return The local port number on which this <code>EndPoint</code> is listening, or <code>0</code> - * if this <code>EndPoint</code> does not represent a network connection. - */ - public int getLocalPort(); + /* ------------------------------------------------------------ */ + /** + * @return The local host name to which this <code>EndPoint</code> is bound, or <code>null</code> + * if this <code>EndPoint</code> does not represent a network connection. + */ + public String getLocalHost(); - /* ------------------------------------------------------------ */ - /** - * @return The remote IP address to which this <code>EndPoint</code> is connected, or <code>null</code> - * if this <code>EndPoint</code> does not represent a network connection. - */ - public String getRemoteAddr(); + /* ------------------------------------------------------------ */ + /** + * @return The local port number on which this <code>EndPoint</code> is listening, or <code>0</code> + * if this <code>EndPoint</code> does not represent a network connection. + */ + public int getLocalPort(); - /* ------------------------------------------------------------ */ - /** - * @return The host name of the remote machine to which this <code>EndPoint</code> is connected, or <code>null</code> - * if this <code>EndPoint</code> does not represent a network connection. - */ - public String getRemoteHost(); + /* ------------------------------------------------------------ */ + /** + * @return The remote IP address to which this <code>EndPoint</code> is connected, or <code>null</code> + * if this <code>EndPoint</code> does not represent a network connection. + */ + public String getRemoteAddr(); - /* ------------------------------------------------------------ */ - /** - * @return The remote port number to which this <code>EndPoint</code> is connected, or <code>0</code> - * if this <code>EndPoint</code> does not represent a network connection. - */ - public int getRemotePort(); + /* ------------------------------------------------------------ */ + /** + * @return The host name of the remote machine to which this <code>EndPoint</code> is connected, or <code>null</code> + * if this <code>EndPoint</code> does not represent a network connection. + */ + public String getRemoteHost(); - /* ------------------------------------------------------------ */ - public boolean isBlocking(); - - /* ------------------------------------------------------------ */ - public boolean blockReadable(long millisecs) throws IOException; - - /* ------------------------------------------------------------ */ - public boolean blockWritable(long millisecs) throws IOException; + /* ------------------------------------------------------------ */ + /** + * @return The remote port number to which this <code>EndPoint</code> is connected, or <code>0</code> + * if this <code>EndPoint</code> does not represent a network connection. + */ + public int getRemotePort(); - /* ------------------------------------------------------------ */ - public boolean isOpen(); + public boolean isBlocking(); + + public boolean blockReadable(long millisecs) throws IOException; - /* ------------------------------------------------------------ */ - /** - * @return The underlying transport object (socket, channel, etc.) - */ - public Object getTransport(); + public boolean blockWritable(long millisecs) throws IOException; + + public boolean isOpen(); + + public SocketChannel getChannel(); - /* ------------------------------------------------------------ */ - /** Flush any buffered output. - * May fail to write all data if endpoint is non-blocking - * @throws EofException If the endpoint is closed or output is shutdown. - */ - public void flush() throws IOException; + /* ------------------------------------------------------------ */ + /** Flush any buffered output. + * May fail to write all data if endpoint is non-blocking + * @throws EofException If the endpoint is closed or output is shutdown. + */ + public void flush() throws IOException; - /* ------------------------------------------------------------ */ - /** Get the max idle time in ms. - * <p>The max idle time is the time the endpoint can be idle before - * extraordinary handling takes place. This loosely corresponds to - * the {@link java.net.Socket#getSoTimeout()} for blocking connections, - * but {@link AsyncEndPoint} implementations must use other mechanisms - * to implement the max idle time. - * @return the max idle time in ms or if ms <= 0 implies an infinite timeout - */ - public int getMaxIdleTime(); + /* ------------------------------------------------------------ */ + /** Get the max idle time in ms. + * <p>The max idle time is the time the endpoint can be idle before + * extraordinary handling takes place. This loosely corresponds to + * the {@link java.net.Socket#getSoTimeout()} for blocking connections, + * but {@link AsyncEndPoint} implementations must use other mechanisms + * to implement the max idle time. + * @return the max idle time in ms or if ms <= 0 implies an infinite timeout + */ + public int getMaxIdleTime(); - /* ------------------------------------------------------------ */ - /** Set the max idle time. - * @param timeMs the max idle time in MS. Timeout <= 0 implies an infinite timeout - * @throws IOException if the timeout cannot be set. - */ - public void setMaxIdleTime(int timeMs) throws IOException; + /* ------------------------------------------------------------ */ + /** Set the max idle time. + * @param timeMs the max idle time in MS. Timeout <= 0 implies an infinite timeout + * @throws IOException if the timeout cannot be set. + */ + public void setMaxIdleTime(int timeMs) throws IOException;
diff -r d35b0a3a7a4a -r bdb6eb0fbf93 src/org/eclipse/jetty/io/nio/ChannelEndPoint.java --- a/src/org/eclipse/jetty/io/nio/ChannelEndPoint.java Sun Oct 16 01:31:05 2016 -0600 +++ b/src/org/eclipse/jetty/io/nio/ChannelEndPoint.java Sun Oct 16 14:53:43 2016 -0600 @@ -25,7 +25,6 @@ import java.nio.ByteBuffer; import java.nio.channels.ByteChannel; import java.nio.channels.GatheringByteChannel; -import java.nio.channels.SelectableChannel; import java.nio.channels.SocketChannel; import org.eclipse.jetty.io.Buffer; @@ -43,36 +42,29 @@ { private static final Logger LOG = LoggerFactory.getLogger(ChannelEndPoint.class); - protected final ByteChannel _channel; - protected final ByteBuffer[] _gather2=new ByteBuffer[2]; + private final SocketChannel _channel; + private final ByteBuffer[] _gather2 = new ByteBuffer[2]; protected final Socket _socket; - protected final InetSocketAddress _local; - protected final InetSocketAddress _remote; + private final InetSocketAddress _local; + private final InetSocketAddress _remote; protected volatile int _maxIdleTime; - private volatile boolean _ishut; - private volatile boolean _oshut; + private volatile boolean _ishut = false; + private volatile boolean _oshut = false; - protected ChannelEndPoint(ByteChannel channel, int maxIdleTime) throws IOException + protected ChannelEndPoint(SocketChannel channel, int maxIdleTime) throws IOException { this._channel = channel; _maxIdleTime = maxIdleTime; - _socket = (channel instanceof SocketChannel)?((SocketChannel)channel).socket():null; - if (_socket!=null) - { - _local = (InetSocketAddress)_socket.getLocalSocketAddress(); - _remote = (InetSocketAddress)_socket.getRemoteSocketAddress(); - _socket.setSoTimeout(_maxIdleTime); - } - else - { - _local = _remote = null; - } + _socket = channel.socket(); + _local = (InetSocketAddress)_socket.getLocalSocketAddress(); + _remote = (InetSocketAddress)_socket.getRemoteSocketAddress(); + _socket.setSoTimeout(_maxIdleTime); } @Override public final boolean isBlocking() { - return !(_channel instanceof SelectableChannel) || ((SelectableChannel)_channel).isBlocking(); + return _channel.isBlocking(); } @Override @@ -100,26 +92,23 @@ _ishut = true; if (_channel.isOpen()) { - if (_socket != null) + try { - try + if (!_socket.isInputShutdown()) { - if (!_socket.isInputShutdown()) - { - _socket.shutdownInput(); - } + _socket.shutdownInput(); } - catch (SocketException e) + } + catch (SocketException e) + { + LOG.debug(e.toString()); + LOG.trace("",e); + } + finally + { + if (_oshut) { - LOG.debug(e.toString()); - LOG.trace("",e); - } - finally - { - if (_oshut) - { - close(); - } + close(); } } } @@ -132,26 +121,23 @@ _oshut = true; if (_channel.isOpen()) { - if (_socket != null) + try { - try + if (!_socket.isOutputShutdown()) { - if (!_socket.isOutputShutdown()) - { - _socket.shutdownOutput(); - } + _socket.shutdownOutput(); } - catch (SocketException e) + } + catch (SocketException e) + { + LOG.debug(e.toString()); + LOG.trace("",e); + } + finally + { + if (_ishut) { - LOG.debug(e.toString()); - LOG.trace("",e); - } - finally - { - if (_ishut) - { - close(); - } + close(); } } } @@ -160,13 +146,13 @@ @Override public final boolean isOutputShutdown() { - return _oshut || !_channel.isOpen() || _socket != null && _socket.isOutputShutdown(); + return _oshut || !_channel.isOpen() || _socket.isOutputShutdown(); } @Override public final boolean isInputShutdown() { - return _ishut || !_channel.isOpen() || _socket != null && _socket.isInputShutdown(); + return _ishut || !_channel.isOpen() || _socket.isInputShutdown(); } @Override @@ -176,9 +162,7 @@ _channel.close(); } - /* (non-Javadoc) - * @see org.eclipse.io.EndPoint#fill(org.eclipse.io.Buffer) - */ + @Override public int fill(Buffer buffer) throws IOException { if (_ishut) @@ -241,9 +225,7 @@ return len; } - /* (non-Javadoc) - * @see org.eclipse.io.EndPoint#flush(org.eclipse.io.Buffer) - */ + @Override public int flush(Buffer buffer) throws IOException { Buffer buf = buffer.buffer(); @@ -284,9 +266,7 @@ return len; } - /* (non-Javadoc) - * @see org.eclipse.io.EndPoint#flush(org.eclipse.io.Buffer, org.eclipse.io.Buffer, org.eclipse.io.Buffer) - */ + @Override public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException { int length=0; @@ -321,7 +301,7 @@ return length; } - protected int gatheringFlush(Buffer header, ByteBuffer bbuf0, Buffer buffer, ByteBuffer bbuf1) throws IOException + private int gatheringFlush(Buffer header, ByteBuffer bbuf0, Buffer buffer, ByteBuffer bbuf1) throws IOException { int length; @@ -355,110 +335,67 @@ return length; } - /* ------------------------------------------------------------ */ - /** - * @return Returns the channel. - */ - public ByteChannel getChannel() + public final SocketChannel getChannel() { return _channel; } - - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.io.EndPoint#getLocalAddr() - */ - public String getLocalAddr() + @Override + public final String getLocalAddr() { - if (_socket==null) - return null; - if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress()) + if (_local.getAddress()==null || _local.getAddress().isAnyLocalAddress()) return StringUtil.ALL_INTERFACES; return _local.getAddress().getHostAddress(); } - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.io.EndPoint#getLocalHost() - */ - public String getLocalHost() + @Override + public final String getLocalHost() { - if (_socket==null) - return null; - if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress()) + if (_local.getAddress()==null || _local.getAddress().isAnyLocalAddress()) return StringUtil.ALL_INTERFACES; return _local.getAddress().getCanonicalHostName(); } - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.io.EndPoint#getLocalPort() - */ - public int getLocalPort() + @Override + public final int getLocalPort() { - if (_socket==null) - return 0; - if (_local==null) - return -1; return _local.getPort(); } - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.io.EndPoint#getRemoteAddr() - */ - public String getRemoteAddr() + @Override + public final String getRemoteAddr() { - if (_socket==null) - return null; - if (_remote==null) - return null; return _remote.getAddress().getHostAddress(); } - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.io.EndPoint#getRemoteHost() - */ - public String getRemoteHost() + @Override + public final String getRemoteHost() { - if (_socket==null) - return null; - if (_remote==null) - return null; return _remote.getAddress().getCanonicalHostName(); } - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.io.EndPoint#getRemotePort() - */ - public int getRemotePort() + @Override + public final int getRemotePort() { - if (_socket==null) - return 0; - return _remote==null?-1:_remote.getPort(); + return _remote.getPort(); } - public Object getTransport() - { - return _channel; - } - + @Override public void flush() throws IOException { } - public int getMaxIdleTime() + @Override + public final int getMaxIdleTime() { return _maxIdleTime; } + @Override public void setMaxIdleTime(int timeMs) throws IOException { - if (_socket!=null && timeMs!=_maxIdleTime) + if (timeMs!=_maxIdleTime) _socket.setSoTimeout(timeMs>0?timeMs:0); _maxIdleTime=timeMs; }
diff -r d35b0a3a7a4a -r bdb6eb0fbf93 src/org/eclipse/jetty/io/nio/SslConnection.java --- a/src/org/eclipse/jetty/io/nio/SslConnection.java Sun Oct 16 01:31:05 2016 -0600 +++ b/src/org/eclipse/jetty/io/nio/SslConnection.java Sun Oct 16 14:53:43 2016 -0600 @@ -20,6 +20,7 @@ import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; import java.util.concurrent.atomic.AtomicBoolean; import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLEngineResult; @@ -667,9 +668,9 @@ return _endp.isOpen(); } - public Object getTransport() + public SocketChannel getChannel() { - return _endp; + return _endp.getChannel(); } public void flush() throws IOException
diff -r d35b0a3a7a4a -r bdb6eb0fbf93 src/org/eclipse/jetty/server/AsyncHttpConnection.java --- a/src/org/eclipse/jetty/server/AsyncHttpConnection.java Sun Oct 16 01:31:05 2016 -0600 +++ b/src/org/eclipse/jetty/server/AsyncHttpConnection.java Sun Oct 16 14:53:43 2016 -0600 @@ -145,7 +145,7 @@ { LOG.warn("Closing EndPoint making no progress: "+_total_no_progress+" "+_endp+" "+this); if (_endp instanceof SelectChannelEndPoint) - ((SelectChannelEndPoint)_endp).getChannel().close(); + _endp.getChannel().close(); } } }
diff -r d35b0a3a7a4a -r bdb6eb0fbf93 src/org/eclipse/jetty/server/nio/BlockingChannelConnector.java --- a/src/org/eclipse/jetty/server/nio/BlockingChannelConnector.java Sun Oct 16 01:31:05 2016 -0600 +++ b/src/org/eclipse/jetty/server/nio/BlockingChannelConnector.java Sun Oct 16 14:53:43 2016 -0600 @@ -138,7 +138,7 @@ { super.customize(endpoint, request); endpoint.setMaxIdleTime(_maxIdleTime); - configure(((SocketChannel)endpoint.getTransport()).socket()); + configure(endpoint.getChannel().socket()); } @@ -148,7 +148,7 @@ private int _timeout; private volatile long _idleTimestamp; - BlockingChannelEndPoint(ByteChannel channel) + BlockingChannelEndPoint(SocketChannel channel) throws IOException { super(channel,BlockingChannelConnector.this._maxIdleTime);