More cleanup, removed connection IDs
This commit is contained in:
@@ -174,7 +174,6 @@ public class Vars implements Loadable{
|
||||
public static EntityGroup<Fire> fireGroup;
|
||||
public static EntityGroup<BaseUnit>[] unitGroups;
|
||||
|
||||
/** all local players, currently only has one player. may be used for local co-op in the future */
|
||||
public static Player player;
|
||||
|
||||
@Override
|
||||
|
||||
@@ -201,7 +201,7 @@ public class NetClient implements ApplicationListener{
|
||||
|
||||
@Remote(targets = Loc.client)
|
||||
public static void onPing(Player player, long time){
|
||||
Call.onPingResponse(player.id, time);
|
||||
Call.onPingResponse(player.con, time);
|
||||
}
|
||||
|
||||
@Remote(variants = Variant.one)
|
||||
|
||||
@@ -60,7 +60,7 @@ public class NetServer implements ApplicationListener{
|
||||
|
||||
net.handleServer(Disconnect.class, (con, packet) -> {
|
||||
if(con.player != null){
|
||||
onDisconnect(player, packet.reason);
|
||||
onDisconnect(con.player, packet.reason);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -104,7 +104,7 @@ public class NetServer implements ApplicationListener{
|
||||
info.lastName = packet.name;
|
||||
info.id = packet.uuid;
|
||||
admins.save();
|
||||
Call.onInfoMessage(con.id, "You are not whitelisted here.");
|
||||
Call.onInfoMessage(con, "You are not whitelisted here.");
|
||||
Log.info("&lcDo &lywhitelist-add {0}&lc to whitelist the player &lb'{1}'", packet.uuid, packet.name);
|
||||
con.kick(KickReason.whitelist);
|
||||
return;
|
||||
@@ -180,7 +180,7 @@ public class NetServer implements ApplicationListener{
|
||||
Log.info("Auto-assigned player {0} to team {1}.", player.name, player.getTeam());
|
||||
}
|
||||
|
||||
sendWorldData(player, con.id);
|
||||
sendWorldData(player);
|
||||
|
||||
platform.updateRPC();
|
||||
|
||||
@@ -293,14 +293,14 @@ public class NetServer implements ApplicationListener{
|
||||
for(Player p : playerGroup.all()){
|
||||
if(p.isAdmin || p.con == null || p == player) continue;
|
||||
|
||||
builder.append("[lightgray] ").append(p.name).append("[accent] (#").append(p.con.id).append(")\n");
|
||||
builder.append("[lightgray] ").append(p.name).append("[accent] (#").append(p.id).append(")\n");
|
||||
}
|
||||
player.sendMessage(builder.toString());
|
||||
}else{
|
||||
Player found;
|
||||
if(args[0].length() > 1 && args[0].startsWith("#") && Strings.canParseInt(args[0].substring(1))){
|
||||
int id = Strings.parseInt(args[0].substring(1));
|
||||
found = playerGroup.find(p -> p.con != null && p.con.id == id);
|
||||
found = playerGroup.find(p -> p.id == id);
|
||||
}else{
|
||||
found = playerGroup.find(p -> p.name.equalsIgnoreCase(args[0]));
|
||||
}
|
||||
@@ -356,8 +356,8 @@ public class NetServer implements ApplicationListener{
|
||||
if(player.isLocal){
|
||||
player.sendMessage("[scarlet]Re-synchronizing as the host is pointless.");
|
||||
}else{
|
||||
Call.onWorldDataBegin(player.con.id);
|
||||
netServer.sendWorldData(player, player.con.id);
|
||||
Call.onWorldDataBegin(player.con);
|
||||
netServer.sendWorldData(player);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -382,13 +382,13 @@ public class NetServer implements ApplicationListener{
|
||||
});
|
||||
}
|
||||
|
||||
public void sendWorldData(Player player, int clientID){
|
||||
public void sendWorldData(Player player){
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
DeflaterOutputStream def = new FastDeflaterOutputStream(stream);
|
||||
NetworkIO.writeWorld(player, def);
|
||||
WorldStream data = new WorldStream();
|
||||
data.stream = new ByteArrayInputStream(stream.toByteArray());
|
||||
net.sendStream(clientID, data);
|
||||
player.con.sendStream(data);
|
||||
|
||||
Log.debug("Packed {0} compressed bytes of world data.", stream.size());
|
||||
}
|
||||
@@ -487,7 +487,7 @@ public class NetServer implements ApplicationListener{
|
||||
newx = x;
|
||||
newy = y;
|
||||
}else if(Mathf.dst(x, y, newx, newy) > correctDist){
|
||||
Call.onPositionSet(player.con.id, newx, newy); //teleport and correct position when necessary
|
||||
Call.onPositionSet(player.con, newx, newy); //teleport and correct position when necessary
|
||||
}
|
||||
|
||||
//reset player to previous synced position so it gets interpolated
|
||||
@@ -530,7 +530,7 @@ public class NetServer implements ApplicationListener{
|
||||
}else if(action == AdminAction.trace){
|
||||
TraceInfo info = new TraceInfo(other.con.address, other.uuid, other.con.modclient, other.con.mobile);
|
||||
if(player.con != null){
|
||||
Call.onTraceInfo(player.con.id, other, info);
|
||||
Call.onTraceInfo(player.con, other, info);
|
||||
}else{
|
||||
NetClient.onTraceInfo(other, info);
|
||||
}
|
||||
@@ -599,7 +599,7 @@ public class NetServer implements ApplicationListener{
|
||||
byte[] stateBytes = syncStream.toByteArray();
|
||||
|
||||
//write basic state data.
|
||||
Call.onStateSnapshot(player.con.id, state.wavetime, state.wave, state.enemies(), (short)stateBytes.length, net.compressSnapshot(stateBytes));
|
||||
Call.onStateSnapshot(player.con, state.wavetime, state.wave, state.enemies(), (short)stateBytes.length, net.compressSnapshot(stateBytes));
|
||||
|
||||
viewport.setSize(player.con.viewWidth, player.con.viewHeight).setCenter(player.con.viewX, player.con.viewY);
|
||||
|
||||
@@ -630,7 +630,7 @@ public class NetServer implements ApplicationListener{
|
||||
if(syncStream.size() > maxSnapshotSize){
|
||||
dataStream.close();
|
||||
byte[] syncBytes = syncStream.toByteArray();
|
||||
Call.onEntitySnapshot(player.con.id, (byte)group.getID(), (short)sent, (short)syncBytes.length, net.compressSnapshot(syncBytes));
|
||||
Call.onEntitySnapshot(player.con, (byte)group.getID(), (short)sent, (short)syncBytes.length, net.compressSnapshot(syncBytes));
|
||||
sent = 0;
|
||||
syncStream.reset();
|
||||
}
|
||||
@@ -640,7 +640,7 @@ public class NetServer implements ApplicationListener{
|
||||
dataStream.close();
|
||||
|
||||
byte[] syncBytes = syncStream.toByteArray();
|
||||
Call.onEntitySnapshot(player.con.id, (byte)group.getID(), (short)sent, (short)syncBytes.length, net.compressSnapshot(syncBytes));
|
||||
Call.onEntitySnapshot(player.con, (byte)group.getID(), (short)sent, (short)syncBytes.length, net.compressSnapshot(syncBytes));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -780,7 +780,7 @@ public class Player extends Unit implements BuilderMinerTrait, ShooterTrait{
|
||||
Vars.ui.chatfrag.addMessage(text, null);
|
||||
}
|
||||
}else{
|
||||
Call.sendMessage(con.id, text, null, null);
|
||||
Call.sendMessage(con, text, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -794,7 +794,7 @@ public class Player extends Unit implements BuilderMinerTrait, ShooterTrait{
|
||||
Vars.ui.chatfrag.addMessage(text, fromName);
|
||||
}
|
||||
}else{
|
||||
Call.sendMessage(con.id, text, fromName, from);
|
||||
Call.sendMessage(con, text, fromName, from);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -134,7 +134,7 @@ public class Net{
|
||||
*/
|
||||
public void closeServer(){
|
||||
for(NetConnection con : getConnections()){
|
||||
Call.onKick(con.id, KickReason.serverClose);
|
||||
Call.onKick(con, KickReason.serverClose);
|
||||
}
|
||||
|
||||
provider.closeServer();
|
||||
@@ -176,43 +176,24 @@ public class Net{
|
||||
return (Iterable<NetConnection>)provider.getConnections();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a connection by ID
|
||||
*/
|
||||
public NetConnection getConnection(int id){
|
||||
return provider.getConnection(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an object to all connected clients, or to the server if this is a client.
|
||||
*/
|
||||
/** Send an object to all connected clients, or to the server if this is a client.*/
|
||||
public void send(Object object, SendMode mode){
|
||||
if(server){
|
||||
provider.sendServer(object, mode);
|
||||
for(NetConnection con : provider.getConnections()){
|
||||
con.send(object, mode);
|
||||
}
|
||||
}else{
|
||||
provider.sendClient(object, mode);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an object to a certain client. Server-side only
|
||||
*/
|
||||
public void sendTo(int id, Object object, SendMode mode){
|
||||
provider.sendServerTo(id, object, mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an object to everyone EXCEPT certain client. Server-side only
|
||||
*/
|
||||
public void sendExcept(int id, Object object, SendMode mode){
|
||||
provider.sendServerExcept(id, object, mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a stream to a specific client. Server-side only.
|
||||
*/
|
||||
public void sendStream(int id, Streamable stream){
|
||||
provider.sendServerStream(id, stream);
|
||||
/** Send an object to everyone EXCEPT a certain client. Server-side only.*/
|
||||
public void sendExcept(NetConnection except, Object object, SendMode mode){
|
||||
for(NetConnection con : getConnections()){
|
||||
if(con != except){
|
||||
con.send(object, mode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -329,7 +310,7 @@ public class Net{
|
||||
|
||||
/**
|
||||
* Discover servers. This should run the callback regardless of whether any servers are found. Should not block.
|
||||
* Callback should be run on libGDX main thread.
|
||||
* Callback should be run on the main thread.
|
||||
* @param done is the callback that should run after discovery.
|
||||
*/
|
||||
void discoverServers(Consumer<Host> callback, Runnable done);
|
||||
@@ -340,65 +321,6 @@ public class Net{
|
||||
/** Host a server at specified port. */
|
||||
void hostServer(int port) throws IOException;
|
||||
|
||||
/** Sends a large stream of data to a specific client. */
|
||||
default void sendServerStream(int id, Streamable stream){
|
||||
NetConnection connection = getConnection(id);
|
||||
if(connection == null) return;
|
||||
try{
|
||||
int cid;
|
||||
StreamBegin begin = new StreamBegin();
|
||||
begin.total = stream.stream.available();
|
||||
begin.type = Registrator.getID(stream.getClass());
|
||||
connection.send(begin, SendMode.tcp);
|
||||
cid = begin.id;
|
||||
|
||||
while(stream.stream.available() > 0){
|
||||
byte[] bytes = new byte[Math.min(512, stream.stream.available())];
|
||||
stream.stream.read(bytes);
|
||||
|
||||
StreamChunk chunk = new StreamChunk();
|
||||
chunk.id = cid;
|
||||
chunk.data = bytes;
|
||||
connection.send(chunk, SendMode.tcp);
|
||||
}
|
||||
}catch(IOException e){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
default void sendServer(Object object, SendMode mode){
|
||||
for(NetConnection con : getConnections()){
|
||||
con.send(object, mode);
|
||||
}
|
||||
}
|
||||
|
||||
default void sendServerTo(int id, Object object, SendMode mode){
|
||||
NetConnection conn = getConnection(id);
|
||||
if(conn == null){
|
||||
Log.err("Failed to find connection with ID {0}.", id);
|
||||
return;
|
||||
}
|
||||
conn.send(object, mode);
|
||||
}
|
||||
|
||||
default void sendServerExcept(int id, Object object, SendMode mode){
|
||||
for(NetConnection con : getConnections()){
|
||||
if(con.id != id){
|
||||
con.send(object, mode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns a connection by ID. */
|
||||
default NetConnection getConnection(int id){
|
||||
for(NetConnection con : getConnections()){
|
||||
if(con.id == id){
|
||||
return con;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Return all connected users. */
|
||||
Iterable<? extends NetConnection> getConnections();
|
||||
|
||||
|
||||
@@ -8,16 +8,13 @@ import io.anuke.mindustry.net.Administration.*;
|
||||
import io.anuke.mindustry.net.Net.*;
|
||||
import io.anuke.mindustry.net.Packets.*;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import static io.anuke.mindustry.Vars.netServer;
|
||||
|
||||
public abstract class NetConnection{
|
||||
private static int lastID;
|
||||
|
||||
public final int id;
|
||||
public final String address;
|
||||
|
||||
public boolean modclient;
|
||||
public boolean mobile;
|
||||
public boolean mobile, modclient;
|
||||
public @Nullable Player player;
|
||||
|
||||
/** ID of last recieved client snapshot. */
|
||||
@@ -25,18 +22,16 @@ public abstract class NetConnection{
|
||||
/** Timestamp of last recieved snapshot. */
|
||||
public long lastRecievedClientTime;
|
||||
|
||||
public boolean hasConnected = false;
|
||||
public boolean hasBegunConnecting = false;
|
||||
public boolean hasConnected, hasBegunConnecting;
|
||||
public float viewWidth, viewHeight, viewX, viewY;
|
||||
|
||||
/** Assigns this connection a unique ID. No two connections will ever have the same ID.*/
|
||||
public NetConnection(String address){
|
||||
this.id = lastID++;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public void kick(KickReason reason){
|
||||
Log.info("Kicking connection #{0} / IP: {1}. Reason: {2}", this.id, address, reason.name());
|
||||
Log.info("Kicking connection {0}; Reason: {2}", address, reason.name());
|
||||
|
||||
if(player != null && (reason == KickReason.kick || reason == KickReason.banned || reason == KickReason.vote) && player.uuid != null){
|
||||
PlayerInfo info = netServer.admins.getInfo(player.uuid);
|
||||
@@ -44,7 +39,7 @@ public abstract class NetConnection{
|
||||
info.lastKicked = Math.max(Time.millis(), info.lastKicked);
|
||||
}
|
||||
|
||||
Call.onKick(id, reason);
|
||||
Call.onKick(this, reason);
|
||||
|
||||
Time.runTask(2f, this::close);
|
||||
|
||||
@@ -55,6 +50,29 @@ public abstract class NetConnection{
|
||||
return true;
|
||||
}
|
||||
|
||||
public void sendStream(Streamable stream){
|
||||
try{
|
||||
int cid;
|
||||
StreamBegin begin = new StreamBegin();
|
||||
begin.total = stream.stream.available();
|
||||
begin.type = Registrator.getID(stream.getClass());
|
||||
send(begin, SendMode.tcp);
|
||||
cid = begin.id;
|
||||
|
||||
while(stream.stream.available() > 0){
|
||||
byte[] bytes = new byte[Math.min(512, stream.stream.available())];
|
||||
stream.stream.read(bytes);
|
||||
|
||||
StreamChunk chunk = new StreamChunk();
|
||||
chunk.id = cid;
|
||||
chunk.data = bytes;
|
||||
send(chunk, SendMode.tcp);
|
||||
}
|
||||
}catch(IOException e){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void send(Object object, SendMode mode);
|
||||
|
||||
public abstract void close();
|
||||
|
||||
@@ -41,7 +41,6 @@ public class Packets{
|
||||
}
|
||||
|
||||
public static class Connect implements Packet{
|
||||
public int id;
|
||||
public String addressTCP;
|
||||
|
||||
@Override
|
||||
@@ -51,7 +50,6 @@ public class Packets{
|
||||
}
|
||||
|
||||
public static class Disconnect implements Packet{
|
||||
public int id;
|
||||
public String reason;
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user