use of io.anuke.mindustry.net.Host in project Mindustry by Anuken.
the class KryoClient method discover.
@Override
public void discover(Consumer<Array<Host>> callback) {
runAsync(() -> {
addresses.clear();
List<InetAddress> list = client.discoverHosts(port, 3000);
ObjectSet<String> hostnames = new ObjectSet<>();
Array<Host> result = new Array<>();
for (InetAddress a : list) {
if (!hostnames.contains(a.getHostName())) {
Host address = addresses.get(a);
if (address != null)
result.add(address);
}
hostnames.add(a.getHostName());
}
Gdx.app.postRunnable(() -> callback.accept(result));
});
}
use of io.anuke.mindustry.net.Host in project Mindustry by Anuken.
the class JoinDialog method addLocalHosts.
void addLocalHosts(Array<Host> array) {
local.clear();
if (array.size == 0) {
local.add("$text.hosts.none").pad(10f);
local.add().growX();
local.addImageButton("icon-loading", 16 * 2f, this::refreshLocal).pad(-10f).padLeft(0).padTop(-6).size(70f, 74f);
} else {
for (Host a : array) {
TextButton button = local.addButton("[accent]" + a.name, "clear", () -> {
connect(a.address, Vars.port);
}).width(w).height(80f).pad(4f).get();
button.left();
button.row();
button.add("[lightgray]" + (a.players != 1 ? Bundles.format("text.players", a.players) : Bundles.format("text.players.single", a.players)));
button.row();
button.add("[lightgray]" + a.address).pad(4).left();
local.row();
local.background((Drawable) null);
}
}
}
use of io.anuke.mindustry.net.Host in project Mindustry by Anuken.
the class KryoClient method pingHost.
@Override
public void pingHost(String address, int port, Consumer<Host> valid, Consumer<Exception> invalid) {
runAsync(() -> {
try {
DatagramSocket socket = new DatagramSocket();
socket.send(new DatagramPacket(new byte[] { -2, 1 }, 2, InetAddress.getByName(address), port));
socket.setSoTimeout(2000);
addresses.clear();
DatagramPacket packet = handler.onRequestNewDatagramPacket();
socket.receive(packet);
ByteBuffer buffer = ByteBuffer.wrap(packet.getData());
Host host = NetworkIO.readServerData(packet.getAddress().getHostAddress(), buffer);
if (host != null) {
Gdx.app.postRunnable(() -> valid.accept(host));
} else {
Gdx.app.postRunnable(() -> invalid.accept(new IOException("Outdated server.")));
}
} catch (Exception e) {
Gdx.app.postRunnable(() -> invalid.accept(e));
}
});
}
Aggregations