forked from lightningMan/flash-netty
-
Notifications
You must be signed in to change notification settings - Fork 0
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
chao.yu
committed
Aug 12, 2018
1 parent
4fd695e
commit 21742a8
Showing
9 changed files
with
235 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package the.flash.protocol.command; | ||
|
||
public interface Command { | ||
|
||
Byte LOGIN_REQUEST = 1; | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/the/flash/protocol/command/LoginRequestPacket.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,19 @@ | ||
package the.flash.protocol.command; | ||
|
||
import lombok.Data; | ||
|
||
import static the.flash.protocol.command.Command.LOGIN_REQUEST; | ||
|
||
@Data | ||
public class LoginRequestPacket extends Packet { | ||
private Integer userId; | ||
|
||
private String username; | ||
|
||
private String password; | ||
|
||
@Override | ||
public Byte getCommand() { | ||
return LOGIN_REQUEST; | ||
} | ||
} |
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,17 @@ | ||
package the.flash.protocol.command; | ||
|
||
import com.alibaba.fastjson.annotation.JSONField; | ||
import lombok.Data; | ||
|
||
@Data | ||
public abstract class Packet { | ||
/** | ||
* 协议版本 | ||
*/ | ||
@JSONField(deserialize = false, serialize = false) | ||
private Byte version = 1; | ||
|
||
|
||
@JSONField(serialize = false) | ||
public abstract Byte getCommand(); | ||
} |
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,85 @@ | ||
package the.flash.protocol.command; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.ByteBufAllocator; | ||
import the.flash.serialize.Serializer; | ||
import the.flash.serialize.impl.JSONSerializer; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static the.flash.protocol.command.Command.LOGIN_REQUEST; | ||
|
||
public class PacketCodeC { | ||
|
||
private static final int MAGIC_NUMBER = 0x1234; | ||
private static final Map<Byte, Class<? extends Packet>> packetTypeMap; | ||
private static final Map<Byte, Serializer> serializerMap; | ||
|
||
static { | ||
packetTypeMap = new HashMap<>(); | ||
packetTypeMap.put(LOGIN_REQUEST, LoginRequestPacket.class); | ||
|
||
serializerMap = new HashMap<>(); | ||
Serializer serializer = new JSONSerializer(); | ||
serializerMap.put(serializer.getSerializerAlogrithm(), serializer); | ||
} | ||
|
||
|
||
public ByteBuf encode(Packet packet) { | ||
// 1. 创建 ByteBuf 对象 | ||
ByteBuf byteBuf = ByteBufAllocator.DEFAULT.ioBuffer(); | ||
// 2. 序列化 java 对象 | ||
byte[] bytes = Serializer.DEFAULT.serialize(packet); | ||
|
||
// 3. 实际编码过程 | ||
byteBuf.writeInt(MAGIC_NUMBER); | ||
byteBuf.writeByte(packet.getVersion()); | ||
byteBuf.writeByte(Serializer.DEFAULT.getSerializerAlogrithm()); | ||
byteBuf.writeByte(packet.getCommand()); | ||
byteBuf.writeInt(bytes.length); | ||
byteBuf.writeBytes(bytes); | ||
|
||
return byteBuf; | ||
} | ||
|
||
|
||
public Packet decode(ByteBuf byteBuf) { | ||
// 跳过 magic number | ||
byteBuf.skipBytes(4); | ||
|
||
// 跳过版本号 | ||
byteBuf.skipBytes(1); | ||
|
||
// 序列化算法 | ||
byte serializeAlgorithm = byteBuf.readByte(); | ||
|
||
// 指令 | ||
byte command = byteBuf.readByte(); | ||
|
||
// 数据包长度 | ||
int length = byteBuf.readInt(); | ||
|
||
byte[] bytes = new byte[length]; | ||
byteBuf.readBytes(bytes); | ||
|
||
Class<? extends Packet> requestType = getRequestType(command); | ||
Serializer serializer = getSerializer(serializeAlgorithm); | ||
|
||
if (requestType != null && serializer != null) { | ||
return serializer.deserialize(requestType, bytes); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private Serializer getSerializer(byte serializeAlgorithm) { | ||
|
||
return serializerMap.get(serializeAlgorithm); | ||
} | ||
|
||
private Class<? extends Packet> getRequestType(byte command) { | ||
|
||
return packetTypeMap.get(command); | ||
} | ||
} |
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,25 @@ | ||
package the.flash.serialize; | ||
|
||
import the.flash.serialize.impl.JSONSerializer; | ||
|
||
public interface Serializer { | ||
|
||
Serializer DEFAULT = new JSONSerializer(); | ||
|
||
/** | ||
* 序列化算法 | ||
* @return | ||
*/ | ||
byte getSerializerAlogrithm(); | ||
|
||
/** | ||
* java 对象转换成二进制 | ||
*/ | ||
byte[] serialize(Object object); | ||
|
||
/** | ||
* 二进制转换成 java 对象 | ||
*/ | ||
<T> T deserialize(Class<T> clazz, byte[] bytes); | ||
|
||
} |
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,8 @@ | ||
package the.flash.serialize; | ||
|
||
public interface SerializerAlogrithm { | ||
/** | ||
* json 序列化 | ||
*/ | ||
byte JSON = 1; | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/the/flash/serialize/impl/JSONSerializer.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,25 @@ | ||
package the.flash.serialize.impl; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import the.flash.serialize.Serializer; | ||
import the.flash.serialize.SerializerAlogrithm; | ||
|
||
public class JSONSerializer implements Serializer { | ||
|
||
@Override | ||
public byte getSerializerAlogrithm() { | ||
return SerializerAlogrithm.JSON; | ||
} | ||
|
||
@Override | ||
public byte[] serialize(Object object) { | ||
|
||
return JSON.toJSONBytes(object); | ||
} | ||
|
||
@Override | ||
public <T> T deserialize(Class<T> clazz, byte[] bytes) { | ||
|
||
return JSON.parseObject(bytes, clazz); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/test/java/the/flash/protocol/command/PacketCodeCTest.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,28 @@ | ||
package the.flash.protocol.command; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import the.flash.serialize.Serializer; | ||
import the.flash.serialize.impl.JSONSerializer; | ||
|
||
public class PacketCodeCTest { | ||
@Test | ||
public void encode() { | ||
|
||
Serializer serializer = new JSONSerializer(); | ||
LoginRequestPacket loginRequestPacket = new LoginRequestPacket(); | ||
|
||
loginRequestPacket.setVersion(((byte) 1)); | ||
loginRequestPacket.setUserId(123); | ||
loginRequestPacket.setUsername("zhangsan"); | ||
loginRequestPacket.setPassword("password"); | ||
|
||
PacketCodeC packetCodeC = new PacketCodeC(); | ||
ByteBuf byteBuf = packetCodeC.encode(loginRequestPacket); | ||
Packet decodedPacket = packetCodeC.decode(byteBuf); | ||
|
||
Assert.assertArrayEquals(serializer.serialize(loginRequestPacket), serializer.serialize(decodedPacket)); | ||
|
||
} | ||
} |