From 8eee5fd8f36df0bef3f663929612b6ce77eb3ba5 Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Wed, 24 Jun 2020 17:40:30 -0700 Subject: [PATCH] Modified generics with API - endpoint should work with any Object as QoS param --- server/netty/src/com/riiablo/net/Endpoint.java | 2 +- .../src/com/riiablo/net/EndpointedChannelHandler.java | 6 +++--- .../src/com/riiablo/net/reliable/ReliableEndpoint.java | 9 +++++---- .../netty/src/com/riiablo/net/reliable/TestClient.java | 6 ++++-- .../netty/src/com/riiablo/net/reliable/TestServer.java | 6 ++++-- server/netty/src/com/riiablo/net/tcp/TcpEndpoint.java | 2 +- server/netty/src/com/riiablo/net/tcp/TestClient.java | 5 +++-- server/netty/src/com/riiablo/net/tcp/TestServer.java | 5 +++-- 8 files changed, 24 insertions(+), 17 deletions(-) diff --git a/server/netty/src/com/riiablo/net/Endpoint.java b/server/netty/src/com/riiablo/net/Endpoint.java index 3d33eed0..e86a0dce 100644 --- a/server/netty/src/com/riiablo/net/Endpoint.java +++ b/server/netty/src/com/riiablo/net/Endpoint.java @@ -2,7 +2,7 @@ package com.riiablo.net; import io.netty.channel.ChannelHandlerContext; -public interface Endpoint extends PacketSender { +public interface Endpoint extends PacketSender { void reset(); void update(float delta); void messageReceived(ChannelHandlerContext ctx, T msg); diff --git a/server/netty/src/com/riiablo/net/EndpointedChannelHandler.java b/server/netty/src/com/riiablo/net/EndpointedChannelHandler.java index a92fd324..6aaa6b62 100644 --- a/server/netty/src/com/riiablo/net/EndpointedChannelHandler.java +++ b/server/netty/src/com/riiablo/net/EndpointedChannelHandler.java @@ -15,7 +15,7 @@ import java.net.SocketAddress; import com.badlogic.gdx.Gdx; -public class EndpointedChannelHandler implements ChannelHandler, ChannelInboundHandler, ChannelOutboundHandler { +public class EndpointedChannelHandler implements ChannelHandler, ChannelInboundHandler, ChannelOutboundHandler { private static final String TAG = "EndpointedChannelHandler"; private static final boolean DEBUG = true; @@ -24,9 +24,9 @@ public class EndpointedChannelHandler implements ChannelHandler, ChannelIn private static final boolean DEBUG_OUTBOUND = DEBUG && true; private final TypeParameterMatcher matcher; - private final Endpoint endpoint; + private final Endpoint endpoint; - public EndpointedChannelHandler(Class packetType, Endpoint endpoint) { + public EndpointedChannelHandler(Class packetType, Endpoint endpoint) { this.endpoint = endpoint; matcher = TypeParameterMatcher.get(packetType); } diff --git a/server/netty/src/com/riiablo/net/reliable/ReliableEndpoint.java b/server/netty/src/com/riiablo/net/reliable/ReliableEndpoint.java index 67c7b29c..ceafbb5d 100644 --- a/server/netty/src/com/riiablo/net/reliable/ReliableEndpoint.java +++ b/server/netty/src/com/riiablo/net/reliable/ReliableEndpoint.java @@ -16,7 +16,7 @@ import com.riiablo.net.reliable.channel.UnreliableMessageChannel; import com.riiablo.net.reliable.channel.UnreliableOrderedMessageChannel; import com.riiablo.util.EnumIntMap; -public class ReliableEndpoint implements Endpoint, MessageChannel.PacketTransceiver { +public class ReliableEndpoint implements Endpoint, MessageChannel.PacketTransceiver { private static final String TAG = "ReliableEndpoint"; private static final boolean DEBUG = true; @@ -69,10 +69,11 @@ public class ReliableEndpoint implements Endpoint, MessageC } @Override - public void sendMessage(QoS qos, ByteBuffer bb) { + public void sendMessage(Object qos, ByteBuffer bb) { if (DEBUG_SEND) Log.debug(TAG, "sendMessage"); - if (DEBUG_QOS) Log.debug(TAG, "sending message with %s QoS (0x%02x)", qos, qos.ordinal()); - int channelId = defaultChannels.get(qos); + assert qos instanceof QoS; + if (DEBUG_QOS) Log.debug(TAG, "sending message with %s QoS (0x%02x)", qos, ((QoS) qos).ordinal()); + int channelId = defaultChannels.get((QoS) qos); sendMessage(channelId, bb); } diff --git a/server/netty/src/com/riiablo/net/reliable/TestClient.java b/server/netty/src/com/riiablo/net/reliable/TestClient.java index bf9a1408..79762404 100644 --- a/server/netty/src/com/riiablo/net/reliable/TestClient.java +++ b/server/netty/src/com/riiablo/net/reliable/TestClient.java @@ -37,7 +37,7 @@ public class TestClient extends ApplicationAdapter implements PacketProcessor { new HeadlessApplication(new TestClient(), config); } - private Endpoint endpoint; + private Endpoint endpoint; @Override public void create() { @@ -51,7 +51,8 @@ public class TestClient extends ApplicationAdapter implements PacketProcessor { .handler(new ChannelInitializer() { @Override protected void initChannel(DatagramChannel ch) { - endpoint = new ReliableEndpoint(ch, TestClient.this); + Endpoint endpoint = new ReliableEndpoint(ch, TestClient.this); + TestClient.this.endpoint = endpoint; ch.pipeline() .addLast(new EndpointedChannelHandler<>(DatagramPacket.class, endpoint)) ; @@ -65,6 +66,7 @@ public class TestClient extends ApplicationAdapter implements PacketProcessor { Gdx.app.error(TAG, t.getMessage(), t); } finally { group.shutdownGracefully(); + Gdx.app.exit(); } } diff --git a/server/netty/src/com/riiablo/net/reliable/TestServer.java b/server/netty/src/com/riiablo/net/reliable/TestServer.java index c6b84b2f..5efa4d9e 100644 --- a/server/netty/src/com/riiablo/net/reliable/TestServer.java +++ b/server/netty/src/com/riiablo/net/reliable/TestServer.java @@ -37,7 +37,7 @@ public class TestServer extends ApplicationAdapter implements PacketProcessor { new HeadlessApplication(new TestServer(), config); } - private Endpoint endpoint; + private Endpoint endpoint; @Override public void create() { @@ -52,7 +52,8 @@ public class TestServer extends ApplicationAdapter implements PacketProcessor { .handler(new ChannelInitializer() { @Override protected void initChannel(DatagramChannel ch) { - endpoint = new ReliableEndpoint(ch, TestServer.this); + ReliableEndpoint endpoint = new ReliableEndpoint(ch, TestServer.this); + TestServer.this.endpoint = endpoint; ch.pipeline() .addLast(new EndpointedChannelHandler<>(DatagramPacket.class, endpoint)) ; @@ -66,6 +67,7 @@ public class TestServer extends ApplicationAdapter implements PacketProcessor { Gdx.app.error(TAG, t.getMessage(), t); } finally { group.shutdownGracefully(); + Gdx.app.exit(); } } diff --git a/server/netty/src/com/riiablo/net/tcp/TcpEndpoint.java b/server/netty/src/com/riiablo/net/tcp/TcpEndpoint.java index 579f6b15..65ecd105 100644 --- a/server/netty/src/com/riiablo/net/tcp/TcpEndpoint.java +++ b/server/netty/src/com/riiablo/net/tcp/TcpEndpoint.java @@ -11,7 +11,7 @@ import com.badlogic.gdx.Gdx; import com.riiablo.net.Endpoint; import com.riiablo.net.PacketProcessor; -public class TcpEndpoint implements Endpoint { +public class TcpEndpoint implements Endpoint { private static final String TAG = "TcpEndpoint"; private static final boolean DEBUG = true; diff --git a/server/netty/src/com/riiablo/net/tcp/TestClient.java b/server/netty/src/com/riiablo/net/tcp/TestClient.java index 1b437ef0..2e1d7143 100644 --- a/server/netty/src/com/riiablo/net/tcp/TestClient.java +++ b/server/netty/src/com/riiablo/net/tcp/TestClient.java @@ -38,7 +38,7 @@ public class TestClient extends ApplicationAdapter implements PacketProcessor { new HeadlessApplication(new TestClient(), config); } - private Endpoint endpoint; + private Endpoint endpoint; @Override public void create() { @@ -53,7 +53,8 @@ public class TestClient extends ApplicationAdapter implements PacketProcessor { .handler(new ChannelInitializer() { @Override protected void initChannel(SocketChannel ch) { - endpoint = new TcpEndpoint(ch, TestClient.this); + Endpoint endpoint = new TcpEndpoint(ch, TestClient.this); + TestClient.this.endpoint = endpoint; ch.pipeline() .addLast(new EndpointedChannelHandler<>(ByteBuf.class, endpoint)) ; diff --git a/server/netty/src/com/riiablo/net/tcp/TestServer.java b/server/netty/src/com/riiablo/net/tcp/TestServer.java index 25fd4afb..f3470cf0 100644 --- a/server/netty/src/com/riiablo/net/tcp/TestServer.java +++ b/server/netty/src/com/riiablo/net/tcp/TestServer.java @@ -33,7 +33,7 @@ public class TestServer extends ApplicationAdapter implements PacketProcessor { new HeadlessApplication(new TestServer(), config); } - private Endpoint endpoint; + private Endpoint endpoint; @Override public void create() { @@ -48,7 +48,8 @@ public class TestServer extends ApplicationAdapter implements PacketProcessor { .childHandler(new ChannelInitializer() { @Override protected void initChannel(SocketChannel ch) { - endpoint = new TcpEndpoint(ch, TestServer.this); + TcpEndpoint endpoint = new TcpEndpoint(ch, TestServer.this); + TestServer.this.endpoint = endpoint; ch.pipeline() .addLast(new EndpointedChannelHandler<>(ByteBuf.class, endpoint)) ;