Search in sources :

Example 11 with LengthFieldPrepender

use of io.netty.handler.codec.LengthFieldPrepender 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 12 with LengthFieldPrepender

use of io.netty.handler.codec.LengthFieldPrepender 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 13 with LengthFieldPrepender

use of io.netty.handler.codec.LengthFieldPrepender 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 14 with LengthFieldPrepender

use of io.netty.handler.codec.LengthFieldPrepender in project neo4j by neo4j.

the class RaftChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
    pipeline.addLast(new VersionPrepender());
    pipeline.addLast("raftMessageEncoder", new RaftMessageEncoder(marshal));
    pipeline.addLast(new ExceptionLoggingHandler(log));
    pipeline.addLast(new ExceptionMonitoringHandler(monitors.newMonitor(ExceptionMonitoringHandler.Monitor.class, SenderService.class)));
    pipeline.addLast(new ExceptionSwallowingHandler());
}
Also used : RaftMessageEncoder(org.neo4j.causalclustering.messaging.marshalling.RaftMessageEncoder) ExceptionSwallowingHandler(org.neo4j.causalclustering.handlers.ExceptionSwallowingHandler) ExceptionLoggingHandler(org.neo4j.causalclustering.handlers.ExceptionLoggingHandler) VersionPrepender(org.neo4j.causalclustering.VersionPrepender) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) ChannelPipeline(io.netty.channel.ChannelPipeline) ExceptionMonitoringHandler(org.neo4j.causalclustering.handlers.ExceptionMonitoringHandler)

Example 15 with LengthFieldPrepender

use of io.netty.handler.codec.LengthFieldPrepender in project neo4j by neo4j.

the class CatchUpClientChannelPipeline method initChannel.

static void initChannel(SocketChannel ch, CatchUpResponseHandler handler, LogProvider logProvider, Monitors monitors) throws Exception {
    CatchupClientProtocol protocol = new CatchupClientProtocol();
    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
    pipeline.addLast(new LengthFieldPrepender(4));
    pipeline.addLast(new VersionDecoder(logProvider));
    pipeline.addLast(new VersionPrepender());
    pipeline.addLast(new TxPullRequestEncoder());
    pipeline.addLast(new GetStoreRequestEncoder());
    pipeline.addLast(new CoreSnapshotRequestEncoder());
    pipeline.addLast(new GetStoreIdRequestEncoder());
    pipeline.addLast(new ResponseMessageTypeEncoder());
    pipeline.addLast(new RequestMessageTypeEncoder());
    pipeline.addLast(new ClientMessageTypeHandler(protocol, logProvider));
    RequestDecoderDispatcher<CatchupClientProtocol.State> decoderDispatcher = new RequestDecoderDispatcher<>(protocol, logProvider);
    decoderDispatcher.register(CatchupClientProtocol.State.STORE_ID, new GetStoreIdResponseDecoder());
    decoderDispatcher.register(CatchupClientProtocol.State.TX_PULL_RESPONSE, new TxPullResponseDecoder());
    decoderDispatcher.register(CatchupClientProtocol.State.CORE_SNAPSHOT, new CoreSnapshotDecoder());
    decoderDispatcher.register(CatchupClientProtocol.State.STORE_COPY_FINISHED, new StoreCopyFinishedResponseDecoder());
    decoderDispatcher.register(CatchupClientProtocol.State.TX_STREAM_FINISHED, new TxStreamFinishedResponseDecoder());
    decoderDispatcher.register(CatchupClientProtocol.State.FILE_HEADER, new FileHeaderDecoder());
    decoderDispatcher.register(CatchupClientProtocol.State.FILE_CONTENTS, new FileChunkDecoder());
    pipeline.addLast(decoderDispatcher);
    pipeline.addLast(new TxPullResponseHandler(protocol, handler));
    pipeline.addLast(new CoreSnapshotResponseHandler(protocol, handler));
    pipeline.addLast(new StoreCopyFinishedResponseHandler(protocol, handler));
    pipeline.addLast(new TxStreamFinishedResponseHandler(protocol, handler));
    pipeline.addLast(new FileHeaderHandler(protocol, handler, logProvider));
    pipeline.addLast(new FileChunkHandler(protocol, handler));
    pipeline.addLast(new GetStoreIdResponseHandler(protocol, handler));
    pipeline.addLast(new ExceptionLoggingHandler(logProvider.getLog(CatchUpClient.class)));
    pipeline.addLast(new ExceptionMonitoringHandler(monitors.newMonitor(ExceptionMonitoringHandler.Monitor.class, CatchUpClient.class)));
    pipeline.addLast(new ExceptionSwallowingHandler());
}
Also used : CoreSnapshotRequestEncoder(org.neo4j.causalclustering.core.state.snapshot.CoreSnapshotRequestEncoder) ExceptionSwallowingHandler(org.neo4j.causalclustering.handlers.ExceptionSwallowingHandler) VersionPrepender(org.neo4j.causalclustering.VersionPrepender) TxPullResponseDecoder(org.neo4j.causalclustering.catchup.tx.TxPullResponseDecoder) StoreCopyFinishedResponseDecoder(org.neo4j.causalclustering.catchup.storecopy.StoreCopyFinishedResponseDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) ExceptionLoggingHandler(org.neo4j.causalclustering.handlers.ExceptionLoggingHandler) TxPullRequestEncoder(org.neo4j.causalclustering.catchup.tx.TxPullRequestEncoder) TxPullResponseHandler(org.neo4j.causalclustering.catchup.tx.TxPullResponseHandler) StoreCopyFinishedResponseHandler(org.neo4j.causalclustering.catchup.storecopy.StoreCopyFinishedResponseHandler) FileChunkDecoder(org.neo4j.causalclustering.catchup.storecopy.FileChunkDecoder) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) GetStoreIdResponseDecoder(org.neo4j.causalclustering.catchup.storecopy.GetStoreIdResponseDecoder) GetStoreRequestEncoder(org.neo4j.causalclustering.catchup.storecopy.GetStoreRequestEncoder) CoreSnapshotDecoder(org.neo4j.causalclustering.core.state.snapshot.CoreSnapshotDecoder) FileHeaderHandler(org.neo4j.causalclustering.catchup.storecopy.FileHeaderHandler) VersionDecoder(org.neo4j.causalclustering.VersionDecoder) TxStreamFinishedResponseDecoder(org.neo4j.causalclustering.catchup.tx.TxStreamFinishedResponseDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline) TxStreamFinishedResponseHandler(org.neo4j.causalclustering.catchup.tx.TxStreamFinishedResponseHandler) ExceptionMonitoringHandler(org.neo4j.causalclustering.handlers.ExceptionMonitoringHandler) FileHeaderDecoder(org.neo4j.causalclustering.catchup.storecopy.FileHeaderDecoder) GetStoreIdRequestEncoder(org.neo4j.causalclustering.catchup.storecopy.GetStoreIdRequestEncoder) GetStoreIdResponseHandler(org.neo4j.causalclustering.catchup.storecopy.GetStoreIdResponseHandler) CoreSnapshotResponseHandler(org.neo4j.causalclustering.core.state.snapshot.CoreSnapshotResponseHandler) FileChunkHandler(org.neo4j.causalclustering.catchup.storecopy.FileChunkHandler)

Aggregations

LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)34 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)27 SocketChannel (io.netty.channel.socket.SocketChannel)14 ChannelPipeline (io.netty.channel.ChannelPipeline)13 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)13 Bootstrap (io.netty.bootstrap.Bootstrap)10 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)10 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)9 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)9 StringDecoder (io.netty.handler.codec.string.StringDecoder)9 StringEncoder (io.netty.handler.codec.string.StringEncoder)9 ChannelFuture (io.netty.channel.ChannelFuture)8 Channel (io.netty.channel.Channel)7 EventLoopGroup (io.netty.channel.EventLoopGroup)5 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)5 Test (org.junit.jupiter.api.Test)5 ByteBuf (io.netty.buffer.ByteBuf)4 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)4 IOException (java.io.IOException)4 VersionPrepender (org.neo4j.causalclustering.VersionPrepender)4