Improved LAN discovery-- fixed #55
This commit is contained in:
@@ -18,7 +18,7 @@ text.public=Public
|
|||||||
text.players={0} players online
|
text.players={0} players online
|
||||||
text.players.single={0} player online
|
text.players.single={0} player online
|
||||||
text.server.mismatch=Packet error: possible client/server version mismatch.\nMake sure you and the host have the\nlatest version of Mindustry!
|
text.server.mismatch=Packet error: possible client/server version mismatch.\nMake sure you and the host have the\nlatest version of Mindustry!
|
||||||
text.server.closing=[accent]Closing server...
|
.server.closing=[accent]Closing server...
|
||||||
text.server.kicked.kick=You have been kicked from the server!
|
text.server.kicked.kick=You have been kicked from the server!
|
||||||
text.server.kicked.invalidPassword=Invalid password!
|
text.server.kicked.invalidPassword=Invalid password!
|
||||||
text.server.connected={0} has joined.
|
text.server.connected={0} has joined.
|
||||||
|
|||||||
@@ -3,9 +3,11 @@ package io.anuke.mindustry.net;
|
|||||||
public class Address {
|
public class Address {
|
||||||
public final String name;
|
public final String name;
|
||||||
public final String address;
|
public final String address;
|
||||||
|
public final int players;
|
||||||
|
|
||||||
public Address(String name, String address){
|
public Address(String name, String address, int players){
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.address = address;
|
this.address = address;
|
||||||
|
this.players = players;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ public class Net{
|
|||||||
private static IntMap<StreamBuilder> streams = new IntMap<>();
|
private static IntMap<StreamBuilder> streams = new IntMap<>();
|
||||||
private static AsyncExecutor executor = new AsyncExecutor(4);
|
private static AsyncExecutor executor = new AsyncExecutor(4);
|
||||||
|
|
||||||
|
/**Sets the client loaded status, or whether it will recieve normal packets from the server.*/
|
||||||
public static void setClientLoaded(boolean loaded){
|
public static void setClientLoaded(boolean loaded){
|
||||||
clientLoaded = loaded;
|
clientLoaded = loaded;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,6 +96,9 @@ public class JoinDialog extends FloatingDialog {
|
|||||||
}).width(w).height(80f).pad(4f).get();
|
}).width(w).height(80f).pad(4f).get();
|
||||||
button.left();
|
button.left();
|
||||||
button.row();
|
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 + " / " + Vars.port).pad(4).left();
|
button.add("[lightgray]" + a.address + " / " + Vars.port).pad(4).left();
|
||||||
|
|
||||||
hosts.row();
|
hosts.row();
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ public class WeaponBlocks{
|
|||||||
shootsound = "bigshot";
|
shootsound = "bigshot";
|
||||||
rotatespeed = 0.2f;
|
rotatespeed = 0.2f;
|
||||||
range = 120;
|
range = 120;
|
||||||
reload = 70f;
|
reload = 55f;
|
||||||
bullet = BulletType.flak;
|
bullet = BulletType.flak;
|
||||||
shots = 3;
|
shots = 3;
|
||||||
inaccuracy = 9f;
|
inaccuracy = 9f;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package io.anuke.kryonet;
|
|||||||
|
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.utils.Array;
|
import com.badlogic.gdx.utils.Array;
|
||||||
|
import com.badlogic.gdx.utils.ObjectMap;
|
||||||
import com.badlogic.gdx.utils.ObjectSet;
|
import com.badlogic.gdx.utils.ObjectSet;
|
||||||
import com.esotericsoftware.kryonet.*;
|
import com.esotericsoftware.kryonet.*;
|
||||||
import io.anuke.mindustry.Vars;
|
import io.anuke.mindustry.Vars;
|
||||||
@@ -17,24 +18,28 @@ import io.anuke.ucore.UCore;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class KryoClient implements ClientProvider{
|
public class KryoClient implements ClientProvider{
|
||||||
Client client;
|
Client client;
|
||||||
|
ObjectMap<InetAddress, Address> addresses = new ObjectMap<>();
|
||||||
|
|
||||||
public KryoClient(){
|
public KryoClient(){
|
||||||
client = new Client();
|
client = new Client();
|
||||||
client.setDiscoveryHandler(new ClientDiscoveryHandler() {
|
client.setDiscoveryHandler(new ClientDiscoveryHandler() {
|
||||||
@Override
|
@Override
|
||||||
public DatagramPacket onRequestNewDatagramPacket() {
|
public DatagramPacket onRequestNewDatagramPacket() {
|
||||||
return new DatagramPacket(new byte[4], 4);
|
return new DatagramPacket(new byte[32], 32);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDiscoveredHost(DatagramPacket datagramPacket) {
|
public void onDiscoveredHost(DatagramPacket datagramPacket) {
|
||||||
//TODO doesn't send data
|
ByteBuffer buffer = ByteBuffer.wrap(datagramPacket.getData());
|
||||||
UCore.log("DATA HOST FOUND: " + Arrays.toString(datagramPacket.getData()));
|
Address address = KryoRegistrator.readServerData(datagramPacket.getAddress(), buffer);
|
||||||
|
addresses.put(datagramPacket.getAddress(), address);
|
||||||
|
UCore.log("Host data found: " + Arrays.toString(datagramPacket.getData()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -139,8 +144,11 @@ public class KryoClient implements ClientProvider{
|
|||||||
Array<Address> result = new Array<>();
|
Array<Address> result = new Array<>();
|
||||||
|
|
||||||
for(InetAddress a : list){
|
for(InetAddress a : list){
|
||||||
if(!hostnames.contains(a.getHostName()))
|
if(!hostnames.contains(a.getHostName())) {
|
||||||
result.add(new Address(a.getCanonicalHostName(), a.getHostAddress()));
|
Address address = addresses.get(a);
|
||||||
|
if(address != null) result.add(address);
|
||||||
|
|
||||||
|
}
|
||||||
hostnames.add(a.getHostName());
|
hostnames.add(a.getHostName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,36 @@
|
|||||||
package io.anuke.kryonet;
|
package io.anuke.kryonet;
|
||||||
|
|
||||||
import com.esotericsoftware.kryo.Kryo;
|
import com.esotericsoftware.kryo.Kryo;
|
||||||
|
import io.anuke.mindustry.Vars;
|
||||||
|
import io.anuke.mindustry.net.Address;
|
||||||
|
import io.anuke.mindustry.net.Net;
|
||||||
|
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
public class KryoRegistrator {
|
public class KryoRegistrator {
|
||||||
|
|
||||||
public static void register(Kryo kryo){
|
public static void register(Kryo kryo){
|
||||||
//TODO register?
|
//TODO register stuff?
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ByteBuffer writeServerData(){
|
||||||
|
ByteBuffer buffer = ByteBuffer.allocate(1 + Vars.player.name.length() + 4);
|
||||||
|
buffer.put((byte)Vars.player.name.length());
|
||||||
|
buffer.put(Vars.player.name.getBytes());
|
||||||
|
buffer.putInt(Net.getConnections().size + 1);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Address readServerData(InetAddress ia, ByteBuffer buffer){
|
||||||
|
//old version address.
|
||||||
|
if(buffer.capacity() == 4) return null;
|
||||||
|
|
||||||
|
byte length = buffer.get();
|
||||||
|
byte[] sname = new byte[length];
|
||||||
|
buffer.get(sname);
|
||||||
|
int players = buffer.getInt();
|
||||||
|
|
||||||
|
return new Address(new String(sname), ia.getHostAddress(), players);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,17 +31,13 @@ public class KryoServer implements ServerProvider {
|
|||||||
public KryoServer(){
|
public KryoServer(){
|
||||||
server = new Server(4096*2, 2048); //TODO tweak
|
server = new Server(4096*2, 2048); //TODO tweak
|
||||||
server.setDiscoveryHandler(new ServerDiscoveryHandler() {
|
server.setDiscoveryHandler(new ServerDiscoveryHandler() {
|
||||||
private ByteBuffer buffer = ByteBuffer.allocate(4);
|
|
||||||
|
|
||||||
{
|
|
||||||
buffer.putInt(987264236);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onDiscoverHost(DatagramChannel datagramChannel, InetSocketAddress fromAddress) throws IOException {
|
public boolean onDiscoverHost(DatagramChannel datagramChannel, InetSocketAddress fromAddress) throws IOException {
|
||||||
//TODO doesn't send data
|
ByteBuffer buffer = KryoRegistrator.writeServerData();
|
||||||
UCore.log("SENDING DATA: " + Arrays.toString(buffer.array()));
|
UCore.log("Replying to discover request with buffer of size " + buffer.capacity());
|
||||||
datagramChannel.send(this.buffer, fromAddress);
|
buffer.position(0);
|
||||||
|
datagramChannel.send(buffer, fromAddress);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -123,7 +119,13 @@ public class KryoServer implements ServerProvider {
|
|||||||
public void host(int port) throws IOException {
|
public void host(int port) throws IOException {
|
||||||
server.bind(port, port);
|
server.bind(port, port);
|
||||||
|
|
||||||
Thread thread = new Thread(server, "Kryonet Server");
|
Thread thread = new Thread(() -> {
|
||||||
|
try{
|
||||||
|
server.run();
|
||||||
|
}catch (Exception e){
|
||||||
|
handleException(e);
|
||||||
|
}
|
||||||
|
}, "Kryonet Server");
|
||||||
thread.setDaemon(true);
|
thread.setDaemon(true);
|
||||||
thread.start();
|
thread.start();
|
||||||
}
|
}
|
||||||
@@ -209,6 +211,10 @@ public class KryoServer implements ServerProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void handleException(Exception e){
|
||||||
|
Gdx.app.postRunnable(() -> { throw new RuntimeException(e);});
|
||||||
|
}
|
||||||
|
|
||||||
Connection getByID(int id){
|
Connection getByID(int id){
|
||||||
for(Connection con : server.getConnections()){
|
for(Connection con : server.getConnections()){
|
||||||
if(con.getID() == id){
|
if(con.getID() == id){
|
||||||
|
|||||||
Reference in New Issue
Block a user