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
闫逍旭
committed
Dec 27, 2015
1 parent
e05ac76
commit a66c3ed
Showing
30 changed files
with
1,370 additions
and
392 deletions.
There are no files selected for viewing
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
24 changes: 14 additions & 10 deletions
24
mpush-connection/src/main/java/com/shinemo/mpush/connection/netty/codec/PacketEncoder.java
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,31 +1,35 @@ | ||
package com.shinemo.mpush.connection.netty.codec; | ||
|
||
import com.shinemo.mpush.api.Constants; | ||
import com.shinemo.mpush.api.protocol.Command; | ||
import com.shinemo.mpush.api.protocol.Packet; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelHandler; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.MessageToByteEncoder; | ||
|
||
/** | ||
* Created by ohun on 2015/12/19. | ||
* length(4)+cmd(1)+cc(2)+flags(1)+sessionId(4)+lrc(1)+body(n) | ||
*/ | ||
@ChannelHandler.Sharable | ||
public class PacketEncoder extends MessageToByteEncoder<Packet> { | ||
public static final PacketEncoder INSTANCE = new PacketEncoder(); | ||
|
||
@Override | ||
protected void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf out) throws Exception { | ||
out.writeByte(Constants.MAGIC_NUM1); | ||
out.writeByte(Constants.MAGIC_NUM2); | ||
out.writeInt(packet.getBodyLength()); | ||
out.writeByte(packet.command); | ||
out.writeByte(packet.flags); | ||
out.writeByte(packet.version); | ||
out.writeInt(packet.msgId); | ||
if (packet.getBodyLength() > 0) { | ||
out.writeBytes(packet.body); | ||
if (packet.cmd == Command.Heartbeat.cmd) { | ||
out.writeByte(Constants.HB); | ||
} else { | ||
out.writeInt(packet.getBodyLength()); | ||
out.writeByte(packet.cmd); | ||
out.writeShort(packet.cc); | ||
out.writeByte(packet.flags); | ||
out.writeInt(packet.sessionId); | ||
out.writeByte(packet.lrc); | ||
if (packet.getBodyLength() > 0) { | ||
out.writeBytes(packet.body); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.