Added packet colater and descriminator.

This commit is contained in:
ProfMobius committed 2014-10-02 03:31:10 +02:00
1 parent 2b081c955b
commit 5d9e5b6806
3 files changed
+59 -8

No files matched your search

@@ -1,11 +1,19 @@
package mcp.mobius.opis.network.rconserver;
import java.util.ArrayList;
import java.util.HashMap;
import net.minecraftforge.common.util.FakePlayer;
import mcp.mobius.opis.modOpis;
import mcp.mobius.opis.network.packets.client.PacketReqChunks;
import mcp.mobius.opis.network.packets.client.PacketReqData;
import mcp.mobius.opis.network.packets.server.NetDataCommand;
import mcp.mobius.opis.network.packets.server.NetDataList;
import mcp.mobius.opis.network.packets.server.NetDataValue;
import mcp.mobius.opis.network.packets.server.PacketChunks;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
@@ -19,13 +27,28 @@ import io.netty.util.ReferenceCountUtil;
public class RConServer implements Runnable {
class ChannelInit extends ChannelInitializer<SocketChannel>{
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new RconMsgColater(), new RConInboundHandler());
}
}
public final static RConServer instance = new RConServer();
public HashMap<FakePlayer, ChannelHandlerContext> fakePlayers = new HashMap<FakePlayer, ChannelHandlerContext>();
public ArrayList<Class> packetTypes = new ArrayList<Class>();
private int port = -1;
private RConServer(){
this.port = modOpis.rconport;
packetTypes.add(PacketReqChunks.class);
packetTypes.add(PacketReqData.class);
packetTypes.add(NetDataCommand.class);
packetTypes.add(NetDataList.class);
packetTypes.add(NetDataValue.class);
packetTypes.add(PacketChunks.class);
}
@Override
@@ -38,12 +61,7 @@ public class RConServer implements Runnable {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new RConInboundHandler());
}
})
.childHandler(new ChannelInit())
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
@@ -52,7 +70,7 @@ public class RConServer implements Runnable {
f.channel().closeFuture().sync();
} catch (Exception e){
modOpis.log.severe("Error in Opis remote control server.");
modOpis.log.error("Error in Opis remote control server.");
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
@@ -0,0 +1,33 @@
package mcp.mobius.opis.network.rconserver;
import java.util.List;
import com.google.common.io.ByteStreams;
import mcp.mobius.opis.modOpis;
import mcp.mobius.opis.network.PacketBase;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
public class RconMsgColater extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (in.readableBytes() < 4) { return; } // Packet starts with the size (int)
int packetSize = in.readInt(); // We read the packet size (not including the packet type byte)
if (in.readableBytes() < 1 + packetSize) { return ;} // We check if there is at least packet type + packetSize available bytes
byte packetType = in.readByte(); // We read the header
// Here we should get a list of potential packets and decode for the corresponding byte
Class packetClass = RConServer.instance.packetTypes.get(packetType);
modOpis.log.info(String.format("%d %d %s\n", packetSize, packetType, packetClass));
PacketBase packet = (PacketBase) packetClass.newInstance();
if (packetSize > 0)
packet.decode(ByteStreams.newDataInput(in.readBytes(packetSize).array()));
out.add(packet);
}
}