| 1 | package net.minecraft.client.network;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.io.IOException;
|
| 5 | import java.net.DatagramPacket;
|
| 6 | import java.net.InetAddress;
|
| 7 | import java.net.MulticastSocket;
|
| 8 | import java.net.SocketTimeoutException;
|
| 9 | import java.util.Collections;
|
| 10 | import java.util.List;
|
| 11 | import java.util.concurrent.atomic.AtomicInteger;
|
| 12 | import net.minecraft.client.Minecraft;
|
| 13 | import net.minecraft.client.multiplayer.ThreadLanServerPing;
|
| 14 | import org.apache.logging.log4j.LogManager;
|
| 15 | import org.apache.logging.log4j.Logger;
|
| 16 |
|
| 17 | public class LanServerDetector
|
| 18 | {
|
| 19 | private static final AtomicInteger field_148551_a = new AtomicInteger(0);
|
| 20 | private static final Logger logger = LogManager.getLogger();
|
| 21 |
|
| 22 | public static class LanServer
|
| 23 | {
|
| 24 | private String lanServerMotd;
|
| 25 | private String lanServerIpPort;
|
| 26 | private long timeLastSeen;
|
| 27 |
|
| 28 | public LanServer(String motd, String address)
|
| 29 | {
|
| 30 | this.lanServerMotd = motd;
|
| 31 | this.lanServerIpPort = address;
|
| 32 | this.timeLastSeen = Minecraft.getSystemTime();
|
| 33 | }
|
| 34 |
|
| 35 | public String getServerMotd()
|
| 36 | {
|
| 37 | return this.lanServerMotd;
|
| 38 | }
|
| 39 |
|
| 40 | public String getServerIpPort()
|
| 41 | {
|
| 42 | return this.lanServerIpPort;
|
| 43 | }
|
| 44 |
|
| 45 | public void updateLastSeen()
|
| 46 | {
|
| 47 | this.timeLastSeen = Minecraft.getSystemTime();
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | public static class LanServerList
|
| 52 | {
|
| 53 | private List<LanServerDetector.LanServer> listOfLanServers = Lists.<LanServerDetector.LanServer>newArrayList();
|
| 54 | boolean wasUpdated;
|
| 55 |
|
| 56 | public synchronized boolean getWasUpdated()
|
| 57 | {
|
| 58 | return this.wasUpdated;
|
| 59 | }
|
| 60 |
|
| 61 | public synchronized void setWasNotUpdated()
|
| 62 | {
|
| 63 | this.wasUpdated = false;
|
| 64 | }
|
| 65 |
|
| 66 | public synchronized List<LanServerDetector.LanServer> getLanServers()
|
| 67 | {
|
| 68 | return Collections.<LanServerDetector.LanServer>unmodifiableList(this.listOfLanServers);
|
| 69 | }
|
| 70 |
|
| 71 | public synchronized void func_77551_a(String p_77551_1_, InetAddress p_77551_2_)
|
| 72 | {
|
| 73 | String s = ThreadLanServerPing.getMotdFromPingResponse(p_77551_1_);
|
| 74 | String s1 = ThreadLanServerPing.getAdFromPingResponse(p_77551_1_);
|
| 75 |
|
| 76 | if (s1 != null)
|
| 77 | {
|
| 78 | s1 = p_77551_2_.getHostAddress() + ":" + s1;
|
| 79 | boolean flag = false;
|
| 80 |
|
| 81 | for (LanServerDetector.LanServer lanserverdetector$lanserver : this.listOfLanServers)
|
| 82 | {
|
| 83 | if (lanserverdetector$lanserver.getServerIpPort().equals(s1))
|
| 84 | {
|
| 85 | lanserverdetector$lanserver.updateLastSeen();
|
| 86 | flag = true;
|
| 87 | break;
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | if (!flag)
|
| 92 | {
|
| 93 | this.listOfLanServers.add(new LanServerDetector.LanServer(s, s1));
|
| 94 | this.wasUpdated = true;
|
| 95 | }
|
| 96 | }
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | public static class ThreadLanServerFind extends Thread
|
| 101 | {
|
| 102 | private final LanServerDetector.LanServerList localServerList;
|
| 103 | private final InetAddress broadcastAddress;
|
| 104 | private final MulticastSocket socket;
|
| 105 |
|
| 106 | public ThreadLanServerFind(LanServerDetector.LanServerList p_i1320_1_) throws IOException
|
| 107 | {
|
| 108 | super("LanServerDetector #" + LanServerDetector.field_148551_a.incrementAndGet());
|
| 109 | this.localServerList = p_i1320_1_;
|
| 110 | this.setDaemon(true);
|
| 111 | this.socket = new MulticastSocket(4445);
|
| 112 | this.broadcastAddress = InetAddress.getByName("224.0.2.60");
|
| 113 | this.socket.setSoTimeout(5000);
|
| 114 | this.socket.joinGroup(this.broadcastAddress);
|
| 115 | }
|
| 116 |
|
| 117 | public void run()
|
| 118 | {
|
| 119 | byte[] abyte = new byte[1024];
|
| 120 |
|
| 121 | while (!this.isInterrupted())
|
| 122 | {
|
| 123 | DatagramPacket datagrampacket = new DatagramPacket(abyte, abyte.length);
|
| 124 |
|
| 125 | try
|
| 126 | {
|
| 127 | this.socket.receive(datagrampacket);
|
| 128 | }
|
| 129 | catch (SocketTimeoutException var5)
|
| 130 | {
|
| 131 | continue;
|
| 132 | }
|
| 133 | catch (IOException ioexception)
|
| 134 | {
|
| 135 | LanServerDetector.logger.error((String)"Couldn\'t ping server", (Throwable)ioexception);
|
| 136 | break;
|
| 137 | }
|
| 138 |
|
| 139 | String s = new String(datagrampacket.getData(), datagrampacket.getOffset(), datagrampacket.getLength());
|
| 140 | LanServerDetector.logger.debug(datagrampacket.getAddress() + ": " + s);
|
| 141 | this.localServerList.func_77551_a(s, datagrampacket.getAddress());
|
| 142 | }
|
| 143 |
|
| 144 | try
|
| 145 | {
|
| 146 | this.socket.leaveGroup(this.broadcastAddress);
|
| 147 | }
|
| 148 | catch (IOException var4)
|
| 149 | {
|
| 150 | ;
|
| 151 | }
|
| 152 |
|
| 153 | this.socket.close();
|
| 154 | }
|
| 155 | }
|
| 156 | }
|
| 157 |
|