Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
chao.yu committed Aug 12, 2018
1 parent 4fd695e commit 21742a8
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

<properties>
<netty-all.version>4.1.6.Final</netty-all.version>
<lombok.version>1.16.12</lombok.version>
<fastjson.version>1.2.29</fastjson.version>
<junit.version>4.12</junit.version>
</properties>

<dependencies>
Expand All @@ -18,6 +21,25 @@
<artifactId>netty-all</artifactId>
<version>${netty-all.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>

</dependencies>

<build>
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/the/flash/protocol/command/Command.java
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 src/main/java/the/flash/protocol/command/LoginRequestPacket.java
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;
}
}
17 changes: 17 additions & 0 deletions src/main/java/the/flash/protocol/command/Packet.java
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();
}
85 changes: 85 additions & 0 deletions src/main/java/the/flash/protocol/command/PacketCodeC.java
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);
}
}
25 changes: 25 additions & 0 deletions src/main/java/the/flash/serialize/Serializer.java
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);

}
8 changes: 8 additions & 0 deletions src/main/java/the/flash/serialize/SerializerAlogrithm.java
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 src/main/java/the/flash/serialize/impl/JSONSerializer.java
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 src/test/java/the/flash/protocol/command/PacketCodeCTest.java
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));

}
}

0 comments on commit 21742a8

Please sign in to comment.