net.hasor.rsf.remoting.transport.protocol.codec.RpcResponseProtocol.java Source code

Java tutorial

Introduction

Here is the source code for net.hasor.rsf.remoting.transport.protocol.codec.RpcResponseProtocol.java

Source

/*
 * Copyright 2008-2009 the original (zyc@hasor.net).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.hasor.rsf.remoting.transport.protocol.codec;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import java.io.IOException;
import net.hasor.rsf.remoting.transport.protocol.block.ResponseSocketBlock;

/**
 * Protocol Interface,for custom network protocol
 * @version : 2014114
 * @author (zyc@hasor.net)
 */
public class RpcResponseProtocol implements Protocol<ResponseSocketBlock> {
    /**encode Message to byte & write to network framework*/
    public void encode(ResponseSocketBlock resMsg, ByteBuf buf) throws IOException {
        //
        //* --------------------------------------------------------bytes =13
        //* byte[1]  version                              RSF(0x81)
        buf.writeByte(resMsg.getVersion());
        //* byte[8]  requestID                            ID
        buf.writeLong(resMsg.getRequestID());
        //* byte[1]  keepData                             ?
        buf.writeByte(0);
        //* byte[3]  contentLength                        ?(max = 16MB)
        ByteBuf responseBody = this.encodeResponse(resMsg);
        int bodyLength = responseBody.readableBytes();
        bodyLength = (bodyLength << 8) >>> 8;//8??8??16777215?
        buf.writeMedium(bodyLength);
        //
        buf.writeBytes(responseBody);
        //
    }

    //
    private ByteBuf encodeResponse(ResponseSocketBlock resMsg) {
        ByteBuf bodyBuf = ByteBufAllocator.DEFAULT.heapBuffer();
        //
        //* --------------------------------------------------------bytes =8
        //* byte[2]  status                               ??
        bodyBuf.writeShort(resMsg.getStatus());
        //* byte[2]  serializeType-(attr-index)           ?
        bodyBuf.writeShort(resMsg.getSerializeType());
        //* byte[2]  returnType-(attr-index)              
        bodyBuf.writeShort(resMsg.getReturnType());
        //* byte[2]  returnData-(attr-index)              ?
        bodyBuf.writeShort(resMsg.getReturnData());
        //* --------------------------------------------------------bytes =1 ~ 1021
        //* byte[1]  optionCount                          ?
        int[] optionMapping = resMsg.getOptions();
        bodyBuf.writeByte(optionMapping.length);
        for (int i = 0; i < optionMapping.length; i++) {
            //* byte[4]  ptype-0-(attr-index,attr-index)  ?1
            //* byte[4]  ptype-1-(attr-index,attr-index)  ?2
            bodyBuf.writeInt(optionMapping[i]);
        }
        //* --------------------------------------------------------bytes =6 ~ 8192
        //* byte[2]  attrPool-size (Max = 2047)           ? 0x07FF
        int[] poolData = resMsg.getPoolData();
        bodyBuf.writeShort(poolData.length);
        for (int i = 0; i < poolData.length; i++) {
            //* byte[4]  ptype-0-(attr-index,attr-index)  1?
            //* byte[4]  ptype-1-(attr-index,attr-index)  2?
            bodyBuf.writeInt(poolData[i]);
        }
        //* --------------------------------------------------------bytes =n
        //* dataBody                                      ?
        resMsg.fillTo(bodyBuf);
        return bodyBuf;
    }

    //
    //
    //
    /**decode stream to object*/
    public ResponseSocketBlock decode(ByteBuf buf) throws IOException {
        //* --------------------------------------------------------bytes =13
        //* byte[1]  version                              RSF(0x80)
        byte version = buf.readByte();
        //* byte[8]  requestID                            ?ID
        long requestID = buf.readLong();
        //* byte[1]  keepData                             ?
        buf.skipBytes(1);
        //* byte[3]  contentLength                        ?
        buf.skipBytes(3);//.readUnsignedMedium()
        //
        ResponseSocketBlock res = new ResponseSocketBlock();
        res.setVersion(version);
        res.setRequestID(requestID);
        //* --------------------------------------------------------bytes =8
        //* byte[2]  status                               ??
        res.setStatus(buf.readShort());
        //* byte[2]  serializeType-(attr-index)           ?
        res.setSerializeType(buf.readShort());
        //* byte[2]  returnType-(attr-index)              
        res.setReturnType(buf.readShort());
        //* byte[2]  returnData-(attr-index)              ?
        res.setReturnData(buf.readShort());
        //* --------------------------------------------------------bytes =1 ~ 1021
        //* byte[1]  optionCount                          ?
        byte optionCount = buf.readByte();
        for (int i = 0; i < optionCount; i++) {
            //* byte[4]  attr-0-(attr-index,attr-index)   ?
            int mergeData = buf.readInt();
            res.addOption(mergeData);
        }
        //* --------------------------------------------------------bytes =6 ~ 8192
        //* byte[2]  attrPool-size (Max = 2047)           ?
        short attrPoolSize = buf.readShort();
        for (int i = 0; i < attrPoolSize; i++) {
            //* byte[4] att-length                        1?
            int length = buf.readInt();
            res.addPoolData(length);
        }
        //* --------------------------------------------------------bytes =n
        //* dataBody                                      ?
        res.fillFrom(buf.readBytes(res.getPoolSize()));
        return res;
    }
}