More codegen fixes, fixed (one) multiplayer connect crash

This commit is contained in:
Anuken
2018-06-07 00:01:08 -04:00
parent ab9cdf5610
commit e451cdd519
12 changed files with 184 additions and 79 deletions

View File

@@ -1,10 +1,9 @@
package io.anuke.mindustry.core;
import com.badlogic.gdx.utils.IntMap;
import com.badlogic.gdx.utils.IntSet;
import com.badlogic.gdx.graphics.Color;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.entities.traits.SyncTrait;
import io.anuke.mindustry.gen.Call;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.net.Net.SendMode;
import io.anuke.mindustry.net.NetworkIO;
@@ -27,13 +26,8 @@ public class NetClient extends Module {
private boolean connecting = false;
/**If true, no message will be shown on disconnect.*/
private boolean quiet = false;
/**List of all recieved entitity IDs, to prevent duplicates.*/
private IntSet recieved = new IntSet();
/**List of recently recieved entities that have not been added to the queue yet.*/
private IntMap<SyncTrait> recent = new IntMap<>();
/**Counter for data timeout.*/
private float timeoutTime = 0f;
private int requests = 0;
public NetClient(){
@@ -43,8 +37,6 @@ public class NetClient extends Module {
player.isAdmin = false;
Net.setClientLoaded(false);
recieved.clear();
recent.clear();
timeoutTime = 0f;
connecting = true;
quiet = false;
@@ -55,7 +47,20 @@ public class NetClient extends Module {
Entities.clear();
//TODO send connect packet here
ConnectPacket c = new ConnectPacket();
c.name = player.name;
c.mobile = mobile;
c.color = Color.rgba8888(player.color);
c.uuid = Platform.instance.getUUID();
if(c.uuid == null){
ui.showError("$text.invalidid");
ui.loadfrag.hide();
disconnectQuietly();
return;
}
Net.send(c, SendMode.tcp);
});
Net.handleClient(Disconnect.class, packet -> {
@@ -78,10 +83,7 @@ public class NetClient extends Module {
finishConnecting();
});
Net.handleClient(InvokePacket.class, packet -> {
//TODO invoke it
});
Net.handleClient(InvokePacket.class, packet -> {});
}
@Override
@@ -115,8 +117,7 @@ public class NetClient extends Module {
ui.loadfrag.hide();
ui.join.hide();
Net.setClientLoaded(true);
//send connect ACK packet
//Timers.runTask(1f, () -> Net.send(new ConnectConfirmPacket(), SendMode.tcp));
Call.connectConfirm();
Timers.runTask(40f, Platform.instance::updateRPC);
}
@@ -130,7 +131,6 @@ public class NetClient extends Module {
}
void sync(){
requests = 0;
if(timer.get(0, playerSyncTime)){
Player player = players[0];

View File

@@ -1,24 +1,24 @@
package io.anuke.mindustry.core;
import com.badlogic.gdx.utils.Base64Coder;
import com.badlogic.gdx.utils.IntMap;
import com.badlogic.gdx.utils.TimeUtils;
import io.anuke.annotations.Annotations.Remote;
import io.anuke.mindustry.content.Mechs;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.gen.RemoteReadServer;
import io.anuke.mindustry.net.Administration;
import io.anuke.mindustry.io.Version;
import io.anuke.mindustry.net.*;
import io.anuke.mindustry.net.Administration.PlayerInfo;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.net.NetConnection;
import io.anuke.mindustry.net.Packets.ClientSnapshotPacket;
import io.anuke.mindustry.net.Packets.Connect;
import io.anuke.mindustry.net.Packets.InvokePacket;
import io.anuke.mindustry.net.Packets.KickReason;
import io.anuke.mindustry.net.Packets.*;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.modules.Module;
import io.anuke.ucore.util.Log;
import io.anuke.ucore.util.Timer;
import java.nio.ByteBuffer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import static io.anuke.mindustry.Vars.*;
@@ -34,7 +34,6 @@ public class NetServer extends Module{
private IntMap<Player> connections = new IntMap<>();
private boolean closing = false;
private Timer timer = new Timer(5);
private ByteBuffer writeBuffer = ByteBuffer.allocate(32);
public NetServer(){
@@ -44,14 +43,81 @@ public class NetServer extends Module{
}
});
Net.handleServer(ClientSnapshotPacket.class, (id, packet) -> {
//...don't do anything here as it's already handled by the packet itself
Net.handleServer(Disconnect.class, (id, packet) -> {});
Net.handleServer(ConnectPacket.class, (id, packet) -> {
String uuid = new String(Base64Coder.encode(packet.uuid));
if(Net.getConnection(id) == null ||
admins.isIPBanned(Net.getConnection(id).address)) return;
TraceInfo trace = admins.getTraceByID(uuid);
PlayerInfo info = admins.getInfo(uuid);
trace.uuid = uuid;
trace.android = packet.mobile;
if(admins.isIDBanned(uuid)){
kick(id, KickReason.banned);
return;
}
if(TimeUtils.millis() - info.lastKicked < kickDuration){
kick(id, KickReason.recentKick);
return;
}
for(Player player : playerGroup.all()){
if(player.name.equalsIgnoreCase(packet.name)){
kick(id, KickReason.nameInUse);
return;
}
}
Log.info("Recieved connect packet for player '{0}' / UUID {1} / IP {2}", packet.name, uuid, trace.ip);
String ip = Net.getConnection(id).address;
admins.updatePlayerJoined(uuid, ip, packet.name);
if(packet.version != Version.build && Version.build != -1 && packet.version != -1){
kick(id, packet.version > Version.build ? KickReason.serverOutdated : KickReason.clientOutdated);
return;
}
if(packet.version == -1){
trace.modclient = true;
}
Player player = new Player();
player.isAdmin = admins.isAdmin(uuid, ip);
player.clientid = id;
player.name = packet.name;
player.uuid = uuid;
player.mech = packet.mobile ? Mechs.standardShip : Mechs.standard;
player.dead = true;
player.setNet(player.x, player.y);
player.setNet(player.x, player.y);
player.color.set(packet.color);
connections.put(id, player);
trace.playerid = player.id;
//TODO try DeflaterOutputStream
ByteArrayOutputStream stream = new ByteArrayOutputStream();
NetworkIO.writeWorld(player, stream);
WorldStream data = new WorldStream();
data.stream = new ByteArrayInputStream(stream.toByteArray());
Net.sendStream(id, data);
Log.info("Packed {0} uncompressed bytes of WORLD data.", stream.size());
Platform.instance.updateRPC();
});
Net.handleServer(InvokePacket.class, (id, packet) -> {
//TODO implement
RemoteReadServer.readPacket(packet.writeBuffer, packet.type, connections.get(id));
});
Net.handleServer(ClientSnapshotPacket.class, (id, packet) -> {});
Net.handleServer(InvokePacket.class, (id, packet) -> RemoteReadServer.readPacket(packet.writeBuffer, packet.type, connections.get(id)));
}
public void update(){
@@ -101,11 +167,14 @@ public class NetServer extends Module{
return connections.get(connectionID).uuid;
}
void sendMessageTo(int id, String message){
//TODO implement
}
void sync(){
//TODO implement snapshot packets w/ delta compression
}
@Remote(server = false)
public static void connectConfirm(Player player){
player.add();
Log.info("&y{0} has connected.", player.name);
netCommon.sendMessage("[accent]" + player.name + " has connected.");
}
}

View File

@@ -11,6 +11,7 @@ import java.nio.ByteBuffer;
import static io.anuke.mindustry.Vars.playerGroup;
import static io.anuke.mindustry.Vars.world;
/**Class for specifying read/write methods for code generation.*/
public class TypeIO {
@WriteClass(Player.class)
@@ -47,4 +48,18 @@ public class TypeIO {
buffer.get(bytes);
return new String(bytes);
}
@WriteClass(byte[].class)
public static void writeBytes(ByteBuffer buffer, byte[] bytes){
buffer.putShort((short)bytes.length);
buffer.put(bytes);
}
@ReadClass(byte[].class)
public static byte[] readBytes(ByteBuffer buffer){
short length = buffer.getShort();
byte[] bytes = new byte[length];
buffer.get(bytes);
return bytes;
}
}

View File

@@ -1,18 +1,5 @@
package io.anuke.mindustry.net;
import io.anuke.annotations.Annotations.Remote;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.world.Tile;
public class NetEvents {
@Remote(unreliable = true, one = true, all = false)
public static void callClientMethod(int something, Player player, String str, boolean bool){
System.out.println("Called " + something + " ? " + bool);
}
@Remote(local = false)
public static void someOtherMethod(Tile tile){
System.out.println("Called with tile " + tile);
}
}

View File

@@ -1,11 +1,14 @@
package io.anuke.mindustry.net;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.TimeUtils;
import io.anuke.mindustry.content.Weapons;
import io.anuke.mindustry.content.blocks.Blocks;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.game.GameMode;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.io.Map;
import io.anuke.mindustry.io.MapMeta;
import io.anuke.mindustry.io.Version;
import io.anuke.mindustry.type.Upgrade;
import io.anuke.mindustry.world.Tile;
@@ -30,7 +33,7 @@ public class NetworkIO {
//--GENERAL STATE--
stream.writeByte(state.mode.ordinal()); //gamemode
stream.writeUTF(world.getMap().name); //map ID
stream.writeUTF(world.getMap().name); //map name
stream.writeInt(state.wave); //wave
stream.writeFloat(state.wavetime); //wave countdown
@@ -133,10 +136,15 @@ public class NetworkIO {
int width = stream.readShort();
int height = stream.readShort();
//TODO send advanced map meta such as author, etc
//TODO scan for cores
Map currentMap = new Map(map, new MapMeta(0, new ObjectMap<>(), width, height, null), true, () -> null);
world.setMap(currentMap);
Tile[][] tiles = world.createTiles(width, height);
for(int x = 0; x < world.width(); x ++){
for(int y = 0; y < world.height(); y ++){
for(int x = 0; x < width; x ++){
for(int y = 0; y < height; y ++){
byte floorid = stream.readByte();
byte blockid = stream.readByte();

View File

@@ -4,8 +4,10 @@ import com.badlogic.gdx.utils.TimeUtils;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.gen.RemoteReadClient;
import io.anuke.mindustry.io.Version;
import io.anuke.mindustry.net.Packet.ImportantPacket;
import io.anuke.mindustry.net.Packet.UnimportantPacket;
import io.anuke.ucore.util.IOUtils;
import java.nio.ByteBuffer;
@@ -19,13 +21,40 @@ public class Packets {
public static class Disconnect implements ImportantPacket{
public int id;
public String addressTCP;
}
public static class WorldStream extends Streamable{
}
public static class ConnectPacket implements Packet{
public int version;
public int players;
public String name;
public boolean mobile;
public int color;
public byte[] uuid;
@Override
public void write(ByteBuffer buffer) {
buffer.putInt(Version.build);
IOUtils.writeString(buffer, name);
buffer.put(mobile ? (byte)1 : 0);
buffer.putInt(color);
buffer.put(uuid);
}
@Override
public void read(ByteBuffer buffer) {
version = buffer.getInt();
name = IOUtils.readString(buffer);
mobile = buffer.get() == 1;
color = buffer.getInt();
uuid = new byte[8];
buffer.get(uuid);
}
}
public static class InvokePacket implements Packet{
public byte type;
@@ -37,8 +66,6 @@ public class Packets {
type = buffer.get();
if(Net.client()){
//TODO implement
//CallClient.readPacket(buffer, type);
RemoteReadClient.readPacket(buffer, type);
}else{
byte[] bytes = new byte[writeLength];

View File

@@ -11,6 +11,7 @@ public class Registrator {
StreamBegin.class,
StreamChunk.class,
WorldStream.class,
ConnectPacket.class,
ClientSnapshotPacket.class,
SnapshotPacket.class,
InvokePacket.class