Fix & cleanup OpenToLAN

This commit is contained in:
TheMode 2021-08-21 10:12:55 +02:00
parent 9ec257ed3d
commit 23c76bc799
2 changed files with 44 additions and 47 deletions

View File

@ -59,34 +59,30 @@ public class OpenToLAN {
*/ */
public static boolean open(@NotNull OpenToLANConfig config) { public static boolean open(@NotNull OpenToLANConfig config) {
Objects.requireNonNull(config, "config"); Objects.requireNonNull(config, "config");
if (socket != null) return false;
if (socket != null) { int port = config.port;
return false; if (port == 0) {
} 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;
}
}
try { try {
socket = new DatagramSocket(port); port = NetworkUtils.getFreePort();
} catch (SocketException e) { } catch (IOException e) {
LOGGER.warn("Could not bind to the port!", e); LOGGER.warn("Could not find an open port!", e);
return false; 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 * @return {@code true} if it was closed, {@code false} if it was already closed
*/ */
public static boolean close() { public static boolean close() {
if (socket == null) { if (socket == null) return false;
return false; task.cancel();
} else { socket.close();
task.cancel();
socket.close();
task = null; task = null;
socket = null; socket = null;
return true;
return true;
}
} }
/** /**
@ -121,23 +113,21 @@ public class OpenToLAN {
* Performs the ping. * Performs the ping.
*/ */
private static void ping() { private static void ping() {
if (MinecraftServer.getServer().getPort() != 0) { if (!MinecraftServer.getServer().isOpen()) return;
if (packet == null || eventCooldown.isReady(System.currentTimeMillis())) { if (packet == null || eventCooldown.isReady(System.currentTimeMillis())) {
final ServerListPingEvent event = new ServerListPingEvent(OPEN_TO_LAN); final ServerListPingEvent event = new ServerListPingEvent(OPEN_TO_LAN);
EventDispatcher.call(event); EventDispatcher.call(event);
final byte[] data = OPEN_TO_LAN.getPingResponse(event.getResponseData()).getBytes(StandardCharsets.UTF_8); final byte[] data = OPEN_TO_LAN.getPingResponse(event.getResponseData()).getBytes(StandardCharsets.UTF_8);
packet = new DatagramPacket(data, data.length, PING_ADDRESS); packet = new DatagramPacket(data, data.length, PING_ADDRESS);
eventCooldown.refreshLastUpdate(System.currentTimeMillis()); eventCooldown.refreshLastUpdate(System.currentTimeMillis());
} }
try {
try { socket.send(packet);
socket.send(packet); } catch (IOException e) {
} catch (IOException e) { LOGGER.warn("Could not send Open to LAN packet!", e);
LOGGER.warn("Could not send Open to LAN packet!", e);
}
} }
} }
} }

View File

@ -5,6 +5,7 @@ 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.SocketAddress; import java.net.SocketAddress;
import java.nio.channels.SelectionKey; import java.nio.channels.SelectionKey;
import java.nio.channels.Selector; import java.nio.channels.Selector;
@ -41,6 +42,12 @@ public final class Server {
} }
public void start(SocketAddress address) throws IOException { 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(); this.serverSocket = ServerSocketChannel.open();
serverSocket.bind(address); serverSocket.bind(address);
serverSocket.configureBlocking(false); serverSocket.configureBlocking(false);