-
Notifications
You must be signed in to change notification settings - Fork 676
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
314 additions
and
96 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Message> { | ||
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); | ||
|
||
} | ||
} |