@SuppressWarnings("unchecked")
public void eventSunk(
ChannelPipeline pipeline, ChannelEvent e) throws Exception {
BaseXnioChannel channel = (BaseXnioChannel) e.getChannel();
if (e instanceof ChannelStateEvent) {
ChannelStateEvent event = (ChannelStateEvent) e;
ChannelFuture future = event.getFuture();
ChannelState state = event.getState();
Object value = event.getValue();
switch (state) {
case OPEN:
if (Boolean.FALSE.equals(value)) {
channel.closeNow(future);
}
break;
case BOUND:
case CONNECTED:
if (value != null) {
if (channel instanceof XnioClientChannel) {
final XnioClientChannel cc = (XnioClientChannel) channel;
synchronized (cc.connectLock) {
if (cc.connecting) {
Exception cause = new ConnectionPendingException();
future.setFailure(cause);
fireExceptionCaught(channel, cause);
} else {
cc.connecting = true;
java.nio.channels.Channel xnioChannel = cc.xnioChannel;
if (xnioChannel == null) {
FutureConnection fc =
cc.xnioConnector.connectTo(value, HANDLER);
fc.addNotifier(new FutureConnectionNotifier(cc), future);
} else {
Exception cause = new AlreadyConnectedException();
future.setFailure(cause);
fireExceptionCaught(cc, cause);
}
}
}
} else {
Exception cause = new UnsupportedOperationException();
future.setFailure(cause);
fireExceptionCaught(channel, cause);
}
} else {
channel.closeNow(future);
}
break;
case INTEREST_OPS:
int interestOps = ((Integer) value).intValue();
java.nio.channels.Channel xnioChannel = channel.xnioChannel;
if (xnioChannel instanceof SuspendableReadChannel) {
if ((interestOps & Channel.OP_READ) == 0) {
((SuspendableReadChannel) xnioChannel).suspendReads();
channel.setRawInterestOpsNow(Channel.OP_NONE);
} else {
((SuspendableReadChannel) xnioChannel).resumeReads();
channel.setRawInterestOpsNow(Channel.OP_READ);
}
}
e.getFuture().setSuccess();
break;
}
} else if (e instanceof MessageEvent) {
MessageEvent event = (MessageEvent) e;
java.nio.channels.Channel xnioChannel = channel.xnioChannel;
if (xnioChannel instanceof GatheringByteChannel ||
xnioChannel instanceof MultipointWritableMessageChannel ||
xnioChannel instanceof WritableMessageChannel) {
boolean offered = channel.writeBuffer.offer(event);
assert offered;
if (xnioChannel instanceof SuspendableWriteChannel) {
((SuspendableWriteChannel) xnioChannel).resumeWrites();
}
} else {
event.getFuture().setFailure(new IllegalStateException());
}
}
}