diff --git a/libprotocol/pom.xml b/libprotocol/pom.xml new file mode 100644 index 0000000..fbe361c --- /dev/null +++ b/libprotocol/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + ASys Library: Protocol + + + + DmitriyMX + mail@dmiriymx.ru + + + + + UTF-8 + 1.8 + 4.0.33.Final + + + asys.lib + protocol + 0.3 + jar + + + + io.netty + netty-buffer + ${netty.version} + + + io.netty + netty-handler + ${netty.version} + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + ${java.version} + ${java.version} + ${project.build.sourceEncoding} + + + + + + \ No newline at end of file diff --git a/libprotocol/src/main/java/asys/lib/protocol/Packet.java b/libprotocol/src/main/java/asys/lib/protocol/Packet.java new file mode 100644 index 0000000..3d3b17f --- /dev/null +++ b/libprotocol/src/main/java/asys/lib/protocol/Packet.java @@ -0,0 +1,36 @@ +/* + * DmitriyMX + * 2016-08-25 + */ +package asys.lib.protocol; + +import io.netty.buffer.ByteBuf; + +import java.nio.charset.Charset; + +public abstract class Packet { + protected static final int MAGIC_NUMBER = 980986; + private static Charset charsetUtf8 = Charset.forName("UTF-8"); + + protected String readString(ByteBuf in) { + int length = in.readInt(); + if (length <= 0) return ""; + + byte[] buff = new byte[length]; + in.readBytes(buff, 0, length); + return new String(buff, charsetUtf8); + } + + protected void writeString(ByteBuf out, String string) { + int length = string.length(); + if (length <= 0) { + out.writeInt(0); + } else { + out.writeInt(length); + out.writeBytes(string.getBytes(charsetUtf8)); + } + } + + public abstract void selfRead(ByteBuf in); + public abstract void selfWrite(ByteBuf out); +} diff --git a/libprotocol/src/main/java/asys/lib/protocol/PacketDecoder.java b/libprotocol/src/main/java/asys/lib/protocol/PacketDecoder.java new file mode 100644 index 0000000..7ea0de4 --- /dev/null +++ b/libprotocol/src/main/java/asys/lib/protocol/PacketDecoder.java @@ -0,0 +1,27 @@ +/* + * DmitriyMX + * 2016-08-25 + */ +package asys.lib.protocol; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; + +import java.util.List; + +public class PacketDecoder extends ByteToMessageDecoder { + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { + if (!in.isReadable()) return; + + int magic = in.readInt(); + if (magic != Packet.MAGIC_NUMBER) + ctx.channel().close(); + + ServerInfoPacket serverInfoPacket = new ServerInfoPacket(); + serverInfoPacket.selfRead(in); + + out.add(serverInfoPacket); + } +} diff --git a/libprotocol/src/main/java/asys/lib/protocol/PacketEncoder.java b/libprotocol/src/main/java/asys/lib/protocol/PacketEncoder.java new file mode 100644 index 0000000..819b0b6 --- /dev/null +++ b/libprotocol/src/main/java/asys/lib/protocol/PacketEncoder.java @@ -0,0 +1,19 @@ +/* + * DmitriyMX + * 2016-08-25 + */ +package asys.lib.protocol; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; + +public class PacketEncoder extends MessageToByteEncoder { + @Override + protected void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf out) throws Exception { + if (!out.isWritable()) return; + + out.writeInt(Packet.MAGIC_NUMBER); + packet.selfWrite(out); + } +} diff --git a/libprotocol/src/main/java/asys/lib/protocol/ServerInfoPacket.java b/libprotocol/src/main/java/asys/lib/protocol/ServerInfoPacket.java new file mode 100644 index 0000000..c961340 --- /dev/null +++ b/libprotocol/src/main/java/asys/lib/protocol/ServerInfoPacket.java @@ -0,0 +1,59 @@ +/* + * DmitriyMX + * 2016-08-24 + */ +package asys.lib.protocol; + +import io.netty.buffer.ByteBuf; + +/* +state:byte: 0 - сервер удаляется, читается только serverId; 1 - сервер добавляется. +serverId:String: ID сервера. +address:String: полный адрес сервера (например "127.0.0.1:25565") + */ +public class ServerInfoPacket extends Packet { + private int state; + private String serverId, address; + + public ServerInfoPacket() { /* empty constructor */ } + + public ServerInfoPacket(int state, String serverId, String address) { + this.state = (byte)state; + this.serverId = serverId; + this.address = address; + + if (serverId == null || serverId.trim().isEmpty()) + throw new IllegalStateException("serverId can't be Null or Empty!"); + + if (state == 1 && (address == null || address.trim().isEmpty())) + throw new IllegalStateException("state = 1, addess can't be Null or Empty!"); + } + + public int getState() { + return state; + } + + public String getServerId() { + return serverId; + } + + public String getAddress() { + return address; + } + + @Override + public void selfRead(ByteBuf in) { + state = in.readByte(); + serverId = readString(in); + if (state == 1) + address = readString(in); + } + + @Override + public void selfWrite(ByteBuf out) { + out.writeByte(state); + writeString(out, serverId); + if (state == 1) + writeString(out, address); + } +}