MCSM: выделяем протокол в отдельный subproject
This commit is contained in:
15
bridge-protocol/build.gradle
Normal file
15
bridge-protocol/build.gradle
Normal file
@@ -0,0 +1,15 @@
|
||||
group = 'asys'
|
||||
version = '0.1-SNAPSHOT'
|
||||
|
||||
task jar(type: Jar, overwrite: true) {
|
||||
// не собирать jar
|
||||
}
|
||||
|
||||
ext {
|
||||
nettyVersion = '4.1.9.Final'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile group: 'com.google.guava', name: 'guava', version: '21.0'
|
||||
compile group: 'io.netty', name: 'netty-codec', version: nettyVersion
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-04-26
|
||||
*/
|
||||
package asys.mcsmanager.packets;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
public class CS_Handshake extends Packet {
|
||||
private String clientId;
|
||||
private String passcode;
|
||||
|
||||
public CS_Handshake() {
|
||||
}
|
||||
|
||||
public CS_Handshake(String clientId, String passcode) {
|
||||
this.clientId = clientId;
|
||||
this.passcode = passcode;
|
||||
}
|
||||
|
||||
public String getClientId() {
|
||||
return clientId;
|
||||
}
|
||||
|
||||
public String getPasscode() {
|
||||
return passcode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelfData(ByteBuf buffer) {
|
||||
this.clientId = this.readString(buffer);
|
||||
this.passcode = this.readString(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelfData(ByteBuf buffer) {
|
||||
this.writeString(buffer, clientId);
|
||||
this.writeString(buffer, passcode);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-04-26
|
||||
*/
|
||||
package asys.mcsmanager.packets;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
public class CS_Ping extends Packet {
|
||||
private long time;
|
||||
private int tps;
|
||||
private int countPlayers;
|
||||
|
||||
public CS_Ping() {
|
||||
}
|
||||
|
||||
public CS_Ping(long time, int tps, int countPlayers) {
|
||||
this.time = time;
|
||||
this.tps = tps;
|
||||
this.countPlayers = countPlayers;
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public int getTps() {
|
||||
return tps;
|
||||
}
|
||||
|
||||
public int getCountPlayers() {
|
||||
return countPlayers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelfData(ByteBuf buffer) {
|
||||
this.time = buffer.readLong();
|
||||
this.tps = buffer.readUnsignedByte();
|
||||
this.countPlayers = buffer.readUnsignedShort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelfData(ByteBuf buffer) {
|
||||
buffer.writeLong(time);
|
||||
buffer.writeByte(tps);
|
||||
buffer.writeShort(countPlayers);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-04-26
|
||||
*/
|
||||
package asys.mcsmanager.packets;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
public interface IPacketHandler {
|
||||
void handle(Packet packet, ChannelHandlerContext context);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-04-26
|
||||
*/
|
||||
package asys.mcsmanager.packets;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
public abstract class Packet {
|
||||
public String readString(ByteBuf buffer) {
|
||||
int length = buffer.readUnsignedShort();
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < length; i++) {
|
||||
sb.append(buffer.readChar());
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void writeString(ByteBuf buffer, String string) {
|
||||
int length = string.length();
|
||||
buffer.writeShort(length);
|
||||
|
||||
if (length > 0) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
buffer.writeChar(string.charAt(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void readSelfData(ByteBuf buffer);
|
||||
public abstract void writeSelfData(ByteBuf buffer);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-04-26
|
||||
*/
|
||||
package asys.mcsmanager.packets;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
public class SC_HandshakeResult extends Packet {
|
||||
private int errorCode;
|
||||
private String message;
|
||||
|
||||
public SC_HandshakeResult() {
|
||||
}
|
||||
|
||||
public SC_HandshakeResult(int errorCode, String message) {
|
||||
this.errorCode = errorCode;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public int getErrorCode() {
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelfData(ByteBuf buffer) {
|
||||
this.errorCode = buffer.readUnsignedByte();
|
||||
this.message = this.readString(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSelfData(ByteBuf buffer) {
|
||||
buffer.writeByte(errorCode);
|
||||
this.writeString(buffer, message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-04-26
|
||||
*/
|
||||
package asys.mcsmanager.packets.codec;
|
||||
|
||||
import asys.mcsmanager.packets.Packet;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ReplayingDecoder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static asys.mcsmanager.packets.codec.Params.KNOWN_HANDLERS;
|
||||
import static asys.mcsmanager.packets.codec.Params.KNOWN_PACKETS;
|
||||
|
||||
public class PacketDecoder extends ReplayingDecoder<Packet> {
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext contect, ByteBuf inBuf, List<Object> out) throws Exception {
|
||||
int id = inBuf.readUnsignedByte();
|
||||
Class<? extends Packet> pktClass = contect.channel().attr(KNOWN_PACKETS).get().get(id);
|
||||
if (pktClass == null) return;
|
||||
|
||||
if (contect.channel().attr(KNOWN_HANDLERS).get().containsKey(pktClass)) {
|
||||
Packet packet = pktClass.newInstance();
|
||||
packet.readSelfData(inBuf);
|
||||
out.add(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-04-26
|
||||
*/
|
||||
package asys.mcsmanager.packets.codec;
|
||||
|
||||
import asys.mcsmanager.packets.Packet;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
|
||||
import static asys.mcsmanager.packets.codec.Params.KNOWN_PACKETS;
|
||||
|
||||
public class PacketEncoder extends MessageToByteEncoder<Packet> {
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext context, Packet packet, ByteBuf outBuf) throws Exception {
|
||||
Integer id = context.channel().attr(KNOWN_PACKETS).get().inverse().get(packet.getClass());
|
||||
if (id == null) return; //TODO в логгере хорошо бы информировать о дропе исходящего пакета
|
||||
|
||||
outBuf.writeByte(id);
|
||||
packet.writeSelfData(outBuf);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-04-26
|
||||
*/
|
||||
package asys.mcsmanager.packets.codec;
|
||||
|
||||
import asys.mcsmanager.packets.Packet;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
|
||||
import static asys.mcsmanager.packets.codec.Params.KNOWN_HANDLERS;
|
||||
|
||||
public class PacketHandler extends SimpleChannelInboundHandler<Packet> {
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext context, Packet packet) throws Exception {
|
||||
context.channel().attr(KNOWN_HANDLERS).get().get(packet.getClass()).handle(packet, context);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-04-26
|
||||
*/
|
||||
package asys.mcsmanager.packets.codec;
|
||||
|
||||
import asys.mcsmanager.packets.Packet;
|
||||
import asys.mcsmanager.packets.IPacketHandler;
|
||||
import com.google.common.collect.BiMap;
|
||||
import io.netty.util.AttributeKey;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public final class Params {
|
||||
public static final AttributeKey<BiMap<Integer, Class<? extends Packet>>> KNOWN_PACKETS = AttributeKey.newInstance("KNOWN_PACKETS");
|
||||
public static final AttributeKey<Map<Class<? extends Packet>, IPacketHandler>> KNOWN_HANDLERS = AttributeKey.newInstance("KNOWN_HANDLERS");
|
||||
}
|
||||
Reference in New Issue
Block a user