@Override protected void initChannel(SocketChannel ch) throws Exception { final ChannelPipeline p = ch.pipeline(); final UUID uuid = UUID.randomUUID(); LOG.debug("KaaTcpServerInitializer Initializing Channel {} connection from {}:{}", uuid, ch.remoteAddress().getAddress().toString(), ch.remoteAddress().getPort()); Attribute<UUID> uuidAttr = ch.attr(AbstractNettyServer.UUID_KEY); uuidAttr.set(uuid); p.addLast("binaryDecoder", new ByteArrayDecoder()); p.addLast("kaaTcpDecoder", getDecoder()); p.addLast("binaryEncoder", new ByteArrayEncoder()); p.addLast("kaaTcpEncoder", new KaaTcpEncoder()); p.addLast("mainHandler", getMainHandler(uuid)); p.addLast("kaaTcpExceptionHandler", new KaaTcpExceptionHandler()); }
@Override protected void initChannel(SocketChannel ch) throws Exception { final ChannelPipeline p = ch.pipeline(); final UUID uuid = UUID.randomUUID(); LOG.info("DefaultServerInitializer Initializing Channel {} connection from {}:{}", uuid, ch.remoteAddress().getAddress().toString(), ch.remoteAddress().getPort()); Attribute<UUID> uuidAttr = ch.attr(AbstractNettyServer.UUID_KEY); uuidAttr.set(uuid); p.addLast("httpDecoder", new HttpRequestDecoder()); p.addLast("httpAggregator", new HttpObjectAggregator(getClientMaxBodySize())); p.addLast("httpDecoderAux", getRequestDecoder()); p.addLast("httpEncoder", new HttpResponseEncoder()); p.addLast("httpEncoderAux", new ResponseEncoder()); p.addLast("handler", getMainHandler(uuid)); p.addLast("httpExceptionHandler", new DefaultExceptionHandler()); }
if (channel.attr(SECURE) != null && channel.attr(SECURE).get() != null && channel.attr(SECURE).get()) { InetSocketAddress remoteAddress = channel.attr(REMOTE_SOCKET).get(); pipeline.addLast(nettySslContextFactory().createClientSslContext().newHandler(channel.alloc(), remoteAddress.getHostName(), remoteAddress.getPort()));
channel.attr(Message.CODEC_REGISTRY_ATTRIBUTE_KEY).set(codecRegistry);
@Override public void initChannel(SocketChannel ch) throws Exception { // 设置编解码处理器 ch.pipeline().addLast(new RpcDecoder(), new RpcChannelHandler<RpcRequest>(proce)) .addLast(new RpcEncoder()); // 注册会话 NettySession session = new NettySession(ch, config); ch.attr(AttributeKey.valueOf(RpcConstant.SESSION_KEY)).set(session); if (LOGGER.isDebugEnabled()) { LOGGER.debug("init channel " + session); } } }).option(ChannelOption.SO_BACKLOG, 128) // (5)
@Override protected void initChannel(SocketChannel channel) throws Exception { // set the codec registry so that it can be accessed by ProtocolDecoder channel.attr(Message.CODEC_REGISTRY_ATTRIBUTE_KEY).set(codecRegistry); ChannelPipeline pipeline = channel.pipeline(); if (sslOptions != null) { pipeline.addLast("ssl", sslOptions.newSSLHandler(channel)); } // pipeline.addLast("debug", new LoggingHandler(LogLevel.INFO)); pipeline.addLast("frameDecoder", new Frame.Decoder()); pipeline.addLast("frameEncoder", frameEncoder); if (compressor != null) { pipeline.addLast("frameDecompressor", new Frame.Decompressor(compressor)); pipeline.addLast("frameCompressor", new Frame.Compressor(compressor)); } pipeline.addLast("messageDecoder", messageDecoder); pipeline.addLast("messageEncoder", messageEncoderFor(protocolVersion)); pipeline.addLast("idleStateHandler", idleStateHandler); pipeline.addLast("dispatcher", connection.dispatcher); nettyOptions.afterChannelInitialized(channel); }
@Override protected void initChannel(SocketChannel channel) throws Exception { // set the codec registry so that it can be accessed by ProtocolDecoder channel.attr(Message.CODEC_REGISTRY_ATTRIBUTE_KEY).set(codecRegistry); ChannelPipeline pipeline = channel.pipeline(); if (sslOptions != null) { pipeline.addLast("ssl", sslOptions.newSSLHandler(channel)); } // pipeline.addLast("debug", new LoggingHandler(LogLevel.INFO)); pipeline.addLast("frameDecoder", new Frame.Decoder()); pipeline.addLast("frameEncoder", frameEncoder); if (compressor != null) { pipeline.addLast("frameDecompressor", new Frame.Decompressor(compressor)); pipeline.addLast("frameCompressor", new Frame.Compressor(compressor)); } pipeline.addLast("messageDecoder", messageDecoder); pipeline.addLast("messageEncoder", messageEncoderFor(protocolVersion)); pipeline.addLast("idleStateHandler", idleStateHandler); pipeline.addLast("dispatcher", connection.dispatcher); nettyOptions.afterChannelInitialized(channel); }
private void cacheNode(Node node, SocketChannel channel) { String name = "node-" + node.getId(); boolean exists = AttributeKey.exists(name); AttributeKey attributeKey; if (exists) { attributeKey = AttributeKey.valueOf(name); } else { attributeKey = AttributeKey.newInstance(name); } Attribute<Node> attribute = channel.attr(attributeKey); attribute.set(node); }
if (channel.attr(SECURE) != null && channel.attr(SECURE).get() != null && channel.attr(SECURE).get()) { InetSocketAddress remoteAddress = channel.attr(REMOTE_SOCKET).get(); pipeline.addLast(nettySslContextFactory().createClientSslContext().newHandler(channel.alloc(), remoteAddress.getHostName(), remoteAddress.getPort()));
/** * 继承SimpleChannelInboundHandler后,只需要重新channelRead0方法,msg会自动释放 * @param ctx * @param msg * @throws Exception */ @Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { SocketChannel channel = (SocketChannel) ctx.channel(); String nodeId = IpUtil.getNodeId(channel.remoteAddress()); Attribute<Node> nodeAttribute = channel.attr(AttributeKey.valueOf("node-" + nodeId)); Node node = nodeAttribute.get(); ByteBuf buf = (ByteBuf) msg; messageProcessor.processor(buf, node); }
@Override protected void initChannel(SocketChannel channel) throws Exception { // set the codec registry so that it can be accessed by ProtocolDecoder channel.attr(Message.CODEC_REGISTRY_ATTRIBUTE_KEY).set(codecRegistry); ChannelPipeline pipeline = channel.pipeline(); if (sslOptions != null) { if (sslOptions instanceof RemoteEndpointAwareSSLOptions) { SslHandler handler = ((RemoteEndpointAwareSSLOptions) sslOptions).newSSLHandler(channel, connection.address); pipeline.addLast("ssl", handler); } else { @SuppressWarnings("deprecation") SslHandler handler = sslOptions.newSSLHandler(channel); pipeline.addLast("ssl", handler); } } // pipeline.addLast("debug", new LoggingHandler(LogLevel.INFO)); pipeline.addLast("frameDecoder", new Frame.Decoder()); pipeline.addLast("frameEncoder", frameEncoder); if (compressor != null) { pipeline.addLast("frameDecompressor", new Frame.Decompressor(compressor)); pipeline.addLast("frameCompressor", new Frame.Compressor(compressor)); } pipeline.addLast("messageDecoder", messageDecoder); pipeline.addLast("messageEncoder", messageEncoderFor(protocolVersion)); pipeline.addLast("idleStateHandler", idleStateHandler); pipeline.addLast("dispatcher", connection.dispatcher); nettyOptions.afterChannelInitialized(channel); }
ChannelPipeline p = ch.pipeline(); p.addLast(new IdleStateHandler(6, 3, 0)); System.out.println(ch.attr(AttributeKey.valueOf("abc")));
@Override public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { super.channelUnregistered(ctx); SocketChannel channel = (SocketChannel) ctx.channel(); String nodeId = IpUtil.getNodeId(channel.remoteAddress()); Attribute<Node> nodeAttribute = channel.attr(AttributeKey.valueOf("node-" + nodeId)); Node node = nodeAttribute.get(); if (node != null && node.getDisconnectListener() != null) { node.getDisconnectListener().action(); } //channelUnregistered之前,channel就已经close了,可以调用channel.isOpen()查看状态 //channel.close(); }
ch.config().setReceiveBufferSize(NettyChannelContext.BUFFER_SIZE); ChannelPromise settingsPromise = ch.newPromise(); ch.attr(NettyChannelContext.SETTINGS_PROMISE_KEY).set(settingsPromise);