2012-06-04 13:31:44 -07:00
|
|
|
/*
|
|
|
|
* Copyright 2012 The Netty Project
|
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2012-05-29 16:41:26 -07:00
|
|
|
package io.netty.bootstrap;
|
|
|
|
|
|
|
|
import io.netty.channel.Channel;
|
2013-05-08 06:57:52 +02:00
|
|
|
import io.netty.channel.ChannelConfig;
|
2013-09-26 08:12:48 -04:00
|
|
|
import io.netty.channel.ChannelException;
|
2012-05-29 16:41:26 -07:00
|
|
|
import io.netty.channel.ChannelHandler;
|
2012-06-07 14:52:33 +09:00
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
Remove MessageList from public API and change ChannelInbound/OutboundHandler accordingly
I must admit MesageList was pain in the ass. Instead of forcing a
handler always loop over the list of messages, this commit splits
messageReceived(ctx, list) into two event handlers:
- messageReceived(ctx, msg)
- mmessageReceivedLast(ctx)
When Netty reads one or more messages, messageReceived(ctx, msg) event
is triggered for each message. Once the current read operation is
finished, messageReceivedLast() is triggered to tell the handler that
the last messageReceived() was the last message in the current batch.
Similarly, for outbound, write(ctx, list) has been split into two:
- write(ctx, msg)
- flush(ctx, promise)
Instead of writing a list of message with a promise, a user is now
supposed to call write(msg) multiple times and then call flush() to
actually flush the buffered messages.
Please note that write() doesn't have a promise with it. You must call
flush() to get notified on completion. (or you can use writeAndFlush())
Other changes:
- Because MessageList is completely hidden, codec framework uses
List<Object> instead of MessageList as an output parameter.
2013-07-08 19:03:40 +09:00
|
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
2012-05-29 16:41:26 -07:00
|
|
|
import io.netty.channel.ChannelInitializer;
|
|
|
|
import io.netty.channel.ChannelOption;
|
2012-09-22 12:05:00 +09:00
|
|
|
import io.netty.channel.ChannelPipeline;
|
2013-09-26 08:12:48 -04:00
|
|
|
import io.netty.channel.EventLoop;
|
2012-08-10 20:17:18 +09:00
|
|
|
import io.netty.channel.EventLoopGroup;
|
2012-05-29 16:41:26 -07:00
|
|
|
import io.netty.channel.ServerChannel;
|
2012-09-11 10:04:05 +02:00
|
|
|
import io.netty.channel.socket.SocketChannel;
|
2012-10-26 14:52:50 -07:00
|
|
|
import io.netty.util.AttributeKey;
|
2013-11-04 19:42:33 +09:00
|
|
|
import io.netty.util.internal.StringUtil;
|
2013-02-26 14:54:25 -08:00
|
|
|
import io.netty.util.internal.logging.InternalLogger;
|
|
|
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
2012-05-14 23:57:23 +09:00
|
|
|
|
2013-09-26 08:12:48 -04:00
|
|
|
import java.lang.reflect.Constructor;
|
2012-05-14 23:57:23 +09:00
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
2013-05-08 06:57:52 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2012-05-14 23:57:23 +09:00
|
|
|
|
2012-09-11 10:04:05 +02:00
|
|
|
/**
|
|
|
|
* {@link Bootstrap} sub-class which allows easy bootstrap of {@link ServerChannel}
|
|
|
|
*
|
|
|
|
*/
|
2013-01-30 21:38:15 +01:00
|
|
|
public final class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, ServerChannel> {
|
2012-05-14 23:57:23 +09:00
|
|
|
|
2012-05-29 16:41:26 -07:00
|
|
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(ServerBootstrap.class);
|
2012-05-14 23:57:23 +09:00
|
|
|
|
2013-09-26 08:12:48 -04:00
|
|
|
private volatile ServerChannelFactory<? extends ServerChannel> channelFactory;
|
2012-05-14 23:57:23 +09:00
|
|
|
private final Map<ChannelOption<?>, Object> childOptions = new LinkedHashMap<ChannelOption<?>, Object>();
|
2012-10-26 14:52:50 -07:00
|
|
|
private final Map<AttributeKey<?>, Object> childAttrs = new LinkedHashMap<AttributeKey<?>, Object>();
|
2013-01-31 11:34:28 +09:00
|
|
|
private volatile EventLoopGroup childGroup;
|
|
|
|
private volatile ChannelHandler childHandler;
|
2012-05-14 23:57:23 +09:00
|
|
|
|
2013-01-30 21:12:02 +09:00
|
|
|
public ServerBootstrap() { }
|
|
|
|
|
2013-01-30 21:55:10 +09:00
|
|
|
private ServerBootstrap(ServerBootstrap bootstrap) {
|
2013-01-30 21:12:02 +09:00
|
|
|
super(bootstrap);
|
2013-09-26 08:12:48 -04:00
|
|
|
channelFactory = bootstrap.channelFactory;
|
2013-01-30 21:12:02 +09:00
|
|
|
childGroup = bootstrap.childGroup;
|
|
|
|
childHandler = bootstrap.childHandler;
|
2013-01-31 11:34:28 +09:00
|
|
|
synchronized (bootstrap.childOptions) {
|
|
|
|
childOptions.putAll(bootstrap.childOptions);
|
|
|
|
}
|
|
|
|
synchronized (bootstrap.childAttrs) {
|
|
|
|
childAttrs.putAll(bootstrap.childAttrs);
|
|
|
|
}
|
2013-01-30 21:12:02 +09:00
|
|
|
}
|
|
|
|
|
2013-09-26 08:12:48 -04:00
|
|
|
/**
|
|
|
|
* The {@link Class} which is used to create {@link Channel} instances from.
|
2013-11-04 19:42:33 +09:00
|
|
|
* You either use this or {@link #channelFactory(ServerChannelFactory)} if your
|
2013-09-26 08:12:48 -04:00
|
|
|
* {@link Channel} implementation has no no-args constructor.
|
|
|
|
*/
|
|
|
|
public ServerBootstrap channel(Class<? extends ServerChannel> channelClass) {
|
|
|
|
if (channelClass == null) {
|
|
|
|
throw new NullPointerException("channelClass");
|
|
|
|
}
|
|
|
|
return channelFactory(new ServerBootstrapChannelFactory<ServerChannel>(channelClass));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@link ChannelFactory} which is used to create {@link Channel} instances from
|
|
|
|
* when calling {@link #bind()}. This method is usually only used if {@link #channel(Class)}
|
|
|
|
* is not working for you because of some more complex needs. If your {@link Channel} implementation
|
|
|
|
* has a no-args constructor, its highly recommend to just use {@link #channel(Class)} for
|
|
|
|
* simplify your code.
|
|
|
|
*/
|
|
|
|
public ServerBootstrap channelFactory(ServerChannelFactory<? extends ServerChannel> channelFactory) {
|
|
|
|
if (channelFactory == null) {
|
|
|
|
throw new NullPointerException("channelFactory");
|
|
|
|
}
|
|
|
|
if (this.channelFactory != null) {
|
|
|
|
throw new IllegalStateException("channelFactory set already");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.channelFactory = channelFactory;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-11-04 19:42:33 +09:00
|
|
|
@Override
|
2013-09-26 08:12:48 -04:00
|
|
|
Channel createChannel() {
|
|
|
|
EventLoop eventLoop = group().next();
|
|
|
|
return channelFactory().newChannel(eventLoop, childGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerChannelFactory<? extends ServerChannel> channelFactory() {
|
|
|
|
return channelFactory;
|
|
|
|
}
|
|
|
|
|
2012-09-11 10:04:05 +02:00
|
|
|
/**
|
|
|
|
* Specify the {@link EventLoopGroup} which is used for the parent (acceptor) and the child (client).
|
|
|
|
*/
|
2012-09-11 08:31:20 +02:00
|
|
|
@Override
|
2012-08-10 20:26:04 +09:00
|
|
|
public ServerBootstrap group(EventLoopGroup group) {
|
2012-09-11 08:31:20 +02:00
|
|
|
return group(group, group);
|
2012-08-10 20:26:04 +09:00
|
|
|
}
|
|
|
|
|
2012-09-11 10:04:05 +02:00
|
|
|
/**
|
|
|
|
* Set the {@link EventLoopGroup} for the parent (acceptor) and the child (client). These
|
|
|
|
* {@link EventLoopGroup}'s are used to handle all the events and IO for {@link SocketChannel} and
|
|
|
|
* {@link Channel}'s.
|
|
|
|
*/
|
2012-08-10 20:17:18 +09:00
|
|
|
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) {
|
2012-09-11 08:31:20 +02:00
|
|
|
super.group(parentGroup);
|
|
|
|
if (childGroup == null) {
|
|
|
|
throw new NullPointerException("childGroup");
|
2012-05-14 23:57:23 +09:00
|
|
|
}
|
2012-09-11 08:31:20 +02:00
|
|
|
if (this.childGroup != null) {
|
|
|
|
throw new IllegalStateException("childGroup set already");
|
2012-05-29 16:41:26 -07:00
|
|
|
}
|
2012-08-10 20:17:18 +09:00
|
|
|
this.childGroup = childGroup;
|
2012-05-14 23:57:23 +09:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-09-11 10:04:05 +02:00
|
|
|
/**
|
|
|
|
* Allow to specify a {@link ChannelOption} which is used for the {@link Channel} instances once they get created
|
2012-11-12 11:51:23 +09:00
|
|
|
* (after the acceptor accepted the {@link Channel}). Use a value of {@code null} to remove a previous set
|
2012-09-11 10:04:05 +02:00
|
|
|
* {@link ChannelOption}.
|
|
|
|
*/
|
2012-05-29 16:41:26 -07:00
|
|
|
public <T> ServerBootstrap childOption(ChannelOption<T> childOption, T value) {
|
2012-05-14 23:57:23 +09:00
|
|
|
if (childOption == null) {
|
|
|
|
throw new NullPointerException("childOption");
|
|
|
|
}
|
|
|
|
if (value == null) {
|
2013-01-31 11:34:28 +09:00
|
|
|
synchronized (childOptions) {
|
|
|
|
childOptions.remove(childOption);
|
|
|
|
}
|
2012-05-14 23:57:23 +09:00
|
|
|
} else {
|
2013-01-31 11:34:28 +09:00
|
|
|
synchronized (childOptions) {
|
|
|
|
childOptions.put(childOption, value);
|
|
|
|
}
|
2012-05-14 23:57:23 +09:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-12-21 11:03:35 +01:00
|
|
|
/**
|
|
|
|
* Set the specific {@link AttributeKey} with the given value on every child {@link Channel}. If the value is
|
|
|
|
* {@code null} the {@link AttributeKey} is removed
|
|
|
|
*/
|
2012-10-26 14:52:50 -07:00
|
|
|
public <T> ServerBootstrap childAttr(AttributeKey<T> childKey, T value) {
|
|
|
|
if (childKey == null) {
|
|
|
|
throw new NullPointerException("childKey");
|
|
|
|
}
|
|
|
|
if (value == null) {
|
|
|
|
childAttrs.remove(childKey);
|
|
|
|
} else {
|
|
|
|
childAttrs.put(childKey, value);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-09-11 10:04:05 +02:00
|
|
|
/**
|
2013-01-28 11:26:53 +01:00
|
|
|
* Set the {@link ChannelHandler} which is used to serve the request for the {@link Channel}'s.
|
2012-09-11 10:04:05 +02:00
|
|
|
*/
|
2012-06-03 01:00:16 -07:00
|
|
|
public ServerBootstrap childHandler(ChannelHandler childHandler) {
|
|
|
|
if (childHandler == null) {
|
|
|
|
throw new NullPointerException("childHandler");
|
2012-05-14 23:57:23 +09:00
|
|
|
}
|
2012-06-03 01:00:16 -07:00
|
|
|
this.childHandler = childHandler;
|
2012-05-14 23:57:23 +09:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-06-10 07:54:53 +02:00
|
|
|
/**
|
|
|
|
* Return the configured {@link EventLoopGroup} which will be used for the child channels or {@code null}
|
|
|
|
* if non is configured yet.
|
|
|
|
*/
|
|
|
|
public EventLoopGroup childGroup() {
|
|
|
|
return childGroup;
|
|
|
|
}
|
|
|
|
|
2012-09-11 08:31:20 +02:00
|
|
|
@Override
|
2013-03-21 21:34:13 +09:00
|
|
|
void init(Channel channel) throws Exception {
|
|
|
|
final Map<ChannelOption<?>, Object> options = options();
|
|
|
|
synchronized (options) {
|
|
|
|
channel.config().setOptions(options);
|
2012-07-07 14:37:44 +09:00
|
|
|
}
|
|
|
|
|
2013-01-31 11:34:28 +09:00
|
|
|
final Map<AttributeKey<?>, Object> attrs = attrs();
|
|
|
|
synchronized (attrs) {
|
|
|
|
for (Entry<AttributeKey<?>, Object> e: attrs.entrySet()) {
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
AttributeKey<Object> key = (AttributeKey<Object>) e.getKey();
|
|
|
|
channel.attr(key).set(e.getValue());
|
|
|
|
}
|
2012-10-26 14:52:50 -07:00
|
|
|
}
|
|
|
|
|
2013-01-30 20:06:40 +09:00
|
|
|
ChannelPipeline p = channel.pipeline();
|
2012-10-09 20:55:24 +02:00
|
|
|
if (handler() != null) {
|
|
|
|
p.addLast(handler());
|
2012-05-14 23:57:23 +09:00
|
|
|
}
|
2013-01-31 11:34:28 +09:00
|
|
|
|
|
|
|
final ChannelHandler currentChildHandler = childHandler;
|
|
|
|
final Entry<ChannelOption<?>, Object>[] currentChildOptions;
|
|
|
|
final Entry<AttributeKey<?>, Object>[] currentChildAttrs;
|
|
|
|
synchronized (childOptions) {
|
|
|
|
currentChildOptions = childOptions.entrySet().toArray(newOptionArray(childOptions.size()));
|
|
|
|
}
|
|
|
|
synchronized (childAttrs) {
|
|
|
|
currentChildAttrs = childAttrs.entrySet().toArray(newAttrArray(childAttrs.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
p.addLast(new ChannelInitializer<Channel>() {
|
|
|
|
@Override
|
|
|
|
public void initChannel(Channel ch) throws Exception {
|
2013-09-26 08:12:48 -04:00
|
|
|
ch.pipeline().addLast(new ServerBootstrapAcceptor(currentChildHandler, currentChildOptions,
|
|
|
|
currentChildAttrs));
|
2013-01-31 11:34:28 +09:00
|
|
|
}
|
|
|
|
});
|
2012-05-14 23:57:23 +09:00
|
|
|
}
|
|
|
|
|
2012-09-11 08:31:20 +02:00
|
|
|
@Override
|
2013-02-08 07:48:47 +01:00
|
|
|
public ServerBootstrap validate() {
|
2012-09-11 08:31:20 +02:00
|
|
|
super.validate();
|
2012-06-03 01:00:16 -07:00
|
|
|
if (childHandler == null) {
|
|
|
|
throw new IllegalStateException("childHandler not set");
|
2012-05-14 23:57:23 +09:00
|
|
|
}
|
2012-08-10 20:17:18 +09:00
|
|
|
if (childGroup == null) {
|
|
|
|
logger.warn("childGroup is not set. Using parentGroup instead.");
|
2012-09-11 08:31:20 +02:00
|
|
|
childGroup = group();
|
2012-05-14 23:57:23 +09:00
|
|
|
}
|
2013-02-08 07:48:47 +01:00
|
|
|
return this;
|
2012-05-14 23:57:23 +09:00
|
|
|
}
|
|
|
|
|
2013-01-31 11:34:28 +09:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private static Entry<ChannelOption<?>, Object>[] newOptionArray(int size) {
|
|
|
|
return new Entry[size];
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private static Entry<AttributeKey<?>, Object>[] newAttrArray(int size) {
|
|
|
|
return new Entry[size];
|
|
|
|
}
|
|
|
|
|
Remove MessageList from public API and change ChannelInbound/OutboundHandler accordingly
I must admit MesageList was pain in the ass. Instead of forcing a
handler always loop over the list of messages, this commit splits
messageReceived(ctx, list) into two event handlers:
- messageReceived(ctx, msg)
- mmessageReceivedLast(ctx)
When Netty reads one or more messages, messageReceived(ctx, msg) event
is triggered for each message. Once the current read operation is
finished, messageReceivedLast() is triggered to tell the handler that
the last messageReceived() was the last message in the current batch.
Similarly, for outbound, write(ctx, list) has been split into two:
- write(ctx, msg)
- flush(ctx, promise)
Instead of writing a list of message with a promise, a user is now
supposed to call write(msg) multiple times and then call flush() to
actually flush the buffered messages.
Please note that write() doesn't have a promise with it. You must call
flush() to get notified on completion. (or you can use writeAndFlush())
Other changes:
- Because MessageList is completely hidden, codec framework uses
List<Object> instead of MessageList as an output parameter.
2013-07-08 19:03:40 +09:00
|
|
|
private static class ServerBootstrapAcceptor extends ChannelInboundHandlerAdapter {
|
2012-06-10 12:22:32 +09:00
|
|
|
|
2013-01-31 11:34:28 +09:00
|
|
|
private final ChannelHandler childHandler;
|
|
|
|
private final Entry<ChannelOption<?>, Object>[] childOptions;
|
|
|
|
private final Entry<AttributeKey<?>, Object>[] childAttrs;
|
|
|
|
|
2013-09-26 08:12:48 -04:00
|
|
|
ServerBootstrapAcceptor(ChannelHandler childHandler, Entry<ChannelOption<?>, Object>[] childOptions,
|
|
|
|
Entry<AttributeKey<?>, Object>[] childAttrs) {
|
2013-01-31 11:34:28 +09:00
|
|
|
this.childHandler = childHandler;
|
|
|
|
this.childOptions = childOptions;
|
|
|
|
this.childAttrs = childAttrs;
|
|
|
|
}
|
|
|
|
|
2012-05-14 23:57:23 +09:00
|
|
|
@Override
|
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
|
|
|
@SuppressWarnings("unchecked")
|
2013-07-09 23:09:28 +09:00
|
|
|
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
Remove MessageList from public API and change ChannelInbound/OutboundHandler accordingly
I must admit MesageList was pain in the ass. Instead of forcing a
handler always loop over the list of messages, this commit splits
messageReceived(ctx, list) into two event handlers:
- messageReceived(ctx, msg)
- mmessageReceivedLast(ctx)
When Netty reads one or more messages, messageReceived(ctx, msg) event
is triggered for each message. Once the current read operation is
finished, messageReceivedLast() is triggered to tell the handler that
the last messageReceived() was the last message in the current batch.
Similarly, for outbound, write(ctx, list) has been split into two:
- write(ctx, msg)
- flush(ctx, promise)
Instead of writing a list of message with a promise, a user is now
supposed to call write(msg) multiple times and then call flush() to
actually flush the buffered messages.
Please note that write() doesn't have a promise with it. You must call
flush() to get notified on completion. (or you can use writeAndFlush())
Other changes:
- Because MessageList is completely hidden, codec framework uses
List<Object> instead of MessageList as an output parameter.
2013-07-08 19:03:40 +09:00
|
|
|
Channel child = (Channel) msg;
|
|
|
|
|
2013-06-14 21:44:30 +02:00
|
|
|
child.pipeline().addLast(childHandler);
|
2012-05-14 23:57:23 +09:00
|
|
|
|
2013-06-14 21:44:30 +02:00
|
|
|
for (Entry<ChannelOption<?>, Object> e: childOptions) {
|
|
|
|
try {
|
|
|
|
if (!child.config().setOption((ChannelOption<Object>) e.getKey(), e.getValue())) {
|
|
|
|
logger.warn("Unknown channel option: " + e);
|
2012-05-14 23:57:23 +09:00
|
|
|
}
|
2013-06-14 21:44:30 +02:00
|
|
|
} catch (Throwable t) {
|
|
|
|
logger.warn("Failed to set a channel option: " + child, t);
|
2012-05-14 23:57:23 +09:00
|
|
|
}
|
2013-06-14 21:44:30 +02:00
|
|
|
}
|
2012-05-14 23:57:23 +09:00
|
|
|
|
2013-06-14 21:44:30 +02:00
|
|
|
for (Entry<AttributeKey<?>, Object> e: childAttrs) {
|
|
|
|
child.attr((AttributeKey<Object>) e.getKey()).set(e.getValue());
|
|
|
|
}
|
2012-10-26 14:52:50 -07:00
|
|
|
|
2013-09-26 08:12:48 -04:00
|
|
|
child.unsafe().register(child.newPromise());
|
2012-05-14 23:57:23 +09:00
|
|
|
}
|
2013-05-08 06:57:52 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
|
final ChannelConfig config = ctx.channel().config();
|
|
|
|
if (config.isAutoRead()) {
|
|
|
|
// stop accept new connections for 1 second to allow the channel to recover
|
|
|
|
// See https://github.com/netty/netty/issues/1328
|
|
|
|
config.setAutoRead(false);
|
|
|
|
ctx.channel().eventLoop().schedule(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
config.setAutoRead(true);
|
|
|
|
}
|
|
|
|
}, 1, TimeUnit.SECONDS);
|
|
|
|
}
|
|
|
|
// still let the exceptionCaught event flow through the pipeline to give the user
|
|
|
|
// a chance to do something with it
|
|
|
|
ctx.fireExceptionCaught(cause);
|
|
|
|
}
|
2012-05-14 23:57:23 +09:00
|
|
|
}
|
2012-11-10 20:43:53 +01:00
|
|
|
|
2013-01-30 21:12:02 +09:00
|
|
|
@Override
|
|
|
|
@SuppressWarnings("CloneDoesntCallSuperClone")
|
|
|
|
public ServerBootstrap clone() {
|
|
|
|
return new ServerBootstrap(this);
|
|
|
|
}
|
|
|
|
|
2012-11-26 16:14:24 +09:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
StringBuilder buf = new StringBuilder(super.toString());
|
|
|
|
buf.setLength(buf.length() - 1);
|
|
|
|
buf.append(", ");
|
|
|
|
if (childGroup != null) {
|
|
|
|
buf.append("childGroup: ");
|
2013-11-04 19:42:33 +09:00
|
|
|
buf.append(StringUtil.simpleClassName(childGroup));
|
2012-11-26 16:14:24 +09:00
|
|
|
buf.append(", ");
|
|
|
|
}
|
2013-01-31 11:34:28 +09:00
|
|
|
synchronized (childOptions) {
|
|
|
|
if (!childOptions.isEmpty()) {
|
|
|
|
buf.append("childOptions: ");
|
|
|
|
buf.append(childOptions);
|
|
|
|
buf.append(", ");
|
|
|
|
}
|
2012-11-26 16:14:24 +09:00
|
|
|
}
|
2013-01-31 11:34:28 +09:00
|
|
|
synchronized (childAttrs) {
|
|
|
|
if (!childAttrs.isEmpty()) {
|
|
|
|
buf.append("childAttrs: ");
|
|
|
|
buf.append(childAttrs);
|
|
|
|
buf.append(", ");
|
|
|
|
}
|
2012-11-26 16:14:24 +09:00
|
|
|
}
|
|
|
|
if (childHandler != null) {
|
|
|
|
buf.append("childHandler: ");
|
|
|
|
buf.append(childHandler);
|
|
|
|
buf.append(", ");
|
|
|
|
}
|
|
|
|
if (buf.charAt(buf.length() - 1) == '(') {
|
|
|
|
buf.append(')');
|
|
|
|
} else {
|
|
|
|
buf.setCharAt(buf.length() - 2, ')');
|
|
|
|
buf.setLength(buf.length() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.toString();
|
|
|
|
}
|
2013-09-26 08:12:48 -04:00
|
|
|
|
|
|
|
private static final class ServerBootstrapChannelFactory<T extends ServerChannel>
|
|
|
|
implements ServerChannelFactory<T> {
|
|
|
|
|
|
|
|
private final Class<? extends T> clazz;
|
|
|
|
|
|
|
|
ServerBootstrapChannelFactory(Class<? extends T> clazz) {
|
|
|
|
this.clazz = clazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public T newChannel(EventLoop eventLoop, EventLoopGroup childGroup) {
|
|
|
|
try {
|
|
|
|
Constructor<? extends T> constructor = clazz.getConstructor(EventLoop.class, EventLoopGroup.class);
|
|
|
|
return constructor.newInstance(eventLoop, childGroup);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
throw new ChannelException("Unable to create Channel from class " + clazz, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2013-11-04 19:42:33 +09:00
|
|
|
return StringUtil.simpleClassName(clazz) + ".class";
|
2013-09-26 08:12:48 -04:00
|
|
|
}
|
|
|
|
}
|
2012-05-14 23:57:23 +09:00
|
|
|
}
|