mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-02 22:47:49 +01:00
Fix & cleanup OpenToLAN
This commit is contained in:
parent
9ec257ed3d
commit
23c76bc799
@ -59,34 +59,30 @@ public class OpenToLAN {
|
||||
*/
|
||||
public static boolean open(@NotNull OpenToLANConfig config) {
|
||||
Objects.requireNonNull(config, "config");
|
||||
if (socket != null) return false;
|
||||
|
||||
if (socket != null) {
|
||||
return false;
|
||||
} else {
|
||||
int port = config.port;
|
||||
|
||||
if (port == 0) {
|
||||
try {
|
||||
port = NetworkUtils.getFreePort();
|
||||
} catch (IOException e) {
|
||||
LOGGER.warn("Could not find an open port!", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int port = config.port;
|
||||
if (port == 0) {
|
||||
try {
|
||||
socket = new DatagramSocket(port);
|
||||
} catch (SocketException e) {
|
||||
LOGGER.warn("Could not bind to the port!", e);
|
||||
port = NetworkUtils.getFreePort();
|
||||
} catch (IOException e) {
|
||||
LOGGER.warn("Could not find an open port!", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
eventCooldown = new Cooldown(config.delayBetweenEvent);
|
||||
task = MinecraftServer.getSchedulerManager().buildTask(OpenToLAN::ping)
|
||||
.repeat(config.delayBetweenPings)
|
||||
.schedule();
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
socket = new DatagramSocket(port);
|
||||
} catch (SocketException e) {
|
||||
LOGGER.warn("Could not bind to the port!", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
eventCooldown = new Cooldown(config.delayBetweenEvent);
|
||||
task = MinecraftServer.getSchedulerManager().buildTask(OpenToLAN::ping)
|
||||
.repeat(config.delayBetweenPings)
|
||||
.schedule();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -95,17 +91,13 @@ public class OpenToLAN {
|
||||
* @return {@code true} if it was closed, {@code false} if it was already closed
|
||||
*/
|
||||
public static boolean close() {
|
||||
if (socket == null) {
|
||||
return false;
|
||||
} else {
|
||||
task.cancel();
|
||||
socket.close();
|
||||
if (socket == null) return false;
|
||||
task.cancel();
|
||||
socket.close();
|
||||
|
||||
task = null;
|
||||
socket = null;
|
||||
|
||||
return true;
|
||||
}
|
||||
task = null;
|
||||
socket = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -121,23 +113,21 @@ public class OpenToLAN {
|
||||
* Performs the ping.
|
||||
*/
|
||||
private static void ping() {
|
||||
if (MinecraftServer.getServer().getPort() != 0) {
|
||||
if (packet == null || eventCooldown.isReady(System.currentTimeMillis())) {
|
||||
final ServerListPingEvent event = new ServerListPingEvent(OPEN_TO_LAN);
|
||||
EventDispatcher.call(event);
|
||||
if (!MinecraftServer.getServer().isOpen()) return;
|
||||
if (packet == null || eventCooldown.isReady(System.currentTimeMillis())) {
|
||||
final ServerListPingEvent event = new ServerListPingEvent(OPEN_TO_LAN);
|
||||
EventDispatcher.call(event);
|
||||
|
||||
final byte[] data = OPEN_TO_LAN.getPingResponse(event.getResponseData()).getBytes(StandardCharsets.UTF_8);
|
||||
packet = new DatagramPacket(data, data.length, PING_ADDRESS);
|
||||
final byte[] data = OPEN_TO_LAN.getPingResponse(event.getResponseData()).getBytes(StandardCharsets.UTF_8);
|
||||
packet = new DatagramPacket(data, data.length, PING_ADDRESS);
|
||||
|
||||
eventCooldown.refreshLastUpdate(System.currentTimeMillis());
|
||||
}
|
||||
eventCooldown.refreshLastUpdate(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
socket.send(packet);
|
||||
} catch (IOException e) {
|
||||
LOGGER.warn("Could not send Open to LAN packet!", e);
|
||||
}
|
||||
try {
|
||||
socket.send(packet);
|
||||
} catch (IOException e) {
|
||||
LOGGER.warn("Could not send Open to LAN packet!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
import java.nio.channels.SelectionKey;
|
||||
import java.nio.channels.Selector;
|
||||
@ -41,6 +42,12 @@ public final class Server {
|
||||
}
|
||||
|
||||
public void start(SocketAddress address) throws IOException {
|
||||
if (address instanceof InetSocketAddress) {
|
||||
InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
|
||||
this.address = inetSocketAddress.getHostString();
|
||||
this.port = inetSocketAddress.getPort();
|
||||
} // TODO unix domain support
|
||||
|
||||
this.serverSocket = ServerSocketChannel.open();
|
||||
serverSocket.bind(address);
|
||||
serverSocket.configureBlocking(false);
|
||||
|
Loading…
Reference in New Issue
Block a user