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-03-05 21:41:19 +01:00
|
|
|
import io.netty.util.concurrent.EventExecutor;
|
|
|
|
import io.netty.util.concurrent.EventExecutorGroup;
|
2013-03-14 15:01:35 +09:00
|
|
|
import io.netty.util.internal.PlatformDependent;
|
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;
|
2013-03-07 12:43:16 +09:00
|
|
|
import java.util.WeakHashMap;
|
2013-03-14 15:01:35 +09:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2012-06-05 11:21:44 +02:00
|
|
|
import java.util.concurrent.Future;
|
2008-08-08 00:37:18 +00:00
|
|
|
|
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
|
|
|
|
2013-03-07 12:43:16 +09:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private static final WeakHashMap<Class<?>, String>[] nameCaches =
|
|
|
|
new WeakHashMap[Runtime.getRuntime().availableProcessors()];
|
|
|
|
|
|
|
|
static {
|
|
|
|
for (int i = 0; i < nameCaches.length; i ++) {
|
|
|
|
nameCaches[i] = new WeakHashMap<Class<?>, String>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-04 11:56:00 -07:00
|
|
|
|
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
|
|
|
|
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) {
|
|
|
|
synchronized (this) {
|
|
|
|
checkDuplicateName(name);
|
2013-04-05 15:46:18 +02:00
|
|
|
DefaultChannelHandlerContext newCtx = new DefaultChannelHandlerContext(this, group, name, handler);
|
|
|
|
addFirst0(name, newCtx);
|
2012-11-12 14:55:05 -08:00
|
|
|
}
|
2012-06-05 11:21:44 +02: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) {
|
2013-04-24 19:02:34 +09:00
|
|
|
checkMultiplicity(newCtx);
|
|
|
|
|
2013-01-09 19:13:43 +09:00
|
|
|
DefaultChannelHandlerContext nextCtx = head.next;
|
|
|
|
newCtx.prev = head;
|
|
|
|
newCtx.next = nextCtx;
|
2013-01-09 20:34:22 +09:00
|
|
|
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
|
|
|
|
2013-04-24 18:55:51 +09:00
|
|
|
callHandlerAdded(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) {
|
|
|
|
synchronized (this) {
|
|
|
|
checkDuplicateName(name);
|
2012-06-05 11:21:44 +02:00
|
|
|
|
2013-04-05 15:46:18 +02:00
|
|
|
DefaultChannelHandlerContext newCtx = new DefaultChannelHandlerContext(this, group, name, handler);
|
|
|
|
addLast0(name, newCtx);
|
2012-06-04 20:34:09 +02: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
|
|
|
|
2013-04-24 19:02:34 +09:00
|
|
|
private void addLast0(final String name, DefaultChannelHandlerContext newCtx) {
|
|
|
|
checkMultiplicity(newCtx);
|
|
|
|
|
2013-01-09 19:13:43 +09:00
|
|
|
DefaultChannelHandlerContext prev = tail.prev;
|
|
|
|
newCtx.prev = prev;
|
|
|
|
newCtx.next = tail;
|
2013-01-09 20:34:22 +09:00
|
|
|
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-04-24 18:55:51 +09:00
|
|
|
callHandlerAdded(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) {
|
|
|
|
synchronized (this) {
|
2013-04-05 15:46:18 +02:00
|
|
|
DefaultChannelHandlerContext ctx = getContextOrDie(baseName);
|
2012-11-12 14:55:05 -08:00
|
|
|
checkDuplicateName(name);
|
2013-04-05 15:46:18 +02:00
|
|
|
DefaultChannelHandlerContext newCtx = new DefaultChannelHandlerContext(this, group, name, handler);
|
|
|
|
addBefore0(name, ctx, newCtx);
|
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-04-24 19:02:34 +09:00
|
|
|
checkMultiplicity(newCtx);
|
2013-01-09 20:34:22 +09:00
|
|
|
|
|
|
|
newCtx.prev = ctx.prev;
|
|
|
|
newCtx.next = ctx;
|
2012-06-03 18:51:42 -07:00
|
|
|
ctx.prev.next = newCtx;
|
|
|
|
ctx.prev = newCtx;
|
2013-04-24 19:02:34 +09:00
|
|
|
|
2012-06-03 18:51:42 -07:00
|
|
|
name2ctx.put(name, newCtx);
|
2012-05-15 14:08:42 +09:00
|
|
|
|
2013-04-24 18:55:51 +09:00
|
|
|
callHandlerAdded(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) {
|
|
|
|
synchronized (this) {
|
2013-04-05 15:46:18 +02:00
|
|
|
DefaultChannelHandlerContext ctx = getContextOrDie(baseName);
|
2012-11-12 14:55:05 -08:00
|
|
|
checkDuplicateName(name);
|
2013-04-05 15:46:18 +02:00
|
|
|
DefaultChannelHandlerContext newCtx = new DefaultChannelHandlerContext(this, group, name, handler);
|
2012-06-06 23:02:47 +09:00
|
|
|
|
2013-04-05 15:46:18 +02:00
|
|
|
addAfter0(name, ctx, newCtx);
|
2012-11-12 14:55:05 -08: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-04-24 19:02:34 +09:00
|
|
|
checkMultiplicity(newCtx);
|
2012-06-04 20:34:09 +02:00
|
|
|
|
2013-01-09 19:13:43 +09:00
|
|
|
newCtx.prev = ctx;
|
|
|
|
newCtx.next = ctx.next;
|
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);
|
|
|
|
|
2013-04-24 18:55:51 +09:00
|
|
|
callHandlerAdded(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;
|
|
|
|
}
|
|
|
|
|
2013-03-07 12:43:16 +09:00
|
|
|
private String generateName(ChannelHandler handler) {
|
|
|
|
WeakHashMap<Class<?>, String> cache = nameCaches[(int) (Thread.currentThread().getId() % nameCaches.length)];
|
|
|
|
Class<?> handlerType = handler.getClass();
|
|
|
|
String name;
|
|
|
|
synchronized (cache) {
|
|
|
|
name = cache.get(handlerType);
|
|
|
|
if (name == null) {
|
|
|
|
Package pkg = handlerType.getPackage();
|
|
|
|
if (pkg != null) {
|
|
|
|
name = handlerType.getName().substring(pkg.getName().length() + 1);
|
|
|
|
} else {
|
|
|
|
name = handlerType.getName();
|
|
|
|
}
|
|
|
|
name += "#0";
|
|
|
|
cache.put(handlerType, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized (this) {
|
|
|
|
// It's not very likely for a user to put more than one handler of the same type, but make sure to avoid
|
|
|
|
// any name conflicts. Note that we don't cache the names generated here.
|
|
|
|
if (name2ctx.containsKey(name)) {
|
|
|
|
String baseName = name.substring(0, name.length() - 1); // Strip the trailing '0'.
|
|
|
|
for (int i = 1;; i ++) {
|
|
|
|
String newName = baseName + i;
|
|
|
|
if (!name2ctx.containsKey(newName)) {
|
|
|
|
name = newName;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
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-04-13 18:19:33 +02:00
|
|
|
remove(getContextOrDie(handler));
|
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-04-13 18:19:33 +02:00
|
|
|
return remove(getContextOrDie(name)).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-04-13 18:19:33 +02:00
|
|
|
return (T) remove(getContextOrDie(handlerType)).handler();
|
2008-08-08 00:37:18 +00:00
|
|
|
}
|
|
|
|
|
2013-04-13 18:19:33 +02:00
|
|
|
private DefaultChannelHandlerContext remove(final DefaultChannelHandlerContext ctx) {
|
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-04-13 18:19:33 +02:00
|
|
|
remove0(ctx);
|
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-04-13 18:19:33 +02:00
|
|
|
remove0(ctx);
|
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-04-13 18:19:33 +02:00
|
|
|
private void remove0(DefaultChannelHandlerContext ctx) {
|
2012-06-04 20:34:09 +02:00
|
|
|
DefaultChannelHandlerContext prev = ctx.prev;
|
|
|
|
DefaultChannelHandlerContext next = ctx.next;
|
|
|
|
prev.next = next;
|
|
|
|
next.prev = prev;
|
|
|
|
name2ctx.remove(ctx.name());
|
|
|
|
|
2013-04-24 18:55:51 +09:00
|
|
|
callHandlerRemoved(ctx, prev, next);
|
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-04-13 18:19:33 +02:00
|
|
|
return remove(head.next).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-04-13 18:19:33 +02:00
|
|
|
return remove(tail.prev).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-04-13 18:19:33 +02:00
|
|
|
replace(getContextOrDie(oldHandler), newName, newHandler);
|
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-04-13 18:19:33 +02:00
|
|
|
return replace(getContextOrDie(oldName), newName, newHandler);
|
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-04-13 18:19:33 +02:00
|
|
|
return (T) replace(getContextOrDie(oldHandlerType), newName, newHandler);
|
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,
|
2013-04-13 18:19:33 +02:00
|
|
|
ChannelHandler newHandler) {
|
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-04-13 18:19:33 +02:00
|
|
|
replace0(ctx, newName, newCtx);
|
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-04-13 18:19:33 +02:00
|
|
|
replace0(ctx, newName, newCtx);
|
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
|
|
|
private void replace0(DefaultChannelHandlerContext ctx, String newName,
|
2013-04-13 18:19:33 +02:00
|
|
|
DefaultChannelHandlerContext newCtx) {
|
2013-04-24 18:55:51 +09:00
|
|
|
checkMultiplicity(newCtx);
|
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 20:34:09 +02:00
|
|
|
prev.next = newCtx;
|
|
|
|
next.prev = newCtx;
|
|
|
|
|
2013-04-24 19:02:34 +09:00
|
|
|
if (!ctx.name().equals(newName)) {
|
2012-06-04 20:34:09 +02:00
|
|
|
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
|
|
|
|
2013-04-05 15:46:18 +02:00
|
|
|
// remove old and add new
|
2013-04-24 18:55:51 +09:00
|
|
|
callHandlerRemoved(ctx, newCtx, newCtx);
|
|
|
|
callHandlerAdded(newCtx);
|
2012-06-04 20:34:09 +02:00
|
|
|
}
|
2012-06-04 11:56:00 -07:00
|
|
|
|
2013-04-24 18:55:51 +09:00
|
|
|
private static void checkMultiplicity(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(
|
2013-04-24 18:55:51 +09:00
|
|
|
h.getClass().getName() +
|
2012-11-18 23:11:39 +13:00
|
|
|
" 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;
|
|
|
|
}
|
2013-04-05 15:46:18 +02:00
|
|
|
}
|
|
|
|
|
2013-04-24 18:55:51 +09:00
|
|
|
private void callHandlerAdded(final ChannelHandlerContext ctx) {
|
2013-04-05 15:46:18 +02:00
|
|
|
if (ctx.channel().isRegistered() && !ctx.executor().inEventLoop()) {
|
|
|
|
ctx.executor().execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2013-04-24 18:55:51 +09:00
|
|
|
callHandlerAdded0(ctx);
|
2013-04-05 15:46:18 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
2008-12-01 10:07:54 +00:00
|
|
|
}
|
2013-04-24 18:55:51 +09:00
|
|
|
callHandlerAdded0(ctx);
|
2008-12-01 10:07:54 +00:00
|
|
|
}
|
|
|
|
|
2013-04-24 18:55:51 +09:00
|
|
|
private void callHandlerAdded0(final ChannelHandlerContext ctx) {
|
2008-12-01 10:07:54 +00:00
|
|
|
try {
|
2013-04-05 15:46:18 +02:00
|
|
|
ctx.handler().handlerAdded(ctx);
|
2008-12-01 10:07:54 +00:00
|
|
|
} catch (Throwable t) {
|
|
|
|
boolean removed = false;
|
|
|
|
try {
|
2013-04-13 18:19:33 +02:00
|
|
|
remove((DefaultChannelHandlerContext) ctx);
|
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) {
|
2013-04-05 15:46:18 +02:00
|
|
|
fireExceptionCaught(new ChannelPipelineException(
|
2012-05-01 17:19:41 +09:00
|
|
|
ctx.handler().getClass().getName() +
|
2013-04-24 18:55:51 +09:00
|
|
|
".handlerAdded() has thrown an exception; removed.", t));
|
2008-12-01 10:07:54 +00:00
|
|
|
} else {
|
2013-04-05 15:46:18 +02:00
|
|
|
fireExceptionCaught(new ChannelPipelineException(
|
2012-05-01 17:19:41 +09:00
|
|
|
ctx.handler().getClass().getName() +
|
2013-04-24 18:55:51 +09:00
|
|
|
".handlerAdded() has thrown an exception; also failed to remove.", t));
|
2008-12-01 10:07:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-24 18:55:51 +09:00
|
|
|
private void callHandlerRemoved(
|
2013-04-05 15:46:18 +02:00
|
|
|
final DefaultChannelHandlerContext ctx, final DefaultChannelHandlerContext ctxPrev,
|
|
|
|
final DefaultChannelHandlerContext ctxNext) {
|
|
|
|
if (ctx.channel().isRegistered() && !ctx.executor().inEventLoop()) {
|
|
|
|
ctx.executor().execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2013-04-24 18:55:51 +09:00
|
|
|
callHandlerRemoved0(ctx, ctxPrev, ctxNext);
|
2013-04-05 15:46:18 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
2008-12-01 10:07:54 +00:00
|
|
|
}
|
2013-04-24 18:55:51 +09:00
|
|
|
callHandlerRemoved0(ctx, ctxPrev, ctxNext);
|
2008-12-01 10:07:54 +00:00
|
|
|
}
|
|
|
|
|
2013-04-24 18:55:51 +09:00
|
|
|
private void callHandlerRemoved0(
|
2013-03-14 15:01:35 +09:00
|
|
|
final DefaultChannelHandlerContext ctx, DefaultChannelHandlerContext ctxPrev,
|
2013-04-13 18:19:33 +02:00
|
|
|
DefaultChannelHandlerContext ctxNext) {
|
2013-03-14 15:01:35 +09:00
|
|
|
|
2013-01-14 21:43:25 +09:00
|
|
|
final ChannelHandler handler = ctx.handler();
|
2013-01-14 20:48:58 +09:00
|
|
|
|
2013-04-24 18:55:51 +09:00
|
|
|
// Finish removal by forwarding buffer content and freeing the buffers.
|
2008-12-01 10:07:54 +00:00
|
|
|
try {
|
2013-04-24 18:46:35 +09:00
|
|
|
ctx.forwardBufferContentAndRemove(ctxPrev, ctxNext);
|
2013-04-24 18:55:51 +09:00
|
|
|
} catch (Throwable t) {
|
|
|
|
fireExceptionCaught(new ChannelPipelineException(
|
|
|
|
"failed to forward buffer content of " + ctx.handler().getClass().getName(), t));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify the complete removal.
|
|
|
|
try {
|
2013-04-05 15:46:18 +02:00
|
|
|
handler.handlerRemoved(ctx);
|
2008-12-01 10:07:54 +00:00
|
|
|
} catch (Throwable t) {
|
2013-04-05 15:46:18 +02:00
|
|
|
fireExceptionCaught(new ChannelPipelineException(
|
2013-04-24 18:57:14 +09:00
|
|
|
ctx.handler().getClass().getName() + ".handlerRemoved() has thrown an exception.", t));
|
2008-12-01 10:07:54 +00:00
|
|
|
}
|
2013-03-14 15:01:35 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Waits for a future to finish. If the task is interrupted, then the current thread will be interrupted.
|
|
|
|
* It is expected that the task performs any appropriate locking.
|
|
|
|
* <p>
|
|
|
|
* If the internal call throws a {@link Throwable}, but it is not an instance of {@link Error} or
|
|
|
|
* {@link RuntimeException}, then it is wrapped inside a {@link ChannelPipelineException} and that is
|
|
|
|
* thrown instead.</p>
|
|
|
|
*
|
|
|
|
* @param future wait for this future
|
|
|
|
* @see Future#get()
|
|
|
|
* @throws Error if the task threw this.
|
|
|
|
* @throws RuntimeException if the task threw this.
|
|
|
|
* @throws ChannelPipelineException with a {@link Throwable} as a cause, if the task threw another type of
|
|
|
|
* {@link Throwable}.
|
|
|
|
*/
|
|
|
|
private static void waitForFuture(Future<?> future) {
|
|
|
|
try {
|
|
|
|
future.get();
|
|
|
|
} catch (ExecutionException ex) {
|
|
|
|
// In the arbitrary case, we can throw Error, RuntimeException, and Exception
|
|
|
|
PlatformDependent.throwException(ex.getCause());
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
// Interrupt the calling thread (note that this method is not called from the event loop)
|
|
|
|
Thread.currentThread().interrupt();
|
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;
|
2013-04-02 21:18:19 -07:00
|
|
|
if (first == head) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-07 12:43:16 +09:00
|
|
|
@Override
|
|
|
|
public Iterator<Map.Entry<String, ChannelHandler>> iterator() {
|
|
|
|
return toMap().entrySet().iterator();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-02-11 09:44:04 +01:00
|
|
|
public ChannelPipeline fireChannelRegistered() {
|
2013-03-14 14:35:56 +09:00
|
|
|
head.initHeadHandler();
|
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() {
|
2013-03-14 14:35:56 +09:00
|
|
|
head.initHeadHandler();
|
2012-06-08 23:11:15 +09:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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-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
|
2013-04-05 15:46:18 +02:00
|
|
|
public void handlerAdded(ChannelHandlerContext ctx) throws Exception { }
|
2013-02-06 12:55:42 +09:00
|
|
|
|
|
|
|
@Override
|
2013-04-05 15:46:18 +02:00
|
|
|
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { }
|
2013-02-06 12:55:42 +09:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
|
logger.warn(
|
2013-03-21 20:22:25 +09:00
|
|
|
"An exceptionCaught() event was fired, and it reached at the tail of the pipeline. " +
|
2013-02-06 12:55:42 +09:00
|
|
|
"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(
|
2013-03-21 20:22:25 +09:00
|
|
|
"Discarded {} inbound byte(s) that reached at the tail of the pipeline. " +
|
2013-02-06 12:55:42 +09:00
|
|
|
"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(
|
2013-03-21 20:22:25 +09:00
|
|
|
"Discarded inbound message {} that reached at the tail of the pipeline. " +
|
2013-02-19 19:08:04 +01:00
|
|
|
"Please check your pipeline configuration.", m);
|
2013-02-06 12:55:42 +09:00
|
|
|
}
|
|
|
|
logger.warn(
|
2013-03-21 20:22:25 +09:00
|
|
|
"Discarded {} inbound message(s) that reached at the tail of the pipeline. " +
|
2013-02-06 12:55:42 +09:00
|
|
|
"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-03-14 14:35:56 +09:00
|
|
|
boolean initialized;
|
2013-02-06 12:55:42 +09:00
|
|
|
|
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) {
|
2013-03-14 14:35:56 +09:00
|
|
|
assert !initialized;
|
2013-02-06 12:55:42 +09:00
|
|
|
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-04-05 15:46:18 +02:00
|
|
|
public final void handlerAdded(ChannelHandlerContext ctx) throws Exception {
|
2012-06-03 18:51:42 -07:00
|
|
|
// NOOP
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-04-05 15:46:18 +02:00
|
|
|
public final void handlerRemoved(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-03-05 07:34:34 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
|
ctx.fireExceptionCaught(cause);
|
|
|
|
}
|
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(
|
2013-03-21 20:22:25 +09:00
|
|
|
"Discarded outbound message {} that reached at the head of the pipeline. " +
|
2013-02-19 19:08:04 +01:00
|
|
|
"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(
|
2013-03-21 20:22:25 +09:00
|
|
|
"Discarded {} outbound message(s) that reached at the head 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(
|
2013-03-21 20:22:25 +09:00
|
|
|
"Discarded {} outbound byte(s) that reached at the head of the pipeline. " +
|
2013-02-06 12:55:42 +09:00
|
|
|
"Please check your pipeline configuration.", byteSinkSize);
|
|
|
|
}
|
|
|
|
unsafe.flush(promise);
|
2013-01-05 15:04:25 +09:00
|
|
|
}
|
|
|
|
}
|
2012-06-08 10:57:38 +09:00
|
|
|
}
|