2012-06-02 02:51:19 +02:00
|
|
|
package io.netty.channel;
|
|
|
|
|
|
|
|
import io.netty.buffer.ChannelBuffer;
|
2012-06-04 09:24:34 +02:00
|
|
|
import io.netty.buffer.ChannelBuffers;
|
2012-06-02 02:51:19 +02:00
|
|
|
import io.netty.util.DefaultAttributeMap;
|
2012-06-04 09:24:34 +02:00
|
|
|
import io.netty.util.internal.QueueFactory;
|
2012-06-02 02:51:19 +02:00
|
|
|
|
|
|
|
import java.net.SocketAddress;
|
2012-06-04 09:24:34 +02:00
|
|
|
import java.util.ArrayDeque;
|
2012-06-02 02:51:19 +02:00
|
|
|
import java.util.Queue;
|
2012-06-04 03:51:42 +02:00
|
|
|
import java.util.concurrent.BlockingQueue;
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
2012-06-02 02:51:19 +02:00
|
|
|
|
|
|
|
final class DefaultChannelHandlerContext extends DefaultAttributeMap implements ChannelInboundHandlerContext<Object>, ChannelOutboundHandlerContext<Object> {
|
|
|
|
volatile DefaultChannelHandlerContext next;
|
|
|
|
volatile DefaultChannelHandlerContext prev;
|
2012-06-02 03:34:19 +02:00
|
|
|
private final Channel channel;
|
2012-06-02 02:51:19 +02:00
|
|
|
private final DefaultChannelPipeline pipeline;
|
2012-06-04 09:24:34 +02:00
|
|
|
EventExecutor executor; // not thread-safe but OK because it never changes once set.
|
2012-06-02 02:51:19 +02:00
|
|
|
private final String name;
|
|
|
|
private final ChannelHandler handler;
|
|
|
|
final ChannelBufferHolder<Object> in;
|
2012-06-04 03:51:42 +02:00
|
|
|
final ChannelBufferHolder<Object> out;
|
|
|
|
|
|
|
|
// When the two handlers run in a different thread and they are next to each other,
|
2012-06-04 09:24:34 +02:00
|
|
|
// each other's buffers can be accessed at the same time resulting in a race condition.
|
2012-06-04 03:51:42 +02:00
|
|
|
// To avoid such situation, we lazily creates an additional thread-safe buffer called
|
|
|
|
// 'bridge' so that the two handlers access each other's buffer only via the bridges.
|
|
|
|
// The content written into a bridge is flushed into the actual buffer by flushBridge().
|
2012-06-04 09:24:34 +02:00
|
|
|
final AtomicReference<MessageBridge> inMsgBridge;
|
|
|
|
final AtomicReference<MessageBridge> outMsgBridge;
|
|
|
|
final AtomicReference<StreamBridge> inByteBridge;
|
|
|
|
final AtomicReference<StreamBridge> outByteBridge;
|
2012-06-02 02:51:19 +02:00
|
|
|
|
|
|
|
// Runnables that calls handlers
|
|
|
|
final Runnable fireChannelRegisteredTask = new Runnable() {
|
|
|
|
@Override
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void run() {
|
|
|
|
DefaultChannelHandlerContext ctx = DefaultChannelHandlerContext.this;
|
|
|
|
try {
|
2012-06-04 20:56:00 +02:00
|
|
|
((ChannelInboundHandler<Object>) ctx.handler).channelRegistered(ctx);
|
2012-06-02 02:51:19 +02:00
|
|
|
} catch (Throwable t) {
|
|
|
|
pipeline.notifyHandlerException(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
final Runnable fireChannelUnregisteredTask = new Runnable() {
|
|
|
|
@Override
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void run() {
|
|
|
|
DefaultChannelHandlerContext ctx = DefaultChannelHandlerContext.this;
|
|
|
|
try {
|
2012-06-04 20:56:00 +02:00
|
|
|
((ChannelInboundHandler<Object>) ctx.handler).channelUnregistered(ctx);
|
2012-06-02 02:51:19 +02:00
|
|
|
} catch (Throwable t) {
|
|
|
|
pipeline.notifyHandlerException(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
final Runnable fireChannelActiveTask = new Runnable() {
|
|
|
|
@Override
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void run() {
|
|
|
|
DefaultChannelHandlerContext ctx = DefaultChannelHandlerContext.this;
|
|
|
|
try {
|
2012-06-04 20:56:00 +02:00
|
|
|
((ChannelInboundHandler<Object>) ctx.handler).channelActive(ctx);
|
2012-06-02 02:51:19 +02:00
|
|
|
} catch (Throwable t) {
|
|
|
|
pipeline.notifyHandlerException(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
final Runnable fireChannelInactiveTask = new Runnable() {
|
|
|
|
@Override
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void run() {
|
|
|
|
DefaultChannelHandlerContext ctx = DefaultChannelHandlerContext.this;
|
|
|
|
try {
|
2012-06-04 20:56:00 +02:00
|
|
|
((ChannelInboundHandler<Object>) ctx.handler).channelInactive(ctx);
|
2012-06-02 02:51:19 +02:00
|
|
|
} catch (Throwable t) {
|
|
|
|
pipeline.notifyHandlerException(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-06-04 09:24:34 +02:00
|
|
|
final Runnable curCtxFireInboundBufferUpdatedTask = new Runnable() {
|
2012-06-02 02:51:19 +02:00
|
|
|
@Override
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void run() {
|
|
|
|
DefaultChannelHandlerContext ctx = DefaultChannelHandlerContext.this;
|
2012-06-04 03:51:42 +02:00
|
|
|
flushBridge();
|
2012-06-02 02:51:19 +02:00
|
|
|
try {
|
2012-06-04 20:56:00 +02:00
|
|
|
((ChannelInboundHandler<Object>) ctx.handler).inboundBufferUpdated(ctx);
|
2012-06-02 02:51:19 +02:00
|
|
|
} catch (Throwable t) {
|
|
|
|
pipeline.notifyHandlerException(t);
|
|
|
|
} finally {
|
2012-06-04 20:56:00 +02:00
|
|
|
if (inByteBridge != null) {
|
|
|
|
ChannelBuffer buf = ctx.in.byteBuffer();
|
|
|
|
if (!buf.readable()) {
|
|
|
|
buf.discardReadBytes();
|
|
|
|
}
|
2012-06-02 02:51:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-06-04 09:24:34 +02:00
|
|
|
private final Runnable nextCtxFireInboundBufferUpdatedTask = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
DefaultChannelHandlerContext next =
|
|
|
|
DefaultChannelPipeline.nextInboundContext(DefaultChannelHandlerContext.this.next);
|
|
|
|
if (next != null) {
|
|
|
|
next.fillBridge();
|
|
|
|
DefaultChannelPipeline.fireInboundBufferUpdated(next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-06-02 02:51:19 +02:00
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
DefaultChannelHandlerContext(
|
|
|
|
DefaultChannelPipeline pipeline, EventExecutor executor,
|
|
|
|
DefaultChannelHandlerContext prev, DefaultChannelHandlerContext next,
|
|
|
|
String name, ChannelHandler handler) {
|
|
|
|
|
|
|
|
if (name == null) {
|
|
|
|
throw new NullPointerException("name");
|
|
|
|
}
|
|
|
|
if (handler == null) {
|
|
|
|
throw new NullPointerException("handler");
|
|
|
|
}
|
|
|
|
|
2012-06-04 20:56:00 +02:00
|
|
|
boolean canHandleInbound = handler instanceof ChannelInboundHandler;
|
|
|
|
boolean canHandleOutbound = handler instanceof ChannelOutboundHandler;
|
2012-06-02 02:51:19 +02:00
|
|
|
if (!canHandleInbound && !canHandleOutbound) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"handler must be either " +
|
|
|
|
ChannelInboundHandler.class.getName() + " or " +
|
|
|
|
ChannelOutboundHandler.class.getName() + '.');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.prev = prev;
|
|
|
|
this.next = next;
|
|
|
|
|
2012-06-02 03:34:19 +02:00
|
|
|
channel = pipeline.channel;
|
2012-06-02 02:51:19 +02:00
|
|
|
this.pipeline = pipeline;
|
|
|
|
this.name = name;
|
|
|
|
this.handler = handler;
|
|
|
|
|
|
|
|
if (executor != null) {
|
|
|
|
// Pin one of the child executors once and remember it so that the same child executor
|
|
|
|
// is used to fire events for the same channel.
|
|
|
|
EventExecutor childExecutor = pipeline.childExecutors.get(executor);
|
|
|
|
if (childExecutor == null) {
|
|
|
|
childExecutor = executor.unsafe().nextChild();
|
|
|
|
pipeline.childExecutors.put(executor, childExecutor);
|
|
|
|
}
|
|
|
|
this.executor = childExecutor;
|
2012-06-04 20:59:31 +02:00
|
|
|
} else if (channel.isRegistered()) {
|
|
|
|
this.executor = channel.eventLoop();
|
2012-06-02 02:51:19 +02:00
|
|
|
} else {
|
|
|
|
this.executor = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (canHandleInbound) {
|
|
|
|
try {
|
|
|
|
in = ((ChannelInboundHandler<Object>) handler).newInboundBuffer(this);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ChannelPipelineException("A user handler failed to create a new inbound buffer.", e);
|
|
|
|
}
|
2012-06-04 03:51:42 +02:00
|
|
|
|
|
|
|
if (!in.isBypass()) {
|
|
|
|
if (in.hasByteBuffer()) {
|
2012-06-04 09:24:34 +02:00
|
|
|
inByteBridge = new AtomicReference<StreamBridge>();
|
2012-06-04 03:51:42 +02:00
|
|
|
inMsgBridge = null;
|
|
|
|
} else {
|
|
|
|
inByteBridge = null;
|
2012-06-04 09:24:34 +02:00
|
|
|
inMsgBridge = new AtomicReference<MessageBridge>();
|
2012-06-04 03:51:42 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inByteBridge = null;
|
|
|
|
inMsgBridge = null;
|
|
|
|
}
|
2012-06-02 02:51:19 +02:00
|
|
|
} else {
|
|
|
|
in = null;
|
2012-06-04 03:51:42 +02:00
|
|
|
inByteBridge = null;
|
|
|
|
inMsgBridge = null;
|
2012-06-02 02:51:19 +02:00
|
|
|
}
|
|
|
|
if (canHandleOutbound) {
|
|
|
|
try {
|
|
|
|
out = ((ChannelOutboundHandler<Object>) handler).newOutboundBuffer(this);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ChannelPipelineException("A user handler failed to create a new outbound buffer.", e);
|
|
|
|
} finally {
|
|
|
|
if (in != null) {
|
|
|
|
// TODO Release the inbound buffer once pooling is implemented.
|
|
|
|
}
|
|
|
|
}
|
2012-06-04 03:51:42 +02:00
|
|
|
|
|
|
|
if (!out.isBypass()) {
|
|
|
|
if (out.hasByteBuffer()) {
|
2012-06-04 09:24:34 +02:00
|
|
|
outByteBridge = new AtomicReference<StreamBridge>();
|
2012-06-04 03:51:42 +02:00
|
|
|
outMsgBridge = null;
|
|
|
|
} else {
|
|
|
|
outByteBridge = null;
|
2012-06-04 09:24:34 +02:00
|
|
|
outMsgBridge = new AtomicReference<MessageBridge>();
|
2012-06-04 03:51:42 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
outByteBridge = null;
|
|
|
|
outMsgBridge = null;
|
|
|
|
}
|
2012-06-02 02:51:19 +02:00
|
|
|
} else {
|
|
|
|
out = null;
|
2012-06-04 03:51:42 +02:00
|
|
|
outByteBridge = null;
|
|
|
|
outMsgBridge = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 09:24:34 +02:00
|
|
|
void fillBridge() {
|
|
|
|
if (inMsgBridge != null) {
|
|
|
|
MessageBridge bridge = inMsgBridge.get();
|
|
|
|
if (bridge != null) {
|
|
|
|
bridge.fill();
|
|
|
|
}
|
|
|
|
} else if (inByteBridge != null) {
|
|
|
|
StreamBridge bridge = inByteBridge.get();
|
|
|
|
if (bridge != null) {
|
|
|
|
bridge.fill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outMsgBridge != null) {
|
|
|
|
MessageBridge bridge = outMsgBridge.get();
|
|
|
|
if (bridge != null) {
|
|
|
|
bridge.fill();
|
|
|
|
}
|
|
|
|
} else if (outByteBridge != null) {
|
|
|
|
StreamBridge bridge = outByteBridge.get();
|
|
|
|
if (bridge != null) {
|
|
|
|
bridge.fill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 03:51:42 +02:00
|
|
|
void flushBridge() {
|
|
|
|
if (inMsgBridge != null) {
|
2012-06-04 09:24:34 +02:00
|
|
|
MessageBridge bridge = inMsgBridge.get();
|
2012-06-04 03:51:42 +02:00
|
|
|
if (bridge != null) {
|
2012-06-04 09:24:34 +02:00
|
|
|
bridge.flush(in.messageBuffer());
|
|
|
|
}
|
|
|
|
} else if (inByteBridge != null) {
|
|
|
|
StreamBridge bridge = inByteBridge.get();
|
|
|
|
if (bridge != null) {
|
|
|
|
bridge.flush(in.byteBuffer());
|
2012-06-04 03:51:42 +02:00
|
|
|
}
|
|
|
|
}
|
2012-06-04 09:24:34 +02:00
|
|
|
|
2012-06-04 03:51:42 +02:00
|
|
|
if (outMsgBridge != null) {
|
2012-06-04 09:24:34 +02:00
|
|
|
MessageBridge bridge = outMsgBridge.get();
|
|
|
|
if (bridge != null) {
|
|
|
|
bridge.flush(out.messageBuffer());
|
|
|
|
}
|
|
|
|
} else if (outByteBridge != null) {
|
|
|
|
StreamBridge bridge = outByteBridge.get();
|
2012-06-04 03:51:42 +02:00
|
|
|
if (bridge != null) {
|
2012-06-04 09:24:34 +02:00
|
|
|
bridge.flush(out.byteBuffer());
|
2012-06-04 03:51:42 +02:00
|
|
|
}
|
2012-06-02 02:51:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Channel channel() {
|
2012-06-02 03:34:19 +02:00
|
|
|
return channel;
|
2012-06-02 02:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelPipeline pipeline() {
|
|
|
|
return pipeline;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EventExecutor executor() {
|
|
|
|
if (executor == null) {
|
2012-06-04 09:24:34 +02:00
|
|
|
return executor = channel.eventLoop();
|
2012-06-02 02:51:19 +02:00
|
|
|
} else {
|
|
|
|
return executor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelHandler handler() {
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String name() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canHandleInbound() {
|
2012-06-04 20:56:00 +02:00
|
|
|
return in != null;
|
2012-06-02 02:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canHandleOutbound() {
|
2012-06-04 20:56:00 +02:00
|
|
|
return out != null;
|
2012-06-02 02:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelBufferHolder<Object> inbound() {
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelBufferHolder<Object> outbound() {
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2012-06-03 13:35:38 +02:00
|
|
|
@Override
|
|
|
|
public boolean hasNextInboundByteBuffer() {
|
|
|
|
return DefaultChannelPipeline.hasNextInboundByteBuffer(next);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasNextInboundMessageBuffer() {
|
|
|
|
return DefaultChannelPipeline.hasNextInboundMessageBuffer(next);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasNextOutboundByteBuffer() {
|
|
|
|
return pipeline.hasNextOutboundByteBuffer(prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasNextOutboundMessageBuffer() {
|
|
|
|
return pipeline.hasNextOutboundMessageBuffer(prev);
|
|
|
|
}
|
|
|
|
|
2012-06-02 02:51:19 +02:00
|
|
|
@Override
|
|
|
|
public ChannelBuffer nextInboundByteBuffer() {
|
2012-06-04 09:24:34 +02:00
|
|
|
return DefaultChannelPipeline.nextInboundByteBuffer(executor(), next);
|
2012-06-02 02:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Queue<Object> nextInboundMessageBuffer() {
|
2012-06-04 03:51:42 +02:00
|
|
|
return DefaultChannelPipeline.nextInboundMessageBuffer(executor(), next);
|
2012-06-02 02:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelBuffer nextOutboundByteBuffer() {
|
2012-06-04 09:24:34 +02:00
|
|
|
return pipeline.nextOutboundByteBuffer(executor(), prev);
|
2012-06-02 02:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Queue<Object> nextOutboundMessageBuffer() {
|
2012-06-04 03:51:42 +02:00
|
|
|
return pipeline.nextOutboundMessageBuffer(executor(), prev);
|
2012-06-02 02:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fireChannelRegistered() {
|
|
|
|
DefaultChannelHandlerContext next = DefaultChannelPipeline.nextInboundContext(this.next);
|
|
|
|
if (next != null) {
|
|
|
|
DefaultChannelPipeline.fireChannelRegistered(next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fireChannelUnregistered() {
|
|
|
|
DefaultChannelHandlerContext next = DefaultChannelPipeline.nextInboundContext(this.next);
|
|
|
|
if (next != null) {
|
|
|
|
DefaultChannelPipeline.fireChannelUnregistered(next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fireChannelActive() {
|
|
|
|
DefaultChannelHandlerContext next = DefaultChannelPipeline.nextInboundContext(this.next);
|
|
|
|
if (next != null) {
|
|
|
|
DefaultChannelPipeline.fireChannelActive(next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fireChannelInactive() {
|
|
|
|
DefaultChannelHandlerContext next = DefaultChannelPipeline.nextInboundContext(this.next);
|
|
|
|
if (next != null) {
|
|
|
|
DefaultChannelPipeline.fireChannelInactive(next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fireExceptionCaught(Throwable cause) {
|
|
|
|
DefaultChannelHandlerContext next = DefaultChannelPipeline.nextInboundContext(this.next);
|
|
|
|
if (next != null) {
|
|
|
|
pipeline.fireExceptionCaught(next, cause);
|
|
|
|
} else {
|
|
|
|
DefaultChannelPipeline.logTerminalException(cause);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fireUserEventTriggered(Object event) {
|
|
|
|
DefaultChannelHandlerContext next = DefaultChannelPipeline.nextInboundContext(this.next);
|
|
|
|
if (next != null) {
|
|
|
|
pipeline.fireUserEventTriggered(next, event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fireInboundBufferUpdated() {
|
2012-06-04 09:24:34 +02:00
|
|
|
EventExecutor executor = executor();
|
|
|
|
if (executor.inEventLoop()) {
|
|
|
|
nextCtxFireInboundBufferUpdatedTask.run();
|
|
|
|
} else {
|
|
|
|
executor.execute(nextCtxFireInboundBufferUpdatedTask);
|
2012-06-02 02:51:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture bind(SocketAddress localAddress) {
|
|
|
|
return bind(localAddress, newFuture());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture connect(SocketAddress remoteAddress) {
|
|
|
|
return connect(remoteAddress, newFuture());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress) {
|
|
|
|
return connect(remoteAddress, localAddress, newFuture());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture disconnect() {
|
|
|
|
return disconnect(newFuture());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture close() {
|
|
|
|
return close(newFuture());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture deregister() {
|
|
|
|
return deregister(newFuture());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture flush() {
|
|
|
|
return flush(newFuture());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture write(Object message) {
|
|
|
|
return write(message, newFuture());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture bind(SocketAddress localAddress, ChannelFuture future) {
|
|
|
|
return pipeline.bind(DefaultChannelPipeline.nextOutboundContext(prev), localAddress, future);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture connect(SocketAddress remoteAddress, ChannelFuture future) {
|
|
|
|
return connect(remoteAddress, null, future);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelFuture future) {
|
|
|
|
return pipeline.connect(DefaultChannelPipeline.nextOutboundContext(prev), remoteAddress, localAddress, future);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture disconnect(ChannelFuture future) {
|
|
|
|
return pipeline.disconnect(DefaultChannelPipeline.nextOutboundContext(prev), future);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture close(ChannelFuture future) {
|
|
|
|
return pipeline.close(DefaultChannelPipeline.nextOutboundContext(prev), future);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture deregister(ChannelFuture future) {
|
|
|
|
return pipeline.deregister(DefaultChannelPipeline.nextOutboundContext(prev), future);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-06-04 09:24:34 +02:00
|
|
|
public ChannelFuture flush(final ChannelFuture future) {
|
|
|
|
EventExecutor executor = executor();
|
|
|
|
if (executor.inEventLoop()) {
|
|
|
|
DefaultChannelHandlerContext prev = DefaultChannelPipeline.nextOutboundContext(this.prev);
|
|
|
|
prev.fillBridge();
|
|
|
|
pipeline.flush(prev, future);
|
|
|
|
} else {
|
|
|
|
executor.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
flush(future);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return future;
|
2012-06-02 02:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture write(Object message, ChannelFuture future) {
|
2012-06-03 13:25:03 +02:00
|
|
|
return pipeline.write(prev, message, future);
|
2012-06-02 02:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture newFuture() {
|
2012-06-02 03:34:19 +02:00
|
|
|
return channel.newFuture();
|
2012-06-02 02:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture newSucceededFuture() {
|
2012-06-02 03:34:19 +02:00
|
|
|
return channel.newSucceededFuture();
|
2012-06-02 02:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture newFailedFuture(Throwable cause) {
|
2012-06-02 03:34:19 +02:00
|
|
|
return channel.newFailedFuture(cause);
|
2012-06-02 02:51:19 +02:00
|
|
|
}
|
2012-06-04 09:24:34 +02:00
|
|
|
|
|
|
|
static final class MessageBridge {
|
|
|
|
final Queue<Object> msgBuf = new ArrayDeque<Object>();
|
|
|
|
final BlockingQueue<Object[]> exchangeBuf = QueueFactory.createQueue();
|
|
|
|
|
|
|
|
void fill() {
|
|
|
|
if (msgBuf.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Object[] data = msgBuf.toArray();
|
|
|
|
msgBuf.clear();
|
|
|
|
exchangeBuf.add(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void flush(Queue<Object> out) {
|
|
|
|
for (;;) {
|
|
|
|
Object[] data = exchangeBuf.poll();
|
|
|
|
if (data == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Object d: data) {
|
|
|
|
out.add(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static final class StreamBridge {
|
|
|
|
final ChannelBuffer byteBuf = ChannelBuffers.dynamicBuffer();
|
|
|
|
final BlockingQueue<ChannelBuffer> exchangeBuf = QueueFactory.createQueue();
|
|
|
|
|
|
|
|
void fill() {
|
|
|
|
if (!byteBuf.readable()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ChannelBuffer data = byteBuf.readBytes(byteBuf.readableBytes());
|
|
|
|
byteBuf.discardReadBytes();
|
|
|
|
exchangeBuf.add(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void flush(ChannelBuffer out) {
|
|
|
|
for (;;) {
|
|
|
|
ChannelBuffer data = exchangeBuf.poll();
|
|
|
|
if (data == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
out.writeBytes(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-02 02:51:19 +02:00
|
|
|
}
|