mirror of
https://github.com/Anuken/Mindustry.git
synced 2025-02-22 20:48:33 +07:00
Fixed local servers sometimes not showing up
This commit is contained in:
parent
4295f08a84
commit
8ac079fb72
@ -18,6 +18,7 @@ import io.anuke.ucore.scene.ui.layout.Cell;
|
|||||||
import io.anuke.ucore.scene.ui.layout.Table;
|
import io.anuke.ucore.scene.ui.layout.Table;
|
||||||
import io.anuke.ucore.scene.utils.UIUtils;
|
import io.anuke.ucore.scene.utils.UIUtils;
|
||||||
import io.anuke.ucore.util.Bundles;
|
import io.anuke.ucore.util.Bundles;
|
||||||
|
import io.anuke.ucore.util.Log;
|
||||||
import io.anuke.ucore.util.Strings;
|
import io.anuke.ucore.util.Strings;
|
||||||
|
|
||||||
import static io.anuke.mindustry.Vars.*;
|
import static io.anuke.mindustry.Vars.*;
|
||||||
@ -254,6 +255,7 @@ public class JoinDialog extends FloatingDialog{
|
|||||||
local.table("button", t -> {
|
local.table("button", t -> {
|
||||||
t.label(() -> "[accent]" + Bundles.get("text.hosts.discovering") + Strings.animated(4, 10f, ".")).pad(10f);
|
t.label(() -> "[accent]" + Bundles.get("text.hosts.discovering") + Strings.animated(4, 10f, ".")).pad(10f);
|
||||||
}).growX();
|
}).growX();
|
||||||
|
Log.info("begin of discover");
|
||||||
Net.discoverServers(this::addLocalHost, this::finishLocalHosts);
|
Net.discoverServers(this::addLocalHost, this::finishLocalHosts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,11 +26,11 @@ import java.nio.channels.ClosedSelectorException;
|
|||||||
import static io.anuke.mindustry.Vars.*;
|
import static io.anuke.mindustry.Vars.*;
|
||||||
|
|
||||||
public class KryoClient implements ClientProvider{
|
public class KryoClient implements ClientProvider{
|
||||||
Client client;
|
final Client client;
|
||||||
|
final Array<InetAddress> foundAddresses = new Array<>();
|
||||||
|
final ClientDiscoveryHandler handler;
|
||||||
|
final LZ4FastDecompressor decompressor = LZ4Factory.fastestInstance().fastDecompressor();
|
||||||
Consumer<Host> lastCallback;
|
Consumer<Host> lastCallback;
|
||||||
Array<InetAddress> foundAddresses = new Array<>();
|
|
||||||
ClientDiscoveryHandler handler;
|
|
||||||
LZ4FastDecompressor decompressor = LZ4Factory.fastestInstance().fastDecompressor();
|
|
||||||
|
|
||||||
public KryoClient(){
|
public KryoClient(){
|
||||||
KryoCore.init();
|
KryoCore.init();
|
||||||
@ -178,24 +178,26 @@ public class KryoClient implements ClientProvider{
|
|||||||
@Override
|
@Override
|
||||||
public void pingHost(String address, int port, Consumer<Host> valid, Consumer<Exception> invalid){
|
public void pingHost(String address, int port, Consumer<Host> valid, Consumer<Exception> invalid){
|
||||||
runAsync(() -> {
|
runAsync(() -> {
|
||||||
try {
|
synchronized(handler){
|
||||||
DatagramSocket socket = new DatagramSocket();
|
try{
|
||||||
socket.send(new DatagramPacket(new byte[]{-2, 1}, 2, InetAddress.getByName(address), port));
|
DatagramSocket socket = new DatagramSocket();
|
||||||
|
socket.send(new DatagramPacket(new byte[]{-2, 1}, 2, InetAddress.getByName(address), port));
|
||||||
|
|
||||||
socket.setSoTimeout(2000);
|
socket.setSoTimeout(2000);
|
||||||
|
|
||||||
lastCallback = valid;
|
lastCallback = valid;
|
||||||
|
|
||||||
DatagramPacket packet = handler.onRequestNewDatagramPacket();
|
DatagramPacket packet = handler.onRequestNewDatagramPacket();
|
||||||
|
|
||||||
socket.receive(packet);
|
socket.receive(packet);
|
||||||
|
|
||||||
ByteBuffer buffer = ByteBuffer.wrap(packet.getData());
|
ByteBuffer buffer = ByteBuffer.wrap(packet.getData());
|
||||||
Host host = NetworkIO.readServerData(packet.getAddress().getHostAddress(), buffer);
|
Host host = NetworkIO.readServerData(packet.getAddress().getHostAddress(), buffer);
|
||||||
|
|
||||||
Gdx.app.postRunnable(() -> valid.accept(host));
|
Gdx.app.postRunnable(() -> valid.accept(host));
|
||||||
} catch (Exception e) {
|
}catch(Exception e){
|
||||||
Gdx.app.postRunnable(() -> invalid.accept(e));
|
Gdx.app.postRunnable(() -> invalid.accept(e));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -203,16 +205,18 @@ public class KryoClient implements ClientProvider{
|
|||||||
@Override
|
@Override
|
||||||
public void discover(Consumer<Host> callback, Runnable done){
|
public void discover(Consumer<Host> callback, Runnable done){
|
||||||
runAsync(() -> {
|
runAsync(() -> {
|
||||||
foundAddresses.clear();
|
synchronized(handler){
|
||||||
lastCallback = callback;
|
foundAddresses.clear();
|
||||||
client.discoverHosts(port, 3000);
|
lastCallback = callback;
|
||||||
Gdx.app.postRunnable(done);
|
client.discoverHosts(port, 3000);
|
||||||
|
Gdx.app.postRunnable(done);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void dispose(){
|
public void dispose(){
|
||||||
try {
|
try{
|
||||||
client.dispose();
|
client.dispose();
|
||||||
}catch (IOException e){
|
}catch (IOException e){
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
Loading…
Reference in New Issue
Block a user