This repository has been archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1814234
commit 923de9d
Showing
24 changed files
with
439 additions
and
183 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
api/src/main/java/fr/atlasworld/network/api/networking/HandshakeInformation.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,87 @@ | ||
package fr.atlasworld.network.api.networking; | ||
|
||
import com.google.common.base.Function; | ||
import com.google.common.base.Preconditions; | ||
import org.jetbrains.annotations.NotNull; | ||
/** | ||
* Represents information exchanged during the handshake. | ||
* | ||
* @param <T> the type of the handshake value | ||
*/ | ||
public final class HandshakeInformation<T> { | ||
private final @NotNull T value; | ||
private final @NotNull Function<T, String> factory; | ||
|
||
public HandshakeInformation(@NotNull T value, @NotNull Function<T, String> factory) { | ||
Preconditions.checkArgument(value != null && factory != null, "Handshake information and string factory may not be null!"); | ||
|
||
this.value = value; | ||
this.factory = factory; | ||
} | ||
|
||
public @NotNull T getValue() { | ||
return value; | ||
} | ||
|
||
/** | ||
* Returns a string representation of the current data. | ||
* This will use the provided function to transform the object into string | ||
* before sending it through the handshake packet. | ||
* | ||
* @return A string representation of the object. | ||
*/ | ||
public String asString() { | ||
return this.factory.apply(this.value); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "handshake_data[data:" + this.asString() + "]"; | ||
} | ||
|
||
// Static Fields | ||
|
||
/** | ||
* Returns a new instance of HandshakeInformation with the provided information. | ||
* | ||
* @param information The information to be included in the handshake. | ||
* @return A new instance of HandshakeInformation with the provided information and resource key. | ||
*/ | ||
public static HandshakeInformation<String> text(@NotNull String information) { | ||
return new HandshakeInformation<>(information, data -> data); | ||
} | ||
|
||
/** | ||
* Returns a new instance of HandshakeInformation with the provided information. | ||
* | ||
* @param information The information to be included in the handshake. | ||
* @return A new instance of HandshakeInformation with the provided information and resource key. | ||
*/ | ||
public static HandshakeInformation<Integer> integer(int information) { | ||
return new HandshakeInformation<>(information, String::valueOf); | ||
} | ||
|
||
/** | ||
* Returns a new instance of HandshakeInformation with the provided information. | ||
* | ||
* @param information The information to be included in the handshake. | ||
* @return A new instance of HandshakeInformation with the provided information and resource key. | ||
*/ | ||
public static HandshakeInformation<Long> asLong(long information) { | ||
return new HandshakeInformation<>(information, String::valueOf); | ||
} | ||
|
||
|
||
/** | ||
* Returns a new instance of HandshakeInformation with the provided value. | ||
* <p> | ||
* String transformation will use the default {@link Object#toString()} method. | ||
* | ||
* @param value The value to be included in the handshake. | ||
* @param <T> The type of the value. | ||
* @return A new instance of HandshakeInformation with the provided value. | ||
*/ | ||
public static <T> HandshakeInformation<T> of(@NotNull T value) { | ||
return new HandshakeInformation<>(value, Object::toString); | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
...src/main/java/fr/atlasworld/network/api/networking/event/HandshakeDataInjectionEvent.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,48 @@ | ||
package fr.atlasworld.network.api.networking.event; | ||
|
||
import fr.atlasworld.network.api.event.Event; | ||
import fr.atlasworld.network.api.module.Module; | ||
import fr.atlasworld.network.api.networking.HandshakeInformation; | ||
import fr.atlasworld.network.api.registry.ResourceKey; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Event called when modules are supposed to inject their data into the handshake packet. | ||
* The packet is in plaintext nor is the client trusted at this state. | ||
* Don't send any sensitive information through here. | ||
*/ | ||
public interface HandshakeDataInjectionEvent extends Event { | ||
|
||
/** | ||
* Injects the provided information into the handshake using the specified key. | ||
* | ||
* @param key The key associated with the information. | ||
* @param information The information to be injected into the handshake. | ||
*/ | ||
void inject(@NotNull String key, @NotNull HandshakeInformation<?> information); | ||
|
||
/** | ||
* Injects the provided information into the handshake using the specified module. | ||
* | ||
* @param module The module associated with the information. | ||
* @param information The information to be injected into the handshake. | ||
*/ | ||
void inject(@NotNull Module module, @NotNull HandshakeInformation<?> information); | ||
|
||
/** | ||
* Injects the provided information into the handshake packet associated with the given resource key. | ||
* | ||
* @param key The resource key that identifies the handshake packet. | ||
* @param information The information to be injected into the handshake packet. | ||
*/ | ||
void inject(@NotNull ResourceKey key, @NotNull HandshakeInformation<?> information); | ||
|
||
/** | ||
* Clears the data of the handshake packet. | ||
* <p> | ||
* Important internal data such as Protocol version or System version are not stored in the handshake packet. | ||
* So you can clear the data without worrying breaking AtlasNetwork's protocol. | ||
* But don't forget you may not be the only one injecting data in the packet and this could lead to unexpected issues. | ||
*/ | ||
void clear(); | ||
} |
7 changes: 7 additions & 0 deletions
7
api/src/main/java/fr/atlasworld/network/api/networking/event/PacketRegistrationEvent.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,7 @@ | ||
package fr.atlasworld.network.api.networking.event; | ||
|
||
import fr.atlasworld.network.api.networking.packet.NetworkPacket; | ||
import fr.atlasworld.network.api.registry.event.RegistrationEvent; | ||
|
||
public interface PacketRegistrationEvent extends RegistrationEvent<NetworkPacket> { | ||
} |
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
17 changes: 17 additions & 0 deletions
17
api/src/test/java/fr/atlasworld/network/api/test/event/NetworkListener.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,17 @@ | ||
package fr.atlasworld.network.api.test.event; | ||
|
||
import fr.atlasworld.network.api.event.EventHandler; | ||
import fr.atlasworld.network.api.event.EventListener; | ||
import fr.atlasworld.network.api.networking.HandshakeInformation; | ||
import fr.atlasworld.network.api.networking.event.HandshakeDataInjectionEvent; | ||
import fr.atlasworld.network.api.test.TestModule; | ||
|
||
public class NetworkListener implements EventListener { | ||
|
||
@EventHandler | ||
private void onHandshake(HandshakeDataInjectionEvent event) { | ||
event.inject("my_unique_id", HandshakeInformation.text("my_custom_info")); | ||
|
||
TestModule.LOGGER.info("Successfully injected handshake data."); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
api/src/test/java/fr/atlasworld/network/api/test/networking/TestPackets.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,14 @@ | ||
package fr.atlasworld.network.api.test.networking; | ||
|
||
import fr.atlasworld.network.api.networking.packet.NetworkPacket; | ||
import fr.atlasworld.network.api.registry.Register; | ||
import fr.atlasworld.network.api.registry.RegistryObject; | ||
import fr.atlasworld.network.api.test.TestModule; | ||
import fr.atlasworld.network.api.test.networking.packets.MyCustomPacket; | ||
|
||
public class TestPackets { | ||
private static final Register<NetworkPacket> PACKETS = new Register<>(TestModule.MODULE); | ||
|
||
public static final RegistryObject<NetworkPacket> MY_CUSTOM_PACKET = PACKETS.register(MyCustomPacket.KEY.getKey(), | ||
MyCustomPacket::new); | ||
} |
58 changes: 58 additions & 0 deletions
58
api/src/test/java/fr/atlasworld/network/api/test/networking/packets/MyCustomPacket.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,58 @@ | ||
package fr.atlasworld.network.api.test.networking.packets; | ||
|
||
import fr.atlasworld.network.api.networking.ConnectionSource; | ||
import fr.atlasworld.network.api.networking.exception.packet.PacketExecutionException; | ||
import fr.atlasworld.network.api.networking.packet.NetworkPacket; | ||
import fr.atlasworld.network.api.networking.packet.PacketByteBuf; | ||
import fr.atlasworld.network.api.registry.ResourceKey; | ||
import fr.atlasworld.network.api.test.TestModule; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class MyCustomPacket implements NetworkPacket { | ||
public static final ResourceKey KEY = new ResourceKey(TestModule.MODULE, "myCustomPacket"); | ||
|
||
private int myInt; | ||
private String myString; | ||
private boolean myBoolean; | ||
|
||
@Override | ||
public @NotNull String getKey() { | ||
return KEY.toString(); | ||
} | ||
|
||
public MyCustomPacket() { | ||
|
||
} | ||
|
||
public MyCustomPacket(int myInt, String myString, boolean myBoolean) { | ||
this.myInt = myInt; | ||
this.myString = myString; | ||
this.myBoolean = myBoolean; | ||
} | ||
|
||
@Override | ||
public void decode(@NotNull ConnectionSource source, @NotNull PacketByteBuf buffer) throws PacketExecutionException { | ||
this.myInt = buffer.readInt(); | ||
this.myString = buffer.readString(); | ||
this.myBoolean = buffer.readBoolean(); | ||
} | ||
|
||
@Override | ||
public void encode(@NotNull PacketByteBuf buffer) throws PacketExecutionException { | ||
buffer.writeInt(this.myInt); | ||
buffer.writeString(buffer.readString()); | ||
buffer.writeBoolean(buffer.readBoolean()); | ||
} | ||
|
||
public int getMyInt() { | ||
return myInt; | ||
} | ||
|
||
public String getMyString() { | ||
return myString; | ||
} | ||
|
||
public boolean isMyBoolean() { | ||
return myBoolean; | ||
} | ||
} |
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
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
Oops, something went wrong.