Netty 3.5到4.1.5迁移,自定义编码器无法正常工作

时间:2016-09-16 14:34:42

标签: java netty nio

我正在尝试从netty 3.5迁移到4.1.5。

我有一个处理不同请求的服务器。我使用netty编写了客户端程序,使用Netty-3与服务器通信,现在我们要迁移到4.1.5。但是在向服务器发送请求时我遇到了问题。我已经制作了一个自定义编码器来构造要发送到服务器的数据包,但它无法发送。我不知道为什么它不会进入Encoder类。

以下是我尝试过的代码:

public class FactorialClientInitializer extends ChannelInitializer<SocketChannel> {
            @Override
            public void initChannel(SocketChannel ch) {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast(new NumberEncoder());
          }
}





public class NumberEncoder extends MessageToByteEncoder<Number> {
         @Override
        protected void encode(ChannelHandlerContext ctx, Number msg, ByteBuf out) {
                     BigInteger v;
            if (msg instanceof BigInteger) {
                v = (BigInteger) msg;
            } else {
                v = new BigInteger(String.valueOf(msg));
            }
                         byte[] data = v.toByteArray();
            int dataLength = data.length;        
            out.writeByte((byte) 'F'); // magic number
            out.writeInt(dataLength);  // data length
            out.writeBytes(data);      // data
        }
    }

使用上面的代码获取输出并能够覆盖encode方法,

但是当我尝试使用自定义编码器类 SDEncoder 时,我无法覆盖encode方法

import java.util.ArrayList;
import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.handler.codec.MessageToByteEncoder;

public class SDuEncoder extends MessageToByteEncoder<SDMessage> {
    private String appPath;
    private int logLevel;

    public SDuEncoder(String appPath, int logLevel) {
        this.appPath = appPath;
        this.logLevel = logLevel;
    }

    /**
     * @param ctx
     * @param channel
     * @param msg
     * @return packet
     * @throws Exception
     */
    @Override
    protected void encode(ChannelHandlerContext ctx, SDMessage msgObj,
            ByteBuf msg) throws Exception {

System.out.println("reached");
        int cmdId = msgObj.getCommandId();
        short logHndl = msgObj.getLogHandle();
        short msgNum = msgObj.getMsgNum();
        SDuMessage SDuMsg = new SDuMessage();
        SDLog logObj = new SDLog();
        String cmdName = null;
        int status = SDBaseMsgConstants.WR_SUCCESS;

        // Usr Login
        if (cmdId == SDuMsgConstants.CommandType.XUT_USER_LOGIN.getId()) {
            cmdName = SDuMsgConstants.CommandType.XUT_USER_LOGIN.getName();
            SDU_UsrLogin obj = (SDU_UsrLogin) msgObj;

            String name = obj.getName();
            String pwd = obj.getPwd();

            status = SDuMsg.makeSendPkt(cmdId, logHndl, msgNum);
            if (status == SDBaseMsgConstants.WR_SUCCESS) {
                status = SDuMsg.addUserLogin(name, pwd);
            }

        } else {
            // throw excption for any unknown msg
            throw new SDMessageException("Invalid message id; " + cmdId
                    + " not found");
        }

        status = SDuMsg.updatePacketLen();

        ByteBuf packet = SDuMsg.getPacket();

        if (logLevel != 0) {
            byte type = packet.getByte(0);
            int size = packet.getInt(1);

            String logMsgContent = "        " + (size - 12);

            // write on log file
            logObj.writeToLog(appPath + "log/SDu_msg.log", packet, (char) type,
                    cmdName, size, logMsgContent, true, logLevel);
        }

        msg.writeBytes(packet);
        System.out.println("==============>"+packet);
        //return packet;
    }

}

如果有人指出我做错了什么会有很大的帮助。 TIA。

0 个答案:

没有答案