From a33cdac5affcc747d381261e4060da54a14e63ed Mon Sep 17 00:00:00 2001 From: Samson Close Date: Sat, 27 Aug 2016 12:57:21 -0400 Subject: [PATCH] Unified socket receivers --- src/ca/afroman/client/ClientGame.java | 27 ++----- .../afroman/client/ClientSocketManager.java | 7 +- .../afroman/client/ClientSocketReceive.java | 53 ------------- src/ca/afroman/interfaces/IPacketParser.java | 12 +++ src/ca/afroman/server/ServerGame.java | 9 ++- .../afroman/server/ServerSocketManager.java | 6 +- .../afroman/server/ServerSocketReceive.java | 72 ------------------ src/ca/afroman/server/ServerSocketSend.java | 13 +--- src/ca/afroman/server/SocketReceive.java | 74 +++++++++++++++++++ 9 files changed, 108 insertions(+), 165 deletions(-) delete mode 100644 src/ca/afroman/client/ClientSocketReceive.java create mode 100644 src/ca/afroman/interfaces/IPacketParser.java delete mode 100644 src/ca/afroman/server/ServerSocketReceive.java create mode 100644 src/ca/afroman/server/SocketReceive.java diff --git a/src/ca/afroman/client/ClientGame.java b/src/ca/afroman/client/ClientGame.java index 72e779e..7cdf75e 100644 --- a/src/ca/afroman/client/ClientGame.java +++ b/src/ca/afroman/client/ClientGame.java @@ -47,6 +47,7 @@ import ca.afroman.gui.GuiScreen; import ca.afroman.gui.GuiSendingLevels; import ca.afroman.input.InputHandler; +import ca.afroman.interfaces.IPacketParser; import ca.afroman.level.ClientLevel; import ca.afroman.level.LevelObjectType; import ca.afroman.level.LevelType; @@ -69,7 +70,7 @@ import ca.afroman.util.VersionUtil; import samson.stream.Console; -public class ClientGame extends DynamicTickRenderThread +public class ClientGame extends DynamicTickRenderThread implements IPacketParser { public static final int WIDTH = 240; public static final int HEIGHT = WIDTH / 16 * 9; @@ -153,7 +154,8 @@ public ClientGame() super(newDefaultThreadGroupInstance(), "Game", 60); } - public void addPacketToProcess(BytePacket pack) + @Override + public void addPacketToParse(BytePacket pack) { synchronized (toSend) { @@ -439,12 +441,6 @@ public void componentShown(ComponentEvent e) final Texture loading = Texture.fromResource(AssetType.INVALID, "loading.png"); DynamicThread renderLoading = new DynamicThread(this.getThreadGroup(), "Loading-Display") { - @Override - public void onPause() - { - - } - @Override public void onRun() { @@ -460,23 +456,12 @@ public void onRun() } } - @Override - public void onStart() - { - - } - @Override public void onStop() { + super.onStop(); loading.getImage().flush(); } - - @Override - public void onUnpause() - { - - } }; renderLoading.startThis(); @@ -531,6 +516,7 @@ public void onUnpause() @Override public void onStop() { + super.onStop(); // TODO always have socket manager open? if (socketManager != null) socketManager.stopThis(); receivedPackets.clear(); @@ -544,6 +530,7 @@ public void onStop() if (sockets() != null) sockets().stopThis(); } + @Override public void parsePacket(BytePacket packet) { IPConnection sender = packet.getConnections().get(0); diff --git a/src/ca/afroman/client/ClientSocketManager.java b/src/ca/afroman/client/ClientSocketManager.java index 9bdabab..bc4c149 100644 --- a/src/ca/afroman/client/ClientSocketManager.java +++ b/src/ca/afroman/client/ClientSocketManager.java @@ -15,6 +15,7 @@ import ca.afroman.network.ConnectedPlayer; import ca.afroman.network.IPConnectedPlayer; import ca.afroman.network.IPConnection; +import ca.afroman.server.SocketReceive; public class ClientSocketManager implements IDynamicRunning { @@ -34,7 +35,7 @@ public static ThreadGroup threadGroupInstance() private IPConnectedPlayer serverConnection; private DatagramSocket socket; - private ClientSocketReceive rSocket; + private SocketReceive rSocket; private ClientSocketSend sSocket; public ClientSocketManager() @@ -52,7 +53,7 @@ public ClientSocketManager() ClientGame.instance().logger().log(ALogType.CRITICAL, "", e); } - rSocket = new ClientSocketReceive(this); + rSocket = new SocketReceive(socket, false, ClientGame.instance()); sSocket = new ClientSocketSend(this); } @@ -108,7 +109,7 @@ public ConnectedPlayer playerByRole(Role role) return null; } - public ClientSocketReceive receiver() + public SocketReceive receiver() { return rSocket; } diff --git a/src/ca/afroman/client/ClientSocketReceive.java b/src/ca/afroman/client/ClientSocketReceive.java deleted file mode 100644 index 5c5a09b..0000000 --- a/src/ca/afroman/client/ClientSocketReceive.java +++ /dev/null @@ -1,53 +0,0 @@ -package ca.afroman.client; - -import java.io.IOException; -import java.net.DatagramPacket; -import java.net.PortUnreachableException; - -import ca.afroman.gui.GuiClickNotification; -import ca.afroman.gui.GuiJoinServer; -import ca.afroman.gui.GuiMainMenu; -import ca.afroman.log.ALogType; -import ca.afroman.log.ALogger; -import ca.afroman.network.IPConnection; -import ca.afroman.packet.BytePacket; -import ca.afroman.thread.DynamicThread; - -public class ClientSocketReceive extends DynamicThread -{ - private ClientSocketManager manager; - - public ClientSocketReceive(ClientSocketManager manager) - { - super(ClientSocketManager.threadGroupInstance(), "Receive"); - - this.manager = manager; - } - - @Override - public void onRun() - { - byte[] buffer = new byte[ClientGame.RECEIVE_PACKET_BUFFER_LIMIT]; - - // Loads up the buffer with incoming data - DatagramPacket packet = new DatagramPacket(buffer, buffer.length); - - try - { - manager.socket().receive(packet); - BytePacket pack = new BytePacket(packet.getData(), new IPConnection(packet.getAddress(), packet.getPort())); - if (ALogger.tracePackets) logger().log(ALogType.DEBUG, "[" + pack.getConnections().get(0).asReadable() + "] " + pack.getType()); - ClientGame.instance().addPacketToProcess(pack); - } - catch (PortUnreachableException e) - { - logger().log(ALogType.CRITICAL, "Port is unreachable", e); - ClientGame.instance().setCurrentScreen(new GuiJoinServer(new GuiMainMenu())); - new GuiClickNotification(ClientGame.instance().getCurrentScreen(), "PORT", "UNREACHABLE"); - } - catch (IOException e) - { - logger().log(ALogType.CRITICAL, "I/O error while reading packet", e); - } - } -} diff --git a/src/ca/afroman/interfaces/IPacketParser.java b/src/ca/afroman/interfaces/IPacketParser.java new file mode 100644 index 0000000..52fed32 --- /dev/null +++ b/src/ca/afroman/interfaces/IPacketParser.java @@ -0,0 +1,12 @@ +package ca.afroman.interfaces; + +import ca.afroman.packet.BytePacket; + +public interface IPacketParser +{ + public void addPacketToParse(BytePacket packet); + + public ThreadGroup getThreadGroup(); + + public void parsePacket(BytePacket packet); +} diff --git a/src/ca/afroman/server/ServerGame.java b/src/ca/afroman/server/ServerGame.java index fc0c14e..bfcc82c 100644 --- a/src/ca/afroman/server/ServerGame.java +++ b/src/ca/afroman/server/ServerGame.java @@ -15,6 +15,7 @@ import ca.afroman.events.IEvent; import ca.afroman.events.TriggerType; import ca.afroman.gfx.PointLight; +import ca.afroman.interfaces.IPacketParser; import ca.afroman.level.Level; import ca.afroman.level.LevelObjectType; import ca.afroman.level.LevelType; @@ -39,7 +40,7 @@ import ca.afroman.util.ByteUtil; import ca.afroman.util.VersionUtil; -public class ServerGame extends DynamicTickThread +public class ServerGame extends DynamicTickThread implements IPacketParser { private static ServerGame game = null; @@ -92,7 +93,8 @@ public void addConnection(IPConnection connection) } } - public void addPacketToProcess(BytePacket pack) + @Override + public void addPacketToParse(BytePacket pack) { synchronized (toProcess) { @@ -229,6 +231,7 @@ public void loadGame() @Override public void onPause() { + super.onPause(); isInGame = false; } @@ -260,6 +263,7 @@ public void onStop() @Override public void onUnpause() { + super.onUnpause(); isInGame = true; } @@ -269,6 +273,7 @@ public void onUnpause() * @param data the of the packet to parse * @param connection the connection that the packet is being sent from */ + @Override public void parsePacket(BytePacket packet) { PacketType type = packet.getType(); diff --git a/src/ca/afroman/server/ServerSocketManager.java b/src/ca/afroman/server/ServerSocketManager.java index f1c0c9e..0ca36ad 100644 --- a/src/ca/afroman/server/ServerSocketManager.java +++ b/src/ca/afroman/server/ServerSocketManager.java @@ -36,7 +36,7 @@ public static ThreadGroup threadGroupInstance() private List playerList; private DatagramSocket socket; - private ServerSocketReceive rSocket; + private SocketReceive rSocket; private ServerSocketSend sSocket; public ServerSocketManager(String port) @@ -72,7 +72,7 @@ public ServerSocketManager(String port) ServerGame.instance().logger().log(ALogType.CRITICAL, "Server already running on this IP and PORT", e); } - rSocket = new ServerSocketReceive(this); + rSocket = new SocketReceive(socket, true, ServerGame.instance()); sSocket = new ServerSocketSend(this); } @@ -179,7 +179,7 @@ public void pauseThis() sSocket.pauseThis(); } - public ServerSocketReceive receiver() + public SocketReceive receiver() { return rSocket; } diff --git a/src/ca/afroman/server/ServerSocketReceive.java b/src/ca/afroman/server/ServerSocketReceive.java deleted file mode 100644 index d689e4b..0000000 --- a/src/ca/afroman/server/ServerSocketReceive.java +++ /dev/null @@ -1,72 +0,0 @@ -package ca.afroman.server; - -import java.io.IOException; -import java.net.DatagramPacket; - -import ca.afroman.client.ClientGame; -import ca.afroman.log.ALogType; -import ca.afroman.log.ALogger; -import ca.afroman.network.IPConnection; -import ca.afroman.packet.BytePacket; -import ca.afroman.thread.DynamicThread; - -public class ServerSocketReceive extends DynamicThread -{ - private ServerSocketManager manager; - - /** - * A new server instance. - */ - public ServerSocketReceive(ServerSocketManager manager) - { - super(ServerGame.instance().getThreadGroup(), "Receive"); - - this.manager = manager; - } - - @Override - public void onPause() - { - - } - - @Override - public void onRun() - { - byte[] buffer = new byte[ClientGame.RECEIVE_PACKET_BUFFER_LIMIT]; - - // Loads up the buffer with incoming data - DatagramPacket packet = new DatagramPacket(buffer, buffer.length); - - try - { - manager.socket().receive(packet); - - BytePacket pack = new BytePacket(packet.getData(), new IPConnection(packet.getAddress(), packet.getPort())); - if (ALogger.tracePackets) logger().log(ALogType.DEBUG, "[" + pack.getConnections().get(0).asReadable() + "] " + pack.getType()); - ServerGame.instance().addPacketToProcess(pack); - } - catch (IOException e) - { - logger().log(ALogType.CRITICAL, "I/O error while receiving", e); - } - } - - @Override - public void onStart() - { - - } - - @Override - public void onStop() - { - - } - - @Override - public void onUnpause() - { - - } -} diff --git a/src/ca/afroman/server/ServerSocketSend.java b/src/ca/afroman/server/ServerSocketSend.java index 6abc79f..d7ca1f6 100644 --- a/src/ca/afroman/server/ServerSocketSend.java +++ b/src/ca/afroman/server/ServerSocketSend.java @@ -49,24 +49,13 @@ public List getPacketQueue() return sendingPackets; } - @Override - public void onPause() - { - - } - @Override public void onStop() { + super.onStop(); sendingPackets.clear(); } - @Override - public void onUnpause() - { - - } - /** * @param connection the connection that this was sending the packet to * @param id the ID of the packet being sent diff --git a/src/ca/afroman/server/SocketReceive.java b/src/ca/afroman/server/SocketReceive.java new file mode 100644 index 0000000..1ec929f --- /dev/null +++ b/src/ca/afroman/server/SocketReceive.java @@ -0,0 +1,74 @@ +package ca.afroman.server; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.PortUnreachableException; + +import ca.afroman.client.ClientGame; +import ca.afroman.entity.api.IServerClient; +import ca.afroman.gui.GuiClickNotification; +import ca.afroman.gui.GuiJoinServer; +import ca.afroman.gui.GuiMainMenu; +import ca.afroman.interfaces.IPacketParser; +import ca.afroman.log.ALogType; +import ca.afroman.log.ALogger; +import ca.afroman.network.IPConnection; +import ca.afroman.packet.BytePacket; +import ca.afroman.thread.DynamicThread; + +public class SocketReceive extends DynamicThread implements IServerClient +{ + private DatagramSocket socket; + private boolean isServer; + private IPacketParser game; + + /** + * A socket that receives BytePackets and parses them through the provided game. + */ + public SocketReceive(DatagramSocket socket, boolean isServer, IPacketParser game) + { + super(game.getThreadGroup(), "Receive"); + + this.socket = socket; + this.isServer = isServer; + this.game = game; + } + + @Override + public boolean isServerSide() + { + return isServer; + } + + @Override + public void onRun() + { + byte[] buffer = new byte[ClientGame.RECEIVE_PACKET_BUFFER_LIMIT]; + + // Loads up the buffer with incoming data + DatagramPacket packet = new DatagramPacket(buffer, buffer.length); + + try + { + socket.receive(packet); + + BytePacket pack = new BytePacket(packet.getData(), new IPConnection(packet.getAddress(), packet.getPort())); + if (ALogger.tracePackets) logger().log(ALogType.DEBUG, "[" + pack.getConnections().get(0).asReadable() + "] " + pack.getType()); + game.addPacketToParse(pack); + } + catch (PortUnreachableException e) + { + logger().log(ALogType.CRITICAL, "Port is unreachable", e); + if (!isServerSide()) + { + ClientGame.instance().setCurrentScreen(new GuiJoinServer(new GuiMainMenu())); + new GuiClickNotification(ClientGame.instance().getCurrentScreen(), "PORT", "UNREACHABLE"); + } + } + catch (IOException e) + { + if (isRunning) logger().log(ALogType.CRITICAL, "I/O error while receiving", e); + } + } +}