Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Commit

Permalink
Packet Api Rework
Browse files Browse the repository at this point in the history
  • Loading branch information
BelgianDev committed Dec 5, 2023
1 parent a3db816 commit 7087c35
Show file tree
Hide file tree
Showing 21 changed files with 604 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,5 @@ public interface Module extends Archive {
* Fail listeners are called when the module crashes.
* @return null if the module is not yet activated.
*/
@Nullable
FutureAction<Void> deactivationFuture();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package fr.atlasworld.network.api.networking;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import fr.atlasworld.network.api.concurrent.action.FutureAction;
import fr.atlasworld.network.api.networking.packet.NetworkPacket;
import fr.atlasworld.network.api.networking.packet.PacketByteBuf;
import fr.atlasworld.network.api.networking.packet.SentPacket;

import java.util.UUID;

public interface ConnectionSource {

/**
* Get the connection's unique identifier.
* @return null if the connection is still in the handshake process.
*/
UUID getUniqueIdentifier();

/**
* Get the state of the connection.
*/
ConnectionState getState();

/**
* Creates an empty PacketByteBuf.
*/
PacketByteBuf createBuffer();

/**
* Sends a packet to the source.
* @param packet packet to send.
* @return future async action of the sending task.
*/
@CanIgnoreReturnValue
FutureAction<SentPacket> send(NetworkPacket packet);

/**
* Sends a packet to the source.
* @param buffer packet to send.
* @return future async action of the sending task.
*/
@CanIgnoreReturnValue
FutureAction<SentPacket> send(PacketByteBuf buffer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package fr.atlasworld.network.api.networking;

public enum ConnectionState {
/**
* Connection is in the handshake process,
* at this state the connection is neither authenticated nor encrypted.
*/
HANDSHAKE,

/**
* Connection is connected and validated,
* at this state the connection has been fully authenticated and is encrypted.
*/
CONNECTED,

/**
* Used for packet registration, this state will never be on an actual connection.
* This makes the packet available in the handshake and connected state.
*/
HANDSHAKE_AND_CONNECTED;
}
18 changes: 18 additions & 0 deletions api/src/main/java/fr/atlasworld/network/api/networking/Socket.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import com.google.gson.JsonElement;
import fr.atlasworld.network.api.concurrent.action.FutureAction;
import fr.atlasworld.network.api.module.Module;
import fr.atlasworld.network.api.networking.packet.NetworkPacket;
import org.jetbrains.annotations.NotNull;

import java.util.function.Supplier;

/**
* Abstract API Socket. Handles networking for AtlasNetwork
*/
public interface Socket {

/**
* Stops the socket.
* @return Async Task of the stop operation.
Expand All @@ -31,4 +36,17 @@ public interface Socket {
* @param data data to attach to the request
*/
void registerHandshakeInfo(Module module, JsonElement data);

/**
* Register a packet in the active state registry.
* @param module module that registers the packet.
* @param builder builder of the packet.
*/
void registerPacket(@NotNull Module module, @NotNull Supplier<? extends NetworkPacket> builder);

/**
* Unregister packet from the active state registry.
* @param key key of the packet.
*/
void unregisterPacket(@NotNull String key);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fr.atlasworld.network.api.networking.exception;

public class PacketExecutionException extends PacketNetworkingException {
public PacketExecutionException(String packetIdentifier) {
super("Something went wrong while processing " + packetIdentifier + ".", packetIdentifier);
}

public PacketExecutionException(String message, String packetIdentifier) {
super(message, packetIdentifier);
}

public PacketExecutionException(String message, Throwable cause, String packetIdentifier) {
super(message, cause, packetIdentifier);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package fr.atlasworld.network.api.networking.exception;

public class PacketNetworkingException extends NetworkingException {
private final String packetIdentifier;

public PacketNetworkingException(String packetIdentifier) {
this.packetIdentifier = packetIdentifier;
}

public PacketNetworkingException(String message, String packetIdentifier) {
super(message);
this.packetIdentifier = packetIdentifier;
}

public PacketNetworkingException(String message, Throwable cause, String packetIdentifier) {
super(message, cause);
this.packetIdentifier = packetIdentifier;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fr.atlasworld.network.api.networking.exception;

public class UnknownPacketException extends PacketNetworkingException {
public UnknownPacketException(String packetIdentifier) {
super("Received unknown packet '" + packetIdentifier + "'.", packetIdentifier);
}

public UnknownPacketException(String message, String packetIdentifier) {
super(message, packetIdentifier);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fr.atlasworld.network.api.networking.packet;

import fr.atlasworld.network.api.networking.ConnectionSource;
import org.jetbrains.annotations.NotNull;

/**
* Represents a network packet.
*/
public interface NetworkPacket {

/**
* Get the unique identifier of the packet.
*/
@NotNull String getKey();

/**
* Decodes and process received requests.
* @param source source from which we received a packet.
* @param buf received buffer.
*/
void decode(@NotNull ConnectionSource source, @NotNull PacketByteBuf buf);

/**
* Used when the packet is created before sending.
* @param buffer to encode.
*/
void encode(@NotNull PacketByteBuf buffer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package fr.atlasworld.network.api.networking.packet;

import fr.atlasworld.network.api.module.Module;
import org.jetbrains.annotations.NotNull;

import java.util.function.Function;
import java.util.function.Supplier;

/**
* Packet Registry, holds every packet.
*/
public interface PacketRegistry {

/**
* Register a packet to the registry.
* @param builder packet builder.
*/
<T extends NetworkPacket> void register(@NotNull Supplier<T> builder);

/**
* Unregister a packet from registry.
* @param key key of the packet.
*/
void unregister(@NotNull String key);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fr.atlasworld.network.api.networking.packet;

import fr.atlasworld.network.api.networking.ConnectionSource;

/**
* Abstract sent packet, this is supposed to be a sent packet.
*/
public interface SentPacket {

/**
* Get the target of the packet.
*/
ConnectionSource getTarget();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fr.atlasworld.network.api.services.database.exceptions;

import java.io.UncheckedIOException;

public class UncheckedDatabaseException extends UncheckedIOException {

public UncheckedDatabaseException(String message, DatabaseException cause) {
super(message, cause);
}

public UncheckedDatabaseException(DatabaseException cause) {
super(cause);
}
}

This file was deleted.

Loading

0 comments on commit 7087c35

Please sign in to comment.