Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create module for network-related objects #241

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
20 changes: 20 additions & 0 deletions network/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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")
}
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -28,7 +27,7 @@ public abstract class Client<I> {
/**
* The underlying channel.
*/
final Channel channel;
protected final Channel channel;

/**
* The IP address.
Expand All @@ -45,9 +44,9 @@ public abstract class Client<I> {
*
* @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");
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
*/

rootProject.name = "luna"

include(":network")
1 change: 0 additions & 1 deletion src/main/java/io/luna/net/LunaChannelInitializer.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/io/luna/net/LunaUpstreamHandler.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/luna/net/client/GameClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,7 +50,7 @@ public void onInactive() {
}

@Override
void onMessageReceived(GameMessage msg) {
public void onMessageReceived(GameMessage msg) {
if (!decodedMessages.offer(msg)) {
msg.getPayload().releaseAll();
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/luna/net/client/IdleClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.luna.net.client;

import io.luna.net.Client;
import io.netty.channel.Channel;

/**
Expand All @@ -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.");
}
}
3 changes: 2 additions & 1 deletion src/main/java/io/luna/net/client/LoginClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/luna/net/codec/login/LoginDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/luna/net/codec/login/LoginResponse.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
31 changes: 0 additions & 31 deletions src/main/java/io/luna/util/NetworkUtils.java

This file was deleted.