Skip to content

Commit

Permalink
Fixed byte arithmetic: byte is signed and can't be used as array
Browse files Browse the repository at this point in the history
index without proper conversion to int.
  • Loading branch information
sbordet committed Jan 5, 2015
1 parent c3582ce commit bb3f296
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions jetty-io/src/main/java/org/eclipse/jetty/io/SelectorManager.java
Expand Up @@ -60,7 +60,7 @@ protected SelectorManager(Executor executor, Scheduler scheduler)

protected SelectorManager(Executor executor, Scheduler scheduler, int selectors)
{
if (selectors<=0)
if (selectors <= 0)
throw new IllegalArgumentException("No selectors");
this.executor = executor;
this.scheduler = scheduler;
Expand Down Expand Up @@ -140,40 +140,43 @@ public int getSelectorCount()

private ManagedSelector chooseSelector(SocketChannel channel)
{
// Ideally we would like to have all connectors from the same client end
// Ideally we would like to have all connections from the same client end
// up on the same selector (to try to avoid smearing the data from a single
// client over all cores), but because of proxies, the remote address may not
// really be the client - so we have to hedge our bets to ensure that all
// channels don't end up on the one selector for a proxy.
ManagedSelector ms0=null;
if (channel!=null)
ManagedSelector candidate1 = null;
if (channel != null)
{
try
{
SocketAddress remote = channel.getRemoteAddress();
if (remote instanceof InetSocketAddress)
{
byte[] addr = ((InetSocketAddress)remote).getAddress().getAddress();
if (addr!=null && addr.length>0)
ms0=_selectors[addr[addr.length-1]%getSelectorCount()];
if (addr != null)
{
int s = addr[addr.length - 1] & 0xFF;
candidate1 = _selectors[s % getSelectorCount()];
}
}
}
catch (IOException e)
catch (IOException x)
{
LOG.ignore(e);
LOG.ignore(x);
}
}

// The ++ increment here is not atomic, but it does not matter,
// so long as the value changes sometimes, then connections will
// be distributed over the available selectors.
long s = _selectorIndex++;
int index = (int)(s % getSelectorCount());
ManagedSelector ms1 = _selectors[index];
if (ms0==null || ms0.size()>=ms1.size()*2)
return ms1;
return ms0;
ManagedSelector candidate2 = _selectors[index];

if (candidate1 == null || candidate1.size() >= candidate2.size() * 2)
return candidate2;
return candidate1;
}

/**
Expand Down Expand Up @@ -207,29 +210,29 @@ public void accept(SocketChannel channel)
* just after a non-blocking connect via {@link SocketChannel#connect(SocketAddress)} that completed
* successfully.</p>
*
* @param channel the channel to register
* @param channel the channel to register
* @param attachment the attachment object
*/
public void accept(SocketChannel channel, Object attachment)
{
final ManagedSelector selector = chooseSelector(channel);
selector.submit(selector.new Accept(channel, attachment));
}

/**
* <p>Registers a server channel for accept operations.
* When a {@link SocketChannel} is accepted from the given {@link ServerSocketChannel}
* then the {@link #accepted(SocketChannel)} method is called, which must be
* overridden by a derivation of this class to handle the accepted channel
*
*
* @param server the server channel to register
*/
public void acceptor(ServerSocketChannel server)
{
final ManagedSelector selector = chooseSelector(null);
selector.submit(selector.new Acceptor(server));
}

/**
* Callback method when a channel is accepted from the {@link ServerSocketChannel}
* passed to {@link #acceptor(ServerSocketChannel)}.
Expand Down Expand Up @@ -312,7 +315,7 @@ public void connectionOpened(Connection connection)
if (isRunning())
LOG.warn("Exception while notifying connection " + connection, x);
else
LOG.debug("Exception while notifying connection {}",connection, x);
LOG.debug("Exception while notifying connection " + connection, x);
}
}

Expand Down Expand Up @@ -342,8 +345,8 @@ protected boolean finishConnect(SocketChannel channel) throws IOException
* <p>Callback method invoked when a non-blocking connect cannot be completed.</p>
* <p>By default it just logs with level warning.</p>
*
* @param channel the channel that attempted the connect
* @param ex the exception that caused the connect to fail
* @param channel the channel that attempted the connect
* @param ex the exception that caused the connect to fail
* @param attachment the attachment object associated at registration
*/
protected void connectionFailed(SocketChannel channel, Throwable ex, Object attachment)
Expand All @@ -356,9 +359,9 @@ protected void connectionFailed(SocketChannel channel, Throwable ex, Object atta
* <p>This method is invoked as a result of the registration of a channel via {@link #connect(SocketChannel, Object)}
* or {@link #accept(SocketChannel)}.</p>
*
* @param channel the channel associated to the endpoint
* @param selector the selector the channel is registered to
* @param selectionKey the selection key
* @param channel the channel associated to the endpoint
* @param selector the selector the channel is registered to
* @param selectionKey the selection key
* @return a new endpoint
* @throws IOException if the endPoint cannot be created
* @see #newConnection(SocketChannel, EndPoint, Object)
Expand Down Expand Up @@ -399,6 +402,7 @@ public interface SelectableEndPoint extends EndPoint
/**
* Callback method invoked when a read or write events has been
* detected by the {@link ManagedSelector} for this endpoint.
*
* @return a job that may block or null
*/
Runnable onSelected();
Expand Down

0 comments on commit bb3f296

Please sign in to comment.