From 0e2479c5bff5fa16b12aaf8ca493666ccc0c4278 Mon Sep 17 00:00:00 2001 From: qianzhenyu <122274659@qq.com> Date: Mon, 1 Feb 2016 11:06:36 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89protobuf=E7=BC=96?= =?UTF-8?q?=E8=A7=A3=E7=A0=81=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Maven__protobuf_protobuf_1_0_SNAPSHOT.xml | 13 + .idea/workspace.xml | 317 ++++++++++++++---- pom.xml | 1 - protobuf/pom.xml | 5 + protobuf/protobuf.iml | 1 + .../main/java/protobuf/analysis/ParseMap.java | 10 +- .../java/protobuf/code/ProtobufDecoder.java | 28 +- .../java/protobuf/code/ProtobufEncoder.java | 35 +- 8 files changed, 314 insertions(+), 96 deletions(-) create mode 100644 .idea/libraries/Maven__protobuf_protobuf_1_0_SNAPSHOT.xml diff --git a/.idea/libraries/Maven__protobuf_protobuf_1_0_SNAPSHOT.xml b/.idea/libraries/Maven__protobuf_protobuf_1_0_SNAPSHOT.xml new file mode 100644 index 0000000..7415bdd --- /dev/null +++ b/.idea/libraries/Maven__protobuf_protobuf_1_0_SNAPSHOT.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9439172..9a43be6 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,17 +2,14 @@ - - - - - - - - - + + + + + + + - @@ -50,16 +47,42 @@ - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -89,12 +112,9 @@ @@ -216,6 +240,138 @@ \ No newline at end of file diff --git a/protobuf/protobuf.iml b/protobuf/protobuf.iml index 738de35..e9ae550 100644 --- a/protobuf/protobuf.iml +++ b/protobuf/protobuf.iml @@ -12,6 +12,7 @@ + diff --git a/protobuf/src/main/java/protobuf/analysis/ParseMap.java b/protobuf/src/main/java/protobuf/analysis/ParseMap.java index 7e9da7f..625195c 100644 --- a/protobuf/src/main/java/protobuf/analysis/ParseMap.java +++ b/protobuf/src/main/java/protobuf/analysis/ParseMap.java @@ -19,6 +19,7 @@ public interface Parsing{ private static final Logger logger = LoggerFactory.getLogger(ParseMap.class); public static HashMap parseMap = new HashMap(); + public static HashMap msg2ptoNum = new HashMap(); public static void register(int ptoNum, ParseMap.Parsing cla) { if (parseMap.get(ptoNum) == null) @@ -29,13 +30,16 @@ public static void register(int ptoNum, ParseMap.Parsing cla) { } } - public static Message parse(int ptoNum, byte[] bytes) throws IOException { + public static Message getMessage(int ptoNum, byte[] bytes) throws IOException { Parsing parser = parseMap.get(ptoNum); if(parser == null) { logger.error("UnKnown Protocol Num: {}", ptoNum); } + Message msg = parser.process(bytes); - return parser.process(bytes); + if(msg2ptoNum.get(msg) != null) { + msg2ptoNum.put(msg, ptoNum); + } + return msg; } - } diff --git a/protobuf/src/main/java/protobuf/code/ProtobufDecoder.java b/protobuf/src/main/java/protobuf/code/ProtobufDecoder.java index 76eb468..fafade6 100644 --- a/protobuf/src/main/java/protobuf/code/ProtobufDecoder.java +++ b/protobuf/src/main/java/protobuf/code/ProtobufDecoder.java @@ -1,11 +1,13 @@ package protobuf.code; +import com.google.protobuf.Message; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import protobuf.analysis.ParseMap; import java.util.List; @@ -27,25 +29,17 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, in.readBytes(byteBuf); try { - byte[] inByte = byteBuf.array(); + /* 解密消息体 + ThreeDES des = ctx.channel().attr(ClientAttr.ENCRYPT).get(); + byte[] bareByte = des.decrypt(inByte);*/ - // 解密消息体 - //ThreeDES des = ctx.channel().attr(ClientAttr.ENCRYPT).get(); - //byte[] bareByte = des.decrypt(inByte); + int ptoNum = in.readInt(); + byte[] body= byteBuf.array(); - /*----------------------------------------------------------------------- - Message msg = Message.parseFrom(bareByte); - logger.info("[APP-SERVER][RECV][remoteAddress:" - + ctx.channel().remoteAddress() + "][total length:" - + length + "][bare length:" + msg.getSerializedSize() - + "]:\r\n" + msg.toString()); + Message msg = ParseMap.getMessage(ptoNum, body); + out.add(msg); + logger.info("GateServer Received Message: length {}, ptoNum: {}", length, ptoNum); - if (msg != null) { - // 获取业务消息头 - out.add(msg); - - } - -----------------------------------------------------------------------*/ } catch (Exception e) { logger.error(ctx.channel().remoteAddress() + ",decode failed.", e); } @@ -59,7 +53,7 @@ int checkLength(ChannelHandlerContext ctx, ByteBuf in){ return 0; } - int length = in.readUnsignedShort(); + int length = in.readInt(); if (length < 0) { ctx.close(); diff --git a/protobuf/src/main/java/protobuf/code/ProtobufEncoder.java b/protobuf/src/main/java/protobuf/code/ProtobufEncoder.java index ec30246..2f206c0 100644 --- a/protobuf/src/main/java/protobuf/code/ProtobufEncoder.java +++ b/protobuf/src/main/java/protobuf/code/ProtobufEncoder.java @@ -1,7 +1,40 @@ package protobuf.code; +import com.google.protobuf.Message; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import protobuf.analysis.ParseMap; + /** * Created by Administrator on 2016/1/29. */ -public class ProtobufEncoder { +public class ProtobufEncoder extends MessageToByteEncoder { + private static final Logger logger = LoggerFactory.getLogger(ProtobufEncoder.class); + + @Override + protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) + throws Exception { + + byte[] bytes = msg.toByteArray();// 将对象转换为byte + int ptoNum = ParseMap.msg2ptoNum.get(msg); + int length = bytes.length; + + /* 加密消息体 + ThreeDES des = ctx.channel().attr(ClientAttr.ENCRYPT).get(); + byte[] encryptByte = des.encrypt(bytes); + int length = encryptByte.length;*/ + + ByteBuf buf = Unpooled.buffer(8 + length); + buf.writeInt(length);// + buf.writeInt(ptoNum); + buf.writeBytes(bytes); + out.writeBytes(buf); + + logger.info("GateServer Send Message, remoteAddress: {}, length {}, ptoNum: {}", ctx.channel().remoteAddress(), length, ptoNum); + + } }