Skip to content

Commit

Permalink
Code cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbordet committed Apr 27, 2020
1 parent 32aa5d6 commit 46ad905
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,11 @@ public String toEndPointString()
name = c.getSimpleName();
}

return String.format("%s@%h{%s<->%s,%s,fill=%s,flush=%s,to=%d/%d}",
return String.format("%s@%h{l=%s,r=%s,%s,fill=%s,flush=%s,to=%d/%d}",
name,
this,
getRemoteAddress(),
getLocalAddress(),
getRemoteAddress(),
_state.get(),
_fillInterest.toStateString(),
_writeFlusher.toStateString(),
Expand Down
110 changes: 51 additions & 59 deletions jetty-io/src/main/java/org/eclipse/jetty/io/ChannelEndPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import java.nio.channels.CancelledKeyException;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;

import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.log.Log;
Expand All @@ -41,21 +41,14 @@ public abstract class ChannelEndPoint extends AbstractEndPoint implements Manage
{
private static final Logger LOG = Log.getLogger(ChannelEndPoint.class);

private final ByteChannel _channel;
private final GatheringByteChannel _gather;
protected final ManagedSelector _selector;
protected final SelectionKey _key;
private final SocketChannel _channel;
private final ManagedSelector _selector;
private final SelectionKey _key;
private boolean _updatePending;

/**
* The current value for {@link SelectionKey#interestOps()}.
*/
protected int _currentInterestOps;

/**
* The desired value for {@link SelectionKey#interestOps()}.
*/
protected int _desiredInterestOps;
// The current value for interestOps.
private int _currentInterestOps;
// The desired value for interestOps.
private int _desiredInterestOps;

private abstract class RunnableTask implements Runnable, Invocable
{
Expand Down Expand Up @@ -94,14 +87,7 @@ public void close()
}
}

private final ManagedSelector.SelectorUpdate _updateKeyAction = new ManagedSelector.SelectorUpdate()
{
@Override
public void update(Selector selector)
{
updateKey();
}
};
private final ManagedSelector.SelectorUpdate _updateKeyAction = selector -> updateKey();

private final Runnable _runFillable = new RunnableCloseable("runFillable")
{
Expand Down Expand Up @@ -166,13 +152,24 @@ public void run()
}
};

public ChannelEndPoint(ByteChannel channel, ManagedSelector selector, SelectionKey key, Scheduler scheduler)
public ChannelEndPoint(SocketChannel channel, ManagedSelector selector, SelectionKey key, Scheduler scheduler)
{
super(scheduler);
_channel = channel;
_selector = selector;
_key = key;
_gather = (channel instanceof GatheringByteChannel) ? (GatheringByteChannel)channel : null;
}

@Override
public InetSocketAddress getLocalAddress()
{
return (InetSocketAddress)_channel.socket().getLocalSocketAddress();
}

@Override
public InetSocketAddress getRemoteAddress()
{
return (InetSocketAddress)_channel.socket().getRemoteSocketAddress();
}

@Override
Expand All @@ -187,6 +184,21 @@ public boolean isOpen()
return _channel.isOpen();
}

@Override
protected void doShutdownOutput()
{
try
{
Socket socket = _channel.socket();
if (!socket.isOutputShutdown())
socket.shutdownOutput();
}
catch (IOException e)
{
LOG.debug(e);
}
}

@Override
public void doClose()
{
Expand Down Expand Up @@ -254,27 +266,10 @@ else if (filled == -1)
@Override
public boolean flush(ByteBuffer... buffers) throws IOException
{
long flushed = 0;
long flushed;
try
{
if (buffers.length == 1)
flushed = _channel.write(buffers[0]);
else if (_gather != null && buffers.length > 1)
flushed = _gather.write(buffers, 0, buffers.length);
else
{
for (ByteBuffer b : buffers)
{
if (b.hasRemaining())
{
int l = _channel.write(b);
if (l > 0)
flushed += l;
if (b.hasRemaining())
break;
}
}
}
flushed = _channel.write(buffers);
if (LOG.isDebugEnabled())
LOG.debug("flushed {} {}", flushed, this);
}
Expand All @@ -295,7 +290,7 @@ else if (_gather != null && buffers.length > 1)
return true;
}

public ByteChannel getChannel()
public SocketChannel getChannel()
{
return _channel;
}
Expand All @@ -321,9 +316,8 @@ protected void onIncompleteFlush()
@Override
public Runnable onSelected()
{
/**
* This method may run concurrently with {@link #changeInterests(int)}.
*/
// This method runs from the selector thread,
// possibly concurrently with changeInterests(int).

int readyOps = _key.readyOps();
int oldInterestOps;
Expand Down Expand Up @@ -360,9 +354,8 @@ public Runnable onSelected()
@Override
public void updateKey()
{
/**
* This method may run concurrently with {@link #changeInterests(int)}.
*/
// This method runs from the selector thread,
// possibly concurrently with changeInterests(int).

try
{
Expand All @@ -385,22 +378,21 @@ public void updateKey()
}
catch (CancelledKeyException x)
{
LOG.debug("Ignoring key update for concurrently closed channel {}", this);
if (LOG.isDebugEnabled())
LOG.debug("Ignoring key update for cancelled key {}", this, x);
close();
}
catch (Throwable x)
{
LOG.warn("Ignoring key update for " + this, x);
LOG.warn("Ignoring key update for {}", this, x);
close();
}
}

private void changeInterests(int operation)
{
/**
* This method may run concurrently with
* {@link #updateKey()} and {@link #onSelected()}.
*/
// This method runs from any thread, possibly
// concurrently with updateKey() and onSelected().

int oldInterestOps;
int newInterestOps;
Expand Down
Loading

0 comments on commit 46ad905

Please sign in to comment.