@Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); logger.info("init channel {}", ch); // 压缩算法 // 加密算法 pipeline.addLast(new LoggingHandler("LOGGER", LogLevel.INFO)); // 解码器 pipeline.addLast( DEFAULT_DECODER_LENGTH, new LengthFieldBasedFrameDecoder( 2048, 0, 4 /** ,0,4 */ )); pipeline.addLast( DEFAULT_DECODER, new DefaultNettyDecoder(this.netServiceHandler.packetAllocator())); pipeline.addLast(DEFAULT_ENCODER_LENGTH_APPENDER, new LengthFieldPrepender(4)); pipeline.addLast(DEFAULT_ENCODER, new DefaultNettyEncoder()); // 处理器 pipeline.addLast(HANDLER, new DefaultNettyHandler(wrapNetConnectionManager())); }
@Override public void enableChannel(NetConnectionType connectionType, Channel ch) throws Exception { if (null == connectionType) return; if (null == ch) return; ChannelPipeline pipeline = ch.pipeline(); switch (connectionType) { case NODE_IN_AGENT_CHAT: case NODE_IN_AGENT_LOGINSERVER: case NODE_IN_AGENT_NPC: case NODE_IN_AGENT_SCENE: logger.info("{} add agent codec", ch); // pipeline.addBefore(DEFAULT_DECODER_LENGTH, AGENT_PACKET_DECODER_LENGTH, new // LengthFieldBasedFrameDecoder(2048,0, 4/** ,0,4*/)); pipeline.addBefore( DEFAULT_DECODER_LENGTH, AGENT_PACKET_DECODER, new AgentNettyDecoder(netServiceHandler)); pipeline.addBefore( DEFAULT_ENCODER_LENGTH_APPENDER, AGENT_PACKET_ENCODER, new AgentNettyEncoder()); // pipeline.addBefore(DEFAULT_ENCODER_LENGTH_APPENDER, AGENT_PACKET_ENCODER_LENGTH_APPENDER, // new LengthFieldPrepender(4)); break; default: break; } }
@Override public void propertyChange(PropertyChangeEvent evt) { if (null == evt.getNewValue()) return; NettyConnection source = (NettyConnection) evt.getSource(); NetConnectionType newValue = (NetConnectionType) evt.getNewValue(); Channel channel = source.getChannel(); logger.info("{} Update Codec", channel); try { enableChannel(newValue, channel); } catch (Exception e) { e.printStackTrace(); } }