Dieser Commit ist enthalten in:
Ursprung
492894ca8d
Commit
cf59dc9720
23
src/de/steamwar/network/packets/MetaInfos.java
Normale Datei
23
src/de/steamwar/network/packets/MetaInfos.java
Normale Datei
@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* 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.packets;
|
||||||
|
|
||||||
|
public interface MetaInfos {
|
||||||
|
}
|
@ -40,6 +40,10 @@ public abstract class NetworkPacket implements Serializable {
|
|||||||
PacketHandler.handlePacket(deserialize(data));
|
PacketHandler.handlePacket(deserialize(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void handle(MetaInfos metaInfos, byte[] data) {
|
||||||
|
PacketHandler.handlePacket(metaInfos, deserialize(data));
|
||||||
|
}
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public static NetworkPacket deserialize(byte[] data) {
|
public static NetworkPacket deserialize(byte[] data) {
|
||||||
ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
||||||
|
@ -36,23 +36,36 @@ public abstract class PacketHandler {
|
|||||||
private final Map<Class<? extends NetworkPacket>, Method> HANDLER_MAP = new HashMap<>();
|
private final Map<Class<? extends NetworkPacket>, Method> HANDLER_MAP = new HashMap<>();
|
||||||
|
|
||||||
public static void handlePacket(NetworkPacket packet) {
|
public static void handlePacket(NetworkPacket packet) {
|
||||||
|
handlePacket(null, packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void handlePacket(MetaInfos metaInfos, NetworkPacket packet) {
|
||||||
for (PacketHandler handler : PACKET_HANDLERS) {
|
for (PacketHandler handler : PACKET_HANDLERS) {
|
||||||
handler.handle(packet);
|
handler.handle(metaInfos, packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected 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])) {
|
Handler handler = method.getAnnotation(Handler.class);
|
||||||
|
if (handler == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (method.isAnnotationPresent(Handler.class)) {
|
if (!(method.getParameterCount() > 0 && method.getParameterCount() < 3)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!NetworkPacket.class.isAssignableFrom(method.getParameterTypes()[0])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (method.getParameterCount() == 2 && !MetaInfos.class.isAssignableFrom(method.getParameterTypes()[1])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
Class<? extends NetworkPacket> packetClass = (Class<? extends NetworkPacket>) method.getParameterTypes()[0];
|
Class<? extends NetworkPacket> packetClass = (Class<? extends NetworkPacket>) method.getParameterTypes()[0];
|
||||||
HANDLER_MAP.put(packetClass, method);
|
HANDLER_MAP.put(packetClass, method);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void register() {
|
public void register() {
|
||||||
PACKET_HANDLERS.add(this);
|
PACKET_HANDLERS.add(this);
|
||||||
@ -63,13 +76,17 @@ public abstract class PacketHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public void handle(NetworkPacket packet) {
|
public void handle(MetaInfos metaInfos, NetworkPacket packet) {
|
||||||
Method method = HANDLER_MAP.get(packet.getClass());
|
Method method = HANDLER_MAP.get(packet.getClass());
|
||||||
if (method == null) {
|
if (method == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (method.getParameterCount() == 2) {
|
||||||
|
method.invoke(this, packet, metaInfos);
|
||||||
|
} else {
|
||||||
method.invoke(this, packet);
|
method.invoke(this, packet);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
protected @interface Handler {}
|
protected @interface Handler {}
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren