diff --git a/build.gradle.kts b/build.gradle.kts index 1a51f8fa..825aef7b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -19,6 +19,7 @@ repositories { val junitVersion: String by project dependencies { + implementation(project(":network")) implementation("com.google.code.gson:gson:2.8.5") implementation("org.apache.logging.log4j:log4j-core:2.14.0") implementation("org.apache.logging.log4j:log4j-api:2.14.0") diff --git a/network/build.gradle.kts b/network/build.gradle.kts new file mode 100644 index 00000000..c79515e7 --- /dev/null +++ b/network/build.gradle.kts @@ -0,0 +1,20 @@ +plugins { + java +} + +group = "luna" +version = "1.0" + +repositories { + jcenter() +} + +val junitVersion: String by project + +dependencies { + implementation("io.netty:netty-all:4.1.56.Final") + implementation("com.google.guava:guava:30.1-jre") + testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion") + testImplementation("org.junit.jupiter:junit-jupiter-params:$junitVersion") + testImplementation("org.junit.jupiter:junit-jupiter-engine:$junitVersion") +} diff --git a/src/main/java/io/luna/net/client/Client.java b/network/src/main/java/io/luna/net/Client.java similarity index 85% rename from src/main/java/io/luna/net/client/Client.java rename to network/src/main/java/io/luna/net/Client.java index a4d3a443..ac6a7c9b 100644 --- a/src/main/java/io/luna/net/client/Client.java +++ b/network/src/main/java/io/luna/net/Client.java @@ -1,13 +1,12 @@ -package io.luna.net.client; +package io.luna.net; import com.google.common.base.MoreObjects; -import io.luna.net.LunaChannelFilter; -import io.luna.util.NetworkUtils; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.util.AttributeKey; import io.netty.util.internal.TypeParameterMatcher; +import java.net.InetSocketAddress; import java.util.Objects; /** @@ -28,7 +27,7 @@ public abstract class Client { /** * The underlying channel. */ - final Channel channel; + protected final Channel channel; /** * The IP address. @@ -45,9 +44,9 @@ public abstract class Client { * * @param channel The underlying channel. */ - Client(Channel channel) { + protected Client(Channel channel) { this.channel = channel; - ipAddress = NetworkUtils.getIpAddress(channel); + ipAddress = getIpAddressFrom(channel); parameterMatcher = TypeParameterMatcher.find(this, Client.class, "I"); } @@ -79,7 +78,7 @@ public final String toString() { * @param msg The message to handle. * @throws Exception If any errors occur. */ - abstract void onMessageReceived(I msg) throws Exception; + protected abstract void onMessageReceived(I msg) throws Exception; /** * Called when the underlying channel is disconnected. @@ -128,10 +127,12 @@ public final Channel getChannel() { } /** - * @return The client's channel filter. + * @param channel + * @return The connected remote address the argued channel. */ - public final LunaChannelFilter getChannelFilter() { - return channel.attr(LunaChannelFilter.KEY).get(); + final String getIpAddressFrom(Channel channel) { + InetSocketAddress socketAddress = (InetSocketAddress) channel.remoteAddress(); + return socketAddress.getAddress().getHostAddress(); } /** diff --git a/src/main/java/io/luna/net/codec/ByteMessage.java b/network/src/main/java/io/luna/net/codec/ByteMessage.java similarity index 100% rename from src/main/java/io/luna/net/codec/ByteMessage.java rename to network/src/main/java/io/luna/net/codec/ByteMessage.java diff --git a/src/main/java/io/luna/net/codec/ByteOrder.java b/network/src/main/java/io/luna/net/codec/ByteOrder.java similarity index 100% rename from src/main/java/io/luna/net/codec/ByteOrder.java rename to network/src/main/java/io/luna/net/codec/ByteOrder.java diff --git a/src/main/java/io/luna/net/codec/IsaacCipher.java b/network/src/main/java/io/luna/net/codec/IsaacCipher.java similarity index 100% rename from src/main/java/io/luna/net/codec/IsaacCipher.java rename to network/src/main/java/io/luna/net/codec/IsaacCipher.java diff --git a/src/main/java/io/luna/net/codec/MessageType.java b/network/src/main/java/io/luna/net/codec/MessageType.java similarity index 100% rename from src/main/java/io/luna/net/codec/MessageType.java rename to network/src/main/java/io/luna/net/codec/MessageType.java diff --git a/src/main/java/io/luna/net/codec/ProgressiveMessageDecoder.java b/network/src/main/java/io/luna/net/codec/ProgressiveMessageDecoder.java similarity index 100% rename from src/main/java/io/luna/net/codec/ProgressiveMessageDecoder.java rename to network/src/main/java/io/luna/net/codec/ProgressiveMessageDecoder.java diff --git a/src/main/java/io/luna/net/codec/ValueType.java b/network/src/main/java/io/luna/net/codec/ValueType.java similarity index 100% rename from src/main/java/io/luna/net/codec/ValueType.java rename to network/src/main/java/io/luna/net/codec/ValueType.java diff --git a/settings.gradle.kts b/settings.gradle.kts index ff326966..2f987c2a 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -3,3 +3,5 @@ */ rootProject.name = "luna" + +include(":network") diff --git a/src/main/java/io/luna/net/LunaChannelInitializer.java b/src/main/java/io/luna/net/LunaChannelInitializer.java index eae415e8..879a5290 100644 --- a/src/main/java/io/luna/net/LunaChannelInitializer.java +++ b/src/main/java/io/luna/net/LunaChannelInitializer.java @@ -1,7 +1,6 @@ package io.luna.net; import io.luna.LunaContext; -import io.luna.net.client.Client; import io.luna.net.client.IdleClient; import io.luna.net.codec.login.LoginDecoder; import io.luna.net.codec.login.LoginEncoder; diff --git a/src/main/java/io/luna/net/LunaUpstreamHandler.java b/src/main/java/io/luna/net/LunaUpstreamHandler.java index cf01574c..2b62c073 100644 --- a/src/main/java/io/luna/net/LunaUpstreamHandler.java +++ b/src/main/java/io/luna/net/LunaUpstreamHandler.java @@ -1,6 +1,5 @@ package io.luna.net; -import io.luna.net.client.Client; import io.netty.channel.ChannelHandler.Sharable; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; diff --git a/src/main/java/io/luna/net/client/GameClient.java b/src/main/java/io/luna/net/client/GameClient.java index 8a3bf3ac..7864e5dd 100644 --- a/src/main/java/io/luna/net/client/GameClient.java +++ b/src/main/java/io/luna/net/client/GameClient.java @@ -5,6 +5,7 @@ import io.luna.net.msg.GameMessageReader; import io.luna.net.msg.GameMessageRepository; import io.luna.net.msg.GameMessageWriter; +import io.luna.net.Client; import io.netty.channel.Channel; import java.util.Queue; @@ -49,7 +50,7 @@ public void onInactive() { } @Override - void onMessageReceived(GameMessage msg) { + public void onMessageReceived(GameMessage msg) { if (!decodedMessages.offer(msg)) { msg.getPayload().releaseAll(); } diff --git a/src/main/java/io/luna/net/client/IdleClient.java b/src/main/java/io/luna/net/client/IdleClient.java index 2e59590b..f1b528e7 100644 --- a/src/main/java/io/luna/net/client/IdleClient.java +++ b/src/main/java/io/luna/net/client/IdleClient.java @@ -1,5 +1,6 @@ package io.luna.net.client; +import io.luna.net.Client; import io.netty.channel.Channel; /** @@ -20,7 +21,7 @@ public IdleClient(Channel channel) { } @Override - void onMessageReceived(Object msg) { + public void onMessageReceived(Object msg) { throw new UnsupportedOperationException("Not ready for I/O."); } } \ No newline at end of file diff --git a/src/main/java/io/luna/net/client/LoginClient.java b/src/main/java/io/luna/net/client/LoginClient.java index f7067522..771304de 100644 --- a/src/main/java/io/luna/net/client/LoginClient.java +++ b/src/main/java/io/luna/net/client/LoginClient.java @@ -13,6 +13,7 @@ import io.luna.net.codec.login.LoginResponse; import io.luna.net.codec.login.LoginResponseMessage; import io.luna.net.msg.GameMessageRepository; +import io.luna.net.Client; import io.netty.channel.Channel; import io.netty.channel.ChannelFutureListener; import org.mindrot.jbcrypt.BCrypt; @@ -59,7 +60,7 @@ public LoginClient(Channel channel, LunaContext context, GameMessageRepository m } @Override - void onMessageReceived(LoginRequestMessage msg) { + public void onMessageReceived(LoginRequestMessage msg) { String username = msg.getUsername(); String password = msg.getPassword(); var player = new Player(context, new PlayerCredentials(username, password)); diff --git a/src/main/java/io/luna/net/codec/login/LoginDecoder.java b/src/main/java/io/luna/net/codec/login/LoginDecoder.java index b7ea93e9..6642f2fb 100644 --- a/src/main/java/io/luna/net/codec/login/LoginDecoder.java +++ b/src/main/java/io/luna/net/codec/login/LoginDecoder.java @@ -2,7 +2,7 @@ import com.moandjiezana.toml.Toml; import io.luna.LunaContext; -import io.luna.net.client.Client; +import io.luna.net.Client; import io.luna.net.client.LoginClient; import io.luna.net.codec.ByteMessage; import io.luna.net.codec.IsaacCipher; diff --git a/src/main/java/io/luna/net/codec/login/LoginResponse.java b/src/main/java/io/luna/net/codec/login/LoginResponse.java index 2e2305ef..c934956d 100644 --- a/src/main/java/io/luna/net/codec/login/LoginResponse.java +++ b/src/main/java/io/luna/net/codec/login/LoginResponse.java @@ -1,6 +1,6 @@ package io.luna.net.codec.login; -import io.luna.net.client.Client; +import io.luna.net.Client; /** * An enumerated type whose values represent login responses. Any response other than {@link #NORMAL} will diff --git a/src/main/java/io/luna/net/codec/login/LoginResponseMessage.java b/src/main/java/io/luna/net/codec/login/LoginResponseMessage.java index f6dd3624..96c5b0a7 100644 --- a/src/main/java/io/luna/net/codec/login/LoginResponseMessage.java +++ b/src/main/java/io/luna/net/codec/login/LoginResponseMessage.java @@ -1,7 +1,7 @@ package io.luna.net.codec.login; import io.luna.game.model.mob.PlayerRights; -import io.luna.net.client.Client; +import io.luna.net.Client; /** * An immutable model representing login response data. diff --git a/src/main/java/io/luna/net/msg/in/CastOnNpcMessageReader.java b/src/main/java/io/luna/net/msg/in/CastOnNpcMessageReader.java index da0a9e94..56d946c8 100644 --- a/src/main/java/io/luna/net/msg/in/CastOnNpcMessageReader.java +++ b/src/main/java/io/luna/net/msg/in/CastOnNpcMessageReader.java @@ -3,7 +3,6 @@ import io.luna.game.action.InteractionAction; import io.luna.game.event.Event; import io.luna.game.event.impl.CastOnNpcEvent; -import io.luna.game.event.impl.CastOnPlayerEvent; import io.luna.game.model.mob.MobList; import io.luna.game.model.mob.Npc; import io.luna.game.model.mob.Player; diff --git a/src/main/java/io/luna/net/msg/in/ObjectClickMessageReader.java b/src/main/java/io/luna/net/msg/in/ObjectClickMessageReader.java index e6da25fe..8fb2f592 100644 --- a/src/main/java/io/luna/net/msg/in/ObjectClickMessageReader.java +++ b/src/main/java/io/luna/net/msg/in/ObjectClickMessageReader.java @@ -17,11 +17,6 @@ import io.luna.net.codec.ValueType; import io.luna.net.msg.GameMessage; import io.luna.net.msg.GameMessageReader; -import javafx.geometry.Pos; - -import java.util.function.BiFunction; -import java.util.function.Function; -import java.util.function.Supplier; import static com.google.common.base.Preconditions.checkState; diff --git a/src/main/java/io/luna/net/msg/out/WidgetTextMessageWriter.java b/src/main/java/io/luna/net/msg/out/WidgetTextMessageWriter.java index 443fb19e..8846fc39 100644 --- a/src/main/java/io/luna/net/msg/out/WidgetTextMessageWriter.java +++ b/src/main/java/io/luna/net/msg/out/WidgetTextMessageWriter.java @@ -1,6 +1,5 @@ package io.luna.net.msg.out; -import io.luna.game.event.impl.PlayerEvent; import io.luna.game.model.mob.Player; import io.luna.net.codec.ByteMessage; import io.luna.net.codec.MessageType; diff --git a/src/main/java/io/luna/util/NetworkUtils.java b/src/main/java/io/luna/util/NetworkUtils.java deleted file mode 100644 index 18d2721b..00000000 --- a/src/main/java/io/luna/util/NetworkUtils.java +++ /dev/null @@ -1,31 +0,0 @@ -package io.luna.util; - - -import io.netty.channel.Channel; - -import java.net.InetSocketAddress; - -/** - * A static-utility class that contains functions for networking. - * - * @author lare96 - */ -public class NetworkUtils { - - /** - * Retrieves the IP address for {@code channel}. - * - * @param channel The channel. - * @return The IP address of the channel. - */ - public static String getIpAddress(Channel channel) { - InetSocketAddress socketAddress = (InetSocketAddress) channel.remoteAddress(); - return socketAddress.getAddress().getHostAddress(); - } - - /** - * A private constructor to discourage external instantiation. - */ - private NetworkUtils() { - } -} \ No newline at end of file