Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.luminiadev.bridge.network.codec.BridgeCodec;
import com.luminiadev.bridge.network.codec.packet.BridgePacket;
import com.luminiadev.bridge.network.codec.packet.handler.BridgePacketHandler;
import com.luminiadev.bridge.network.codec.packet.handler.BridgeSinglePacketHandler;
import com.luminiadev.bridge.network.codec.packet.handler.TypedBridgePacketHandler;
import io.netty.buffer.ByteBuf;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;
Expand Down Expand Up @@ -36,8 +38,14 @@ public void setCodec(BridgeCodec codec) {
}

@Override
public void addPacketHandler(BridgePacketHandler handler) {
public BridgePacketHandler addPacketHandler(BridgePacketHandler handler) {
handlers.add(handler);
return handler;
}

@Override
public <T extends BridgePacket> BridgePacketHandler addPacketHandler(Class<T> packetClass, BridgeSinglePacketHandler<T> handler) {
return this.addPacketHandler(new TypedBridgePacketHandler<>(packetClass, handler));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.luminiadev.bridge.network.codec.BridgeCodec;
import com.luminiadev.bridge.network.codec.packet.BridgePacket;
import com.luminiadev.bridge.network.codec.packet.handler.BridgePacketHandler;
import com.luminiadev.bridge.network.codec.packet.handler.BridgeSinglePacketHandler;
import io.netty.buffer.ByteBuf;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -48,9 +49,19 @@ public interface BridgeNetwork {
/**
* Add a packet handler.
*
* @param handler The BridgePacketHandler
* @param handler the BridgePacketHandler
* @return the added packet handler
*/
BridgePacketHandler addPacketHandler(BridgePacketHandler handler);

/**
* Add a typed packet handler.
*
* @param packetClass the class of packet
* @param handler the BridgeSinglePacketHandler
* @return the added TypedBridgePacketHandler
*/
void addPacketHandler(BridgePacketHandler handler);
<T extends BridgePacket> BridgePacketHandler addPacketHandler(Class<T> packetClass, BridgeSinglePacketHandler<T> handler);

/**
* Remove a packet handler.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.luminiadev.bridge.network.codec.packet.handler;

import com.luminiadev.bridge.network.codec.packet.BridgePacket;
import com.luminiadev.bridge.network.codec.packet.BridgePacketDirection;

/**
* The handler for handling one type of packet.
*
* @param <T> Packet type to handle
* @see TypedBridgePacketHandler
*/
@FunctionalInterface
public interface BridgeSinglePacketHandler<T extends BridgePacket> {

/**
* Called when a packet is sent or received by the service.
*
* @param packet Packet object
* @param direction Packet direction (from or to service)
* @param serviceId Sender's service Id
*/
void handle(T packet, BridgePacketDirection direction, String serviceId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.luminiadev.bridge.network.codec.packet.handler;

import com.luminiadev.bridge.network.codec.packet.BridgePacket;
import com.luminiadev.bridge.network.codec.packet.BridgePacketDirection;

/**
* Internal implementation of handler to handle one type of packet.
*
* @param <T> Packet type to handle
*/
public final class TypedBridgePacketHandler<T extends BridgePacket> implements BridgePacketHandler {

private final Class<T> packetClass;
private final BridgeSinglePacketHandler<T> handler;

public TypedBridgePacketHandler(Class<T> packetClass, BridgeSinglePacketHandler<T> handler) {
this.packetClass = packetClass;
this.handler = handler;
}

@Override
public void handle(BridgePacket packet, BridgePacketDirection direction, String serviceId) {
if (packetClass.isInstance(packet)) {
handler.handle(packetClass.cast(packet), direction, serviceId);
}
}
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ java {

allprojects {
group = "com.luminiadev.bridge"
version = "1.0.6-SNAPSHOT"
version = "1.1.0-SNAPSHOT"
}

subprojects {
Expand Down