Search in sources :

Example 21 with LengthFieldBasedFrameDecoder

use of io.netty.handler.codec.LengthFieldBasedFrameDecoder in project tutorials-java by Artister.

the class NettyServer method service.

public static void service() throws Exception {
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup);
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            // TODO Auto-generated method stub
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
            pipeline.addLast(new LengthFieldPrepender(4));
            pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
            pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
            pipeline.addLast(new TcpServerHandler());
        }
    });
    ChannelFuture f = bootstrap.bind(IP, PORT).sync();
    f.channel().closeFuture().sync();
    System.out.println("TCP服务器已启动");
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) StringDecoder(io.netty.handler.codec.string.StringDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) TcpServerHandler(org.ko.netty.tn.handler.TcpServerHandler) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 22 with LengthFieldBasedFrameDecoder

use of io.netty.handler.codec.LengthFieldBasedFrameDecoder in project JaPS by JackWhite20.

the class ClusterPublisherChannelInitializer method initChannel.

@Override
protected void initChannel(Channel channel) throws Exception {
    try {
        channel.config().setOption(ChannelOption.IP_TOS, 0x18);
    } catch (ChannelException e) {
    // Not supported
    }
    channel.config().setAllocator(PooledByteBufAllocator.DEFAULT);
    channel.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4));
    channel.pipeline().addLast(new JSONObjectDecoder());
    channel.pipeline().addLast(new LengthFieldPrepender(4));
    channel.pipeline().addLast(new JSONObjectEncoder());
    channel.pipeline().addLast(clusterPublisher);
}
Also used : JSONObjectEncoder(de.jackwhite20.japs.shared.pipeline.handler.JSONObjectEncoder) JSONObjectDecoder(de.jackwhite20.japs.shared.pipeline.handler.JSONObjectDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) ChannelException(io.netty.channel.ChannelException)

Example 23 with LengthFieldBasedFrameDecoder

use of io.netty.handler.codec.LengthFieldBasedFrameDecoder in project drill by axbaretto.

the class AbstractRemoteConnection method addSecurityHandlers.

/**
 * Helps to add all the required security handler's after negotiation for encryption is completed.
 * <p>Handler's before encryption is negotiated are:</p>
 * <ul>
 *    <li>  PROTOCOL_DECODER {@link ProtobufLengthDecoder} </li>
 *    <li>  MESSAGE_DECODER {@link RpcDecoder}  </li>
 *    <li>  PROTOCOL_ENCODER {@link RpcEncoder} </li>
 *    <li>  HANDSHAKE_HANDLER {@link org.apache.drill.exec.rpc.BasicClient.ClientHandshakeHandler} OR
 *                            {@link org.apache.drill.exec.rpc.BasicServer.ServerHandshakeHandler}  </li>
 *    <li>  optional - IDLE_STATE_HANDLER {@link org.apache.drill.exec.rpc.BasicClient.IdlePingHandler} OR
 *                   - TIMEOUT_HANDLER {@link org.apache.drill.exec.rpc.BasicServer.LoggingReadTimeoutHandler}  </li>
 *    <li>  MESSAGE_HANDLER {@link org.apache.drill.exec.rpc.RpcBus.InboundHandler} </li>
 *    <li>  EXCEPTION_HANDLER {@link RpcExceptionHandler} </li>
 * </ul>
 * <p>Handler's after encryption is negotiated are:</p>
 * <ul>
 *    <li>  LENGTH_DECODER_HANDLER {@link LengthFieldBasedFrameDecoder}
 *    <li>  SASL_DECRYPTION_HANDLER {@link SaslDecryptionHandler}
 *    <li>  PROTOCOL_DECODER {@link ProtobufLengthDecoder}
 *    <li>  MESSAGE_DECODER {@link RpcDecoder}
 *    <li>  SASL_ENCRYPTION_HANDLER {@link SaslEncryptionHandler}
 *    <li>  CHUNK_CREATION_HANDLER {@link ChunkCreationHandler}
 *    <li>  PROTOCOL_ENCODER {@link RpcEncoder}
 *    <li>  HANDSHAKE_HANDLER {@link org.apache.drill.exec.rpc.BasicClient.ClientHandshakeHandler} OR
 *                            {@link org.apache.drill.exec.rpc.BasicServer.ServerHandshakeHandler}
 *    <li>  optional - IDLE_STATE_HANDLER {@link org.apache.drill.exec.rpc.BasicClient.IdlePingHandler} OR
 *                   - TIMEOUT_HANDLER {@link org.apache.drill.exec.rpc.BasicServer.LoggingReadTimeoutHandler}
 *    <li>  MESSAGE_HANDLER {@link org.apache.drill.exec.rpc.RpcBus.InboundHandler}
 *    <li>  EXCEPTION_HANDLER {@link RpcExceptionHandler}
 * </ul>
 * <p>
 *  If encryption is enabled ChunkCreationHandler is always added to divide the Rpc message into chunks of
 *  negotiated {@link EncryptionContextImpl#wrapSizeLimit} bytes. This helps to make a generic encryption handler.
 * </p>
 */
@Override
public void addSecurityHandlers() {
    final ChannelPipeline channelPipeline = getChannel().pipeline();
    if (channelPipeline.names().contains(RpcConstants.SSL_HANDLER)) {
        channelPipeline.addAfter(RpcConstants.SSL_HANDLER, RpcConstants.SASL_DECRYPTION_HANDLER, new SaslDecryptionHandler(saslCodec, getMaxWrappedSize(), OutOfMemoryHandler.DEFAULT_INSTANCE));
        channelPipeline.addAfter(RpcConstants.SSL_HANDLER, RpcConstants.LENGTH_DECODER_HANDLER, new LengthFieldBasedFrameDecoder(ByteOrder.BIG_ENDIAN, Integer.MAX_VALUE, RpcConstants.LENGTH_FIELD_OFFSET, RpcConstants.LENGTH_FIELD_LENGTH, RpcConstants.LENGTH_ADJUSTMENT, RpcConstants.INITIAL_BYTES_TO_STRIP, true));
    } else {
        channelPipeline.addFirst(RpcConstants.SASL_DECRYPTION_HANDLER, new SaslDecryptionHandler(saslCodec, getMaxWrappedSize(), OutOfMemoryHandler.DEFAULT_INSTANCE));
        channelPipeline.addFirst(RpcConstants.LENGTH_DECODER_HANDLER, new LengthFieldBasedFrameDecoder(ByteOrder.BIG_ENDIAN, Integer.MAX_VALUE, RpcConstants.LENGTH_FIELD_OFFSET, RpcConstants.LENGTH_FIELD_LENGTH, RpcConstants.LENGTH_ADJUSTMENT, RpcConstants.INITIAL_BYTES_TO_STRIP, true));
    }
    channelPipeline.addAfter(RpcConstants.MESSAGE_DECODER, RpcConstants.SASL_ENCRYPTION_HANDLER, new SaslEncryptionHandler(saslCodec, getWrapSizeLimit(), OutOfMemoryHandler.DEFAULT_INSTANCE));
    channelPipeline.addAfter(RpcConstants.SASL_ENCRYPTION_HANDLER, RpcConstants.CHUNK_CREATION_HANDLER, new ChunkCreationHandler(getWrapSizeLimit()));
}
Also used : LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 24 with LengthFieldBasedFrameDecoder

use of io.netty.handler.codec.LengthFieldBasedFrameDecoder in project scalecube by scalecube.

the class NettyStreamChannelInitializer method initChannel.

@Override
protected void initChannel(Channel channel) {
    ChannelPipeline pipeline = channel.pipeline();
    // contexs contexts contexs
    channel.pipeline().addLast(channelContextHandler);
    // frame codecs
    pipeline.addLast(new LengthFieldPrepender(LENGTH_FIELD_LENGTH));
    pipeline.addLast(new LengthFieldBasedFrameDecoder(MAX_FRAME_LENGTH, 0, LENGTH_FIELD_LENGTH, 0, LENGTH_FIELD_LENGTH));
    // message acceptor
    pipeline.addLast(messageHandler);
    // at-least-something exception handler
    pipeline.addLast(new ChannelInboundHandlerAdapter() {

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) {
            // Hint: at this point one can look at throwable, make some exception translation, and via channelContext post
            // ChannelContextError event, and hence give business layer ability to react on low level system error events
            LOGGER.warn("Exception caught for channel {}, {}", ctx.channel(), throwable);
        }
    });
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 25 with LengthFieldBasedFrameDecoder

use of io.netty.handler.codec.LengthFieldBasedFrameDecoder in project drill by apache.

the class AbstractRemoteConnection method addSecurityHandlers.

/**
 * Helps to add all the required security handler's after negotiation for encryption is completed.
 * <p>Handler's before encryption is negotiated are:</p>
 * <ul>
 *    <li>  PROTOCOL_DECODER {@link ProtobufLengthDecoder} </li>
 *    <li>  MESSAGE_DECODER {@link RpcDecoder}  </li>
 *    <li>  PROTOCOL_ENCODER {@link RpcEncoder} </li>
 *    <li>  HANDSHAKE_HANDLER {@link org.apache.drill.exec.rpc.BasicClient.ClientHandshakeHandler} OR
 *                            {@link org.apache.drill.exec.rpc.BasicServer.ServerHandshakeHandler}  </li>
 *    <li>  optional - IDLE_STATE_HANDLER {@link org.apache.drill.exec.rpc.BasicClient.IdlePingHandler} OR
 *                   - TIMEOUT_HANDLER {@link org.apache.drill.exec.rpc.BasicServer.LoggingReadTimeoutHandler}  </li>
 *    <li>  MESSAGE_HANDLER {@link org.apache.drill.exec.rpc.RpcBus.InboundHandler} </li>
 *    <li>  EXCEPTION_HANDLER {@link RpcExceptionHandler} </li>
 * </ul>
 * <p>Handler's after encryption is negotiated are:</p>
 * <ul>
 *    <li>  LENGTH_DECODER_HANDLER {@link LengthFieldBasedFrameDecoder}
 *    <li>  SASL_DECRYPTION_HANDLER {@link SaslDecryptionHandler}
 *    <li>  PROTOCOL_DECODER {@link ProtobufLengthDecoder}
 *    <li>  MESSAGE_DECODER {@link RpcDecoder}
 *    <li>  SASL_ENCRYPTION_HANDLER {@link SaslEncryptionHandler}
 *    <li>  CHUNK_CREATION_HANDLER {@link ChunkCreationHandler}
 *    <li>  PROTOCOL_ENCODER {@link RpcEncoder}
 *    <li>  HANDSHAKE_HANDLER {@link org.apache.drill.exec.rpc.BasicClient.ClientHandshakeHandler} OR
 *                            {@link org.apache.drill.exec.rpc.BasicServer.ServerHandshakeHandler}
 *    <li>  optional - IDLE_STATE_HANDLER {@link org.apache.drill.exec.rpc.BasicClient.IdlePingHandler} OR
 *                   - TIMEOUT_HANDLER {@link org.apache.drill.exec.rpc.BasicServer.LoggingReadTimeoutHandler}
 *    <li>  MESSAGE_HANDLER {@link org.apache.drill.exec.rpc.RpcBus.InboundHandler}
 *    <li>  EXCEPTION_HANDLER {@link RpcExceptionHandler}
 * </ul>
 * <p>
 *  If encryption is enabled ChunkCreationHandler is always added to divide the Rpc message into chunks of
 *  negotiated {@link EncryptionContextImpl#wrapSizeLimit} bytes. This helps to make a generic encryption handler.
 * </p>
 */
@Override
public void addSecurityHandlers() {
    final ChannelPipeline channelPipeline = getChannel().pipeline();
    if (channelPipeline.names().contains(RpcConstants.SSL_HANDLER)) {
        channelPipeline.addAfter(RpcConstants.SSL_HANDLER, RpcConstants.SASL_DECRYPTION_HANDLER, new SaslDecryptionHandler(saslCodec, getMaxWrappedSize(), OutOfMemoryHandler.DEFAULT_INSTANCE));
        channelPipeline.addAfter(RpcConstants.SSL_HANDLER, RpcConstants.LENGTH_DECODER_HANDLER, new LengthFieldBasedFrameDecoder(ByteOrder.BIG_ENDIAN, Integer.MAX_VALUE, RpcConstants.LENGTH_FIELD_OFFSET, RpcConstants.LENGTH_FIELD_LENGTH, RpcConstants.LENGTH_ADJUSTMENT, RpcConstants.INITIAL_BYTES_TO_STRIP, true));
    } else {
        channelPipeline.addFirst(RpcConstants.SASL_DECRYPTION_HANDLER, new SaslDecryptionHandler(saslCodec, getMaxWrappedSize(), OutOfMemoryHandler.DEFAULT_INSTANCE));
        channelPipeline.addFirst(RpcConstants.LENGTH_DECODER_HANDLER, new LengthFieldBasedFrameDecoder(ByteOrder.BIG_ENDIAN, Integer.MAX_VALUE, RpcConstants.LENGTH_FIELD_OFFSET, RpcConstants.LENGTH_FIELD_LENGTH, RpcConstants.LENGTH_ADJUSTMENT, RpcConstants.INITIAL_BYTES_TO_STRIP, true));
    }
    channelPipeline.addAfter(RpcConstants.MESSAGE_DECODER, RpcConstants.SASL_ENCRYPTION_HANDLER, new SaslEncryptionHandler(saslCodec, getWrapSizeLimit(), OutOfMemoryHandler.DEFAULT_INSTANCE));
    channelPipeline.addAfter(RpcConstants.SASL_ENCRYPTION_HANDLER, RpcConstants.CHUNK_CREATION_HANDLER, new ChunkCreationHandler(getWrapSizeLimit()));
}
Also used : LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)68 SocketChannel (io.netty.channel.socket.SocketChannel)35 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)35 ChannelPipeline (io.netty.channel.ChannelPipeline)30 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)25 Bootstrap (io.netty.bootstrap.Bootstrap)19 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)19 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)19 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)18 ChannelFuture (io.netty.channel.ChannelFuture)14 EventLoopGroup (io.netty.channel.EventLoopGroup)14 Channel (io.netty.channel.Channel)13 StringEncoder (io.netty.handler.codec.string.StringEncoder)13 StringDecoder (io.netty.handler.codec.string.StringDecoder)12 SslContext (io.netty.handler.ssl.SslContext)12 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)9 CommandDecoder (io.pravega.shared.protocol.netty.CommandDecoder)8 CommandEncoder (io.pravega.shared.protocol.netty.CommandEncoder)8 IOException (java.io.IOException)8 ExceptionLoggingHandler (io.pravega.shared.protocol.netty.ExceptionLoggingHandler)7