From f1df48b46826fb2f31494499e74b94e33f1aadb2 Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Wed, 17 Jun 2020 22:02:33 -0700 Subject: [PATCH] Changed ServerHandler to be a subclass of PacketHandler -- replaced anon PacketHandler with ServerHandler impl --- .../src/com/riiablo/server/netty/Main.java | 96 ++++++------------- 1 file changed, 28 insertions(+), 68 deletions(-) diff --git a/server/netty/src/com/riiablo/server/netty/Main.java b/server/netty/src/com/riiablo/server/netty/Main.java index 3a25a0cd..a85e93aa 100644 --- a/server/netty/src/com/riiablo/server/netty/Main.java +++ b/server/netty/src/com/riiablo/server/netty/Main.java @@ -1,18 +1,14 @@ package com.riiablo.server.netty; import io.netty.bootstrap.Bootstrap; -import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; -import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.DatagramChannel; -import io.netty.channel.socket.DatagramPacket; import io.netty.channel.socket.nio.NioDatagramChannel; -import java.nio.ByteBuffer; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; @@ -28,7 +24,6 @@ import com.riiablo.Riiablo; import com.riiablo.codec.Animation; import com.riiablo.net.packet.netty.Connection; import com.riiablo.net.packet.netty.Disconnect; -import com.riiablo.net.packet.netty.Header; import com.riiablo.net.packet.netty.Netty; import com.riiablo.net.packet.netty.NettyData; import com.riiablo.net.packet.netty.Ping; @@ -78,38 +73,7 @@ public class Main extends ApplicationAdapter { protected void initChannel(DatagramChannel ch) { ch.pipeline() .addLast(new ReliableInboundHandler()) - .addLast(new PacketHandler() { - final String TAG = "PacketHandler$1"; - - @Override - protected void processPacket(ChannelHandlerContext ctx, Netty netty) { - Gdx.app.debug(TAG, "Processing packet..."); - byte dataType = netty.dataType(); - if (0 <= dataType && dataType < NettyData.names.length) { - Gdx.app.debug(TAG, "dataType=" + NettyData.name(dataType)); - } - switch (dataType) { - case NettyData.Connection: { - Connection packet = (Connection) netty.data(new Connection()); - break; - } - case NettyData.Disconnect: { - Disconnect packet = (Disconnect) netty.data(new Disconnect()); - break; - } - case NettyData.Ping: { - Ping ping = (Ping) netty.data(new Ping()); - break; - } - default: - if (0 <= dataType && dataType < NettyData.names.length) { - Gdx.app.error(TAG, "Ignoring packet /w data type " + dataType + " (" + NettyData.name(dataType) + ")"); - } else { - Gdx.app.error(TAG, "Ignoring packet /w data type " + dataType); - } - } - } - }) + .addLast(new ServerHandler()) .addLast(new ReliableOutboundHandler()) ; } @@ -126,40 +90,36 @@ public class Main extends ApplicationAdapter { } } - public static class ServerHandler extends SimpleChannelInboundHandler { - ServerHandler() { - super(false); - } + public static class ServerHandler extends PacketHandler { + private static final String TAG = "ServerHandler"; @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - System.out.println("Channel active."); - } - - @Override - protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception { - Gdx.app.log(TAG, "Packet from " + msg.sender().getHostName() + ":" + msg.sender().getPort()); - ByteBuf in = msg.content(); - try { - ByteBuffer buffer = in.nioBuffer(); - Packet packet = Packet.obtain(0, buffer); - Gdx.app.log(TAG, " " + NettyData.name(packet.data.dataType())); - Header header = packet.data.header(new Header()); - Gdx.app.log(TAG, " " + String.format("SEQ:%d ACK:%d ACK_BITS:%08x", header.sequence(), header.ack(), header.ackBits())); - } finally { - in.release(); // TODO: release after packet is processed by server + protected void processPacket(ChannelHandlerContext ctx, Netty netty) { + Gdx.app.debug(TAG, "Processing packet..."); + byte dataType = netty.dataType(); + if (0 <= dataType && dataType < NettyData.names.length) { + Gdx.app.debug(TAG, "dataType=" + NettyData.name(dataType)); + } + switch (dataType) { + case NettyData.Connection: { + Connection packet = (Connection) netty.data(new Connection()); + break; + } + case NettyData.Disconnect: { + Disconnect packet = (Disconnect) netty.data(new Disconnect()); + break; + } + case NettyData.Ping: { + Ping ping = (Ping) netty.data(new Ping()); + break; + } + default: + if (0 <= dataType && dataType < NettyData.names.length) { + Gdx.app.error(TAG, "Ignoring packet /w data type " + dataType + " (" + NettyData.name(dataType) + ")"); + } else { + Gdx.app.error(TAG, "Ignoring packet /w data type " + dataType); + } } - } - - @Override - public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { - System.out.println("Read complete."); - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { - Gdx.app.error(TAG, cause.getMessage(), cause); - ctx.close(); } } }