Skip to content

Commit cba9934

Browse files
committed
refactor: use pattern matching for instanceof checks
Simplify instanceof checks and casting using Java's pattern matching feature. Remove unused HTTP endpoints for connection management and banning. Improve error message for unknown framework messages.
1 parent 38a670a commit cba9934

File tree

3 files changed

+12
-64
lines changed

3 files changed

+12
-64
lines changed

src/playerconnect/HttpServer.java

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import com.fasterxml.jackson.databind.SerializationFeature;
1313

1414
import arc.Events;
15-
import arc.net.DcReason;
16-
import arc.struct.Seq;
1715
import arc.util.Log;
1816
import io.javalin.Javalin;
1917
import io.javalin.json.JavalinJackson;
@@ -90,55 +88,6 @@ public HttpServer() {
9088

9189
app.get("ping", ctx -> ctx.result("pong"));
9290

93-
app.get("connections", ctx -> {
94-
if (!auth(ctx)) {
95-
return;
96-
}
97-
98-
var result = Seq.with(PlayerConnect.relay.getConnections())
99-
.map(connection -> {
100-
101-
ConnectionDto dto = new ConnectionDto();
102-
dto.id = Utils.toString(connection);
103-
dto.ip = Utils.getIP(connection);
104-
105-
return dto;
106-
}).list();
107-
108-
ctx.json(result);
109-
});
110-
111-
app.post("ban", ctx -> {
112-
if (!auth(ctx)) {
113-
return;
114-
}
115-
116-
var ip = ctx.body();
117-
118-
for (var connection : PlayerConnect.relay.getConnections()) {
119-
if (Utils.getIP(connection).equals(ip)) {
120-
connection.close(DcReason.closed);
121-
Log.info("Kicked client <" + Utils.toString(connection) + "> for IP ban");
122-
Events.fire(new PlayerConnectEvents.ClientKickedEvent(connection));
123-
}
124-
}
125-
126-
Configs.IP_BLACK_LIST.add(ip);
127-
128-
ctx.json(Configs.IP_BLACK_LIST.list());
129-
});
130-
131-
app.post("unban", ctx -> {
132-
if (!auth(ctx)) {
133-
return;
134-
}
135-
136-
var ip = ctx.body();
137-
Configs.IP_BLACK_LIST.remove(ip);
138-
139-
ctx.json(Configs.IP_BLACK_LIST.list());
140-
});
141-
14291
app.start(Integer.parseInt(System.getenv("PLAYER_CONNECT_HTTP_PORT")));
14392

14493
Events.on(PlayerConnectEvents.RoomCreatedEvent.class, event -> {

src/playerconnect/NetworkRelay.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
public class NetworkRelay extends Server implements NetListener {
2727
protected boolean isClosed;
28-
28+
2929
/**
3030
* Keeps a cache of packets received from connections that are not yet in a
3131
* room. (queue of 3 packets)<br>
@@ -385,8 +385,8 @@ public Object read(ByteBuffer buffer) {
385385
if (id == Packets.id) {
386386
Packets.Packet packet = Packets.newPacket(buffer.get());
387387
packet.read(new ByteBufferInput(buffer));
388-
if (packet instanceof Packets.ConnectionPacketWrapPacket) // This one is special
389-
((Packets.ConnectionPacketWrapPacket) packet).buffer = (ByteBuffer) ((ByteBuffer) last.get()
388+
if (packet instanceof Packets.ConnectionPacketWrapPacket wrap) // This one is special
389+
wrap.buffer = (ByteBuffer) ((ByteBuffer) last.get()
390390
.clear()).put(buffer).flip();
391391
return packet;
392392
}
@@ -408,8 +408,8 @@ public void write(ByteBuffer buffer, Object object) {
408408
Packets.Packet packet = (Packets.Packet) object;
409409
buffer.put(Packets.id).put(Packets.getId(packet));
410410
packet.write(new ByteBufferOutput(buffer));
411-
if (packet instanceof Packets.ConnectionPacketWrapPacket) // This one is special
412-
buffer.put(((Packets.ConnectionPacketWrapPacket) packet).buffer);
411+
if (packet instanceof Packets.ConnectionPacketWrapPacket wrapPacket) // This one is special
412+
buffer.put(wrapPacket.buffer);
413413
}
414414
}
415415

@@ -448,7 +448,7 @@ public FrameworkMessage readFramework(ByteBuffer buffer) {
448448
p.connectionID = buffer.getInt();
449449
return p;
450450
} else {
451-
throw new RuntimeException("Unknown framework message!");
451+
throw new RuntimeException("Unknown framework message: " + id);
452452
}
453453
}
454454
}

src/playerconnect/ServerRoom.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ public void received(Connection connection, Object object) {
9797
if (connection == host) {
9898
// Only claj packets are allowed in the host's connection
9999
// and can only be ConnectionPacketWrapPacket at this point.
100-
if (!(object instanceof Packets.ConnectionPacketWrapPacket))
100+
if (!(object instanceof Packets.ConnectionPacketWrapPacket wrap))
101101
return;
102102

103-
int connectionId = ((Packets.ConnectionPacketWrapPacket) object).connectionId;
103+
int connectionId = wrap.connectionId;
104104
Connection con = clients.get(connectionId);
105105

106106
if (con != null && con.isConnected()) {
107-
boolean tcp = ((Packets.ConnectionPacketWrapPacket) object).isTCP;
108-
Object o = ((Packets.ConnectionPacketWrapPacket) object).buffer;
107+
boolean tcp = wrap.isTCP;
108+
Object o = wrap.buffer;
109109

110110
if (tcp)
111111
con.sendTCP(o);
@@ -125,14 +125,13 @@ public void received(Connection connection, Object object) {
125125
// We never send claj packets to anyone other than the room host, framework
126126
// packets are ignored
127127
// and mindustry packets are saved as raw buffer.
128-
if (!(object instanceof ByteBuffer))
128+
if (!(object instanceof ByteBuffer buffer))
129129
return;
130130

131131
Packets.ConnectionPacketWrapPacket p = new Packets.ConnectionPacketWrapPacket();
132132
p.connectionId = connection.getID();
133-
p.buffer = (ByteBuffer) object;
133+
p.buffer = buffer;
134134
host.sendTCP(p);
135-
136135
}
137136
}
138137

0 commit comments

Comments
 (0)