Mercurial Hosting > luan
changeset 955:6f49b8dfffe6
simplify SelectChannelEndPoint
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 13 Oct 2016 16:55:53 -0600 |
parents | a021c4c9c244 |
children | 1094975d013b |
files | src/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java |
diffstat | 1 files changed, 37 insertions(+), 126 deletions(-) [+] |
line wrap: on
line diff
diff -r a021c4c9c244 -r 6f49b8dfffe6 src/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java --- a/src/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java Thu Oct 13 00:54:10 2016 -0600 +++ b/src/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java Thu Oct 13 16:55:53 2016 -0600 @@ -41,7 +41,7 @@ /** * An Endpoint that can be scheduled by {@link SelectorManager}. */ -public class SelectChannelEndPoint extends ChannelEndPoint implements AsyncEndPoint, ConnectedEndPoint +public final class SelectChannelEndPoint extends ChannelEndPoint implements AsyncEndPoint, ConnectedEndPoint { public static final Logger LOG=LoggerFactory.getLogger("org.eclipse.jetty.io.nio"); @@ -69,12 +69,9 @@ private static final int STATE_DISPATCHED = 1; private int _state; - private boolean _onIdle; - /** true if the last write operation succeed and wrote all offered bytes */ private volatile boolean _writable = true; - /** True if a thread has is blocked in {@link #blockReadable(long)} */ private boolean _readBlocked; @@ -84,12 +81,10 @@ /** true if {@link SelectSet#destroyEndPoint(SelectChannelEndPoint)} has not been called */ private boolean _open; - private volatile long _idleTimestamp; private volatile boolean _checkIdle; private boolean _ishut; - /* ------------------------------------------------------------ */ public SelectChannelEndPoint(SocketChannel channel, SelectSet selectSet, SelectionKey key, int maxIdleTime) throws IOException { @@ -97,33 +92,20 @@ _manager = selectSet.getManager(); _selectSet = selectSet; - _state=STATE_UNDISPATCHED; - _onIdle=false; - _open=true; + _state = STATE_UNDISPATCHED; + _open = true; _key = key; setCheckForIdle(true); } - /* ------------------------------------------------------------ */ - public SelectionKey getSelectionKey() - { - synchronized (this) - { - return _key; - } - } - - public SelectorManager getSelectManager() - { - return _manager; - } - + @Override public Connection getConnection() { return _connection; } + @Override public void setConnection(Connection connection) { _connection = (AsyncConnection)connection; @@ -138,8 +120,8 @@ // If there is no key, then do nothing if (_key == null || !_key.isValid()) { - _readBlocked=false; - _writeBlocked=false; + _readBlocked = false; + _writeBlocked = false; this.notifyAll(); return; } @@ -151,7 +133,7 @@ if (_readBlocked && _key.isReadable()) _readBlocked=false; if (_writeBlocked && _key.isWritable()) - _writeBlocked=false; + _writeBlocked = false; // wake them up is as good as a dispatched. this.notifyAll(); @@ -182,22 +164,18 @@ } } + @Override public synchronized void dispatch() { if (_state<=STATE_UNDISPATCHED) { - if (_onIdle) + _state = STATE_DISPATCHED; + try { + _manager.execute(_handler); + } catch(RejectedExecutionException e) { _state = STATE_NEEDS_DISPATCH; - else - { - _state = STATE_DISPATCHED; - try { - _manager.execute(_handler); - } catch(RejectedExecutionException e) { - _state = STATE_NEEDS_DISPATCH; - LOG.warn("Dispatched Failed! "+this+" to "+_manager); - updateKey(); - } + LOG.warn("Dispatched Failed! "+this+" to "+_manager); + updateKey(); } } } @@ -209,89 +187,27 @@ * @return If false is returned, the endpoint has been redispatched and * thread must keep handling the endpoint. */ - protected synchronized void undispatch() + private synchronized void undispatch() { _state = STATE_UNDISPATCHED; updateKey(); } + @Override public void setCheckForIdle(boolean check) { if (check) { - _idleTimestamp=System.currentTimeMillis(); - _checkIdle=true; + _checkIdle = true; } else - _checkIdle=false; - } - - private boolean isCheckForIdle() - { - return _checkIdle; - } - - protected void notIdle() - { - _idleTimestamp=System.currentTimeMillis(); - } - - public void checkIdleTimestamp(long now) - { - if (isCheckForIdle() && _maxIdleTime>0) - { - final long idleForMs=now-_idleTimestamp; - - if (idleForMs>_maxIdleTime) - { - // Don't idle out again until onIdleExpired task completes. - setCheckForIdle(false); - _manager.execute(new Runnable() - { - public void run() - { - try - { - onIdleExpired(idleForMs); - } - finally - { - setCheckForIdle(true); - } - } - }); - } - } - } - - private void onIdleExpired(long idleForMs) - { - try - { - synchronized (this) - { - _onIdle=true; - } - - _connection.onIdleExpired(idleForMs); - } - finally - { - synchronized (this) - { - _onIdle=false; - if (_state==STATE_NEEDS_DISPATCH) - dispatch(); - } - } + _checkIdle = false; } @Override public int fill(Buffer buffer) throws IOException { int fill=super.fill(buffer); - if (fill>0) - notIdle(); return fill; } @@ -305,15 +221,14 @@ { synchronized (this) { - _writable=false; + _writable = false; if (_state<STATE_DISPATCHED) updateKey(); } } else if (l>0) { - _writable=true; - notIdle(); + _writable = true; } return l; } @@ -328,15 +243,14 @@ { synchronized (this) { - _writable=false; + _writable = false; if (_state<STATE_DISPATCHED) updateKey(); } } else if (l>0) { - _writable=true; - notIdle(); + _writable = true; } return l; @@ -354,13 +268,13 @@ if (isInputShutdown()) throw new EofException(); - long now=_selectSet.getNow(); - long end=now+timeoutMs; - boolean check=isCheckForIdle(); + long now = _selectSet.getNow(); + long end = now+timeoutMs; + boolean check = _checkIdle; setCheckForIdle(true); try { - _readBlocked=true; + _readBlocked = true; while (!isInputShutdown() && _readBlocked) { try @@ -383,7 +297,7 @@ } finally { - _readBlocked=false; + _readBlocked = false; setCheckForIdle(check); } } @@ -404,11 +318,11 @@ long now=_selectSet.getNow(); long end=now+timeoutMs; - boolean check=isCheckForIdle(); + boolean check = _checkIdle; setCheckForIdle(true); try { - _writeBlocked=true; + _writeBlocked = true; while (_writeBlocked && !isOutputShutdown()) { try @@ -430,13 +344,14 @@ } finally { - _writeBlocked=false; + _writeBlocked = false; setCheckForIdle(check); } } return true; } + @Override public boolean hasProgressed() { return false; @@ -472,7 +387,7 @@ LOG.trace("",e); } } - changed=_interestOps!=current_ops; + changed = _interestOps!=current_ops; } if(changed) @@ -518,7 +433,7 @@ { _selectSet.destroyEndPoint(this); } - _open=false; + _open = false; _key = null; } } @@ -543,17 +458,14 @@ if (_open) { - _open=false; + _open = false; _selectSet.destroyEndPoint(this); } _key = null; } } - /* ------------------------------------------------------------ */ - /* - */ - protected void handle() + private void handle() { boolean dispatched = true; try @@ -648,7 +560,6 @@ } } - /* ------------------------------------------------------------ */ @Override public String toString() { @@ -699,7 +610,7 @@ @Override public void setMaxIdleTime(int timeMs) throws IOException { - _maxIdleTime=timeMs; + _maxIdleTime = timeMs; } }