| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void start(ServerListener serverListener) throws IOException { |
| 219 | listener = checkNotNull(serverListener, "serverListener"); |
| 220 | |
| 221 | final ServerBootstrap b = new ServerBootstrap(); |
| 222 | b.option(ALLOCATOR, Utils.getByteBufAllocator(forceHeapBuffer)); |
| 223 | b.childOption(ALLOCATOR, Utils.getByteBufAllocator(forceHeapBuffer)); |
| 224 | b.group(bossExecutor, workerGroup); |
| 225 | b.channelFactory(channelFactory); |
| 226 | // For non-socket based channel, the option will be ignored. |
| 227 | b.childOption(SO_KEEPALIVE, true); |
| 228 | |
| 229 | if (channelOptions != null) { |
| 230 | for (Map.Entry<ChannelOption<?>, ?> entry : channelOptions.entrySet()) { |
| 231 | @SuppressWarnings("unchecked") |
| 232 | ChannelOption<Object> key = (ChannelOption<Object>) entry.getKey(); |
| 233 | b.option(key, entry.getValue()); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | if (childChannelOptions != null) { |
| 238 | for (Map.Entry<ChannelOption<?>, ?> entry : childChannelOptions.entrySet()) { |
| 239 | @SuppressWarnings("unchecked") |
| 240 | ChannelOption<Object> key = (ChannelOption<Object>) entry.getKey(); |
| 241 | b.childOption(key, entry.getValue()); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | b.childHandler(new ChannelInitializer<Channel>() { |
| 246 | @Override |
| 247 | public void initChannel(Channel ch) { |
| 248 | |
| 249 | ChannelPromise channelDone = ch.newPromise(); |
| 250 | |
| 251 | long maxConnectionAgeInNanos = NettyServer.this.maxConnectionAgeInNanos; |
| 252 | if (maxConnectionAgeInNanos != MAX_CONNECTION_AGE_NANOS_DISABLED) { |
| 253 | // apply a random jitter of +/-10% to max connection age |
| 254 | maxConnectionAgeInNanos = |
| 255 | (long) ((.9D + Math.random() * .2D) * maxConnectionAgeInNanos); |
| 256 | } |
| 257 | |
| 258 | NettyServerTransport transport = |
| 259 | new NettyServerTransport( |
| 260 | ch, |
| 261 | channelDone, |
| 262 | protocolNegotiator, |
| 263 | streamTracerFactories, |
| 264 | transportTracerFactory.create(), |
| 265 | maxStreamsPerConnection, |
| 266 | autoFlowControl, |
| 267 | flowControlWindow, |
| 268 | maxMessageSize, |
| 269 | maxHeaderListSize, |
| 270 | softLimitHeaderListSize, |
| 271 | keepAliveTimeInNanos, |
| 272 | keepAliveTimeoutInNanos, |
| 273 | maxConnectionIdleInNanos, |
| 274 | maxConnectionAgeInNanos, |