mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-31 21:48:08 +01:00
Swap to SocketAddress
This commit is contained in:
parent
a6294ffd5b
commit
17d584a2e3
@ -9,7 +9,6 @@ import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.extras.query.event.BasicQueryEvent;
|
||||
import net.minestom.server.extras.query.response.QueryResponse;
|
||||
import net.minestom.server.timer.Task;
|
||||
import net.minestom.server.utils.InetAddressWithPort;
|
||||
import net.minestom.server.utils.NetworkUtils;
|
||||
import net.minestom.server.utils.binary.BinaryWriter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -19,6 +18,7 @@ import org.slf4j.LoggerFactory;
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.SocketAddress;
|
||||
import java.net.SocketException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@ -38,7 +38,7 @@ public class Query {
|
||||
private static volatile DatagramSocket socket;
|
||||
private static volatile Thread thread;
|
||||
|
||||
private static final Int2ObjectMap<InetAddressWithPort> CHALLENGE_TOKENS = Int2ObjectMaps.synchronize(new Int2ObjectOpenHashMap<>());
|
||||
private static final Int2ObjectMap<SocketAddress> CHALLENGE_TOKENS = Int2ObjectMaps.synchronize(new Int2ObjectOpenHashMap<>());
|
||||
private static volatile Task task;
|
||||
|
||||
private Query() { }
|
||||
@ -161,7 +161,7 @@ public class Query {
|
||||
int sessionID = data.readInt();
|
||||
int challengeToken = RANDOM.nextInt();
|
||||
|
||||
CHALLENGE_TOKENS.put(challengeToken, new InetAddressWithPort(packet.getAddress(), packet.getPort()));
|
||||
CHALLENGE_TOKENS.put(challengeToken, packet.getSocketAddress());
|
||||
|
||||
// send the response
|
||||
BinaryWriter response = new BinaryWriter(32);
|
||||
@ -171,24 +171,24 @@ public class Query {
|
||||
|
||||
try {
|
||||
byte[] responseData = response.toByteArray();
|
||||
socket.send(new DatagramPacket(responseData, responseData.length, packet.getAddress(), packet.getPort()));
|
||||
socket.send(new DatagramPacket(responseData, responseData.length, packet.getSocketAddress()));
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("An error occurred whilst sending a query handshake packet.", e);
|
||||
}
|
||||
} else if (type == 0) { // stat
|
||||
int sessionID = data.readInt();
|
||||
int challengeToken = data.readInt();
|
||||
InetAddressWithPort sender = new InetAddressWithPort(packet.getAddress(), packet.getPort());
|
||||
SocketAddress sender = packet.getSocketAddress();
|
||||
|
||||
if (CHALLENGE_TOKENS.containsKey(challengeToken) && CHALLENGE_TOKENS.get(challengeToken).equals(sender)) {
|
||||
int remaining = data.readableBytes();
|
||||
|
||||
if (remaining == 0) { // basic
|
||||
BasicQueryEvent event = new BasicQueryEvent(new InetAddressWithPort(packet.getAddress(), packet.getPort()));
|
||||
BasicQueryEvent event = new BasicQueryEvent(sender);
|
||||
MinecraftServer.getGlobalEventHandler().callCancellableEvent(BasicQueryEvent.class, event,
|
||||
() -> sendResponse(event.getQueryResponse(), sessionID, sender));
|
||||
} else if (remaining == 8) { // full
|
||||
BasicQueryEvent event = new BasicQueryEvent(new InetAddressWithPort(packet.getAddress(), packet.getPort()));
|
||||
BasicQueryEvent event = new BasicQueryEvent(sender);
|
||||
MinecraftServer.getGlobalEventHandler().callCancellableEvent(BasicQueryEvent.class, event,
|
||||
() -> sendResponse(event.getQueryResponse(), sessionID, sender));
|
||||
}
|
||||
@ -197,7 +197,7 @@ public class Query {
|
||||
}
|
||||
}
|
||||
|
||||
private static void sendResponse(@NotNull QueryResponse queryResponse, int sessionID, @NotNull InetAddressWithPort sender) {
|
||||
private static void sendResponse(@NotNull QueryResponse queryResponse, int sessionID, @NotNull SocketAddress sender) {
|
||||
// header
|
||||
BinaryWriter response = new BinaryWriter();
|
||||
response.writeByte((byte) 0);
|
||||
@ -209,7 +209,7 @@ public class Query {
|
||||
// send!
|
||||
byte[] responseData = response.toByteArray();
|
||||
try {
|
||||
socket.send(new DatagramPacket(responseData, responseData.length, sender.getInetAddress(), sender.getPort()));
|
||||
socket.send(new DatagramPacket(responseData, responseData.length, sender));
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("An error occurred whilst sending a query handshake packet.", e);
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
package net.minestom.server.extras.query.event;
|
||||
|
||||
import net.minestom.server.extras.query.response.BasicQueryResponse;
|
||||
import net.minestom.server.utils.InetAddressWithPort;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
|
||||
/**
|
||||
* An event called when a basic query is received and ready to be responded to.
|
||||
*/
|
||||
@ -14,7 +15,7 @@ public class BasicQueryEvent extends QueryEvent<BasicQueryResponse> {
|
||||
*
|
||||
* @param sender the sender
|
||||
*/
|
||||
public BasicQueryEvent(@NotNull InetAddressWithPort sender) {
|
||||
public BasicQueryEvent(@NotNull SocketAddress sender) {
|
||||
super(sender, new BasicQueryResponse());
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
package net.minestom.server.extras.query.event;
|
||||
|
||||
import net.minestom.server.extras.query.response.FullQueryResponse;
|
||||
import net.minestom.server.utils.InetAddressWithPort;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
|
||||
/**
|
||||
* An event called when a full query is received and ready to be responded to.
|
||||
*/
|
||||
@ -14,7 +15,7 @@ public class FullQueryEvent extends QueryEvent<FullQueryResponse> {
|
||||
*
|
||||
* @param sender the sender
|
||||
*/
|
||||
public FullQueryEvent(@NotNull InetAddressWithPort sender) {
|
||||
public FullQueryEvent(@NotNull SocketAddress sender) {
|
||||
super(sender, new FullQueryResponse());
|
||||
}
|
||||
}
|
||||
|
@ -3,9 +3,9 @@ package net.minestom.server.extras.query.event;
|
||||
import net.minestom.server.event.CancellableEvent;
|
||||
import net.minestom.server.event.Event;
|
||||
import net.minestom.server.extras.query.response.QueryResponse;
|
||||
import net.minestom.server.utils.InetAddressWithPort;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@ -14,7 +14,7 @@ import java.util.Objects;
|
||||
* @param <T> the type of the response
|
||||
*/
|
||||
public abstract class QueryEvent<T extends QueryResponse> extends Event implements CancellableEvent {
|
||||
private final InetAddressWithPort sender;
|
||||
private final SocketAddress sender;
|
||||
|
||||
private T response;
|
||||
private boolean cancelled;
|
||||
@ -25,7 +25,7 @@ public abstract class QueryEvent<T extends QueryResponse> extends Event implemen
|
||||
* @param sender the sender
|
||||
* @param response the initial response
|
||||
*/
|
||||
public QueryEvent(@NotNull InetAddressWithPort sender, @NotNull T response) {
|
||||
public QueryEvent(@NotNull SocketAddress sender, @NotNull T response) {
|
||||
this.sender = sender;
|
||||
this.response = response;
|
||||
this.cancelled = false;
|
||||
@ -51,11 +51,11 @@ public abstract class QueryEvent<T extends QueryResponse> extends Event implemen
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the IP address and port of the initiator of the query.
|
||||
* Gets the socket address of the initiator of the query.
|
||||
*
|
||||
* @return the initiator
|
||||
*/
|
||||
public @NotNull InetAddressWithPort getSender() {
|
||||
public @NotNull SocketAddress getSender() {
|
||||
return this.sender;
|
||||
}
|
||||
|
||||
|
@ -1,60 +0,0 @@
|
||||
package net.minestom.server.utils;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* A utility class to hold an {@link InetAddress} and a port.
|
||||
*/
|
||||
public class InetAddressWithPort {
|
||||
private final InetAddress inetAddress;
|
||||
private final int port;
|
||||
|
||||
/**
|
||||
* Creates a new {@link InetAddressWithPort}.
|
||||
*
|
||||
* @param inetAddress the inet address
|
||||
* @param port the port
|
||||
*/
|
||||
public InetAddressWithPort(@NotNull InetAddress inetAddress, int port) {
|
||||
Validate.inclusiveBetween(1, 65535, port, "port must be a valid port");
|
||||
|
||||
this.inetAddress = Objects.requireNonNull(inetAddress, "inetAddress");
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the inet address.
|
||||
*
|
||||
* @return the inet address
|
||||
*/
|
||||
public @NotNull InetAddress getInetAddress() {
|
||||
return this.inetAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the port.
|
||||
*
|
||||
* @return the port
|
||||
*/
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
InetAddressWithPort that = (InetAddressWithPort) o;
|
||||
return port == that.port && Objects.equals(inetAddress, that.inetAddress);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(inetAddress, port);
|
||||
}
|
||||
}
|
@ -109,10 +109,10 @@ public class Main {
|
||||
// useful for testing - we don't need to worry about event calls so just set this to a long time
|
||||
OpenToLAN.open(new OpenToLANConfig().setDelayBetweenEventCalls(new UpdateOption(1, TimeUnit.DAY)));
|
||||
|
||||
Query.start(25566);
|
||||
|
||||
minecraftServer.start("0.0.0.0", 25565);
|
||||
//Runtime.getRuntime().addShutdownHook(new Thread(MinecraftServer::stopCleanly));
|
||||
|
||||
Query.start(25566);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user