All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taobao.tdhs.client.net.netty.TDHSDecoder Maven / Gradle / Ivy

There is a newer version: 0.4.1
Show newest version
/*
 * Copyright(C) 2011-2012 Alibaba Group Holding Limited
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  Authors:
 *    wentong 
 */

package com.taobao.tdhs.client.net.netty;

import com.taobao.tdhs.client.packet.BasePacket;
import com.taobao.tdhs.client.response.TDHSResponseEnum;
import com.taobao.tdhs.client.util.ConvertUtil;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.frame.FrameDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 文通
 * @since 11-10-31 下午2:50
 */
public class TDHSDecoder extends FrameDecoder {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * Method decode ...
     *
     * @param ctx     of type ChannelHandlerContext
     * @param channel of type Channel
     * @param buffer  of type ChannelBuffer
     *
     * @return Object
     *
     * @throws Exception when
     */
    @Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer)
            throws Exception {
        if (buffer.readableBytes() < BasePacket.TDH_SOCKET_HEADER_LENGTH) {
            return null;
        }

        //read length
        long dataLength = buffer.getUnsignedInt(
                buffer.readerIndex() + BasePacket.TDH_SOCKET_HEADER_LENGTH - BasePacket.TDH_SOCKET_SIZE_LENGTH);
        if (buffer.readableBytes() < BasePacket.TDH_SOCKET_HEADER_LENGTH + dataLength) {
            return null;
        }

        //read magic code
        //long magicCode = buffer.getUnsignedInt(buffer.readerIndex());

        //read resp code
        long response = buffer.getUnsignedInt(buffer.readerIndex() + BasePacket.TDHS_MAGIC_CODE_SIZE);

        //read seq id
        long seqId = buffer.getUnsignedInt(
                buffer.readerIndex() + BasePacket.TDHS_MAGIC_CODE_SIZE + BasePacket.TDH_SOCKET_COMAND_LENGTH);

        long reserved = buffer.getUnsignedInt(
                buffer.readerIndex() + BasePacket.TDHS_MAGIC_CODE_SIZE + BasePacket.TDH_SOCKET_COMAND_LENGTH +
                        BasePacket.TDH_SOCKET_ID_LENGTH);

        byte data[] = new byte[(int) dataLength];
        int dataOffset = buffer.readerIndex() + BasePacket.TDH_SOCKET_HEADER_LENGTH;
        buffer.getBytes(dataOffset, data);
        buffer.readerIndex(dataOffset + (int) dataLength);

        if (logger.isDebugEnabled()) {
            StringBuilder sb = new StringBuilder("Response data hex:[");
            for (byte b : data) {
                sb.append(ConvertUtil.toHex(b));
                sb.append(" ");
            }
            sb.append("]");
            logger.debug(sb.toString());
        }
        return new BasePacket(TDHSResponseEnum.ClientStatus.valueOf((int) response), seqId, reserved, data);
    }

//    private long getUnsignInt(final ChannelBuffer buffer, final int pos) {
//        long ret = 0;
//        byte data[] = new byte[4];
//        buffer.getBytes(pos, data);
//        for (int i = 0; i < 4; i++) {
//            ret += ((data[i] & 0xFF) << (8 * i));
//        }
//        return ret;
//    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy