Протокол связи с ASys
Протокол основан на Netty 4.x
This commit is contained in:
55
libprotocol/pom.xml
Normal file
55
libprotocol/pom.xml
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
|
||||||
|
http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<name>ASys Library: Protocol</name>
|
||||||
|
|
||||||
|
<developers>
|
||||||
|
<developer>
|
||||||
|
<name>DmitriyMX</name>
|
||||||
|
<email>mail@dmiriymx.ru</email>
|
||||||
|
</developer>
|
||||||
|
</developers>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
<netty.version>4.0.33.Final</netty.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<groupId>asys.lib</groupId>
|
||||||
|
<artifactId>protocol</artifactId>
|
||||||
|
<version>0.3</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.netty</groupId>
|
||||||
|
<artifactId>netty-buffer</artifactId>
|
||||||
|
<version>${netty.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.netty</groupId>
|
||||||
|
<artifactId>netty-handler</artifactId>
|
||||||
|
<version>${netty.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.5.1</version>
|
||||||
|
<configuration>
|
||||||
|
<source>${java.version}</source>
|
||||||
|
<target>${java.version}</target>
|
||||||
|
<encoding>${project.build.sourceEncoding}</encoding>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
36
libprotocol/src/main/java/asys/lib/protocol/Packet.java
Normal file
36
libprotocol/src/main/java/asys/lib/protocol/Packet.java
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 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<Object> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 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<Packet> {
|
||||||
|
@Override
|
||||||
|
protected void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf out) throws Exception {
|
||||||
|
if (!out.isWritable()) return;
|
||||||
|
|
||||||
|
out.writeInt(Packet.MAGIC_NUMBER);
|
||||||
|
packet.selfWrite(out);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user