Add MetaInfos
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
yoyosource 2022-06-14 09:47:45 +02:00
Ursprung 492894ca8d
Commit cf59dc9720
3 geänderte Dateien mit 51 neuen und 7 gelöschten Zeilen

Datei anzeigen

@ -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 {
}

Datei anzeigen

@ -40,6 +40,10 @@ public abstract class NetworkPacket implements Serializable {
PacketHandler.handlePacket(deserialize(data));
}
public static void handle(MetaInfos metaInfos, byte[] data) {
PacketHandler.handlePacket(metaInfos, deserialize(data));
}
@SneakyThrows
public static NetworkPacket deserialize(byte[] data) {
ByteArrayInputStream bais = new ByteArrayInputStream(data);

Datei anzeigen

@ -36,21 +36,34 @@ public abstract class PacketHandler {
private final Map<Class<? extends NetworkPacket>, Method> HANDLER_MAP = new HashMap<>();
public static void handlePacket(NetworkPacket packet) {
handlePacket(null, packet);
}
public static void handlePacket(MetaInfos metaInfos, NetworkPacket packet) {
for (PacketHandler handler : PACKET_HANDLERS) {
handler.handle(packet);
handler.handle(metaInfos, packet);
}
}
protected PacketHandler() {
Method[] methods = getClass().getMethods();
for (Method method : methods) {
if(method.getParameterCount() != 1 || !NetworkPacket.class.isAssignableFrom(method.getParameterTypes()[0])) {
Handler handler = method.getAnnotation(Handler.class);
if (handler == null) {
continue;
}
if (method.isAnnotationPresent(Handler.class)) {
Class<? extends NetworkPacket> packetClass = (Class<? extends NetworkPacket>) method.getParameterTypes()[0];
HANDLER_MAP.put(packetClass, method);
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];
HANDLER_MAP.put(packetClass, method);
}
}
@ -63,12 +76,16 @@ public abstract class PacketHandler {
}
@SneakyThrows
public void handle(NetworkPacket packet) {
public void handle(MetaInfos metaInfos, NetworkPacket packet) {
Method method = HANDLER_MAP.get(packet.getClass());
if (method == null) {
return;
}
method.invoke(this, packet);
if (method.getParameterCount() == 2) {
method.invoke(this, packet, metaInfos);
} else {
method.invoke(this, packet);
}
}
@Retention(RetentionPolicy.RUNTIME)