| 1 | package net.minecraft.client.server;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import java.io.IOException;
|
| 6 | import java.net.DatagramPacket;
|
| 7 | import java.net.InetAddress;
|
| 8 | import java.net.MulticastSocket;
|
| 9 | import java.net.SocketTimeoutException;
|
| 10 | import java.nio.charset.StandardCharsets;
|
| 11 | import java.util.List;
|
| 12 | import java.util.concurrent.atomic.AtomicInteger;
|
| 13 | import net.fabricmc.api.EnvType;
|
| 14 | import net.fabricmc.api.Environment;
|
| 15 | import net.minecraft.DefaultUncaughtExceptionHandler;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 | import org.slf4j.Logger;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class LanServerDetection {
|
| 21 | private static final AtomicInteger UNIQUE_THREAD_ID = new AtomicInteger(0);
|
| 22 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | public static class LanServerDetector extends Thread {
|
| 26 | private final LanServerDetection.LanServerList serverList;
|
| 27 | private final InetAddress pingGroup;
|
| 28 | private final MulticastSocket socket;
|
| 29 |
|
| 30 | public LanServerDetector(final LanServerDetection.LanServerList serverList) throws IOException {
|
| 31 | super("LanServerDetector #" + LanServerDetection.UNIQUE_THREAD_ID.incrementAndGet());
|
| 32 | this.serverList = serverList;
|
| 33 | this.setDaemon(true);
|
| 34 | this.setUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler(LanServerDetection.LOGGER));
|
| 35 | this.socket = new MulticastSocket(4445);
|
| 36 | this.pingGroup = InetAddress.getByName("224.0.2.60");
|
| 37 | this.socket.setSoTimeout(5000);
|
| 38 | this.socket.joinGroup(this.pingGroup);
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public void run() {
|
| 43 | byte[] buf = new byte[1024];
|
| 44 |
|
| 45 | while (!this.isInterrupted()) {
|
| 46 | DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
| 47 |
|
| 48 | try {
|
| 49 | this.socket.receive(packet);
|
| 50 | } catch (SocketTimeoutException ignored) {
|
| 51 | continue;
|
| 52 | } catch (IOException e) {
|
| 53 | LanServerDetection.LOGGER.error("Couldn't ping server", e);
|
| 54 | break;
|
| 55 | }
|
| 56 |
|
| 57 | String received = new String(packet.getData(), packet.getOffset(), packet.getLength(), StandardCharsets.UTF_8);
|
| 58 | LanServerDetection.LOGGER.debug("{}: {}", packet.getAddress(), received);
|
| 59 | this.serverList.addServer(received, packet.getAddress());
|
| 60 | }
|
| 61 |
|
| 62 | try {
|
| 63 | this.socket.leaveGroup(this.pingGroup);
|
| 64 | } catch (IOException var4) {
|
| 65 | }
|
| 66 |
|
| 67 | this.socket.close();
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | @Environment(EnvType.CLIENT)
|
| 72 | public static class LanServerList {
|
| 73 | private final List<LanServer> servers = Lists.newArrayList();
|
| 74 | private boolean isDirty;
|
| 75 |
|
| 76 | public synchronized @Nullable List<LanServer> takeDirtyServers() {
|
| 77 | if (this.isDirty) {
|
| 78 | List<LanServer> newServers = List.copyOf(this.servers);
|
| 79 | this.isDirty = false;
|
| 80 | return newServers;
|
| 81 | } else {
|
| 82 | return null;
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | public synchronized void addServer(final String pingData, final InetAddress socketAddress) {
|
| 87 | String motd = LanServerPinger.parseMotd(pingData);
|
| 88 | String address = LanServerPinger.parseAddress(pingData);
|
| 89 | if (address != null) {
|
| 90 | address = socketAddress.getHostAddress() + ":" + address;
|
| 91 | boolean found = false;
|
| 92 |
|
| 93 | for (LanServer server : this.servers) {
|
| 94 | if (server.getAddress().equals(address)) {
|
| 95 | server.updatePingTime();
|
| 96 | found = true;
|
| 97 | break;
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | if (!found) {
|
| 102 | this.servers.add(new LanServer(motd, address));
|
| 103 | this.isDirty = true;
|
| 104 | }
|
| 105 | }
|
| 106 | }
|
| 107 | }
|
| 108 | }
|
| 109 |
|