Skip to content

Commit

Permalink
Unified socket receivers
Browse files Browse the repository at this point in the history
  • Loading branch information
defaultsamson committed Aug 27, 2016
1 parent a2785de commit a33cdac
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 165 deletions.
27 changes: 7 additions & 20 deletions src/ca/afroman/client/ClientGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -153,7 +154,8 @@ public ClientGame()
super(newDefaultThreadGroupInstance(), "Game", 60);
}

public void addPacketToProcess(BytePacket pack)
@Override
public void addPacketToParse(BytePacket pack)
{
synchronized (toSend)
{
Expand Down Expand Up @@ -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()
{
Expand All @@ -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();

Expand Down Expand Up @@ -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();
Expand All @@ -544,6 +530,7 @@ public void onStop()
if (sockets() != null) sockets().stopThis();
}

@Override
public void parsePacket(BytePacket packet)
{
IPConnection sender = packet.getConnections().get(0);
Expand Down
7 changes: 4 additions & 3 deletions src/ca/afroman/client/ClientSocketManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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()
Expand All @@ -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);
}

Expand Down Expand Up @@ -108,7 +109,7 @@ public ConnectedPlayer playerByRole(Role role)
return null;
}

public ClientSocketReceive receiver()
public SocketReceive receiver()
{
return rSocket;
}
Expand Down
53 changes: 0 additions & 53 deletions src/ca/afroman/client/ClientSocketReceive.java

This file was deleted.

12 changes: 12 additions & 0 deletions src/ca/afroman/interfaces/IPacketParser.java
Original file line number Diff line number Diff line change
@@ -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);
}
9 changes: 7 additions & 2 deletions src/ca/afroman/server/ServerGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -92,7 +93,8 @@ public void addConnection(IPConnection connection)
}
}

public void addPacketToProcess(BytePacket pack)
@Override
public void addPacketToParse(BytePacket pack)
{
synchronized (toProcess)
{
Expand Down Expand Up @@ -229,6 +231,7 @@ public void loadGame()
@Override
public void onPause()
{
super.onPause();
isInGame = false;
}

Expand Down Expand Up @@ -260,6 +263,7 @@ public void onStop()
@Override
public void onUnpause()
{
super.onUnpause();
isInGame = true;
}

Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/ca/afroman/server/ServerSocketManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static ThreadGroup threadGroupInstance()
private List<IPConnectedPlayer> playerList;
private DatagramSocket socket;

private ServerSocketReceive rSocket;
private SocketReceive rSocket;
private ServerSocketSend sSocket;

public ServerSocketManager(String port)
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -179,7 +179,7 @@ public void pauseThis()
sSocket.pauseThis();
}

public ServerSocketReceive receiver()
public SocketReceive receiver()
{
return rSocket;
}
Expand Down
72 changes: 0 additions & 72 deletions src/ca/afroman/server/ServerSocketReceive.java

This file was deleted.

13 changes: 1 addition & 12 deletions src/ca/afroman/server/ServerSocketSend.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,13 @@ public List<BytePacket> 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
Expand Down
Loading

0 comments on commit a33cdac

Please sign in to comment.