mirror of https://github.com/Minestom/Minestom.git
Remove deprecated ResponseDataConsumer, add support for unix domain socket
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
a0549fdcad
commit
e0e5fe049d
|
@ -17,7 +17,6 @@ import net.minestom.server.network.PacketProcessor;
|
||||||
import net.minestom.server.network.packet.server.play.PluginMessagePacket;
|
import net.minestom.server.network.packet.server.play.PluginMessagePacket;
|
||||||
import net.minestom.server.network.packet.server.play.ServerDifficultyPacket;
|
import net.minestom.server.network.packet.server.play.ServerDifficultyPacket;
|
||||||
import net.minestom.server.network.socket.Server;
|
import net.minestom.server.network.socket.Server;
|
||||||
import net.minestom.server.ping.ResponseDataConsumer;
|
|
||||||
import net.minestom.server.recipe.RecipeManager;
|
import net.minestom.server.recipe.RecipeManager;
|
||||||
import net.minestom.server.scoreboard.TeamManager;
|
import net.minestom.server.scoreboard.TeamManager;
|
||||||
import net.minestom.server.storage.StorageManager;
|
import net.minestom.server.storage.StorageManager;
|
||||||
|
@ -31,13 +30,13 @@ import net.minestom.server.world.DimensionTypeManager;
|
||||||
import net.minestom.server.world.biomes.BiomeManager;
|
import net.minestom.server.world.biomes.BiomeManager;
|
||||||
import org.jetbrains.annotations.ApiStatus;
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.jetbrains.annotations.UnknownNullability;
|
import org.jetbrains.annotations.UnknownNullability;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.SocketAddress;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main server class used to start the server and retrieve all the managers.
|
* The main server class used to start the server and retrieve all the managers.
|
||||||
|
@ -74,7 +73,6 @@ public final class MinecraftServer {
|
||||||
private static int entityViewDistance = Integer.getInteger("minestom.entity-view-distance", 5);
|
private static int entityViewDistance = Integer.getInteger("minestom.entity-view-distance", 5);
|
||||||
private static int compressionThreshold = 256;
|
private static int compressionThreshold = 256;
|
||||||
private static boolean terminalEnabled = System.getProperty("minestom.terminal.disabled") == null;
|
private static boolean terminalEnabled = System.getProperty("minestom.terminal.disabled") == null;
|
||||||
private static ResponseDataConsumer responseDataConsumer;
|
|
||||||
private static String brandName = "Minestom";
|
private static String brandName = "Minestom";
|
||||||
private static Difficulty difficulty = Difficulty.NORMAL;
|
private static Difficulty difficulty = Difficulty.NORMAL;
|
||||||
|
|
||||||
|
@ -312,7 +310,7 @@ public final class MinecraftServer {
|
||||||
/**
|
/**
|
||||||
* Changes the compression threshold of the server.
|
* Changes the compression threshold of the server.
|
||||||
* <p>
|
* <p>
|
||||||
* WARNING: this need to be called before {@link #start(String, int, ResponseDataConsumer)}.
|
* WARNING: this need to be called before {@link #start(SocketAddress)}.
|
||||||
*
|
*
|
||||||
* @param compressionThreshold the new compression threshold, 0 to disable compression
|
* @param compressionThreshold the new compression threshold, 0 to disable compression
|
||||||
* @throws IllegalStateException if this is called after the server started
|
* @throws IllegalStateException if this is called after the server started
|
||||||
|
@ -341,17 +339,6 @@ public final class MinecraftServer {
|
||||||
MinecraftServer.terminalEnabled = enabled;
|
MinecraftServer.terminalEnabled = enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the consumer executed to show server-list data.
|
|
||||||
*
|
|
||||||
* @return the response data consumer
|
|
||||||
* @deprecated listen to the {@link net.minestom.server.event.server.ServerListPingEvent} instead
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public static ResponseDataConsumer getResponseDataConsumer() {
|
|
||||||
return responseDataConsumer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static DimensionTypeManager getDimensionTypeManager() {
|
public static DimensionTypeManager getDimensionTypeManager() {
|
||||||
return serverProcess.dimension();
|
return serverProcess.dimension();
|
||||||
}
|
}
|
||||||
|
@ -381,30 +368,16 @@ public final class MinecraftServer {
|
||||||
* <p>
|
* <p>
|
||||||
* It should be called after {@link #init()} and probably your own initialization code.
|
* It should be called after {@link #init()} and probably your own initialization code.
|
||||||
*
|
*
|
||||||
* @param address the server address
|
* @param address the server address
|
||||||
* @param port the server port
|
|
||||||
* @param responseDataConsumer the response data consumer, can be null
|
|
||||||
* @throws IllegalStateException if called before {@link #init()} or if the server is already running
|
* @throws IllegalStateException if called before {@link #init()} or if the server is already running
|
||||||
* @deprecated use {@link #start(String, int)} and listen to the {@link net.minestom.server.event.server.ServerListPingEvent} event instead of ResponseDataConsumer
|
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
public void start(@NotNull SocketAddress address) {
|
||||||
public void start(@NotNull String address, int port, @Nullable ResponseDataConsumer responseDataConsumer) {
|
serverProcess.start(address);
|
||||||
MinecraftServer.responseDataConsumer = responseDataConsumer;
|
new TickSchedulerThread(serverProcess).start();
|
||||||
start(address, port);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Starts the server.
|
|
||||||
* <p>
|
|
||||||
* It should be called after {@link #init()} and probably your own initialization code.
|
|
||||||
*
|
|
||||||
* @param address the server address
|
|
||||||
* @param port the server port
|
|
||||||
* @throws IllegalStateException if called before {@link #init()} or if the server is already running
|
|
||||||
*/
|
|
||||||
public void start(@NotNull String address, int port) {
|
public void start(@NotNull String address, int port) {
|
||||||
serverProcess.start(new InetSocketAddress(address, port));
|
start(new InetSocketAddress(address, port));
|
||||||
new TickSchedulerThread(serverProcess).start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
package net.minestom.server.event.server;
|
package net.minestom.server.event.server;
|
||||||
|
|
||||||
import net.minestom.server.MinecraftServer;
|
|
||||||
import net.minestom.server.event.trait.CancellableEvent;
|
import net.minestom.server.event.trait.CancellableEvent;
|
||||||
import net.minestom.server.network.player.PlayerConnection;
|
import net.minestom.server.network.player.PlayerConnection;
|
||||||
import net.minestom.server.ping.ResponseData;
|
import net.minestom.server.ping.ResponseData;
|
||||||
import net.minestom.server.ping.ResponseDataConsumer;
|
|
||||||
import net.minestom.server.ping.ServerListPingType;
|
import net.minestom.server.ping.ServerListPingType;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
@ -38,14 +36,7 @@ public class ServerListPingEvent implements CancellableEvent {
|
||||||
* @param type the ping type to respond with
|
* @param type the ping type to respond with
|
||||||
*/
|
*/
|
||||||
public ServerListPingEvent(@Nullable PlayerConnection connection, @NotNull ServerListPingType type) {
|
public ServerListPingEvent(@Nullable PlayerConnection connection, @NotNull ServerListPingType type) {
|
||||||
//noinspection deprecation we need to continue doing this until the consumer is removed - todo remove
|
|
||||||
ResponseDataConsumer consumer = MinecraftServer.getResponseDataConsumer();
|
|
||||||
this.responseData = new ResponseData();
|
this.responseData = new ResponseData();
|
||||||
|
|
||||||
if (consumer != null) {
|
|
||||||
consumer.accept(connection, responseData);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ import net.minestom.dependencies.maven.MavenRepository;
|
||||||
import net.minestom.server.ServerProcess;
|
import net.minestom.server.ServerProcess;
|
||||||
import net.minestom.server.event.Event;
|
import net.minestom.server.event.Event;
|
||||||
import net.minestom.server.event.EventNode;
|
import net.minestom.server.event.EventNode;
|
||||||
import net.minestom.server.ping.ResponseDataConsumer;
|
|
||||||
import net.minestom.server.utils.validate.Check;
|
import net.minestom.server.utils.validate.Check;
|
||||||
import org.jetbrains.annotations.ApiStatus;
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
@ -56,7 +55,7 @@ public class ExtensionManager {
|
||||||
* <p>
|
* <p>
|
||||||
* Default value is 'true'.
|
* Default value is 'true'.
|
||||||
*
|
*
|
||||||
* @return true if extensions are loaded in {@link net.minestom.server.MinecraftServer#start(String, int, ResponseDataConsumer)}
|
* @return true if extensions are loaded in {@link net.minestom.server.MinecraftServer#start(java.net.SocketAddress)}
|
||||||
*/
|
*/
|
||||||
public boolean shouldLoadOnStartup() {
|
public boolean shouldLoadOnStartup() {
|
||||||
return state != State.DO_NOT_START;
|
return state != State.DO_NOT_START;
|
||||||
|
|
|
@ -6,12 +6,12 @@ import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.*;
|
||||||
import java.net.SocketAddress;
|
|
||||||
import java.nio.channels.SelectionKey;
|
import java.nio.channels.SelectionKey;
|
||||||
import java.nio.channels.Selector;
|
import java.nio.channels.Selector;
|
||||||
import java.nio.channels.ServerSocketChannel;
|
import java.nio.channels.ServerSocketChannel;
|
||||||
import java.nio.channels.SocketChannel;
|
import java.nio.channels.SocketChannel;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -31,6 +31,7 @@ public final class Server {
|
||||||
private int index;
|
private int index;
|
||||||
|
|
||||||
private ServerSocketChannel serverSocket;
|
private ServerSocketChannel serverSocket;
|
||||||
|
private SocketAddress socketAddress;
|
||||||
private String address;
|
private String address;
|
||||||
private int port;
|
private int port;
|
||||||
|
|
||||||
|
@ -43,16 +44,25 @@ public final class Server {
|
||||||
|
|
||||||
@ApiStatus.Internal
|
@ApiStatus.Internal
|
||||||
public void init(SocketAddress address) throws IOException {
|
public void init(SocketAddress address) throws IOException {
|
||||||
|
ProtocolFamily family;
|
||||||
if (address instanceof InetSocketAddress inetSocketAddress) {
|
if (address instanceof InetSocketAddress inetSocketAddress) {
|
||||||
this.address = inetSocketAddress.getHostString();
|
this.address = inetSocketAddress.getHostString();
|
||||||
this.port = inetSocketAddress.getPort();
|
this.port = inetSocketAddress.getPort();
|
||||||
} // TODO unix domain support
|
family = inetSocketAddress.getAddress().getAddress().length == 4 ? StandardProtocolFamily.INET : StandardProtocolFamily.INET6;
|
||||||
|
} else if (address instanceof UnixDomainSocketAddress unixDomainSocketAddress) {
|
||||||
|
this.address = "unix://" + unixDomainSocketAddress.getPath();
|
||||||
|
this.port = 0;
|
||||||
|
family = StandardProtocolFamily.UNIX;
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Address must be an InetSocketAddress or a UnixDomainSocketAddress");
|
||||||
|
}
|
||||||
|
|
||||||
ServerSocketChannel server = ServerSocketChannel.open();
|
ServerSocketChannel server = ServerSocketChannel.open(family);
|
||||||
server.bind(address);
|
server.bind(address);
|
||||||
server.configureBlocking(false);
|
server.configureBlocking(false);
|
||||||
server.register(selector, SelectionKey.OP_ACCEPT);
|
server.register(selector, SelectionKey.OP_ACCEPT);
|
||||||
this.serverSocket = server;
|
this.serverSocket = server;
|
||||||
|
this.socketAddress = address;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiStatus.Internal
|
@ApiStatus.Internal
|
||||||
|
@ -77,6 +87,14 @@ public final class Server {
|
||||||
MinecraftServer.getExceptionManager().handleException(e);
|
MinecraftServer.getExceptionManager().handleException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
serverSocket.close();
|
||||||
|
if (socketAddress instanceof UnixDomainSocketAddress unixDomainSocketAddress) {
|
||||||
|
Files.deleteIfExists(unixDomainSocketAddress.getPath());
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
MinecraftServer.getExceptionManager().handleException(e);
|
||||||
|
}
|
||||||
}, "Ms-entrypoint").start();
|
}, "Ms-entrypoint").start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,9 +113,8 @@ public final class Server {
|
||||||
return packetProcessor;
|
return packetProcessor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiStatus.Internal
|
public SocketAddress socketAddress() {
|
||||||
public List<Worker> workers() {
|
return socketAddress;
|
||||||
return workers;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAddress() {
|
public String getAddress() {
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
package net.minestom.server.ping;
|
|
||||||
|
|
||||||
import net.minestom.server.network.player.PlayerConnection;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Consumer used to fill a {@link ResponseData} object before being sent to a connection.
|
|
||||||
*
|
|
||||||
* <p>Can be specified in {@link net.minestom.server.MinecraftServer#start(String, int,
|
|
||||||
* ResponseDataConsumer)}.
|
|
||||||
*
|
|
||||||
* @deprecated listen to the {@link net.minestom.server.event.server.ServerListPingEvent} instead
|
|
||||||
*/
|
|
||||||
@FunctionalInterface
|
|
||||||
@Deprecated
|
|
||||||
public interface ResponseDataConsumer {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A method to fill the data of the response.
|
|
||||||
*
|
|
||||||
* @param playerConnection The player connection to which the response should be sent.
|
|
||||||
* @param responseData The data for the response.
|
|
||||||
*/
|
|
||||||
void accept(PlayerConnection playerConnection, ResponseData responseData);
|
|
||||||
}
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
package net.minestom.server.network.socket;
|
||||||
|
|
||||||
|
import net.minestom.server.network.PacketProcessor;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.UnixDomainSocketAddress;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class ServerAddressTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void inetAddressTest() throws IOException {
|
||||||
|
InetSocketAddress address = new InetSocketAddress("localhost", 25565);
|
||||||
|
var server = new Server(new PacketProcessor());
|
||||||
|
server.init(address);
|
||||||
|
assertSame(address, server.socketAddress());
|
||||||
|
assertEquals(address.getHostString(), server.getAddress());
|
||||||
|
assertEquals(address.getPort(), server.getPort());
|
||||||
|
|
||||||
|
assertDoesNotThrow(server::start);
|
||||||
|
assertDoesNotThrow(server::stop);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void unixAddressTest() throws IOException, InterruptedException {
|
||||||
|
UnixDomainSocketAddress address = UnixDomainSocketAddress.of("minestom.sock");
|
||||||
|
var server = new Server(new PacketProcessor());
|
||||||
|
server.init(address);
|
||||||
|
assertTrue(Files.exists(address.getPath()));
|
||||||
|
assertSame(address, server.socketAddress());
|
||||||
|
assertEquals("unix://" + address.getPath(), server.getAddress());
|
||||||
|
assertEquals(0, server.getPort());
|
||||||
|
|
||||||
|
assertDoesNotThrow(server::start);
|
||||||
|
assertDoesNotThrow(server::stop);
|
||||||
|
|
||||||
|
Thread.sleep(250);
|
||||||
|
assertFalse(Files.exists(address.getPath()), "The socket file should be deleted");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue