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 23, 2015
1 parent
cb85f28
commit 23cc7a7
Showing
12 changed files
with
173 additions
and
57 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
10 changes: 10 additions & 0 deletions
10
mpush-api/src/main/java/com/shinemo/mpush/api/exception/DecodeException.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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.shinemo.mpush.api.exception; | ||
|
||
/** | ||
* Created by ohun on 2015/12/23. | ||
*/ | ||
public class DecodeException extends RuntimeException { | ||
public DecodeException(String message) { | ||
super(message); | ||
} | ||
} |
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
71 changes: 71 additions & 0 deletions
71
mpush-api/src/main/java/com/shinemo/mpush/api/protocol/PacketDecoder.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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.shinemo.mpush.api.protocol; | ||
|
||
import com.shinemo.mpush.api.Constants; | ||
import com.shinemo.mpush.api.exception.DecodeException; | ||
import com.shinemo.mpush.api.protocol.Packet; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.ByteToMessageDecoder; | ||
import io.netty.handler.codec.DecoderException; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Created by ohun on 2015/12/19. | ||
* magic(2)+length(4)+cmd(1)+version(1)+flags(1)+msgId(4)+body(n) | ||
*/ | ||
public class PacketDecoder extends ByteToMessageDecoder { | ||
|
||
@Override | ||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { | ||
decodeFrames(in, out); | ||
} | ||
|
||
private void decodeFrames(ByteBuf in, List<Object> out) throws Exception { | ||
try { | ||
while (in.readableBytes() >= Constants.HEADER_LEN) { | ||
//1.记录当前读取位置位置.如果读取到非完整的frame,要恢复到该位置,便于下次读取 | ||
in.markReaderIndex(); | ||
out.add(decodeFrame(in)); | ||
} | ||
} catch (DecodeException e) { | ||
//2.读取到不完整的frame,恢复到最近一次正常读取的位置,便于下次读取 | ||
in.resetReaderIndex(); | ||
} | ||
} | ||
|
||
private Packet decodeFrame(ByteBuf in) throws Exception { | ||
int bufferSize = in.readableBytes(); | ||
if (in.readByte() != Constants.MAGIC_NUM1 || in.readByte() != Constants.MAGIC_NUM2) { | ||
throw new RuntimeException("ERROR MAGIC_NUM"); | ||
} | ||
int bodyLength = in.readInt(); | ||
if (bufferSize < (bodyLength + Constants.HEADER_LEN)) { | ||
throw new DecodeException("invalid frame"); | ||
} | ||
return readPacket(in, bodyLength); | ||
} | ||
|
||
private Packet readPacket(ByteBuf in, int bodyLength) { | ||
byte command = in.readByte(); | ||
byte version = in.readByte(); | ||
byte flags = in.readByte(); | ||
int msgId = in.readInt(); | ||
byte[] body = null; | ||
if (bodyLength > 0) { | ||
if (bodyLength > Constants.MAX_PACKET_SIZE) { | ||
throw new RuntimeException("ERROR PACKET_SIZE:" + bodyLength); | ||
} | ||
body = new byte[bodyLength]; | ||
in.readBytes(body); | ||
} | ||
Packet packet = new Packet(); | ||
packet.command = command; | ||
packet.version = version; | ||
packet.flags = flags; | ||
packet.msgId = msgId; | ||
packet.body = body; | ||
return packet; | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
mpush-api/src/main/java/com/shinemo/mpush/api/protocol/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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.shinemo.mpush.api.protocol; | ||
|
||
import com.shinemo.mpush.api.Constants; | ||
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. | ||
*/ | ||
@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); | ||
} | ||
} | ||
} |
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
18 changes: 0 additions & 18 deletions
18
mpush-connection/src/main/java/com/shinemo/mpush/connection/PacketDecode.java
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
mpush-connection/src/main/java/com/shinemo/mpush/connection/PacketEncode.java
This file was deleted.
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