com.liubs.shadowrpcfly.server.handler.ShadowChannelInitializer Maven / Gradle / Ivy
package com.liubs.shadowrpcfly.server.handler;
import com.liubs.shadowrpcfly.config.ServerConfig;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
/**
* @author Liubsyy
* @date 2023/12/4 11:45 PM
**/
public class ShadowChannelInitializer extends ChannelInitializer {
private final ServerConfig serverConfig;
public ShadowChannelInitializer(ServerConfig serverConfig) {
this.serverConfig = serverConfig;
}
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
//qps请求量统计
if(serverConfig.isQpsStat()) {
pipeline.addLast(new QpsStatHandler());
}
//处理帧边界,解决拆包和粘包问题
pipeline.addLast(new LengthFieldBasedFrameDecoder(serverConfig.getMaxFrameLength(),
0, 4, 0, 4));
//消息序列化和反序列化
pipeline.addLast(new MessageHandler());
ServerHandler serverHandler = new ServerHandler();
pipeline.addLast(serverHandler);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy