mirror of
https://github.com/Anuken/Mindustry.git
synced 2025-03-12 19:09:34 +07:00
Fixed server discovery
This commit is contained in:
parent
b1a38c3e61
commit
49aef2bf99
@ -41,10 +41,9 @@ public class NetServer extends Module{
|
||||
|
||||
public NetServer(){
|
||||
|
||||
Net.handleServer(Connect.class, connect -> UCore.log("Connection found: " + connect.addressTCP));
|
||||
Net.handleServer(Connect.class, (id, connect) -> UCore.log("Connection found: " + connect.addressTCP));
|
||||
|
||||
Net.handleServer(ConnectPacket.class, packet -> {
|
||||
int id = Net.getLastConnection();
|
||||
Net.handleServer(ConnectPacket.class, (id, packet) -> {
|
||||
|
||||
UCore.log("Sending world data to client (ID=" + id + ")");
|
||||
|
||||
@ -69,8 +68,8 @@ public class NetServer extends Module{
|
||||
|
||||
});
|
||||
|
||||
Net.handleServer(ConnectConfirmPacket.class, packet -> {
|
||||
Player player = connections.get(Net.getLastConnection());
|
||||
Net.handleServer(ConnectConfirmPacket.class, (id, packet) -> {
|
||||
Player player = connections.get(id);
|
||||
|
||||
if (player == null) return;
|
||||
|
||||
@ -78,7 +77,7 @@ public class NetServer extends Module{
|
||||
sendMessage("[accent]" + Bundles.format("text.server.connected", player.name));
|
||||
});
|
||||
|
||||
Net.handleServer(Disconnect.class, packet -> {
|
||||
Net.handleServer(Disconnect.class, (id, packet) -> {
|
||||
Player player = connections.get(packet.id);
|
||||
|
||||
if (player == null) {
|
||||
@ -95,24 +94,24 @@ public class NetServer extends Module{
|
||||
Net.send(dc, SendMode.tcp);
|
||||
});
|
||||
|
||||
Net.handleServer(PositionPacket.class, pos -> {
|
||||
Player player = connections.get(Net.getLastConnection());
|
||||
player.read(ByteBuffer.wrap(pos.data));
|
||||
Net.handleServer(PositionPacket.class, (id, packet) -> {
|
||||
Player player = connections.get(id);
|
||||
player.read(ByteBuffer.wrap(packet.data));
|
||||
});
|
||||
|
||||
Net.handleServer(ShootPacket.class, packet -> {
|
||||
Player player = connections.get(Net.getLastConnection());
|
||||
Net.handleServer(ShootPacket.class, (id, packet) -> {
|
||||
Player player = connections.get(id);
|
||||
|
||||
Weapon weapon = (Weapon) Upgrade.getByID(packet.weaponid);
|
||||
weapon.shoot(player, packet.x, packet.y, packet.rotation);
|
||||
packet.playerid = player.id;
|
||||
|
||||
Net.sendExcept(Net.getLastConnection(), packet, SendMode.udp);
|
||||
Net.sendExcept(id, packet, SendMode.udp);
|
||||
});
|
||||
|
||||
Net.handleServer(PlacePacket.class, packet -> {
|
||||
Net.handleServer(PlacePacket.class, (id, packet) -> {
|
||||
Vars.control.input.placeBlockInternal(packet.x, packet.y, Block.getByID(packet.block), packet.rotation, true, false);
|
||||
packet.playerid = connections.get(Net.getLastConnection()).id;
|
||||
packet.playerid = connections.get(id).id;
|
||||
|
||||
Recipe recipe = Recipes.getByResult(Block.getByID(packet.block));
|
||||
if (recipe != null) {
|
||||
@ -121,21 +120,21 @@ public class NetServer extends Module{
|
||||
}
|
||||
}
|
||||
|
||||
Net.sendExcept(Net.getLastConnection(), packet, SendMode.tcp);
|
||||
Net.sendExcept(id, packet, SendMode.tcp);
|
||||
});
|
||||
|
||||
Net.handleServer(BreakPacket.class, packet -> {
|
||||
Net.handleServer(BreakPacket.class, (id, packet) -> {
|
||||
Vars.control.input.breakBlockInternal(packet.x, packet.y, false);
|
||||
packet.playerid = connections.get(Net.getLastConnection()).id;
|
||||
packet.playerid = connections.get(id).id;
|
||||
|
||||
Net.sendExcept(Net.getLastConnection(), packet, SendMode.tcp);
|
||||
Net.sendExcept(id, packet, SendMode.tcp);
|
||||
});
|
||||
|
||||
Net.handleServer(ChatPacket.class, packet -> {
|
||||
Player player = connections.get(Net.getLastConnection());
|
||||
Net.handleServer(ChatPacket.class, (id, packet) -> {
|
||||
Player player = connections.get(id);
|
||||
|
||||
if (player == null) {
|
||||
Gdx.app.error("Mindustry", "Could not find player for chat: " + Net.getLastConnection());
|
||||
Gdx.app.error("Mindustry", "Could not find player for chat: " + id);
|
||||
return; //GHOSTS AAAA
|
||||
}
|
||||
|
||||
@ -145,8 +144,8 @@ public class NetServer extends Module{
|
||||
Vars.ui.chatfrag.addMessage(packet.text, Vars.netClient.colorizeName(packet.id, packet.name));
|
||||
});
|
||||
|
||||
Net.handleServer(UpgradePacket.class, packet -> {
|
||||
Player player = connections.get(Net.getLastConnection());
|
||||
Net.handleServer(UpgradePacket.class, (id, packet) -> {
|
||||
Player player = connections.get(id);
|
||||
|
||||
Weapon weapon = (Weapon) Upgrade.getByID(packet.id);
|
||||
|
||||
@ -156,8 +155,8 @@ public class NetServer extends Module{
|
||||
Vars.control.removeItems(UpgradeRecipes.get(weapon));
|
||||
});
|
||||
|
||||
Net.handleServer(WeaponSwitchPacket.class, packet -> {
|
||||
Player player = connections.get(Net.getLastConnection());
|
||||
Net.handleServer(WeaponSwitchPacket.class, (id, packet) -> {
|
||||
Player player = connections.get(id);
|
||||
|
||||
if (player == null) return;
|
||||
|
||||
@ -169,23 +168,23 @@ public class NetServer extends Module{
|
||||
Net.sendExcept(player.clientid, packet, SendMode.tcp);
|
||||
});
|
||||
|
||||
Net.handleServer(BlockTapPacket.class, packet -> {
|
||||
Net.handleServer(BlockTapPacket.class, (id, packet) -> {
|
||||
Tile tile = Vars.world.tile(packet.position);
|
||||
tile.block().tapped(tile);
|
||||
|
||||
Net.sendExcept(Net.getLastConnection(), packet, SendMode.tcp);
|
||||
Net.sendExcept(id, packet, SendMode.tcp);
|
||||
});
|
||||
|
||||
Net.handleServer(BlockConfigPacket.class, packet -> {
|
||||
Net.handleServer(BlockConfigPacket.class, (id, packet) -> {
|
||||
Tile tile = Vars.world.tile(packet.position);
|
||||
if (tile != null) tile.block().configure(tile, packet.data);
|
||||
|
||||
Net.sendExcept(Net.getLastConnection(), packet, SendMode.tcp);
|
||||
Net.sendExcept(id, packet, SendMode.tcp);
|
||||
});
|
||||
|
||||
Net.handleServer(EntityRequestPacket.class, packet -> {
|
||||
Net.handleServer(EntityRequestPacket.class, (cid, packet) -> {
|
||||
int id = packet.id;
|
||||
int dest = Net.getLastConnection();
|
||||
int dest = id;
|
||||
if (Vars.control.playerGroup.getByID(id) != null) {
|
||||
Player player = Vars.control.playerGroup.getByID(id);
|
||||
PlayerSpawnPacket p = new PlayerSpawnPacket();
|
||||
@ -197,7 +196,7 @@ public class NetServer extends Module{
|
||||
p.weaponright = player.weaponRight.id;
|
||||
p.android = player.isAndroid;
|
||||
Net.sendTo(dest, p, SendMode.tcp);
|
||||
Gdx.app.error("Mindustry", "Replying to entity request (" + Net.getLastConnection() + "): player, " + id);
|
||||
Gdx.app.error("Mindustry", "Replying to entity request (" + id + "): player, " + id);
|
||||
} else if (Vars.control.enemyGroup.getByID(id) != null) {
|
||||
Enemy enemy = Vars.control.enemyGroup.getByID(id);
|
||||
EnemySpawnPacket e = new EnemySpawnPacket();
|
||||
@ -209,7 +208,7 @@ public class NetServer extends Module{
|
||||
e.type = enemy.type.id;
|
||||
e.health = (short) enemy.health;
|
||||
Net.sendTo(dest, e, SendMode.tcp);
|
||||
Gdx.app.error("Mindustry", "Replying to entity request(" + Net.getLastConnection() + "): enemy, " + id);
|
||||
Gdx.app.error("Mindustry", "Replying to entity request(" + id + "): enemy, " + id);
|
||||
} else {
|
||||
Gdx.app.error("Mindustry", "Entity request target not found!");
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ import io.anuke.mindustry.net.Streamable.StreamBuilder;
|
||||
import io.anuke.mindustry.net.Streamable.StreamChunk;
|
||||
import io.anuke.ucore.UCore;
|
||||
import io.anuke.ucore.core.Timers;
|
||||
import io.anuke.ucore.function.BiConsumer;
|
||||
import io.anuke.ucore.function.Consumer;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -22,11 +23,10 @@ public class Net{
|
||||
private static boolean active;
|
||||
private static boolean clientLoaded;
|
||||
private static ObjectMap<Class<?>, Consumer> clientListeners = new ObjectMap<>();
|
||||
private static ObjectMap<Class<?>, Consumer> serverListeners = new ObjectMap<>();
|
||||
private static ObjectMap<Class<?>, BiConsumer<Integer, Object>> serverListeners = new ObjectMap<>();
|
||||
private static ClientProvider clientProvider;
|
||||
private static ServerProvider serverProvider;
|
||||
|
||||
private static int lastConnection = -1;
|
||||
private static IntMap<StreamBuilder> streams = new IntMap<>();
|
||||
private static AsyncExecutor executor = new AsyncExecutor(4);
|
||||
|
||||
@ -128,8 +128,8 @@ public class Net{
|
||||
}
|
||||
|
||||
/**Registers a server listener for when an object is recieved.*/
|
||||
public static <T> void handleServer(Class<T> type, Consumer<T> listener){
|
||||
serverListeners.put(type, listener);
|
||||
public static <T> void handleServer(Class<T> type, BiConsumer<Integer, T> listener){
|
||||
serverListeners.put(type, (BiConsumer<Integer, Object>) listener);
|
||||
}
|
||||
|
||||
/**Call to handle a packet being recieved for the client.*/
|
||||
@ -160,10 +160,9 @@ public class Net{
|
||||
}
|
||||
|
||||
/**Call to handle a packet being recieved for the server.*/
|
||||
public static void handleServerReceived(Object object, int connection){
|
||||
public static void handleServerReceived(int connection, Object object){
|
||||
if(serverListeners.get(object.getClass()) != null){
|
||||
lastConnection = connection;
|
||||
serverListeners.get(object.getClass()).accept(object);
|
||||
serverListeners.get(object.getClass()).accept(connection, object);
|
||||
}else{
|
||||
Gdx.app.error("Mindustry::Net", "Unhandled packet type: '" + object.getClass() + "'!");
|
||||
}
|
||||
@ -183,11 +182,6 @@ public class Net{
|
||||
public static int getPing(){
|
||||
return server() ? 0 : clientProvider.getPing();
|
||||
}
|
||||
|
||||
/**Returns the last connection that sent a packet to this server.*/
|
||||
public static int getLastConnection(){
|
||||
return lastConnection;
|
||||
}
|
||||
|
||||
/**Whether the net is active, e.g. whether this is a multiplayer game.*/
|
||||
public static boolean active(){
|
||||
|
@ -28,7 +28,7 @@ public class WebsocketClient implements ClientProvider {
|
||||
|
||||
@Override
|
||||
public void connect(String ip, int port) throws IOException {
|
||||
socket = new Websocket("ws://" + ip + ":" + Vars.webPort);
|
||||
socket = new Websocket("wss://" + ip + ":" + Vars.webPort);
|
||||
socket.addListener(new WebsocketListener() {
|
||||
public void onMessage(byte[] bytes) {
|
||||
try {
|
||||
@ -105,7 +105,7 @@ public class WebsocketClient implements ClientProvider {
|
||||
@Override
|
||||
public void pingHost(String address, int port, Consumer<Host> valid, Consumer<IOException> failed) {
|
||||
failed.accept(new IOException());
|
||||
Websocket socket = new Websocket("ws://" + address + ":" + Vars.webPort);
|
||||
Websocket socket = new Websocket("wss://" + address + ":" + Vars.webPort);
|
||||
final boolean[] accepted = {false};
|
||||
socket.addListener(new WebsocketListener() {
|
||||
@Override
|
||||
|
@ -5,9 +5,7 @@ import com.badlogic.gdx.utils.Array;
|
||||
import com.badlogic.gdx.utils.ObjectMap;
|
||||
import com.badlogic.gdx.utils.ObjectSet;
|
||||
import com.esotericsoftware.kryonet.*;
|
||||
import com.esotericsoftware.kryonet.FrameworkMessage.DiscoverHost;
|
||||
import com.esotericsoftware.kryonet.Listener.LagListener;
|
||||
import com.esotericsoftware.kryonet.serialization.Serialization;
|
||||
import io.anuke.mindustry.Vars;
|
||||
import io.anuke.mindustry.net.Host;
|
||||
import io.anuke.mindustry.net.Net;
|
||||
@ -147,16 +145,8 @@ public class KryoClient implements ClientProvider{
|
||||
public void pingHost(String address, int port, Consumer<Host> valid, Consumer<IOException> invalid){
|
||||
Thread thread = new Thread(() -> {
|
||||
try {
|
||||
|
||||
Serialization ser = (Serialization) UCore.getPrivate(client, "serialization");
|
||||
DatagramSocket socket = new DatagramSocket();
|
||||
ByteBuffer dataBuffer = ByteBuffer.allocate(64);
|
||||
ser.write(dataBuffer, new DiscoverHost());
|
||||
|
||||
dataBuffer.flip();
|
||||
byte[] data = new byte[dataBuffer.limit()];
|
||||
dataBuffer.get(data);
|
||||
socket.send(new DatagramPacket(data, data.length, InetAddress.getByName(address), port));
|
||||
socket.send(new DatagramPacket(new byte[]{-2, 1}, 2, InetAddress.getByName(address), Vars.port));
|
||||
|
||||
socket.setSoTimeout(2000);
|
||||
|
||||
@ -166,9 +156,8 @@ public class KryoClient implements ClientProvider{
|
||||
|
||||
socket.receive(packet);
|
||||
|
||||
handler.onDiscoveredHost(packet);
|
||||
|
||||
Host host = addresses.values().next();
|
||||
ByteBuffer buffer = ByteBuffer.wrap(packet.getData());
|
||||
Host host = KryoRegistrator.readServerData(packet.getAddress(), buffer);
|
||||
|
||||
if (host != null) {
|
||||
Gdx.app.postRunnable(() -> valid.accept(host));
|
||||
|
@ -67,7 +67,7 @@ public class KryoServer implements ServerProvider {
|
||||
c.addressTCP = connection.getRemoteAddressTCP().toString();
|
||||
|
||||
connections.add(kn);
|
||||
Gdx.app.postRunnable(() -> Net.handleServerReceived(c, kn.id));
|
||||
Gdx.app.postRunnable(() -> Net.handleServerReceived(kn.id, c));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -79,7 +79,7 @@ public class KryoServer implements ServerProvider {
|
||||
Disconnect c = new Disconnect();
|
||||
c.id = k.id;
|
||||
|
||||
Gdx.app.postRunnable(() -> Net.handleServerReceived(c, c.id));
|
||||
Gdx.app.postRunnable(() -> Net.handleServerReceived(k.id, c));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -89,7 +89,7 @@ public class KryoServer implements ServerProvider {
|
||||
|
||||
Gdx.app.postRunnable(() -> {
|
||||
try{
|
||||
Net.handleServerReceived(object, k.id);
|
||||
Net.handleServerReceived(k.id, object);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -377,7 +377,7 @@ public class KryoServer implements ServerProvider {
|
||||
if(k == null) return;
|
||||
Disconnect disconnect = new Disconnect();
|
||||
disconnect.id = k.id;
|
||||
Net.handleServerReceived(disconnect, k.id);
|
||||
Net.handleServerReceived(k.id, disconnect);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -396,7 +396,7 @@ public class KryoServer implements ServerProvider {
|
||||
if (debug) UCore.log("Decoded: " + Arrays.toString(out));
|
||||
ByteBuffer buffer = ByteBuffer.wrap(out);
|
||||
Object o = serializer.read(buffer);
|
||||
Net.handleServerReceived(o, k.id);
|
||||
Net.handleServerReceived(k.id, o);
|
||||
}
|
||||
}catch (Exception e){
|
||||
UCore.log("Error reading message!");
|
||||
|
Loading…
Reference in New Issue
Block a user