2008-08-08 00:37:18 +00:00
|
|
|
/*
|
2012-06-04 13:31:44 -07:00
|
|
|
* Copyright 2012 The Netty Project
|
2008-08-08 00:37:18 +00:00
|
|
|
*
|
2011-12-09 14:18:34 +09:00
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
2008-08-08 00:37:18 +00:00
|
|
|
*
|
2012-06-04 13:31:44 -07:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-08-08 01:27:24 +00:00
|
|
|
*
|
2009-08-28 07:15:49 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 14:18:34 +09:00
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2009-08-28 07:15:49 +00:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2008-08-08 00:37:18 +00:00
|
|
|
*/
|
2011-12-09 12:38:59 +09:00
|
|
|
package io.netty.channel;
|
2008-08-08 00:37:18 +00:00
|
|
|
|
2013-02-06 12:55:42 +09:00
|
|
|
import io.netty.buffer.Buf;
|
2012-06-10 11:08:43 +09:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2012-06-11 10:43:47 +09:00
|
|
|
import io.netty.buffer.MessageBuf;
|
2013-02-10 13:10:09 +09:00
|
|
|
import io.netty.buffer.ReferenceCounted;
|
2012-06-11 17:02:00 +09:00
|
|
|
import io.netty.buffer.Unpooled;
|
2013-02-07 23:58:21 +09:00
|
|
|
import io.netty.channel.Channel.Unsafe;
|
2013-02-26 14:54:25 -08:00
|
|
|
import io.netty.util.internal.logging.InternalLogger;
|
|
|
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
2012-05-01 17:19:41 +09:00
|
|
|
|
|
|
|
import java.net.SocketAddress;
|
2011-08-02 08:43:10 +09:00
|
|
|
import java.util.ArrayList;
|
2008-08-08 00:37:18 +00:00
|
|
|
import java.util.HashMap;
|
2012-06-01 17:51:19 -07:00
|
|
|
import java.util.IdentityHashMap;
|
2013-02-08 07:10:46 +01:00
|
|
|
import java.util.Iterator;
|
2008-08-08 00:37:18 +00:00
|
|
|
import java.util.LinkedHashMap;
|
2011-08-02 08:43:10 +09:00
|
|
|
import java.util.List;
|
2008-08-08 00:37:18 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.NoSuchElementException;
|
2012-06-05 11:21:44 +02:00
|
|
|
import java.util.concurrent.Future;
|
2008-08-08 00:37:18 +00:00
|
|
|
|
2012-11-10 07:54:33 +09:00
|
|
|
import static io.netty.channel.DefaultChannelHandlerContext.*;
|
|
|
|
|
2008-09-02 07:13:20 +00:00
|
|
|
/**
|
2012-05-01 17:19:41 +09:00
|
|
|
* The default {@link ChannelPipeline} implementation. It is usually created
|
|
|
|
* by a {@link Channel} implementation when the {@link Channel} is created.
|
2008-09-02 07:13:20 +00:00
|
|
|
*/
|
2012-12-23 15:54:14 +01:00
|
|
|
final class DefaultChannelPipeline implements ChannelPipeline {
|
2008-08-08 00:37:18 +00:00
|
|
|
|
2008-08-09 15:05:53 +00:00
|
|
|
static final InternalLogger logger = InternalLoggerFactory.getInstance(DefaultChannelPipeline.class);
|
2009-03-11 10:45:55 +00:00
|
|
|
|
2012-06-01 17:51:19 -07:00
|
|
|
final Channel channel;
|
2012-06-01 18:34:19 -07:00
|
|
|
|
2012-06-07 14:52:33 +09:00
|
|
|
final DefaultChannelHandlerContext head;
|
2013-01-09 19:13:43 +09:00
|
|
|
final DefaultChannelHandlerContext tail;
|
2013-01-07 08:44:16 +01:00
|
|
|
|
2008-08-08 00:37:18 +00:00
|
|
|
private final Map<String, DefaultChannelHandlerContext> name2ctx =
|
|
|
|
new HashMap<String, DefaultChannelHandlerContext>(4);
|
2012-05-30 11:32:39 -07:00
|
|
|
private boolean firedChannelActive;
|
2012-06-04 11:56:00 -07:00
|
|
|
private boolean fireInboundBufferUpdatedOnActivation;
|
|
|
|
|
2012-08-10 20:17:18 +09:00
|
|
|
final Map<EventExecutorGroup, EventExecutor> childExecutors =
|
|
|
|
new IdentityHashMap<EventExecutorGroup, EventExecutor>();
|
2012-06-06 23:02:47 +09:00
|
|
|
|
2013-02-21 15:19:42 -08:00
|
|
|
private boolean inboundShutdown;
|
|
|
|
private boolean outboundShutdown;
|
2013-01-07 08:44:16 +01:00
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
public DefaultChannelPipeline(Channel channel) {
|
2008-08-08 00:37:18 +00:00
|
|
|
if (channel == null) {
|
|
|
|
throw new NullPointerException("channel");
|
|
|
|
}
|
|
|
|
this.channel = channel;
|
2012-06-03 18:51:42 -07:00
|
|
|
|
2013-02-06 12:55:42 +09:00
|
|
|
TailHandler tailHandler = new TailHandler();
|
|
|
|
tail = new DefaultChannelHandlerContext(this, generateName(tailHandler), tailHandler);
|
2013-01-05 15:04:25 +09:00
|
|
|
|
|
|
|
HeadHandler headHandler;
|
|
|
|
switch (channel.metadata().bufferType()) {
|
|
|
|
case BYTE:
|
2013-02-07 23:58:21 +09:00
|
|
|
headHandler = new ByteHeadHandler(channel.unsafe());
|
2013-01-05 15:04:25 +09:00
|
|
|
break;
|
|
|
|
case MESSAGE:
|
2013-02-07 23:58:21 +09:00
|
|
|
headHandler = new MessageHeadHandler(channel.unsafe());
|
2013-01-05 15:04:25 +09:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("unknown buffer type: " + channel.metadata().bufferType());
|
|
|
|
}
|
|
|
|
|
2013-02-06 12:55:42 +09:00
|
|
|
head = new DefaultChannelHandlerContext(this, generateName(headHandler), headHandler);
|
2013-01-09 20:36:16 +09:00
|
|
|
|
|
|
|
head.next = tail;
|
2013-01-09 19:13:43 +09:00
|
|
|
tail.prev = head;
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-05-01 17:19:41 +09:00
|
|
|
public Channel channel() {
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-06-01 17:51:19 -07:00
|
|
|
public ChannelPipeline addFirst(String name, ChannelHandler handler) {
|
|
|
|
return addFirst(null, name, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-11-12 14:55:05 -08:00
|
|
|
public ChannelPipeline addFirst(EventExecutorGroup group, final String name, ChannelHandler handler) {
|
|
|
|
final DefaultChannelHandlerContext newCtx;
|
|
|
|
|
|
|
|
synchronized (this) {
|
|
|
|
checkDuplicateName(name);
|
2013-01-09 20:36:16 +09:00
|
|
|
newCtx = new DefaultChannelHandlerContext(this, group, name, handler);
|
2012-11-12 14:55:05 -08:00
|
|
|
|
|
|
|
if (!newCtx.channel().isRegistered() || newCtx.executor().inEventLoop()) {
|
2013-01-09 19:13:43 +09:00
|
|
|
addFirst0(name, newCtx);
|
2012-11-12 14:55:05 -08:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
2012-06-05 11:21:44 +02:00
|
|
|
|
2012-11-12 14:55:05 -08:00
|
|
|
// Run the following 'waiting' code outside of the above synchronized block
|
|
|
|
// in order to avoid deadlock
|
2012-06-06 23:02:47 +09:00
|
|
|
|
2012-11-12 14:55:05 -08:00
|
|
|
newCtx.executeOnEventLoop(new Runnable() {
|
Read only when requested (read-on-demand)
This pull request introduces a new operation called read() that replaces the existing inbound traffic control method. EventLoop now performs socket reads only when the read() operation has been issued. Once the requested read() operation is actually performed, EventLoop triggers an inboundBufferSuspended event that tells the handlers that the requested read() operation has been performed and the inbound traffic has been suspended again. A handler can decide to continue reading or not.
Unlike other outbound operations, read() does not use ChannelFuture at all to avoid GC cost. If there's a good reason to create a new future per read at the GC cost, I'll change this.
This pull request consequently removes the readable property in ChannelHandlerContext, which means how the traffic control works changed significantly.
This pull request also adds a new configuration property ChannelOption.AUTO_READ whose default value is true. If true, Netty will call ctx.read() for you. If you need a close control over when read() is called, you can set it to false.
Another interesting fact is that non-terminal handlers do not really need to call read() at all. Only the last inbound handler will have to call it, and that's just enough. Actually, you don't even need to call it at the last handler in most cases because of the ChannelOption.AUTO_READ mentioned above.
There's no serious backward compatibility issue. If the compiler complains your handler does not implement the read() method, add the following:
public void read(ChannelHandlerContext ctx) throws Exception {
ctx.read();
}
Note that this pull request certainly makes bounded inbound buffer support very easy, but itself does not add the bounded inbound buffer support.
2012-12-30 21:53:59 +09:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
synchronized (DefaultChannelPipeline.this) {
|
|
|
|
checkDuplicateName(name);
|
2013-01-09 19:13:43 +09:00
|
|
|
addFirst0(name, newCtx);
|
2012-11-12 14:55:05 -08:00
|
|
|
}
|
Read only when requested (read-on-demand)
This pull request introduces a new operation called read() that replaces the existing inbound traffic control method. EventLoop now performs socket reads only when the read() operation has been issued. Once the requested read() operation is actually performed, EventLoop triggers an inboundBufferSuspended event that tells the handlers that the requested read() operation has been performed and the inbound traffic has been suspended again. A handler can decide to continue reading or not.
Unlike other outbound operations, read() does not use ChannelFuture at all to avoid GC cost. If there's a good reason to create a new future per read at the GC cost, I'll change this.
This pull request consequently removes the readable property in ChannelHandlerContext, which means how the traffic control works changed significantly.
This pull request also adds a new configuration property ChannelOption.AUTO_READ whose default value is true. If true, Netty will call ctx.read() for you. If you need a close control over when read() is called, you can set it to false.
Another interesting fact is that non-terminal handlers do not really need to call read() at all. Only the last inbound handler will have to call it, and that's just enough. Actually, you don't even need to call it at the last handler in most cases because of the ChannelOption.AUTO_READ mentioned above.
There's no serious backward compatibility issue. If the compiler complains your handler does not implement the read() method, add the following:
public void read(ChannelHandlerContext ctx) throws Exception {
ctx.read();
}
Note that this pull request certainly makes bounded inbound buffer support very easy, but itself does not add the bounded inbound buffer support.
2012-12-30 21:53:59 +09:00
|
|
|
}
|
|
|
|
});
|
2012-06-04 11:56:00 -07:00
|
|
|
|
2012-11-12 14:55:05 -08:00
|
|
|
return this;
|
2012-06-04 20:34:09 +02:00
|
|
|
}
|
2012-06-06 23:02:47 +09:00
|
|
|
|
2013-01-09 19:13:43 +09:00
|
|
|
private void addFirst0(String name, DefaultChannelHandlerContext newCtx) {
|
|
|
|
DefaultChannelHandlerContext nextCtx = head.next;
|
|
|
|
newCtx.prev = head;
|
|
|
|
newCtx.next = nextCtx;
|
2013-01-09 20:34:22 +09:00
|
|
|
|
|
|
|
callBeforeAdd(newCtx);
|
|
|
|
|
|
|
|
head.next = newCtx;
|
2013-01-09 19:13:43 +09:00
|
|
|
nextCtx.prev = newCtx;
|
2012-11-16 13:30:34 +08:00
|
|
|
|
2012-06-03 18:51:42 -07:00
|
|
|
name2ctx.put(name, newCtx);
|
2012-05-15 14:08:42 +09:00
|
|
|
|
2012-06-03 18:51:42 -07:00
|
|
|
callAfterAdd(newCtx);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
2012-06-04 11:56:00 -07:00
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-06-01 17:51:19 -07:00
|
|
|
public ChannelPipeline addLast(String name, ChannelHandler handler) {
|
|
|
|
return addLast(null, name, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-11-12 14:55:05 -08:00
|
|
|
public ChannelPipeline addLast(EventExecutorGroup group, final String name, ChannelHandler handler) {
|
2013-01-09 19:13:43 +09:00
|
|
|
final DefaultChannelHandlerContext newCtx;
|
2012-06-05 11:21:44 +02:00
|
|
|
|
2012-11-12 14:55:05 -08:00
|
|
|
synchronized (this) {
|
|
|
|
checkDuplicateName(name);
|
2012-06-05 11:21:44 +02:00
|
|
|
|
2013-01-09 20:36:16 +09:00
|
|
|
newCtx = new DefaultChannelHandlerContext(this, group, name, handler);
|
2013-01-09 19:13:43 +09:00
|
|
|
if (!newCtx.channel().isRegistered() || newCtx.executor().inEventLoop()) {
|
|
|
|
addLast0(name, newCtx);
|
2012-11-12 14:55:05 -08:00
|
|
|
return this;
|
2012-06-04 20:49:31 +02:00
|
|
|
}
|
2012-06-04 20:34:09 +02:00
|
|
|
}
|
2012-06-06 23:02:47 +09:00
|
|
|
|
2012-11-12 14:55:05 -08:00
|
|
|
// Run the following 'waiting' code outside of the above synchronized block
|
|
|
|
// in order to avoid deadlock
|
|
|
|
|
2013-01-09 19:13:43 +09:00
|
|
|
newCtx.executeOnEventLoop(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
synchronized (DefaultChannelPipeline.this) {
|
|
|
|
checkDuplicateName(name);
|
|
|
|
addLast0(name, newCtx);
|
2012-11-12 14:55:05 -08:00
|
|
|
}
|
2013-01-09 19:13:43 +09:00
|
|
|
}
|
|
|
|
});
|
2012-11-12 14:55:05 -08:00
|
|
|
|
|
|
|
return this;
|
2012-06-04 20:34:09 +02:00
|
|
|
}
|
2012-06-04 11:56:00 -07:00
|
|
|
|
2012-06-08 19:28:12 +09:00
|
|
|
private void addLast0(
|
2013-01-09 19:13:43 +09:00
|
|
|
final String name, DefaultChannelHandlerContext newCtx) {
|
|
|
|
DefaultChannelHandlerContext prev = tail.prev;
|
|
|
|
newCtx.prev = prev;
|
|
|
|
newCtx.next = tail;
|
2013-01-09 20:34:22 +09:00
|
|
|
|
|
|
|
callBeforeAdd(newCtx);
|
|
|
|
|
|
|
|
prev.next = newCtx;
|
2013-01-09 19:13:43 +09:00
|
|
|
tail.prev = newCtx;
|
2013-01-07 08:44:16 +01:00
|
|
|
|
2013-01-09 19:13:43 +09:00
|
|
|
name2ctx.put(name, newCtx);
|
2012-05-15 14:08:42 +09:00
|
|
|
|
2013-01-09 19:13:43 +09:00
|
|
|
callAfterAdd(newCtx);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-06-01 17:51:19 -07:00
|
|
|
public ChannelPipeline addBefore(String baseName, String name, ChannelHandler handler) {
|
|
|
|
return addBefore(null, baseName, name, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-06-08 19:28:12 +09:00
|
|
|
public ChannelPipeline addBefore(
|
2012-11-12 14:55:05 -08:00
|
|
|
EventExecutorGroup group, String baseName, final String name, ChannelHandler handler) {
|
|
|
|
final DefaultChannelHandlerContext ctx;
|
|
|
|
final DefaultChannelHandlerContext newCtx;
|
|
|
|
|
|
|
|
synchronized (this) {
|
|
|
|
ctx = getContextOrDie(baseName);
|
|
|
|
checkDuplicateName(name);
|
2013-01-09 20:36:16 +09:00
|
|
|
newCtx = new DefaultChannelHandlerContext(this, group, name, handler);
|
2012-11-12 14:55:05 -08:00
|
|
|
|
|
|
|
if (!newCtx.channel().isRegistered() || newCtx.executor().inEventLoop()) {
|
|
|
|
addBefore0(name, ctx, newCtx);
|
|
|
|
return this;
|
2012-06-04 20:49:31 +02:00
|
|
|
}
|
2012-11-12 14:55:05 -08:00
|
|
|
}
|
2012-06-06 23:02:47 +09:00
|
|
|
|
2012-11-12 14:55:05 -08:00
|
|
|
// Run the following 'waiting' code outside of the above synchronized block
|
|
|
|
// in order to avoid deadlock
|
2012-06-05 11:21:44 +02:00
|
|
|
|
2012-11-12 14:55:05 -08:00
|
|
|
newCtx.executeOnEventLoop(new Runnable() {
|
2013-01-15 16:23:09 +09:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
synchronized (DefaultChannelPipeline.this) {
|
|
|
|
checkDuplicateName(name);
|
|
|
|
addBefore0(name, ctx, newCtx);
|
2012-11-12 14:55:05 -08:00
|
|
|
}
|
2013-01-15 16:23:09 +09:00
|
|
|
}
|
|
|
|
});
|
2012-06-06 23:02:47 +09:00
|
|
|
|
2012-11-12 14:55:05 -08:00
|
|
|
return this;
|
2012-06-04 20:34:09 +02:00
|
|
|
}
|
|
|
|
|
2012-06-04 11:56:00 -07:00
|
|
|
private void addBefore0(final String name, DefaultChannelHandlerContext ctx, DefaultChannelHandlerContext newCtx) {
|
2013-01-09 20:34:22 +09:00
|
|
|
|
|
|
|
newCtx.prev = ctx.prev;
|
|
|
|
newCtx.next = ctx;
|
|
|
|
|
2012-06-03 18:51:42 -07:00
|
|
|
callBeforeAdd(newCtx);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
2012-06-03 18:51:42 -07:00
|
|
|
ctx.prev.next = newCtx;
|
|
|
|
ctx.prev = newCtx;
|
|
|
|
name2ctx.put(name, newCtx);
|
2012-05-15 14:08:42 +09:00
|
|
|
|
2012-06-04 11:56:00 -07:00
|
|
|
callAfterAdd(newCtx);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
2012-06-04 11:56:00 -07:00
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-06-01 17:51:19 -07:00
|
|
|
public ChannelPipeline addAfter(String baseName, String name, ChannelHandler handler) {
|
|
|
|
return addAfter(null, baseName, name, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-06-08 19:28:12 +09:00
|
|
|
public ChannelPipeline addAfter(
|
2012-11-12 14:55:05 -08:00
|
|
|
EventExecutorGroup group, String baseName, final String name, ChannelHandler handler) {
|
|
|
|
final DefaultChannelHandlerContext ctx;
|
|
|
|
final DefaultChannelHandlerContext newCtx;
|
|
|
|
|
|
|
|
synchronized (this) {
|
|
|
|
ctx = getContextOrDie(baseName);
|
|
|
|
checkDuplicateName(name);
|
2013-01-09 20:36:16 +09:00
|
|
|
newCtx = new DefaultChannelHandlerContext(this, group, name, handler);
|
2012-06-06 23:02:47 +09:00
|
|
|
|
2012-11-12 14:55:05 -08:00
|
|
|
if (!newCtx.channel().isRegistered() || newCtx.executor().inEventLoop()) {
|
|
|
|
addAfter0(name, ctx, newCtx);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
2012-06-06 23:02:47 +09:00
|
|
|
|
2012-11-12 14:55:05 -08:00
|
|
|
// Run the following 'waiting' code outside of the above synchronized block
|
|
|
|
// in order to avoid deadlock
|
2012-06-05 11:21:44 +02:00
|
|
|
|
2012-11-12 14:55:05 -08:00
|
|
|
newCtx.executeOnEventLoop(new Runnable() {
|
Read only when requested (read-on-demand)
This pull request introduces a new operation called read() that replaces the existing inbound traffic control method. EventLoop now performs socket reads only when the read() operation has been issued. Once the requested read() operation is actually performed, EventLoop triggers an inboundBufferSuspended event that tells the handlers that the requested read() operation has been performed and the inbound traffic has been suspended again. A handler can decide to continue reading or not.
Unlike other outbound operations, read() does not use ChannelFuture at all to avoid GC cost. If there's a good reason to create a new future per read at the GC cost, I'll change this.
This pull request consequently removes the readable property in ChannelHandlerContext, which means how the traffic control works changed significantly.
This pull request also adds a new configuration property ChannelOption.AUTO_READ whose default value is true. If true, Netty will call ctx.read() for you. If you need a close control over when read() is called, you can set it to false.
Another interesting fact is that non-terminal handlers do not really need to call read() at all. Only the last inbound handler will have to call it, and that's just enough. Actually, you don't even need to call it at the last handler in most cases because of the ChannelOption.AUTO_READ mentioned above.
There's no serious backward compatibility issue. If the compiler complains your handler does not implement the read() method, add the following:
public void read(ChannelHandlerContext ctx) throws Exception {
ctx.read();
}
Note that this pull request certainly makes bounded inbound buffer support very easy, but itself does not add the bounded inbound buffer support.
2012-12-30 21:53:59 +09:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
synchronized (DefaultChannelPipeline.this) {
|
|
|
|
checkDuplicateName(name);
|
|
|
|
addAfter0(name, ctx, newCtx);
|
2012-06-04 20:49:31 +02:00
|
|
|
}
|
Read only when requested (read-on-demand)
This pull request introduces a new operation called read() that replaces the existing inbound traffic control method. EventLoop now performs socket reads only when the read() operation has been issued. Once the requested read() operation is actually performed, EventLoop triggers an inboundBufferSuspended event that tells the handlers that the requested read() operation has been performed and the inbound traffic has been suspended again. A handler can decide to continue reading or not.
Unlike other outbound operations, read() does not use ChannelFuture at all to avoid GC cost. If there's a good reason to create a new future per read at the GC cost, I'll change this.
This pull request consequently removes the readable property in ChannelHandlerContext, which means how the traffic control works changed significantly.
This pull request also adds a new configuration property ChannelOption.AUTO_READ whose default value is true. If true, Netty will call ctx.read() for you. If you need a close control over when read() is called, you can set it to false.
Another interesting fact is that non-terminal handlers do not really need to call read() at all. Only the last inbound handler will have to call it, and that's just enough. Actually, you don't even need to call it at the last handler in most cases because of the ChannelOption.AUTO_READ mentioned above.
There's no serious backward compatibility issue. If the compiler complains your handler does not implement the read() method, add the following:
public void read(ChannelHandlerContext ctx) throws Exception {
ctx.read();
}
Note that this pull request certainly makes bounded inbound buffer support very easy, but itself does not add the bounded inbound buffer support.
2012-12-30 21:53:59 +09:00
|
|
|
}
|
|
|
|
});
|
2012-06-06 23:02:47 +09:00
|
|
|
|
2012-11-12 14:55:05 -08:00
|
|
|
return this;
|
2012-05-15 14:08:42 +09:00
|
|
|
}
|
|
|
|
|
2012-06-04 11:56:00 -07:00
|
|
|
private void addAfter0(final String name, DefaultChannelHandlerContext ctx, DefaultChannelHandlerContext newCtx) {
|
2012-06-04 20:34:09 +02:00
|
|
|
checkDuplicateName(name);
|
|
|
|
|
2013-01-09 19:13:43 +09:00
|
|
|
newCtx.prev = ctx;
|
|
|
|
newCtx.next = ctx.next;
|
2013-01-09 20:34:22 +09:00
|
|
|
|
|
|
|
callBeforeAdd(newCtx);
|
|
|
|
|
2012-06-04 20:34:09 +02:00
|
|
|
ctx.next.prev = newCtx;
|
|
|
|
ctx.next = newCtx;
|
2013-01-09 19:13:43 +09:00
|
|
|
|
2012-06-04 20:34:09 +02:00
|
|
|
name2ctx.put(name, newCtx);
|
|
|
|
|
2012-06-04 11:56:00 -07:00
|
|
|
callAfterAdd(newCtx);
|
2012-06-04 20:34:09 +02:00
|
|
|
}
|
2012-06-04 11:56:00 -07:00
|
|
|
|
2012-05-15 14:08:42 +09:00
|
|
|
@Override
|
|
|
|
public ChannelPipeline addFirst(ChannelHandler... handlers) {
|
2012-06-01 17:51:19 -07:00
|
|
|
return addFirst(null, handlers);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-08-10 20:17:18 +09:00
|
|
|
public ChannelPipeline addFirst(EventExecutorGroup executor, ChannelHandler... handlers) {
|
2012-05-15 14:08:42 +09:00
|
|
|
if (handlers == null) {
|
|
|
|
throw new NullPointerException("handlers");
|
|
|
|
}
|
2012-05-15 14:49:23 +09:00
|
|
|
if (handlers.length == 0 || handlers[0] == null) {
|
2012-05-15 14:08:42 +09:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
int size;
|
|
|
|
for (size = 1; size < handlers.length; size ++) {
|
|
|
|
if (handlers[size] == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = size - 1; i >= 0; i --) {
|
|
|
|
ChannelHandler h = handlers[i];
|
2012-06-01 17:51:19 -07:00
|
|
|
addFirst(executor, generateName(h), h);
|
2012-05-15 14:08:42 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelPipeline addLast(ChannelHandler... handlers) {
|
2012-06-01 17:51:19 -07:00
|
|
|
return addLast(null, handlers);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-08-10 20:17:18 +09:00
|
|
|
public ChannelPipeline addLast(EventExecutorGroup executor, ChannelHandler... handlers) {
|
2012-05-15 14:08:42 +09:00
|
|
|
if (handlers == null) {
|
|
|
|
throw new NullPointerException("handlers");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ChannelHandler h: handlers) {
|
|
|
|
if (h == null) {
|
|
|
|
break;
|
|
|
|
}
|
2012-06-01 17:51:19 -07:00
|
|
|
addLast(executor, generateName(h), h);
|
2012-05-15 14:08:42 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-05-29 16:41:26 -07:00
|
|
|
private static String generateName(ChannelHandler handler) {
|
2012-05-15 14:08:42 +09:00
|
|
|
String type = handler.getClass().getSimpleName();
|
|
|
|
StringBuilder buf = new StringBuilder(type.length() + 10);
|
|
|
|
buf.append(type);
|
|
|
|
buf.append("-0");
|
|
|
|
buf.append(Long.toHexString(System.identityHashCode(handler) & 0xFFFFFFFFL | 0x100000000L));
|
|
|
|
buf.setCharAt(buf.length() - 9, 'x');
|
|
|
|
return buf.toString();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-12-14 17:06:31 +01:00
|
|
|
public ChannelPipeline remove(ChannelHandler handler) {
|
2013-01-24 18:58:05 +01:00
|
|
|
remove(getContextOrDie(handler), false);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelPipeline removeAndForward(ChannelHandler handler) {
|
|
|
|
remove(getContextOrDie(handler), true);
|
2012-12-14 17:06:31 +01:00
|
|
|
return this;
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-06-05 11:21:44 +02:00
|
|
|
public ChannelHandler remove(String name) {
|
2013-01-24 18:58:05 +01:00
|
|
|
return remove(getContextOrDie(name), false).handler();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelHandler removeAndForward(String name) {
|
|
|
|
return remove(getContextOrDie(name), true).handler();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 22:33:11 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-06-05 11:21:44 +02:00
|
|
|
public <T extends ChannelHandler> T remove(Class<T> handlerType) {
|
2013-01-24 18:58:05 +01:00
|
|
|
return (T) remove(getContextOrDie(handlerType), false).handler();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2013-01-24 18:58:05 +01:00
|
|
|
private DefaultChannelHandlerContext remove(final DefaultChannelHandlerContext ctx, final boolean forward) {
|
2013-01-09 19:13:43 +09:00
|
|
|
assert ctx != head && ctx != tail;
|
|
|
|
|
2012-11-12 14:55:05 -08:00
|
|
|
DefaultChannelHandlerContext context;
|
2012-11-16 06:04:37 +09:00
|
|
|
Future<?> future;
|
2012-11-12 14:55:05 -08:00
|
|
|
|
|
|
|
synchronized (this) {
|
2013-01-09 19:13:43 +09:00
|
|
|
if (!ctx.channel().isRegistered() || ctx.executor().inEventLoop()) {
|
2013-01-24 18:58:05 +01:00
|
|
|
remove0(ctx, forward);
|
2013-01-09 19:13:43 +09:00
|
|
|
return ctx;
|
2012-11-12 14:55:05 -08:00
|
|
|
} else {
|
2013-01-09 19:13:43 +09:00
|
|
|
future = ctx.executor().submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
synchronized (DefaultChannelPipeline.this) {
|
2013-01-24 18:58:05 +01:00
|
|
|
remove0(ctx, forward);
|
2013-01-09 19:13:43 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
context = ctx;
|
2012-06-04 20:34:09 +02:00
|
|
|
}
|
2012-11-12 14:55:05 -08:00
|
|
|
}
|
2012-06-06 23:02:47 +09:00
|
|
|
|
2012-11-12 14:55:05 -08:00
|
|
|
// Run the following 'waiting' code outside of the above synchronized block
|
|
|
|
// in order to avoid deadlock
|
2012-06-06 23:02:47 +09:00
|
|
|
|
2012-11-16 06:04:37 +09:00
|
|
|
waitForFuture(future);
|
2012-06-06 23:02:47 +09:00
|
|
|
|
2012-11-12 14:55:05 -08:00
|
|
|
return context;
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
2012-06-04 11:56:00 -07:00
|
|
|
|
2013-01-24 18:58:05 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@Override
|
|
|
|
public <T extends ChannelHandler> T removeAndForward(Class<T> handlerType) {
|
|
|
|
return (T) remove(getContextOrDie(handlerType), true).handler();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void remove0(DefaultChannelHandlerContext ctx, boolean forward) {
|
2012-06-04 20:34:09 +02:00
|
|
|
callBeforeRemove(ctx);
|
|
|
|
|
|
|
|
DefaultChannelHandlerContext prev = ctx.prev;
|
|
|
|
DefaultChannelHandlerContext next = ctx.next;
|
|
|
|
prev.next = next;
|
|
|
|
next.prev = prev;
|
|
|
|
name2ctx.remove(ctx.name());
|
|
|
|
|
2013-01-24 18:58:05 +01:00
|
|
|
callAfterRemove(ctx, forward);
|
2012-06-04 20:34:09 +02:00
|
|
|
}
|
2008-08-08 00:37:18 +00:00
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-06-05 11:21:44 +02:00
|
|
|
public ChannelHandler removeFirst() {
|
2013-01-09 19:13:43 +09:00
|
|
|
if (head.next == tail) {
|
2008-08-18 02:38:54 +00:00
|
|
|
throw new NoSuchElementException();
|
|
|
|
}
|
2013-01-24 18:58:05 +01:00
|
|
|
return remove(head.next, false).handler();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-06-05 11:21:44 +02:00
|
|
|
public ChannelHandler removeLast() {
|
2013-01-09 19:13:43 +09:00
|
|
|
if (head.next == tail) {
|
|
|
|
throw new NoSuchElementException();
|
2012-06-04 20:34:09 +02:00
|
|
|
}
|
2013-01-24 18:58:05 +01:00
|
|
|
return remove(tail.prev, false).handler();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-12-14 17:06:31 +01:00
|
|
|
public ChannelPipeline replace(ChannelHandler oldHandler, String newName, ChannelHandler newHandler) {
|
2013-01-24 18:58:05 +01:00
|
|
|
replace(getContextOrDie(oldHandler), newName, newHandler, false);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelPipeline replaceAndForward(ChannelHandler oldHandler, String newName, ChannelHandler newHandler) {
|
|
|
|
replace(getContextOrDie(oldHandler), newName, newHandler, true);
|
2012-12-14 17:06:31 +01:00
|
|
|
return this;
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-06-05 11:21:44 +02:00
|
|
|
public ChannelHandler replace(String oldName, String newName, ChannelHandler newHandler) {
|
2013-01-24 18:58:05 +01:00
|
|
|
return replace(getContextOrDie(oldName), newName, newHandler, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelHandler replaceAndForward(String oldName, String newName, ChannelHandler newHandler) {
|
|
|
|
return replace(getContextOrDie(oldName), newName, newHandler, true);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
2012-06-05 11:21:44 +02:00
|
|
|
public <T extends ChannelHandler> T replace(
|
2008-08-08 00:37:18 +00:00
|
|
|
Class<T> oldHandlerType, String newName, ChannelHandler newHandler) {
|
2013-01-24 18:58:05 +01:00
|
|
|
return (T) replace(getContextOrDie(oldHandlerType), newName, newHandler, false);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 19:28:12 +09:00
|
|
|
private ChannelHandler replace(
|
2013-01-24 18:58:05 +01:00
|
|
|
final DefaultChannelHandlerContext ctx, final String newName,
|
|
|
|
ChannelHandler newHandler, final boolean forward) {
|
2013-01-09 19:13:43 +09:00
|
|
|
|
|
|
|
assert ctx != head && ctx != tail;
|
|
|
|
|
2012-11-16 06:04:37 +09:00
|
|
|
Future<?> future;
|
2012-11-12 14:55:05 -08:00
|
|
|
synchronized (this) {
|
2013-01-09 19:13:43 +09:00
|
|
|
boolean sameName = ctx.name().equals(newName);
|
|
|
|
if (!sameName) {
|
|
|
|
checkDuplicateName(newName);
|
2013-01-07 08:44:16 +01:00
|
|
|
}
|
2012-06-05 11:21:44 +02:00
|
|
|
|
2013-01-09 19:13:43 +09:00
|
|
|
final DefaultChannelHandlerContext newCtx =
|
2013-01-09 20:36:16 +09:00
|
|
|
new DefaultChannelHandlerContext(this, ctx.executor, newName, newHandler);
|
2012-06-05 11:21:44 +02:00
|
|
|
|
2013-01-09 19:13:43 +09:00
|
|
|
if (!newCtx.channel().isRegistered() || newCtx.executor().inEventLoop()) {
|
2013-01-24 18:58:05 +01:00
|
|
|
replace0(ctx, newName, newCtx, forward);
|
|
|
|
|
2013-01-09 19:13:43 +09:00
|
|
|
return ctx.handler();
|
|
|
|
} else {
|
|
|
|
future = newCtx.executor().submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
synchronized (DefaultChannelPipeline.this) {
|
2013-01-24 18:58:05 +01:00
|
|
|
replace0(ctx, newName, newCtx, forward);
|
2013-01-09 19:13:43 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
2012-11-12 14:55:05 -08:00
|
|
|
}
|
2012-06-06 23:02:47 +09:00
|
|
|
|
2012-11-12 14:55:05 -08:00
|
|
|
// Run the following 'waiting' code outside of the above synchronized block
|
|
|
|
// in order to avoid deadlock
|
2012-06-05 11:21:44 +02:00
|
|
|
|
2012-11-16 06:04:37 +09:00
|
|
|
waitForFuture(future);
|
2012-11-12 14:55:05 -08:00
|
|
|
|
|
|
|
return ctx.handler();
|
2012-06-04 20:34:09 +02:00
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
|
2013-01-24 18:58:05 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@Override
|
|
|
|
public <T extends ChannelHandler> T replaceAndForward(
|
|
|
|
Class<T> oldHandlerType, String newName, ChannelHandler newHandler) {
|
|
|
|
return (T) replace(getContextOrDie(oldHandlerType), newName, newHandler, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void replace0(DefaultChannelHandlerContext ctx, String newName,
|
|
|
|
DefaultChannelHandlerContext newCtx, boolean forward) {
|
2012-06-04 20:34:09 +02:00
|
|
|
boolean sameName = ctx.name().equals(newName);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
2012-06-04 20:34:09 +02:00
|
|
|
DefaultChannelHandlerContext prev = ctx.prev;
|
|
|
|
DefaultChannelHandlerContext next = ctx.next;
|
2013-01-09 20:34:22 +09:00
|
|
|
newCtx.prev = prev;
|
|
|
|
newCtx.next = next;
|
2012-06-04 11:56:00 -07:00
|
|
|
|
2012-06-04 20:34:09 +02:00
|
|
|
callBeforeRemove(ctx);
|
|
|
|
callBeforeAdd(newCtx);
|
|
|
|
|
|
|
|
prev.next = newCtx;
|
|
|
|
next.prev = newCtx;
|
|
|
|
|
|
|
|
if (!sameName) {
|
|
|
|
name2ctx.remove(ctx.name());
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
2012-06-04 20:34:09 +02:00
|
|
|
name2ctx.put(newName, newCtx);
|
2008-12-01 10:07:54 +00:00
|
|
|
|
2012-12-21 17:10:36 +01:00
|
|
|
ChannelPipelineException removeException = null;
|
|
|
|
ChannelPipelineException addException = null;
|
2012-06-04 20:34:09 +02:00
|
|
|
boolean removed = false;
|
|
|
|
try {
|
2013-01-24 18:58:05 +01:00
|
|
|
callAfterRemove(ctx, forward);
|
2012-06-04 20:34:09 +02:00
|
|
|
removed = true;
|
2012-12-21 17:10:36 +01:00
|
|
|
} catch (ChannelPipelineException e) {
|
2012-06-04 20:34:09 +02:00
|
|
|
removeException = e;
|
|
|
|
}
|
2008-08-08 00:37:18 +00:00
|
|
|
|
2012-06-04 20:34:09 +02:00
|
|
|
boolean added = false;
|
|
|
|
try {
|
|
|
|
callAfterAdd(newCtx);
|
|
|
|
added = true;
|
2012-12-21 17:10:36 +01:00
|
|
|
} catch (ChannelPipelineException e) {
|
2012-06-04 20:34:09 +02:00
|
|
|
addException = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!removed && !added) {
|
|
|
|
logger.warn(removeException.getMessage(), removeException);
|
|
|
|
logger.warn(addException.getMessage(), addException);
|
2012-12-21 17:10:36 +01:00
|
|
|
throw new ChannelPipelineException(
|
2012-06-04 20:34:09 +02:00
|
|
|
"Both " + ctx.handler().getClass().getName() +
|
|
|
|
".afterRemove() and " + newCtx.handler().getClass().getName() +
|
|
|
|
".afterAdd() failed; see logs.");
|
|
|
|
} else if (!removed) {
|
|
|
|
throw removeException;
|
|
|
|
} else if (!added) {
|
|
|
|
throw addException;
|
|
|
|
}
|
|
|
|
}
|
2012-06-04 11:56:00 -07:00
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
private static void callBeforeAdd(ChannelHandlerContext ctx) {
|
2012-05-31 14:54:48 -07:00
|
|
|
ChannelHandler handler = ctx.handler();
|
2013-02-06 12:55:42 +09:00
|
|
|
if (handler instanceof ChannelHandlerAdapter) {
|
|
|
|
ChannelHandlerAdapter h = (ChannelHandlerAdapter) handler;
|
2012-05-31 14:54:48 -07:00
|
|
|
if (!h.isSharable() && h.added) {
|
2012-12-21 17:10:36 +01:00
|
|
|
throw new ChannelPipelineException(
|
2012-11-18 23:11:39 +13:00
|
|
|
h.getClass().getName() +
|
|
|
|
" is not a @Sharable handler, so can't be added or removed multiple times.");
|
2012-05-31 14:54:48 -07:00
|
|
|
}
|
|
|
|
h.added = true;
|
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
try {
|
2012-05-31 14:54:48 -07:00
|
|
|
handler.beforeAdd(ctx);
|
2008-12-01 10:07:54 +00:00
|
|
|
} catch (Throwable t) {
|
2012-12-21 17:10:36 +01:00
|
|
|
throw new ChannelPipelineException(
|
2012-05-31 14:54:48 -07:00
|
|
|
handler.getClass().getName() +
|
2008-12-01 10:07:54 +00:00
|
|
|
".beforeAdd() has thrown an exception; not adding.", t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void callAfterAdd(ChannelHandlerContext ctx) {
|
|
|
|
try {
|
2012-05-01 17:19:41 +09:00
|
|
|
ctx.handler().afterAdd(ctx);
|
2008-12-01 10:07:54 +00:00
|
|
|
} catch (Throwable t) {
|
|
|
|
boolean removed = false;
|
|
|
|
try {
|
2013-01-24 18:58:05 +01:00
|
|
|
remove((DefaultChannelHandlerContext) ctx, false);
|
2008-12-01 10:07:54 +00:00
|
|
|
removed = true;
|
|
|
|
} catch (Throwable t2) {
|
2012-02-17 10:37:41 +01:00
|
|
|
if (logger.isWarnEnabled()) {
|
2012-05-01 17:19:41 +09:00
|
|
|
logger.warn("Failed to remove a handler: " + ctx.name(), t2);
|
2012-02-17 10:37:41 +01:00
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (removed) {
|
2012-12-21 17:10:36 +01:00
|
|
|
throw new ChannelPipelineException(
|
2012-05-01 17:19:41 +09:00
|
|
|
ctx.handler().getClass().getName() +
|
2008-12-01 10:07:54 +00:00
|
|
|
".afterAdd() has thrown an exception; removed.", t);
|
|
|
|
} else {
|
2012-12-21 17:10:36 +01:00
|
|
|
throw new ChannelPipelineException(
|
2012-05-01 17:19:41 +09:00
|
|
|
ctx.handler().getClass().getName() +
|
2008-12-01 10:07:54 +00:00
|
|
|
".afterAdd() has thrown an exception; also failed to remove.", t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
private static void callBeforeRemove(ChannelHandlerContext ctx) {
|
2008-12-01 10:07:54 +00:00
|
|
|
try {
|
2012-05-01 17:19:41 +09:00
|
|
|
ctx.handler().beforeRemove(ctx);
|
2008-12-01 10:07:54 +00:00
|
|
|
} catch (Throwable t) {
|
2012-12-21 17:10:36 +01:00
|
|
|
throw new ChannelPipelineException(
|
2012-05-01 17:19:41 +09:00
|
|
|
ctx.handler().getClass().getName() +
|
2008-12-01 10:07:54 +00:00
|
|
|
".beforeRemove() has thrown an exception; not removing.", t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 15:33:09 -08:00
|
|
|
private void callAfterRemove(final DefaultChannelHandlerContext ctx, boolean forward) {
|
2013-01-14 21:43:25 +09:00
|
|
|
final ChannelHandler handler = ctx.handler();
|
2013-01-14 20:48:58 +09:00
|
|
|
|
|
|
|
// Notify the complete removal.
|
2008-12-01 10:07:54 +00:00
|
|
|
try {
|
2013-01-14 20:48:58 +09:00
|
|
|
handler.afterRemove(ctx);
|
2008-12-01 10:07:54 +00:00
|
|
|
} catch (Throwable t) {
|
2012-12-21 17:10:36 +01:00
|
|
|
throw new ChannelPipelineException(
|
2012-05-01 17:19:41 +09:00
|
|
|
ctx.handler().getClass().getName() +
|
2008-12-01 10:07:54 +00:00
|
|
|
".afterRemove() has thrown an exception.", t);
|
|
|
|
}
|
2013-01-14 21:43:25 +09:00
|
|
|
|
2013-01-24 18:58:05 +01:00
|
|
|
if (forward) {
|
|
|
|
ctx.forwardBufferContent();
|
2013-01-14 21:49:01 +09:00
|
|
|
} else {
|
2013-01-24 18:58:05 +01:00
|
|
|
ctx.clearBuffer();
|
2013-01-14 21:49:01 +09:00
|
|
|
}
|
2013-01-24 18:58:05 +01:00
|
|
|
|
2013-01-23 07:27:00 +01:00
|
|
|
ctx.removed = true;
|
2013-01-14 21:49:01 +09:00
|
|
|
|
2013-01-24 18:58:05 +01:00
|
|
|
// Free all buffers before completing removal.
|
|
|
|
if (!channel.isRegistered()) {
|
|
|
|
ctx.freeHandlerBuffersAfterRemoval();
|
2013-01-14 21:49:01 +09:00
|
|
|
}
|
2008-12-01 10:07:54 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-08-28 13:03:41 +09:00
|
|
|
public ChannelHandler first() {
|
2012-06-03 18:51:42 -07:00
|
|
|
DefaultChannelHandlerContext first = head.next;
|
|
|
|
if (first == null) {
|
2008-08-18 02:27:11 +00:00
|
|
|
return null;
|
|
|
|
}
|
2012-06-03 18:51:42 -07:00
|
|
|
return first.handler();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-08-28 13:03:41 +09:00
|
|
|
public ChannelHandlerContext firstContext() {
|
|
|
|
return head.next;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelHandler last() {
|
2013-01-09 19:13:43 +09:00
|
|
|
DefaultChannelHandlerContext last = tail.prev;
|
|
|
|
if (last == head) {
|
2008-08-18 02:27:11 +00:00
|
|
|
return null;
|
|
|
|
}
|
2012-06-03 18:51:42 -07:00
|
|
|
return last.handler();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-08-28 13:03:41 +09:00
|
|
|
public ChannelHandlerContext lastContext() {
|
2013-01-09 19:13:43 +09:00
|
|
|
DefaultChannelHandlerContext last = tail.prev;
|
|
|
|
if (last == head) {
|
2012-08-28 13:03:41 +09:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelHandler get(String name) {
|
|
|
|
ChannelHandlerContext ctx = context(name);
|
2008-08-08 00:37:18 +00:00
|
|
|
if (ctx == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
2012-05-01 17:19:41 +09:00
|
|
|
return ctx.handler();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-21 22:33:11 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-08-28 13:03:41 +09:00
|
|
|
public <T extends ChannelHandler> T get(Class<T> handlerType) {
|
2012-05-01 17:19:41 +09:00
|
|
|
ChannelHandlerContext ctx = context(handlerType);
|
2008-08-08 00:37:18 +00:00
|
|
|
if (ctx == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
2012-05-01 17:19:41 +09:00
|
|
|
return (T) ctx.handler();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-08-28 13:03:41 +09:00
|
|
|
public ChannelHandlerContext context(String name) {
|
2008-08-08 00:37:18 +00:00
|
|
|
if (name == null) {
|
|
|
|
throw new NullPointerException("name");
|
|
|
|
}
|
2012-08-28 13:03:41 +09:00
|
|
|
|
|
|
|
synchronized (this) {
|
|
|
|
return name2ctx.get(name);
|
|
|
|
}
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-08-28 13:03:41 +09:00
|
|
|
public ChannelHandlerContext context(ChannelHandler handler) {
|
2008-08-08 00:37:18 +00:00
|
|
|
if (handler == null) {
|
|
|
|
throw new NullPointerException("handler");
|
|
|
|
}
|
2012-08-28 13:03:41 +09:00
|
|
|
|
|
|
|
DefaultChannelHandlerContext ctx = head.next;
|
2008-08-08 00:37:18 +00:00
|
|
|
for (;;) {
|
2013-01-07 08:44:16 +01:00
|
|
|
|
2012-08-28 13:03:41 +09:00
|
|
|
if (ctx == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
if (ctx.handler() == handler) {
|
2008-08-08 00:37:18 +00:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = ctx.next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-08-28 13:03:41 +09:00
|
|
|
public ChannelHandlerContext context(Class<? extends ChannelHandler> handlerType) {
|
2009-12-17 10:57:57 +00:00
|
|
|
if (handlerType == null) {
|
|
|
|
throw new NullPointerException("handlerType");
|
|
|
|
}
|
|
|
|
|
2012-06-03 18:51:42 -07:00
|
|
|
DefaultChannelHandlerContext ctx = head.next;
|
2008-08-08 00:37:18 +00:00
|
|
|
for (;;) {
|
2012-08-28 13:03:41 +09:00
|
|
|
if (ctx == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2012-05-01 17:19:41 +09:00
|
|
|
if (handlerType.isAssignableFrom(ctx.handler().getClass())) {
|
2008-08-08 00:37:18 +00:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
ctx = ctx.next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-02 08:43:10 +09:00
|
|
|
@Override
|
2012-05-01 17:19:41 +09:00
|
|
|
public List<String> names() {
|
2011-08-02 08:43:10 +09:00
|
|
|
List<String> list = new ArrayList<String>();
|
2012-06-03 18:51:42 -07:00
|
|
|
DefaultChannelHandlerContext ctx = head.next;
|
2011-08-02 08:43:10 +09:00
|
|
|
for (;;) {
|
|
|
|
if (ctx == null) {
|
2012-08-28 13:03:41 +09:00
|
|
|
return list;
|
2011-08-02 08:43:10 +09:00
|
|
|
}
|
2012-08-28 13:03:41 +09:00
|
|
|
list.add(ctx.name());
|
|
|
|
ctx = ctx.next;
|
2011-08-02 08:43:10 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2008-08-08 00:37:18 +00:00
|
|
|
public Map<String, ChannelHandler> toMap() {
|
|
|
|
Map<String, ChannelHandler> map = new LinkedHashMap<String, ChannelHandler>();
|
2012-06-03 18:51:42 -07:00
|
|
|
DefaultChannelHandlerContext ctx = head.next;
|
2008-08-08 00:37:18 +00:00
|
|
|
for (;;) {
|
2013-01-09 19:13:43 +09:00
|
|
|
if (ctx == tail) {
|
2012-08-28 13:03:41 +09:00
|
|
|
return map;
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
2012-08-28 13:03:41 +09:00
|
|
|
map.put(ctx.name(), ctx.handler());
|
|
|
|
ctx = ctx.next;
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-03 04:09:46 +00:00
|
|
|
/**
|
|
|
|
* Returns the {@link String} representation of this pipeline.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
StringBuilder buf = new StringBuilder();
|
|
|
|
buf.append(getClass().getSimpleName());
|
|
|
|
buf.append('{');
|
2012-06-03 18:51:42 -07:00
|
|
|
DefaultChannelHandlerContext ctx = head.next;
|
2008-09-03 04:09:46 +00:00
|
|
|
for (;;) {
|
2013-01-09 19:16:09 +09:00
|
|
|
if (ctx == tail) {
|
2012-08-28 13:03:41 +09:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-09-03 04:09:46 +00:00
|
|
|
buf.append('(');
|
2012-05-01 17:19:41 +09:00
|
|
|
buf.append(ctx.name());
|
2008-09-03 04:09:46 +00:00
|
|
|
buf.append(" = ");
|
2012-05-01 17:19:41 +09:00
|
|
|
buf.append(ctx.handler().getClass().getName());
|
2008-09-03 04:09:46 +00:00
|
|
|
buf.append(')');
|
2012-08-28 13:03:41 +09:00
|
|
|
|
2008-09-03 04:09:46 +00:00
|
|
|
ctx = ctx.next;
|
2013-01-09 19:13:43 +09:00
|
|
|
if (ctx == tail) {
|
2008-09-03 04:09:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-08-28 13:03:41 +09:00
|
|
|
|
2008-09-03 04:09:46 +00:00
|
|
|
buf.append(", ");
|
|
|
|
}
|
|
|
|
buf.append('}');
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2012-11-30 22:49:51 +09:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public <T> MessageBuf<T> inboundMessageBuffer() {
|
|
|
|
return (MessageBuf<T>) head.nextInboundMessageBuffer();
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-06-10 11:08:43 +09:00
|
|
|
public ByteBuf inboundByteBuffer() {
|
2012-06-08 23:11:15 +09:00
|
|
|
return head.nextInboundByteBuffer();
|
2012-06-01 17:51:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-11-30 22:49:51 +09:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public <T> MessageBuf<T> outboundMessageBuffer() {
|
2013-01-15 16:23:09 +09:00
|
|
|
return (MessageBuf<T>) tail.nextOutboundMessageBuffer();
|
2012-06-01 17:51:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-06-10 11:08:43 +09:00
|
|
|
public ByteBuf outboundByteBuffer() {
|
2013-01-15 16:23:09 +09:00
|
|
|
return tail.nextOutboundByteBuffer();
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
2013-02-21 15:19:42 -08:00
|
|
|
boolean isInboundShutdown() {
|
|
|
|
return inboundShutdown;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean isOutboundShutdown() {
|
|
|
|
return outboundShutdown;
|
|
|
|
}
|
|
|
|
|
|
|
|
void shutdownInbound() {
|
|
|
|
inboundShutdown = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void shutdownOutbound() {
|
|
|
|
outboundShutdown = true;
|
|
|
|
}
|
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
@Override
|
2013-02-11 09:44:04 +01:00
|
|
|
public ChannelPipeline fireChannelRegistered() {
|
2012-06-08 23:11:15 +09:00
|
|
|
head.fireChannelRegistered();
|
2013-02-11 09:44:04 +01:00
|
|
|
return this;
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-02-11 09:44:04 +01:00
|
|
|
public ChannelPipeline fireChannelUnregistered() {
|
2012-06-08 23:11:15 +09:00
|
|
|
head.fireChannelUnregistered();
|
2012-11-16 06:04:37 +09:00
|
|
|
|
|
|
|
// Free all buffers if channel is closed and unregistered.
|
|
|
|
if (!channel.isOpen()) {
|
2013-01-15 16:23:09 +09:00
|
|
|
head.invokeFreeInboundBuffer();
|
2012-11-16 06:04:37 +09:00
|
|
|
}
|
2013-02-11 09:44:04 +01:00
|
|
|
return this;
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-02-11 09:44:04 +01:00
|
|
|
public ChannelPipeline fireChannelActive() {
|
2012-06-08 23:11:15 +09:00
|
|
|
firedChannelActive = true;
|
|
|
|
head.fireChannelActive();
|
Read only when requested (read-on-demand)
This pull request introduces a new operation called read() that replaces the existing inbound traffic control method. EventLoop now performs socket reads only when the read() operation has been issued. Once the requested read() operation is actually performed, EventLoop triggers an inboundBufferSuspended event that tells the handlers that the requested read() operation has been performed and the inbound traffic has been suspended again. A handler can decide to continue reading or not.
Unlike other outbound operations, read() does not use ChannelFuture at all to avoid GC cost. If there's a good reason to create a new future per read at the GC cost, I'll change this.
This pull request consequently removes the readable property in ChannelHandlerContext, which means how the traffic control works changed significantly.
This pull request also adds a new configuration property ChannelOption.AUTO_READ whose default value is true. If true, Netty will call ctx.read() for you. If you need a close control over when read() is called, you can set it to false.
Another interesting fact is that non-terminal handlers do not really need to call read() at all. Only the last inbound handler will have to call it, and that's just enough. Actually, you don't even need to call it at the last handler in most cases because of the ChannelOption.AUTO_READ mentioned above.
There's no serious backward compatibility issue. If the compiler complains your handler does not implement the read() method, add the following:
public void read(ChannelHandlerContext ctx) throws Exception {
ctx.read();
}
Note that this pull request certainly makes bounded inbound buffer support very easy, but itself does not add the bounded inbound buffer support.
2012-12-30 21:53:59 +09:00
|
|
|
|
|
|
|
if (channel.config().isAutoRead()) {
|
|
|
|
channel.read();
|
|
|
|
}
|
|
|
|
|
2012-06-08 23:11:15 +09:00
|
|
|
if (fireInboundBufferUpdatedOnActivation) {
|
|
|
|
fireInboundBufferUpdatedOnActivation = false;
|
|
|
|
head.fireInboundBufferUpdated();
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
2013-02-11 09:44:04 +01:00
|
|
|
return this;
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
2012-06-01 17:51:19 -07:00
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
@Override
|
2013-02-11 09:44:04 +01:00
|
|
|
public ChannelPipeline fireChannelInactive() {
|
2012-06-08 23:11:15 +09:00
|
|
|
// Some implementations such as EmbeddedChannel can trigger inboundBufferUpdated()
|
|
|
|
// after deactivation, so it's safe not to revert the firedChannelActive flag here.
|
|
|
|
// Also, all known transports never get re-activated.
|
|
|
|
//firedChannelActive = false;
|
|
|
|
head.fireChannelInactive();
|
2013-02-11 09:44:04 +01:00
|
|
|
return this;
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-02-11 09:44:04 +01:00
|
|
|
public ChannelPipeline fireExceptionCaught(Throwable cause) {
|
2012-06-08 23:11:15 +09:00
|
|
|
head.fireExceptionCaught(cause);
|
2013-02-11 09:44:04 +01:00
|
|
|
return this;
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
2008-08-08 00:37:18 +00:00
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
@Override
|
2013-02-11 09:44:04 +01:00
|
|
|
public ChannelPipeline fireUserEventTriggered(Object event) {
|
2012-06-08 23:11:15 +09:00
|
|
|
head.fireUserEventTriggered(event);
|
2013-02-11 09:44:04 +01:00
|
|
|
return this;
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:45:39 +09:00
|
|
|
@Override
|
2013-02-11 09:44:04 +01:00
|
|
|
public ChannelPipeline fireInboundBufferUpdated() {
|
2012-05-30 11:32:39 -07:00
|
|
|
if (!firedChannelActive) {
|
|
|
|
fireInboundBufferUpdatedOnActivation = true;
|
2013-02-11 09:44:04 +01:00
|
|
|
return this;
|
2012-05-30 11:32:39 -07:00
|
|
|
}
|
2012-06-08 23:11:15 +09:00
|
|
|
head.fireInboundBufferUpdated();
|
2013-02-11 09:44:04 +01:00
|
|
|
return this;
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
Read only when requested (read-on-demand)
This pull request introduces a new operation called read() that replaces the existing inbound traffic control method. EventLoop now performs socket reads only when the read() operation has been issued. Once the requested read() operation is actually performed, EventLoop triggers an inboundBufferSuspended event that tells the handlers that the requested read() operation has been performed and the inbound traffic has been suspended again. A handler can decide to continue reading or not.
Unlike other outbound operations, read() does not use ChannelFuture at all to avoid GC cost. If there's a good reason to create a new future per read at the GC cost, I'll change this.
This pull request consequently removes the readable property in ChannelHandlerContext, which means how the traffic control works changed significantly.
This pull request also adds a new configuration property ChannelOption.AUTO_READ whose default value is true. If true, Netty will call ctx.read() for you. If you need a close control over when read() is called, you can set it to false.
Another interesting fact is that non-terminal handlers do not really need to call read() at all. Only the last inbound handler will have to call it, and that's just enough. Actually, you don't even need to call it at the last handler in most cases because of the ChannelOption.AUTO_READ mentioned above.
There's no serious backward compatibility issue. If the compiler complains your handler does not implement the read() method, add the following:
public void read(ChannelHandlerContext ctx) throws Exception {
ctx.read();
}
Note that this pull request certainly makes bounded inbound buffer support very easy, but itself does not add the bounded inbound buffer support.
2012-12-30 21:53:59 +09:00
|
|
|
@Override
|
2013-02-11 09:44:04 +01:00
|
|
|
public ChannelPipeline fireChannelReadSuspended() {
|
2013-02-06 12:55:42 +09:00
|
|
|
head.fireChannelReadSuspended();
|
Read only when requested (read-on-demand)
This pull request introduces a new operation called read() that replaces the existing inbound traffic control method. EventLoop now performs socket reads only when the read() operation has been issued. Once the requested read() operation is actually performed, EventLoop triggers an inboundBufferSuspended event that tells the handlers that the requested read() operation has been performed and the inbound traffic has been suspended again. A handler can decide to continue reading or not.
Unlike other outbound operations, read() does not use ChannelFuture at all to avoid GC cost. If there's a good reason to create a new future per read at the GC cost, I'll change this.
This pull request consequently removes the readable property in ChannelHandlerContext, which means how the traffic control works changed significantly.
This pull request also adds a new configuration property ChannelOption.AUTO_READ whose default value is true. If true, Netty will call ctx.read() for you. If you need a close control over when read() is called, you can set it to false.
Another interesting fact is that non-terminal handlers do not really need to call read() at all. Only the last inbound handler will have to call it, and that's just enough. Actually, you don't even need to call it at the last handler in most cases because of the ChannelOption.AUTO_READ mentioned above.
There's no serious backward compatibility issue. If the compiler complains your handler does not implement the read() method, add the following:
public void read(ChannelHandlerContext ctx) throws Exception {
ctx.read();
}
Note that this pull request certainly makes bounded inbound buffer support very easy, but itself does not add the bounded inbound buffer support.
2012-12-30 21:53:59 +09:00
|
|
|
if (channel.config().isAutoRead()) {
|
2013-01-15 16:23:09 +09:00
|
|
|
read();
|
Read only when requested (read-on-demand)
This pull request introduces a new operation called read() that replaces the existing inbound traffic control method. EventLoop now performs socket reads only when the read() operation has been issued. Once the requested read() operation is actually performed, EventLoop triggers an inboundBufferSuspended event that tells the handlers that the requested read() operation has been performed and the inbound traffic has been suspended again. A handler can decide to continue reading or not.
Unlike other outbound operations, read() does not use ChannelFuture at all to avoid GC cost. If there's a good reason to create a new future per read at the GC cost, I'll change this.
This pull request consequently removes the readable property in ChannelHandlerContext, which means how the traffic control works changed significantly.
This pull request also adds a new configuration property ChannelOption.AUTO_READ whose default value is true. If true, Netty will call ctx.read() for you. If you need a close control over when read() is called, you can set it to false.
Another interesting fact is that non-terminal handlers do not really need to call read() at all. Only the last inbound handler will have to call it, and that's just enough. Actually, you don't even need to call it at the last handler in most cases because of the ChannelOption.AUTO_READ mentioned above.
There's no serious backward compatibility issue. If the compiler complains your handler does not implement the read() method, add the following:
public void read(ChannelHandlerContext ctx) throws Exception {
ctx.read();
}
Note that this pull request certainly makes bounded inbound buffer support very easy, but itself does not add the bounded inbound buffer support.
2012-12-30 21:53:59 +09:00
|
|
|
}
|
2013-02-11 09:44:04 +01:00
|
|
|
return this;
|
Read only when requested (read-on-demand)
This pull request introduces a new operation called read() that replaces the existing inbound traffic control method. EventLoop now performs socket reads only when the read() operation has been issued. Once the requested read() operation is actually performed, EventLoop triggers an inboundBufferSuspended event that tells the handlers that the requested read() operation has been performed and the inbound traffic has been suspended again. A handler can decide to continue reading or not.
Unlike other outbound operations, read() does not use ChannelFuture at all to avoid GC cost. If there's a good reason to create a new future per read at the GC cost, I'll change this.
This pull request consequently removes the readable property in ChannelHandlerContext, which means how the traffic control works changed significantly.
This pull request also adds a new configuration property ChannelOption.AUTO_READ whose default value is true. If true, Netty will call ctx.read() for you. If you need a close control over when read() is called, you can set it to false.
Another interesting fact is that non-terminal handlers do not really need to call read() at all. Only the last inbound handler will have to call it, and that's just enough. Actually, you don't even need to call it at the last handler in most cases because of the ChannelOption.AUTO_READ mentioned above.
There's no serious backward compatibility issue. If the compiler complains your handler does not implement the read() method, add the following:
public void read(ChannelHandlerContext ctx) throws Exception {
ctx.read();
}
Note that this pull request certainly makes bounded inbound buffer support very easy, but itself does not add the bounded inbound buffer support.
2012-12-30 21:53:59 +09:00
|
|
|
}
|
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
@Override
|
2012-05-09 22:09:06 +09:00
|
|
|
public ChannelFuture bind(SocketAddress localAddress) {
|
2013-01-15 16:23:09 +09:00
|
|
|
return tail.bind(localAddress);
|
2012-05-09 22:09:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture connect(SocketAddress remoteAddress) {
|
2013-01-15 16:23:09 +09:00
|
|
|
return tail.connect(remoteAddress);
|
2012-05-09 22:09:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress) {
|
2013-01-15 16:23:09 +09:00
|
|
|
return tail.connect(remoteAddress, localAddress);
|
2012-05-09 22:09:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture disconnect() {
|
2013-01-15 16:23:09 +09:00
|
|
|
return tail.disconnect();
|
2012-05-09 22:09:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture close() {
|
2013-01-15 16:23:09 +09:00
|
|
|
return tail.close();
|
2012-05-09 22:09:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture deregister() {
|
2013-01-15 16:23:09 +09:00
|
|
|
return tail.deregister();
|
2012-05-09 22:09:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture flush() {
|
2013-01-15 16:23:09 +09:00
|
|
|
return tail.flush();
|
2012-05-09 22:09:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelFuture write(Object message) {
|
2013-01-15 16:23:09 +09:00
|
|
|
return tail.write(message);
|
2012-05-09 22:09:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-12-30 17:40:24 +01:00
|
|
|
public ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise) {
|
2013-01-15 16:23:09 +09:00
|
|
|
return tail.bind(localAddress, promise);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-12-30 17:40:24 +01:00
|
|
|
public ChannelFuture connect(SocketAddress remoteAddress, ChannelPromise promise) {
|
2013-01-15 16:23:09 +09:00
|
|
|
return tail.connect(remoteAddress, promise);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-12-30 17:40:24 +01:00
|
|
|
public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {
|
2013-01-15 16:23:09 +09:00
|
|
|
return tail.connect(remoteAddress, localAddress, promise);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-12-30 17:40:24 +01:00
|
|
|
public ChannelFuture disconnect(ChannelPromise promise) {
|
2013-01-15 16:23:09 +09:00
|
|
|
return tail.disconnect(promise);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-12-30 17:40:24 +01:00
|
|
|
public ChannelFuture close(ChannelPromise promise) {
|
2013-01-15 16:23:09 +09:00
|
|
|
return tail.close(promise);
|
2012-05-01 17:19:41 +09:00
|
|
|
}
|
2008-08-08 00:37:18 +00:00
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
@Override
|
2012-12-30 17:40:24 +01:00
|
|
|
public ChannelFuture deregister(final ChannelPromise promise) {
|
2013-01-15 16:23:09 +09:00
|
|
|
return tail.deregister(promise);
|
2012-10-24 18:27:26 +02:00
|
|
|
}
|
Read only when requested (read-on-demand)
This pull request introduces a new operation called read() that replaces the existing inbound traffic control method. EventLoop now performs socket reads only when the read() operation has been issued. Once the requested read() operation is actually performed, EventLoop triggers an inboundBufferSuspended event that tells the handlers that the requested read() operation has been performed and the inbound traffic has been suspended again. A handler can decide to continue reading or not.
Unlike other outbound operations, read() does not use ChannelFuture at all to avoid GC cost. If there's a good reason to create a new future per read at the GC cost, I'll change this.
This pull request consequently removes the readable property in ChannelHandlerContext, which means how the traffic control works changed significantly.
This pull request also adds a new configuration property ChannelOption.AUTO_READ whose default value is true. If true, Netty will call ctx.read() for you. If you need a close control over when read() is called, you can set it to false.
Another interesting fact is that non-terminal handlers do not really need to call read() at all. Only the last inbound handler will have to call it, and that's just enough. Actually, you don't even need to call it at the last handler in most cases because of the ChannelOption.AUTO_READ mentioned above.
There's no serious backward compatibility issue. If the compiler complains your handler does not implement the read() method, add the following:
public void read(ChannelHandlerContext ctx) throws Exception {
ctx.read();
}
Note that this pull request certainly makes bounded inbound buffer support very easy, but itself does not add the bounded inbound buffer support.
2012-12-30 21:53:59 +09:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void read() {
|
2013-01-15 16:23:09 +09:00
|
|
|
tail.read();
|
Read only when requested (read-on-demand)
This pull request introduces a new operation called read() that replaces the existing inbound traffic control method. EventLoop now performs socket reads only when the read() operation has been issued. Once the requested read() operation is actually performed, EventLoop triggers an inboundBufferSuspended event that tells the handlers that the requested read() operation has been performed and the inbound traffic has been suspended again. A handler can decide to continue reading or not.
Unlike other outbound operations, read() does not use ChannelFuture at all to avoid GC cost. If there's a good reason to create a new future per read at the GC cost, I'll change this.
This pull request consequently removes the readable property in ChannelHandlerContext, which means how the traffic control works changed significantly.
This pull request also adds a new configuration property ChannelOption.AUTO_READ whose default value is true. If true, Netty will call ctx.read() for you. If you need a close control over when read() is called, you can set it to false.
Another interesting fact is that non-terminal handlers do not really need to call read() at all. Only the last inbound handler will have to call it, and that's just enough. Actually, you don't even need to call it at the last handler in most cases because of the ChannelOption.AUTO_READ mentioned above.
There's no serious backward compatibility issue. If the compiler complains your handler does not implement the read() method, add the following:
public void read(ChannelHandlerContext ctx) throws Exception {
ctx.read();
}
Note that this pull request certainly makes bounded inbound buffer support very easy, but itself does not add the bounded inbound buffer support.
2012-12-30 21:53:59 +09:00
|
|
|
}
|
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
@Override
|
2012-12-30 17:40:24 +01:00
|
|
|
public ChannelFuture flush(ChannelPromise promise) {
|
2013-01-15 16:23:09 +09:00
|
|
|
return tail.flush(promise);
|
2012-05-09 22:09:06 +09:00
|
|
|
}
|
|
|
|
|
2013-01-15 16:23:09 +09:00
|
|
|
@Override
|
|
|
|
public ChannelFuture sendFile(FileRegion region) {
|
|
|
|
return tail.sendFile(region);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 16:23:09 +09:00
|
|
|
@Override
|
|
|
|
public ChannelFuture sendFile(FileRegion region, ChannelPromise promise) {
|
|
|
|
return tail.sendFile(region, promise);
|
2012-06-01 18:34:19 -07:00
|
|
|
}
|
|
|
|
|
2012-05-01 17:19:41 +09:00
|
|
|
@Override
|
2012-12-30 17:40:24 +01:00
|
|
|
public ChannelFuture write(Object message, ChannelPromise promise) {
|
2013-01-15 16:23:09 +09:00
|
|
|
return tail.write(message, promise);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void checkDuplicateName(String name) {
|
|
|
|
if (name2ctx.containsKey(name)) {
|
2011-11-23 14:07:26 +09:00
|
|
|
throw new IllegalArgumentException("Duplicate handler name: " + name);
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private DefaultChannelHandlerContext getContextOrDie(String name) {
|
2012-05-01 17:19:41 +09:00
|
|
|
DefaultChannelHandlerContext ctx = (DefaultChannelHandlerContext) context(name);
|
2013-01-09 19:16:09 +09:00
|
|
|
if (ctx == null) {
|
2008-08-08 00:37:18 +00:00
|
|
|
throw new NoSuchElementException(name);
|
|
|
|
} else {
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private DefaultChannelHandlerContext getContextOrDie(ChannelHandler handler) {
|
2012-05-01 17:19:41 +09:00
|
|
|
DefaultChannelHandlerContext ctx = (DefaultChannelHandlerContext) context(handler);
|
2013-01-09 19:16:09 +09:00
|
|
|
if (ctx == null) {
|
2008-08-08 00:37:18 +00:00
|
|
|
throw new NoSuchElementException(handler.getClass().getName());
|
|
|
|
} else {
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private DefaultChannelHandlerContext getContextOrDie(Class<? extends ChannelHandler> handlerType) {
|
2012-05-01 17:19:41 +09:00
|
|
|
DefaultChannelHandlerContext ctx = (DefaultChannelHandlerContext) context(handlerType);
|
2013-01-09 19:16:09 +09:00
|
|
|
if (ctx == null) {
|
2008-08-08 00:37:18 +00:00
|
|
|
throw new NoSuchElementException(handlerType.getName());
|
|
|
|
} else {
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
}
|
2012-06-03 18:51:42 -07:00
|
|
|
|
2013-02-06 12:55:42 +09:00
|
|
|
// A special catch-all handler that handles both bytes and messages.
|
|
|
|
static final class TailHandler implements ChannelInboundHandler {
|
|
|
|
|
|
|
|
final ByteBuf byteSink = Unpooled.buffer(0);
|
|
|
|
final MessageBuf<Object> msgSink = Unpooled.messageBuffer(0);
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void channelRegistered(ChannelHandlerContext ctx) throws Exception { }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void channelActive(ChannelHandlerContext ctx) throws Exception { }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception { }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void channelReadSuspended(ChannelHandlerContext ctx) throws Exception { }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void beforeAdd(ChannelHandlerContext ctx) throws Exception { }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterAdd(ChannelHandlerContext ctx) throws Exception { }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void beforeRemove(ChannelHandlerContext ctx) throws Exception { }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterRemove(ChannelHandlerContext ctx) throws Exception { }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
|
logger.warn(
|
|
|
|
"An exceptionCaught() event was fired, and it reached at the end of the pipeline. " +
|
|
|
|
"It usually means the last handler in the pipeline did not handle the exception.", cause);
|
2013-01-07 08:44:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-02-06 12:55:42 +09:00
|
|
|
public Buf newInboundBuffer(ChannelHandlerContext ctx) throws Exception {
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
|
2013-02-10 13:10:09 +09:00
|
|
|
byteSink.release();
|
|
|
|
msgSink.release();
|
2013-02-06 12:55:42 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void inboundBufferUpdated(ChannelHandlerContext ctx) throws Exception {
|
|
|
|
int byteSinkSize = byteSink.readableBytes();
|
|
|
|
if (byteSinkSize != 0) {
|
|
|
|
byteSink.clear();
|
|
|
|
logger.warn(
|
|
|
|
"Discarded {} inbound byte(s) that reached at the end of the pipeline. " +
|
|
|
|
"Please check your pipeline configuration.", byteSinkSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
int msgSinkSize = msgSink.size();
|
|
|
|
if (msgSinkSize != 0) {
|
|
|
|
MessageBuf<Object> in = msgSink;
|
|
|
|
for (;;) {
|
|
|
|
Object m = in.poll();
|
|
|
|
if (m == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-02-10 13:10:09 +09:00
|
|
|
if (m instanceof ReferenceCounted) {
|
|
|
|
((ReferenceCounted) m).release();
|
2013-02-06 12:55:42 +09:00
|
|
|
}
|
2013-02-19 19:08:04 +01:00
|
|
|
logger.debug(
|
|
|
|
"Discarded inbound message {} that reached at the end of the pipeline. " +
|
|
|
|
"Please check your pipeline configuration.", m);
|
2013-02-06 12:55:42 +09:00
|
|
|
}
|
|
|
|
logger.warn(
|
|
|
|
"Discarded {} inbound message(s) that reached at the end of the pipeline. " +
|
|
|
|
"Please check your pipeline configuration.", msgSinkSize);
|
2013-01-07 08:44:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 00:00:14 +09:00
|
|
|
abstract static class HeadHandler implements ChannelOutboundHandler {
|
2013-02-06 12:55:42 +09:00
|
|
|
|
2013-02-07 23:58:21 +09:00
|
|
|
protected final Unsafe unsafe;
|
2013-02-06 12:55:42 +09:00
|
|
|
ByteBuf byteSink;
|
|
|
|
MessageBuf<Object> msgSink;
|
|
|
|
|
2013-02-07 23:58:21 +09:00
|
|
|
protected HeadHandler(Unsafe unsafe) {
|
|
|
|
this.unsafe = unsafe;
|
|
|
|
}
|
|
|
|
|
2013-02-06 12:55:42 +09:00
|
|
|
void init(ChannelHandlerContext ctx) {
|
|
|
|
switch (ctx.channel().metadata().bufferType()) {
|
|
|
|
case BYTE:
|
|
|
|
byteSink = ctx.alloc().ioBuffer();
|
|
|
|
msgSink = Unpooled.messageBuffer(0);
|
|
|
|
break;
|
|
|
|
case MESSAGE:
|
|
|
|
byteSink = Unpooled.buffer(0);
|
|
|
|
msgSink = Unpooled.messageBuffer();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-16 06:04:37 +09:00
|
|
|
@Override
|
2013-01-05 15:04:25 +09:00
|
|
|
public final void beforeAdd(ChannelHandlerContext ctx) throws Exception {
|
2012-06-03 18:51:42 -07:00
|
|
|
// NOOP
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-01-05 15:04:25 +09:00
|
|
|
public final void afterAdd(ChannelHandlerContext ctx) throws Exception {
|
2012-06-03 18:51:42 -07:00
|
|
|
// NOOP
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-01-05 15:04:25 +09:00
|
|
|
public final void beforeRemove(ChannelHandlerContext ctx) throws Exception {
|
2012-06-03 18:51:42 -07:00
|
|
|
// NOOP
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-01-05 15:04:25 +09:00
|
|
|
public final void afterRemove(ChannelHandlerContext ctx) throws Exception {
|
2012-06-03 18:51:42 -07:00
|
|
|
// NOOP
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-01-05 15:04:25 +09:00
|
|
|
public final void bind(
|
2012-12-30 17:40:24 +01:00
|
|
|
ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise)
|
2012-06-03 18:51:42 -07:00
|
|
|
throws Exception {
|
2012-12-30 17:40:24 +01:00
|
|
|
unsafe.bind(localAddress, promise);
|
2012-06-03 18:51:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-01-05 15:04:25 +09:00
|
|
|
public final void connect(
|
2012-06-07 14:52:33 +09:00
|
|
|
ChannelHandlerContext ctx,
|
2012-06-03 18:51:42 -07:00
|
|
|
SocketAddress remoteAddress, SocketAddress localAddress,
|
2012-12-30 17:40:24 +01:00
|
|
|
ChannelPromise promise) throws Exception {
|
|
|
|
unsafe.connect(remoteAddress, localAddress, promise);
|
2012-06-03 18:51:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-01-05 15:04:25 +09:00
|
|
|
public final void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
|
2012-12-30 17:40:24 +01:00
|
|
|
unsafe.disconnect(promise);
|
2012-06-03 18:51:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-01-05 15:04:25 +09:00
|
|
|
public final void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
|
2012-12-30 17:40:24 +01:00
|
|
|
unsafe.close(promise);
|
2012-06-03 18:51:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-01-05 15:04:25 +09:00
|
|
|
public final void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
|
2012-12-30 17:40:24 +01:00
|
|
|
unsafe.deregister(promise);
|
2012-06-03 18:51:42 -07:00
|
|
|
}
|
|
|
|
|
Read only when requested (read-on-demand)
This pull request introduces a new operation called read() that replaces the existing inbound traffic control method. EventLoop now performs socket reads only when the read() operation has been issued. Once the requested read() operation is actually performed, EventLoop triggers an inboundBufferSuspended event that tells the handlers that the requested read() operation has been performed and the inbound traffic has been suspended again. A handler can decide to continue reading or not.
Unlike other outbound operations, read() does not use ChannelFuture at all to avoid GC cost. If there's a good reason to create a new future per read at the GC cost, I'll change this.
This pull request consequently removes the readable property in ChannelHandlerContext, which means how the traffic control works changed significantly.
This pull request also adds a new configuration property ChannelOption.AUTO_READ whose default value is true. If true, Netty will call ctx.read() for you. If you need a close control over when read() is called, you can set it to false.
Another interesting fact is that non-terminal handlers do not really need to call read() at all. Only the last inbound handler will have to call it, and that's just enough. Actually, you don't even need to call it at the last handler in most cases because of the ChannelOption.AUTO_READ mentioned above.
There's no serious backward compatibility issue. If the compiler complains your handler does not implement the read() method, add the following:
public void read(ChannelHandlerContext ctx) throws Exception {
ctx.read();
}
Note that this pull request certainly makes bounded inbound buffer support very easy, but itself does not add the bounded inbound buffer support.
2012-12-30 21:53:59 +09:00
|
|
|
@Override
|
2013-01-05 15:04:25 +09:00
|
|
|
public final void read(ChannelHandlerContext ctx) {
|
Read only when requested (read-on-demand)
This pull request introduces a new operation called read() that replaces the existing inbound traffic control method. EventLoop now performs socket reads only when the read() operation has been issued. Once the requested read() operation is actually performed, EventLoop triggers an inboundBufferSuspended event that tells the handlers that the requested read() operation has been performed and the inbound traffic has been suspended again. A handler can decide to continue reading or not.
Unlike other outbound operations, read() does not use ChannelFuture at all to avoid GC cost. If there's a good reason to create a new future per read at the GC cost, I'll change this.
This pull request consequently removes the readable property in ChannelHandlerContext, which means how the traffic control works changed significantly.
This pull request also adds a new configuration property ChannelOption.AUTO_READ whose default value is true. If true, Netty will call ctx.read() for you. If you need a close control over when read() is called, you can set it to false.
Another interesting fact is that non-terminal handlers do not really need to call read() at all. Only the last inbound handler will have to call it, and that's just enough. Actually, you don't even need to call it at the last handler in most cases because of the ChannelOption.AUTO_READ mentioned above.
There's no serious backward compatibility issue. If the compiler complains your handler does not implement the read() method, add the following:
public void read(ChannelHandlerContext ctx) throws Exception {
ctx.read();
}
Note that this pull request certainly makes bounded inbound buffer support very easy, but itself does not add the bounded inbound buffer support.
2012-12-30 21:53:59 +09:00
|
|
|
unsafe.beginRead();
|
|
|
|
}
|
|
|
|
|
2012-10-24 18:27:26 +02:00
|
|
|
@Override
|
2013-01-05 15:04:25 +09:00
|
|
|
public final void sendFile(
|
|
|
|
ChannelHandlerContext ctx, FileRegion region, ChannelPromise promise) throws Exception {
|
2012-12-30 17:40:24 +01:00
|
|
|
unsafe.sendFile(region, promise);
|
2012-10-24 18:27:26 +02:00
|
|
|
}
|
2013-01-05 15:04:25 +09:00
|
|
|
|
|
|
|
@Override
|
2013-02-06 12:55:42 +09:00
|
|
|
public final Buf newOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
|
|
|
|
throw new Error();
|
2013-01-05 15:04:25 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-02-06 12:55:42 +09:00
|
|
|
public final void freeOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
|
2013-02-10 13:10:09 +09:00
|
|
|
msgSink.release();
|
|
|
|
byteSink.release();
|
2013-01-05 15:04:25 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-07 23:58:21 +09:00
|
|
|
private static final class ByteHeadHandler extends HeadHandler {
|
|
|
|
|
|
|
|
private ByteHeadHandler(Unsafe unsafe) {
|
|
|
|
super(unsafe);
|
|
|
|
}
|
|
|
|
|
2013-01-05 15:04:25 +09:00
|
|
|
@Override
|
2013-02-06 12:55:42 +09:00
|
|
|
public void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
|
2013-02-08 01:36:41 +09:00
|
|
|
int discardedMessages = 0;
|
|
|
|
MessageBuf<Object> in = msgSink;
|
|
|
|
for (;;) {
|
|
|
|
Object m = in.poll();
|
|
|
|
if (m == null) {
|
|
|
|
break;
|
|
|
|
}
|
2013-02-06 12:55:42 +09:00
|
|
|
|
2013-02-08 01:36:41 +09:00
|
|
|
if (m instanceof ByteBuf) {
|
|
|
|
ByteBuf src = (ByteBuf) m;
|
|
|
|
byteSink.writeBytes(src, src.readerIndex(), src.readableBytes());
|
|
|
|
} else {
|
2013-02-19 19:08:04 +01:00
|
|
|
logger.debug(
|
|
|
|
"Discarded outbound message {} that reached at the end of the pipeline. " +
|
|
|
|
"Please check your pipeline configuration.", m);
|
2013-02-08 01:36:41 +09:00
|
|
|
discardedMessages ++;
|
|
|
|
}
|
|
|
|
|
2013-02-10 13:10:09 +09:00
|
|
|
if (m instanceof ReferenceCounted) {
|
|
|
|
((ReferenceCounted) m).release();
|
2013-02-06 12:55:42 +09:00
|
|
|
}
|
2013-02-08 01:36:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if (discardedMessages != 0) {
|
2013-02-06 12:55:42 +09:00
|
|
|
logger.warn(
|
|
|
|
"Discarded {} outbound message(s) that reached at the end of the pipeline. " +
|
2013-02-08 01:36:41 +09:00
|
|
|
"Please check your pipeline configuration.", discardedMessages);
|
2013-02-06 12:55:42 +09:00
|
|
|
}
|
2013-02-08 01:36:41 +09:00
|
|
|
|
2013-02-06 12:55:42 +09:00
|
|
|
unsafe.flush(promise);
|
2013-01-05 15:04:25 +09:00
|
|
|
}
|
2013-02-06 12:55:42 +09:00
|
|
|
}
|
2013-01-05 15:04:25 +09:00
|
|
|
|
2013-02-07 23:58:21 +09:00
|
|
|
private static final class MessageHeadHandler extends HeadHandler {
|
|
|
|
|
|
|
|
private MessageHeadHandler(Unsafe unsafe) {
|
|
|
|
super(unsafe);
|
|
|
|
}
|
|
|
|
|
2013-01-05 15:04:25 +09:00
|
|
|
@Override
|
2013-02-06 12:55:42 +09:00
|
|
|
public void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
|
|
|
|
int byteSinkSize = byteSink.readableBytes();
|
|
|
|
if (byteSinkSize != 0) {
|
|
|
|
byteSink.clear();
|
|
|
|
logger.warn(
|
|
|
|
"Discarded {} outbound byte(s) that reached at the end of the pipeline. " +
|
|
|
|
"Please check your pipeline configuration.", byteSinkSize);
|
|
|
|
}
|
|
|
|
unsafe.flush(promise);
|
2013-01-05 15:04:25 +09:00
|
|
|
}
|
|
|
|
}
|
2013-02-08 07:10:46 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterator<Map.Entry<String, ChannelHandler>> iterator() {
|
|
|
|
return toMap().entrySet().iterator();
|
|
|
|
}
|
2012-06-08 10:57:38 +09:00
|
|
|
}
|