Skip to content

Commit

Permalink
Miscellaneous cleanup in server code
Browse files Browse the repository at this point in the history
These are drive-by fixes while debugging an issue, no functional
changes.

Highlight: renaming
PacketHandlerFactoryGeneratorClassHelperImplementationDecorator ->
PacketHandlerFactory. Looks like the original implementor was having fun
with design pattern names.
  • Loading branch information
StenAL committed May 18, 2024
1 parent 472b64d commit 068ddc1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 28 deletions.
18 changes: 7 additions & 11 deletions server/src/main/java/org/moparforia/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void start() {
return;
}

packetHandlers = PacketHandlerFactoryGeneratorClassHelperImplementationDecorator.getPacketHandlers();
packetHandlers = PacketHandlerFactory.getPacketHandlers();
System.out.println("Loaded " + packetHandlers.size() + " packet handler type(s)");

ChannelFactory factory = new NioServerSocketChannelFactory(
Expand All @@ -179,16 +179,12 @@ public void start() {
ServerBootstrap bootstrap = new ServerBootstrap(factory);
final ClientChannelHandler clientHandler = new ClientChannelHandler(this);
final IdleStateHandler idleState = new IdleStateHandler(new HashedWheelTimer(1, TimeUnit.SECONDS), 2, 0, 0);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() {
return Channels.pipeline(
new DelimiterBasedFrameDecoder(250, Delimiters.lineDelimiter()),
new PacketDecoder(),
new PacketEncoder(),
idleState,
clientHandler);
}
});
bootstrap.setPipelineFactory(() -> Channels.pipeline(
new DelimiterBasedFrameDecoder(250, Delimiters.lineDelimiter()),
new PacketDecoder(),
new PacketEncoder(),
idleState,
clientHandler));
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public ClientDisconnectedEvent(Channel channel) {
@Override
public void process(Server server) {
Player player;
if((player = (Player)channel.getAttachment()) != null) {
if ((player = (Player)channel.getAttachment()) != null) {
if (player.getLobby() != null) {
player.getLobby().removePlayer(player, Lobby.PART_REASON_USERLEFT,null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import org.moparforia.server.Server;

@SuppressWarnings("SameParameterValue")
public abstract class TimedEvent extends Event {

private final long time;

public TimedEvent(long time) {
this.time = System.currentTimeMillis() + time;
public TimedEvent(long milliseconds) {
this.time = System.currentTimeMillis() + milliseconds;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,24 @@ public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exce
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
//noinspection ThrowableResultOfMethodCallIgnored
e.getCause().printStackTrace();
e.getChannel().close();
}

@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
server.addEvent(new ClientConnectedEvent(e.getChannel()));
}

@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) {
Channel channel = e.getChannel();
server.addEvent(new ClientDisconnectedEvent(channel));
final int id = channel.getId();
if (server.hasPlayer(id)) {
server.addEvent(new TimedEvent(30000) { // todo: confirm this time
server.addEvent(new TimedEvent(30_000) { // todo: confirm this time
@Override
public void process(Server server) {
if (server.hasPlayer(id) && !server.getPlayer(id).getChannel().isOpen()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
import java.util.ArrayList;
import java.util.HashMap;

/**
* Playforia
* 14.6.2013
*/
public class PacketHandlerFactoryGeneratorClassHelperImplementationDecorator {
public class PacketHandlerFactory {

//todo packethandlers for each game type ( should be pretty much the same for golf and golf2 )
public static final HashMap<PacketType, ArrayList<PacketHandler>> getPacketHandlers() {
Expand All @@ -19,10 +15,10 @@ public static final HashMap<PacketType, ArrayList<PacketHandler>> getPacketHandl
new LoginTypeHandler(), new NewHandler(), new PongHandler(), new ReconnectHandler(),
new TlogHandler(), new TrackTestLoginHandler(), new VersionHandler(), new LobbyCreateSinglePlayerHandler(),
new LobbyMultiplayerHandler(), new LobbyDualplayerHandler(), new QuitHandler()};
ArrayList<PacketHandler> data = new ArrayList<PacketHandler>();
ArrayList<PacketHandler> command = new ArrayList<PacketHandler>();
ArrayList<PacketHandler> string = new ArrayList<PacketHandler>();
ArrayList<PacketHandler> none = new ArrayList<PacketHandler>();
ArrayList<PacketHandler> data = new ArrayList<>();
ArrayList<PacketHandler> command = new ArrayList<>();
ArrayList<PacketHandler> string = new ArrayList<>();
ArrayList<PacketHandler> none = new ArrayList<>();
for (PacketHandler handler : handlers) {
switch (handler.getType()) {
case DATA:
Expand Down

0 comments on commit 068ddc1

Please sign in to comment.