diff src/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java @ 947:64f3d8dae31d

simplify SelectChannelEndPoint
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 11 Oct 2016 21:33:40 -0600
parents 901dcfa05c32
children f5aefdc4a81a
line wrap: on
line diff
--- a/src/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java	Tue Oct 11 21:10:17 2016 -0600
+++ b/src/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java	Tue Oct 11 21:33:40 2016 -0600
@@ -67,7 +67,6 @@
 	private static final int STATE_NEEDS_DISPATCH=-1;
 	private static final int STATE_UNDISPATCHED=0;
 	private static final int STATE_DISPATCHED=1;
-	private static final int STATE_ASYNC=2;
 	private int _state;
 	
 	private boolean _onIdle;
@@ -186,25 +185,21 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
-	public void dispatch()
+	public synchronized void dispatch()
 	{
-		synchronized(this)
+		if (_state<=STATE_UNDISPATCHED)
 		{
-			if (_state<=STATE_UNDISPATCHED)
+			if (_onIdle)
+				_state = STATE_NEEDS_DISPATCH;
+			else
 			{
-				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();
 				}
 			}
 		}
@@ -217,25 +212,13 @@
 	 * @return If false is returned, the endpoint has been redispatched and
 	 * thread must keep handling the endpoint.
 	 */
-	protected boolean undispatch()
+	protected synchronized void undispatch()
 	{
-		synchronized (this)
-		{
-			switch(_state)
-			{
-				case STATE_ASYNC:
-					_state=STATE_DISPATCHED;
-					return false;
-
-				default:
-					_state=STATE_UNDISPATCHED;
-					updateKey();
-					return true;
-			}
-		}
+		_state = STATE_UNDISPATCHED;
+		updateKey();
+//		return true;
 	}
 
-	/* ------------------------------------------------------------ */
 	public void setCheckForIdle(boolean check)
 	{
 		if (check)
@@ -247,19 +230,16 @@
 			_checkIdle=false;
 	}
 
-	/* ------------------------------------------------------------ */
 	public boolean isCheckForIdle()
 	{
 		return _checkIdle;
 	}
 
-	/* ------------------------------------------------------------ */
 	protected void notIdle()
 	{
 		_idleTimestamp=System.currentTimeMillis();
 	}
 
-	/* ------------------------------------------------------------ */
 	public void checkIdleTimestamp(long now)
 	{
 		if (isCheckForIdle() && _maxIdleTime>0)
@@ -288,7 +268,6 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
 	public void onIdleExpired(long idleForMs)
 	{
 		try
@@ -311,7 +290,6 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
 	@Override
 	public int fill(Buffer buffer) throws IOException
 	{
@@ -321,7 +299,6 @@
 		return fill;
 	}
 
-	/* ------------------------------------------------------------ */
 	@Override
 	public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
 	{
@@ -345,9 +322,6 @@
 		return l;
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 */
 	@Override
 	public int flush(Buffer buffer) throws IOException
 	{
@@ -608,81 +582,74 @@
 	 */
 	protected void handle()
 	{
-		boolean dispatched=true;
+		boolean dispatched = true;
 		try
 		{
-			while(dispatched)
+			try
 			{
-				try
+				while(true)
 				{
-					while(true)
+					final AsyncConnection next = (AsyncConnection)_connection.handle();
+					if (next!=_connection)
 					{
-						final AsyncConnection next = (AsyncConnection)_connection.handle();
-						if (next!=_connection)
-						{
-							LOG.debug("{} replaced {}",next,_connection);
-							_connection=next;
-							continue;
-						}
-						break;
+						LOG.debug("{} replaced {}",next,_connection);
+						_connection=next;
+						continue;
+					}
+					break;
+				}
+			}
+			catch (ClosedChannelException e)
+			{
+				LOG.trace("",e);
+			}
+			catch (EofException e)
+			{
+				LOG.debug("EOF", e);
+				try{close();}
+				catch(IOException e2){LOG.trace("",e2);}
+			}
+			catch (IOException e)
+			{
+				LOG.warn(e.toString());
+				try{close();}
+				catch(IOException e2){LOG.trace("",e2);}
+			}
+			catch (Throwable e)
+			{
+				LOG.warn("handle failed", e);
+				try{close();}
+				catch(IOException e2){LOG.trace("",e2);}
+			}
+			finally
+			{
+				if (!_ishut && isInputShutdown() && isOpen())
+				{
+					_ishut = true;
+					try
+					{
+						_connection.onInputShutdown();
+					}
+					catch(Throwable x)
+					{
+						LOG.warn("onInputShutdown failed", x);
+						try{close();}
+						catch(IOException e2){LOG.trace("",e2);}
+					}
+					finally
+					{
+						updateKey();
 					}
 				}
-				catch (ClosedChannelException e)
-				{
-					LOG.trace("",e);
-				}
-				catch (EofException e)
-				{
-					LOG.debug("EOF", e);
-					try{close();}
-					catch(IOException e2){LOG.trace("",e2);}
-				}
-				catch (IOException e)
-				{
-					LOG.warn(e.toString());
-					try{close();}
-					catch(IOException e2){LOG.trace("",e2);}
-				}
-				catch (Throwable e)
-				{
-					LOG.warn("handle failed", e);
-					try{close();}
-					catch(IOException e2){LOG.trace("",e2);}
-				}
-				finally
-				{
-					if (!_ishut && isInputShutdown() && isOpen())
-					{
-						_ishut=true;
-						try
-						{
-							_connection.onInputShutdown();
-						}
-						catch(Throwable x)
-						{
-							LOG.warn("onInputShutdown failed", x);
-							try{close();}
-							catch(IOException e2){LOG.trace("",e2);}
-						}
-						finally
-						{
-							updateKey();
-						}
-					}
-					dispatched=!undispatch();
-				}
+				undispatch();
+				dispatched = false;
 			}
 		}
 		finally
 		{
 			if (dispatched)
 			{
-				dispatched=!undispatch();
-				while (dispatched)
-				{
-					LOG.warn("SCEP.run() finally DISPATCHED");
-					dispatched=!undispatch();
-				}
+				undispatch();
 			}
 		}
 	}