🎨 Refactor
Dieser Commit ist enthalten in:
Ursprung
ddc7353ad1
Commit
d51b8db0bf
@ -19,6 +19,8 @@
|
|||||||
|
|
||||||
package de.steamwar.network.packets;
|
package de.steamwar.network.packets;
|
||||||
|
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
@ -39,7 +41,7 @@ public abstract class PacketHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public PacketHandler() {
|
protected PacketHandler() {
|
||||||
Method[] methods = getClass().getMethods();
|
Method[] methods = getClass().getMethods();
|
||||||
for (Method method : methods) {
|
for (Method method : methods) {
|
||||||
if(method.getParameterCount() != 1 || !NetworkPacket.class.isAssignableFrom(method.getParameterTypes()[0])) {
|
if(method.getParameterCount() != 1 || !NetworkPacket.class.isAssignableFrom(method.getParameterTypes()[0])) {
|
||||||
@ -60,16 +62,13 @@ public abstract class PacketHandler {
|
|||||||
PACKET_HANDLERS.remove(this);
|
PACKET_HANDLERS.remove(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
public void handle(NetworkPacket packet) {
|
public void handle(NetworkPacket packet) {
|
||||||
Method method = HANDLER_MAP.get(packet.getClass());
|
Method method = HANDLER_MAP.get(packet.getClass());
|
||||||
if (method == null) {
|
if (method == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
method.invoke(this, packet);
|
method.invoke(this, packet);
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
52
testsrc/de/steamwar/network/PacketHandlerTest.java
Normale Datei
52
testsrc/de/steamwar/network/PacketHandlerTest.java
Normale Datei
@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of the SteamWar software.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2022 SteamWar.de-Serverteam
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.steamwar.network;
|
||||||
|
|
||||||
|
import de.steamwar.network.packets.PacketHandler;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.*;
|
||||||
|
import static org.hamcrest.Matchers.*;
|
||||||
|
|
||||||
|
public class PacketHandlerTest {
|
||||||
|
|
||||||
|
TestPacketHandler packetHandler;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
packetHandler = new TestPacketHandler();
|
||||||
|
packetHandler.register();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPacketHandler() {
|
||||||
|
TestPacket packet = new TestPacket();
|
||||||
|
try {
|
||||||
|
PacketHandler.handlePacket(packet);
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertThat(e, instanceOf(InvocationTargetException.class));
|
||||||
|
assertThat(e.getCause(), instanceOf(SecurityException.class));
|
||||||
|
assertThat(e.getCause().getMessage(), equalTo(packet.getClass().getName()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -22,7 +22,8 @@ package de.steamwar.network;
|
|||||||
import de.steamwar.network.packets.NetworkPacket;
|
import de.steamwar.network.packets.NetworkPacket;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.hamcrest.Matchers.*;
|
||||||
|
import static org.hamcrest.MatcherAssert.*;
|
||||||
|
|
||||||
public class PacketTest {
|
public class PacketTest {
|
||||||
|
|
||||||
@ -31,8 +32,7 @@ public class PacketTest {
|
|||||||
for (NetworkPacket allPacket : AllPackets.getAllPackets()) {
|
for (NetworkPacket allPacket : AllPackets.getAllPackets()) {
|
||||||
byte[] data = allPacket.serialize();
|
byte[] data = allPacket.serialize();
|
||||||
NetworkPacket deserializedPacket = NetworkPacket.deserialize(data);
|
NetworkPacket deserializedPacket = NetworkPacket.deserialize(data);
|
||||||
assertEquals("Packet: " + allPacket.getClass().getName() + " is not correctly deserialized", allPacket, deserializedPacket);
|
assertThat("Packet: " + allPacket.getClass().getName() + " is not correctly deserialized", allPacket, equalTo(deserializedPacket));
|
||||||
System.out.println("Packet: " + allPacket.getClass().getName() + " is correctly deserialized");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
30
testsrc/de/steamwar/network/TestPacket.java
Normale Datei
30
testsrc/de/steamwar/network/TestPacket.java
Normale Datei
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of the SteamWar software.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2022 SteamWar.de-Serverteam
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.steamwar.network;
|
||||||
|
|
||||||
|
import de.steamwar.network.packets.NetworkPacket;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class TestPacket extends NetworkPacket {
|
||||||
|
private static final long serialVersionUID = -9079104421295590626L;
|
||||||
|
}
|
30
testsrc/de/steamwar/network/TestPacketHandler.java
Normale Datei
30
testsrc/de/steamwar/network/TestPacketHandler.java
Normale Datei
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of the SteamWar software.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2022 SteamWar.de-Serverteam
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.steamwar.network;
|
||||||
|
|
||||||
|
import de.steamwar.network.packets.PacketHandler;
|
||||||
|
|
||||||
|
public class TestPacketHandler extends PacketHandler {
|
||||||
|
|
||||||
|
@Handler
|
||||||
|
public void handle(TestPacket packet) {
|
||||||
|
throw new SecurityException(packet.getClass().getName());
|
||||||
|
}
|
||||||
|
}
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren