Clean up AbstractUnsafe#bind and AbstractUnsafe#disconnect.

This commit is contained in:
Jeff Pinner 2013-07-28 13:05:34 -07:00 committed by Norman Maurer
parent bf2430d255
commit 3922c518cd
2 changed files with 44 additions and 37 deletions

View File

@ -412,7 +412,7 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
}); });
} catch (Throwable t) { } catch (Throwable t) {
logger.warn( logger.warn(
"Force-closing a channel whose registration task was unaccepted by an event loop: {}", "Force-closing a channel whose registration task was not accepted by an event loop: {}",
AbstractChannel.this, t); AbstractChannel.this, t);
closeForcibly(); closeForcibly();
closeFuture.setClosed(); closeFuture.setClosed();
@ -453,51 +453,58 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
return; return;
} }
try { // See: https://github.com/netty/netty/issues/576
boolean wasActive = isActive(); if (!PlatformDependent.isWindows() && !PlatformDependent.isRoot() &&
Boolean.TRUE.equals(config().getOption(ChannelOption.SO_BROADCAST)) &&
// See: https://github.com/netty/netty/issues/576 localAddress instanceof InetSocketAddress &&
if (!PlatformDependent.isWindows() && !PlatformDependent.isRoot() && !((InetSocketAddress) localAddress).getAddress().isAnyLocalAddress()) {
Boolean.TRUE.equals(config().getOption(ChannelOption.SO_BROADCAST)) && // Warn a user about the fact that a non-root user can't receive a
localAddress instanceof InetSocketAddress && // broadcast packet on *nix if the socket is bound on non-wildcard address.
!((InetSocketAddress) localAddress).getAddress().isAnyLocalAddress()) { logger.warn(
// Warn a user about the fact that a non-root user can't receive a "A non-root user can't receive a broadcast packet if the socket " +
// broadcast packet on *nix if the socket is bound on non-wildcard address. "is not bound to a wildcard address; binding to a non-wildcard " +
logger.warn( "address (" + localAddress + ") anyway as requested.");
"A non-root user can't receive a broadcast packet if the socket " +
"is not bound to a wildcard address; binding to a non-wildcard " +
"address (" + localAddress + ") anyway as requested.");
}
doBind(localAddress);
promise.setSuccess();
if (!wasActive && isActive()) {
pipeline.fireChannelActive();
}
} catch (Throwable t) {
promise.setFailure(t);
closeIfClosed();
} }
boolean wasActive = isActive();
try {
doBind(localAddress);
} catch (Throwable t) {
closeIfClosed();
promise.setFailure(t);
return;
}
if (!wasActive && isActive()) {
invokeLater(new Runnable() {
@Override
public void run() {
pipeline.fireChannelActive();
}
});
}
promise.setSuccess();
} }
@Override @Override
public final void disconnect(final ChannelPromise promise) { public final void disconnect(final ChannelPromise promise) {
boolean wasActive = isActive();
try { try {
boolean wasActive = isActive();
doDisconnect(); doDisconnect();
promise.setSuccess();
if (wasActive && !isActive()) {
invokeLater(new Runnable() {
@Override
public void run() {
pipeline.fireChannelInactive();
}
});
}
} catch (Throwable t) { } catch (Throwable t) {
promise.setFailure(t);
closeIfClosed(); closeIfClosed();
promise.setFailure(t);
return;
} }
if (wasActive && !isActive()) {
invokeLater(new Runnable() {
@Override
public void run() {
pipeline.fireChannelInactive();
}
});
}
closeIfClosed(); // doDisconnect() might have closed the channel
promise.setSuccess();
} }
@Override @Override

View File

@ -285,7 +285,7 @@ public abstract class AbstractNioChannel extends AbstractChannel {
return; return;
} catch (CancelledKeyException e) { } catch (CancelledKeyException e) {
if (!selected) { if (!selected) {
// Force the Selector to select now as the "canceled" SelectionKey may still be // Force the Selector to select now as the "canceled" SelectionKey may still be
// cached and not removed because no Select.select(..) operation was called yet. // cached and not removed because no Select.select(..) operation was called yet.
eventLoop().selectNow(); eventLoop().selectNow();
selected = true; selected = true;