Search in sources :

Example 1 with Host

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));
    });
}
Also used : Array(com.badlogic.gdx.utils.Array) Host(io.anuke.mindustry.net.Host) ObjectSet(com.badlogic.gdx.utils.ObjectSet) InetAddress(java.net.InetAddress)

Example 2 with Host

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);
        }
    }
}
Also used : TextButton(io.anuke.ucore.scene.ui.TextButton) Host(io.anuke.mindustry.net.Host)

Example 3 with Host

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));
        }
    });
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) Host(io.anuke.mindustry.net.Host) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Aggregations

Host (io.anuke.mindustry.net.Host)3 Array (com.badlogic.gdx.utils.Array)1 ObjectSet (com.badlogic.gdx.utils.ObjectSet)1 TextButton (io.anuke.ucore.scene.ui.TextButton)1 IOException (java.io.IOException)1 DatagramPacket (java.net.DatagramPacket)1 DatagramSocket (java.net.DatagramSocket)1 InetAddress (java.net.InetAddress)1 ByteBuffer (java.nio.ByteBuffer)1 ClosedSelectorException (java.nio.channels.ClosedSelectorException)1