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服务器已启动");
}
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);
}
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);
}
});
}
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());
}
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());
}
Aggregations