Java tutorial
/* * 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.RequestSocketBlock; /** * Protocol Interface,for custom network protocol * @version : 20141025 * @author (zyc@hasor.net) */ public class RpcRequestProtocol implements Protocol<RequestSocketBlock> { /**encode Message to byte & write to network framework*/ public void encode(RequestSocketBlock reqMsg, ByteBuf buf) throws IOException { //* --------------------------------------------------------bytes =13 //* byte[1] version RSF(0xC1) buf.writeByte(reqMsg.getVersion()); //* byte[8] requestID ID buf.writeLong(reqMsg.getRequestID()); //* byte[1] keepData ? buf.writeByte(0); //* byte[3] contentLength ?(max = 16MB) ByteBuf requestBody = this.encodeRequest(reqMsg); int bodyLength = requestBody.readableBytes(); bodyLength = (bodyLength << 8) >>> 8;//8??8??16777215? buf.writeMedium(bodyLength); // buf.writeBytes(requestBody); } // private ByteBuf encodeRequest(RequestSocketBlock reqMsg) { ByteBuf bodyBuf = ByteBufAllocator.DEFAULT.heapBuffer(); //* --------------------------------------------------------bytes =14 //* byte[2] servicesName-(attr-index) ??? bodyBuf.writeShort(reqMsg.getServiceName()); //* byte[2] servicesGroup-(attr-index) ? bodyBuf.writeShort(reqMsg.getServiceGroup()); //* byte[2] servicesVersion-(attr-index) ? bodyBuf.writeShort(reqMsg.getServiceVersion()); //* byte[2] servicesMethod-(attr-index) ??? bodyBuf.writeShort(reqMsg.getTargetMethod()); //* byte[2] serializeType-(attr-index) ? bodyBuf.writeShort(reqMsg.getSerializeType()); //* byte[4] clientTimeout bodyBuf.writeInt(reqMsg.getClientTimeout()); //* --------------------------------------------------------bytes =1 ~ 1021 //* byte[1] paramCount ? int[] paramMapping = reqMsg.getParameters(); bodyBuf.writeByte(paramMapping.length); for (int i = 0; i < paramMapping.length; i++) { //* byte[4] ptype-0-(attr-index,attr-index) ?1 //* byte[4] ptype-1-(attr-index,attr-index) ?2 bodyBuf.writeInt(paramMapping[i]); } //* --------------------------------------------------------bytes =1 ~ 1021 //* byte[1] optionCount ? int[] optionMapping = reqMsg.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 = reqMsg.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) 1? bodyBuf.writeInt(poolData[i]); } //* --------------------------------------------------------bytes =n //* dataBody ? reqMsg.fillTo(bodyBuf); return bodyBuf; } // // // /**decode stream to object*/ public RequestSocketBlock decode(ByteBuf buf) throws IOException { //* --------------------------------------------------------bytes =13 //* byte[1] version RSF(0xC1) 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() // RequestSocketBlock req = new RequestSocketBlock(); req.setVersion(version); req.setRequestID(requestID); //* --------------------------------------------------------bytes =14 //* byte[2] servicesName-(attr-index) ??? req.setServiceName(buf.readShort()); //* byte[2] servicesGroup-(attr-index) ? req.setServiceGroup(buf.readShort()); //* byte[2] servicesVersion-(attr-index) ? req.setServiceVersion(buf.readShort()); //* byte[2] servicesMethod-(attr-index) ??? req.setTargetMethod(buf.readShort()); //* byte[2] serializeType-(attr-index) ? req.setSerializeType(buf.readShort()); //* byte[4] clientTimeout req.setClientTimeout(buf.readInt()); //* --------------------------------------------------------bytes =1 ~ 1021 //* byte[1] paramCount ? byte paramCount = buf.readByte(); for (int i = 0; i < paramCount; i++) { //* byte[4] ptype-0-(attr-index,attr-index) ? int mergeData = buf.readInt(); req.addParameter(mergeData); } //* --------------------------------------------------------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(); req.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(); req.addPoolData(length); } //* --------------------------------------------------------bytes =n //* dataBody ? req.fillFrom(buf.readBytes(req.getPoolSize())); return req; } }