-
-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add basic packet processing interfaces
- Loading branch information
Showing
21 changed files
with
414 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,22 @@ | ||
package dev.protocollib.api; | ||
|
||
/** | ||
* Representing a raw binary packet with a packet id and payload. | ||
*/ | ||
public interface BinaryPacket { | ||
|
||
/** | ||
* Retrieves the packet id. | ||
* | ||
* @return the packet ID | ||
*/ | ||
int id(); | ||
|
||
/** | ||
* Retrieves the payload (data) of the packet. | ||
* | ||
* @return the packet payload as a byte array | ||
*/ | ||
byte[] payload(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,95 @@ | ||
package dev.protocollib.api; | ||
|
||
import java.net.InetSocketAddress; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.Optional; | ||
|
||
import org.bukkit.entity.Player; | ||
|
||
import dev.protocollib.api.listener.PacketSentListener; | ||
|
||
/** | ||
* Representing a connection of a player. | ||
*/ | ||
public interface Connection { | ||
|
||
@Nullable | ||
Player player(); | ||
/** | ||
* Retrieves the player associated with the connection, if available. | ||
* | ||
* @return an optional containing the player, or empty if the player is not present | ||
*/ | ||
Optional<Player> player(); | ||
|
||
/** | ||
* Retrieves the address of the connection. | ||
* | ||
* @return the remote address of the connection | ||
*/ | ||
InetSocketAddress address(); | ||
|
||
/** | ||
* Retrieves the protocol version used by the connection. | ||
* | ||
* @return the protocol version | ||
*/ | ||
int protocolVersion(); | ||
|
||
ProtocolPhase protocolPhase(PacketDirection packetDirection); | ||
/** | ||
* Retrieves the current protocol phase of the connection for a given direction. | ||
* | ||
* @param packetDirection the direction of the packet (clientbound or serverbound) | ||
* @return the protocol phase of the connection | ||
*/ | ||
ProtocolPhase protocolPhase(ProtocolDirection packetDirection); | ||
|
||
/** | ||
* Checks if the connection is currently open. | ||
* | ||
* @return true if the connection is open, false otherwise | ||
*/ | ||
boolean isConnected(); | ||
|
||
/** | ||
* Sends a binary packet over the connection. | ||
* | ||
* @param packet the binary packet to send | ||
*/ | ||
void sendPacket(BinaryPacket packet); | ||
|
||
/** | ||
* Sends a binary packet over the connection and registers a listener for when the packet is sent. | ||
* | ||
* @param packet the binary packet to send | ||
* @param listener the listener to invoke once the packet is sent | ||
*/ | ||
void sendPacket(BinaryPacket packet, PacketSentListener listener); | ||
|
||
/** | ||
* Sends a packet container over the connection. | ||
* | ||
* @param packet the packet container to send | ||
*/ | ||
void sendPacket(PacketContainer packet); | ||
|
||
/** | ||
* Sends a packet container over the connection and registers a listener for when the packet is sent. | ||
* | ||
* @param packet the packet container to send | ||
* @param listener the listener to invoke once the packet is sent | ||
*/ | ||
void sendPacket(PacketContainer packet, PacketSentListener listener); | ||
|
||
/** | ||
* Receives a packet container from the connection. | ||
* | ||
* @param packet the packet container received | ||
*/ | ||
void receivePacket(PacketContainer packet); | ||
|
||
/** | ||
* Disconnects the connection with the specified reason. | ||
* | ||
* @param reason the reason for disconnecting | ||
*/ | ||
void disconnect(String reason); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,22 @@ | ||
package dev.protocollib.api; | ||
|
||
/** | ||
* Representing a container for a packet. | ||
*/ | ||
public interface PacketContainer { | ||
|
||
/** | ||
* Retrieves the type of the packet. | ||
* | ||
* @return the packet type | ||
*/ | ||
PacketType packetType(); | ||
|
||
/** | ||
* Retrieves the raw packet object. | ||
* | ||
* @return the packet object | ||
*/ | ||
Object packet(); | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
src/main/java/dev/protocollib/api/PacketListenerBuilder.java
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
src/main/java/dev/protocollib/api/PacketListenerPriority.java
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
src/main/java/dev/protocollib/api/PacketListenerRegistration.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,40 @@ | ||
package dev.protocollib.api; | ||
|
||
public interface PacketType { | ||
import java.util.Optional; | ||
|
||
PacketDirection packetDirection(); | ||
import net.kyori.adventure.key.Keyed; | ||
|
||
Class<?> packetClass(); | ||
/** | ||
* Representing the type of a network packet. | ||
* | ||
* <p>A {@code PacketType} identifies a specific type of packet in the protocol, | ||
* including information about its direction, associated class (if any), and | ||
* whether the packet is currently supported.</p> | ||
*/ | ||
public interface PacketType extends Keyed { | ||
|
||
boolean isSupported(); | ||
/** | ||
* Retrieves the direction in which the packet is being sent. | ||
* | ||
* @return the {@link ProtocolDirection} of the packet, either clientbound or serverbound | ||
*/ | ||
ProtocolDirection protocolDirection(); | ||
|
||
/** | ||
* Retrieves the class associated with the packet type, if available. | ||
* | ||
* <p>Not all packet types have an associated class. If there is no class, | ||
* an empty {@link Optional} is returned.</p> | ||
* | ||
* @return an {@link Optional} containing the class of the packet, or empty if not applicable | ||
*/ | ||
Optional<Class<?>> packetClass(); | ||
|
||
boolean isDeprecated(); | ||
/** | ||
* Checks whether the packet type is supported by the current protocol version. | ||
* | ||
* @return {@code true} if the packet type is supported, {@code false} otherwise | ||
*/ | ||
boolean isSupported(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.protocollib.api; | ||
|
||
/** | ||
* Representing the direction of a packet, either sent to or from the server. | ||
*/ | ||
public enum ProtocolDirection { | ||
|
||
/** Packet sent from the client to the server. */ | ||
SERVERBOUND, | ||
|
||
/** Packet sent from the server to the client. */ | ||
CLIENTBOUND; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/dev/protocollib/api/listener/AsyncPacketListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package dev.protocollib.api.listener; | ||
|
||
import dev.protocollib.api.PacketContainer; | ||
|
||
/** | ||
* Functional interface for handling packets asynchronously. | ||
* | ||
* <p>Once a packet is processed by the listener, the context's | ||
* {@code resumeProcessing()} or {@code resumeProcessingWithException(Throwable)} | ||
* methods must be called to signal that the listener is done with the packet. | ||
* Failing to call one of these methods will cause the packet to remain in | ||
* a waiting state until it times out, preventing further listeners from | ||
* receiving the packet. | ||
* </p> | ||
*/ | ||
@FunctionalInterface | ||
public interface AsyncPacketListener { | ||
|
||
/** | ||
* Handles a packet that was sent or received, asynchronously. | ||
* | ||
* <p>Once processing is complete, ensure that one of the {@code resumeProcessing} | ||
* methods from the {@link AsyncPacketListenerContext} is called. This allows the | ||
* packet to continue to the next listener. If not called, the packet will remain | ||
* in a waiting state and will only proceed after a timeout occurs.</p> | ||
* | ||
* @param packet the packet to handle | ||
* @param context the context providing additional information about the packet and connection | ||
*/ | ||
void handlePacket(PacketContainer packet, AsyncPacketListenerContext context); | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/dev/protocollib/api/listener/AsyncPacketListenerContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package dev.protocollib.api.listener; | ||
|
||
/** | ||
* Representing the context of an asynchronous packet listener. | ||
*/ | ||
public interface AsyncPacketListenerContext extends SyncPacketListenerContext { | ||
|
||
/** | ||
* Singles the listener is done with processing the packet. | ||
*/ | ||
void resumeProcessing(); | ||
|
||
/** | ||
* Singles the listener is done with processing the packet and finished | ||
* with an exception. | ||
* | ||
* @param throwable the processing exception | ||
*/ | ||
void resumeProcessingWithException(Throwable throwable); | ||
|
||
} |
Oops, something went wrong.