More cleanup, removed connection IDs
This commit is contained in:
@@ -118,7 +118,7 @@ public class RemoteReadGenerator{
|
|||||||
if(entry.forward && entry.where.isServer && needsPlayer){
|
if(entry.forward && entry.where.isServer && needsPlayer){
|
||||||
//call forwarded method
|
//call forwarded method
|
||||||
readBlock.addStatement(packageName + "." + entry.className + "." + entry.element.getSimpleName() +
|
readBlock.addStatement(packageName + "." + entry.className + "." + entry.element.getSimpleName() +
|
||||||
"__forward(player.con.id" + (varResult.length() == 0 ? "" : ", ") + varResult.toString() + ")");
|
"__forward(player.con" + (varResult.length() == 0 ? "" : ", ") + varResult.toString() + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
readBlock.nextControlFlow("catch (java.lang.Exception e)");
|
readBlock.nextControlFlow("catch (java.lang.Exception e)");
|
||||||
|
|||||||
@@ -85,12 +85,12 @@ public class RemoteWriteGenerator{
|
|||||||
|
|
||||||
//if toAll is false, it's a 'send to one player' variant, so add the player as a parameter
|
//if toAll is false, it's a 'send to one player' variant, so add the player as a parameter
|
||||||
if(!toAll){
|
if(!toAll){
|
||||||
method.addParameter(int.class, "playerClientID");
|
method.addParameter(ClassName.bestGuess("io.anuke.mindustry.net.NetConnection"), "playerConnection");
|
||||||
}
|
}
|
||||||
|
|
||||||
//add sender to ignore
|
//add sender to ignore
|
||||||
if(forwarded){
|
if(forwarded){
|
||||||
method.addParameter(int.class, "exceptSenderID");
|
method.addParameter(ClassName.bestGuess("io.anuke.mindustry.net.NetConnection"), "exceptConnection");
|
||||||
}
|
}
|
||||||
|
|
||||||
//call local method if applicable, shouldn't happen when forwarding method as that already happens by default
|
//call local method if applicable, shouldn't happen when forwarding method as that already happens by default
|
||||||
@@ -194,18 +194,18 @@ public class RemoteWriteGenerator{
|
|||||||
|
|
||||||
if(forwarded){ //forward packet
|
if(forwarded){ //forward packet
|
||||||
if(!methodEntry.local.isClient){ //if the client doesn't get it called locally, forward it back after validation
|
if(!methodEntry.local.isClient){ //if the client doesn't get it called locally, forward it back after validation
|
||||||
sendString = "send(";
|
sendString = "io.anuke.mindustry.Vars.net.send(";
|
||||||
}else{
|
}else{
|
||||||
sendString = "sendExcept(exceptSenderID, ";
|
sendString = "io.anuke.mindustry.Vars.net.sendExcept(exceptConnection, ";
|
||||||
}
|
}
|
||||||
}else if(toAll){ //send to all players / to server
|
}else if(toAll){ //send to all players / to server
|
||||||
sendString = "send(";
|
sendString = "io.anuke.mindustry.Vars.net.send(";
|
||||||
}else{ //send to specific client from server
|
}else{ //send to specific client from server
|
||||||
sendString = "sendTo(playerClientID, ";
|
sendString = "playerConnection.send(";
|
||||||
}
|
}
|
||||||
|
|
||||||
//send the actual packet
|
//send the actual packet
|
||||||
method.addStatement("io.anuke.mindustry.Vars.net." + sendString + "packet, " +
|
method.addStatement(sendString + "packet, " +
|
||||||
(methodEntry.unreliable ? "io.anuke.mindustry.net.Net.SendMode.udp" : "io.anuke.mindustry.net.Net.SendMode.tcp") + ")");
|
(methodEntry.unreliable ? "io.anuke.mindustry.net.Net.SendMode.udp" : "io.anuke.mindustry.net.Net.SendMode.tcp") + ")");
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -174,7 +174,6 @@ public class Vars implements Loadable{
|
|||||||
public static EntityGroup<Fire> fireGroup;
|
public static EntityGroup<Fire> fireGroup;
|
||||||
public static EntityGroup<BaseUnit>[] unitGroups;
|
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;
|
public static Player player;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -201,7 +201,7 @@ public class NetClient implements ApplicationListener{
|
|||||||
|
|
||||||
@Remote(targets = Loc.client)
|
@Remote(targets = Loc.client)
|
||||||
public static void onPing(Player player, long time){
|
public static void onPing(Player player, long time){
|
||||||
Call.onPingResponse(player.id, time);
|
Call.onPingResponse(player.con, time);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Remote(variants = Variant.one)
|
@Remote(variants = Variant.one)
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ public class NetServer implements ApplicationListener{
|
|||||||
|
|
||||||
net.handleServer(Disconnect.class, (con, packet) -> {
|
net.handleServer(Disconnect.class, (con, packet) -> {
|
||||||
if(con.player != null){
|
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.lastName = packet.name;
|
||||||
info.id = packet.uuid;
|
info.id = packet.uuid;
|
||||||
admins.save();
|
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);
|
Log.info("&lcDo &lywhitelist-add {0}&lc to whitelist the player &lb'{1}'", packet.uuid, packet.name);
|
||||||
con.kick(KickReason.whitelist);
|
con.kick(KickReason.whitelist);
|
||||||
return;
|
return;
|
||||||
@@ -180,7 +180,7 @@ public class NetServer implements ApplicationListener{
|
|||||||
Log.info("Auto-assigned player {0} to team {1}.", player.name, player.getTeam());
|
Log.info("Auto-assigned player {0} to team {1}.", player.name, player.getTeam());
|
||||||
}
|
}
|
||||||
|
|
||||||
sendWorldData(player, con.id);
|
sendWorldData(player);
|
||||||
|
|
||||||
platform.updateRPC();
|
platform.updateRPC();
|
||||||
|
|
||||||
@@ -293,14 +293,14 @@ public class NetServer implements ApplicationListener{
|
|||||||
for(Player p : playerGroup.all()){
|
for(Player p : playerGroup.all()){
|
||||||
if(p.isAdmin || p.con == null || p == player) continue;
|
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());
|
player.sendMessage(builder.toString());
|
||||||
}else{
|
}else{
|
||||||
Player found;
|
Player found;
|
||||||
if(args[0].length() > 1 && args[0].startsWith("#") && Strings.canParseInt(args[0].substring(1))){
|
if(args[0].length() > 1 && args[0].startsWith("#") && Strings.canParseInt(args[0].substring(1))){
|
||||||
int id = Strings.parseInt(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{
|
}else{
|
||||||
found = playerGroup.find(p -> p.name.equalsIgnoreCase(args[0]));
|
found = playerGroup.find(p -> p.name.equalsIgnoreCase(args[0]));
|
||||||
}
|
}
|
||||||
@@ -356,8 +356,8 @@ public class NetServer implements ApplicationListener{
|
|||||||
if(player.isLocal){
|
if(player.isLocal){
|
||||||
player.sendMessage("[scarlet]Re-synchronizing as the host is pointless.");
|
player.sendMessage("[scarlet]Re-synchronizing as the host is pointless.");
|
||||||
}else{
|
}else{
|
||||||
Call.onWorldDataBegin(player.con.id);
|
Call.onWorldDataBegin(player.con);
|
||||||
netServer.sendWorldData(player, player.con.id);
|
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();
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||||
DeflaterOutputStream def = new FastDeflaterOutputStream(stream);
|
DeflaterOutputStream def = new FastDeflaterOutputStream(stream);
|
||||||
NetworkIO.writeWorld(player, def);
|
NetworkIO.writeWorld(player, def);
|
||||||
WorldStream data = new WorldStream();
|
WorldStream data = new WorldStream();
|
||||||
data.stream = new ByteArrayInputStream(stream.toByteArray());
|
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());
|
Log.debug("Packed {0} compressed bytes of world data.", stream.size());
|
||||||
}
|
}
|
||||||
@@ -487,7 +487,7 @@ public class NetServer implements ApplicationListener{
|
|||||||
newx = x;
|
newx = x;
|
||||||
newy = y;
|
newy = y;
|
||||||
}else if(Mathf.dst(x, y, newx, newy) > correctDist){
|
}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
|
//reset player to previous synced position so it gets interpolated
|
||||||
@@ -530,7 +530,7 @@ public class NetServer implements ApplicationListener{
|
|||||||
}else if(action == AdminAction.trace){
|
}else if(action == AdminAction.trace){
|
||||||
TraceInfo info = new TraceInfo(other.con.address, other.uuid, other.con.modclient, other.con.mobile);
|
TraceInfo info = new TraceInfo(other.con.address, other.uuid, other.con.modclient, other.con.mobile);
|
||||||
if(player.con != null){
|
if(player.con != null){
|
||||||
Call.onTraceInfo(player.con.id, other, info);
|
Call.onTraceInfo(player.con, other, info);
|
||||||
}else{
|
}else{
|
||||||
NetClient.onTraceInfo(other, info);
|
NetClient.onTraceInfo(other, info);
|
||||||
}
|
}
|
||||||
@@ -599,7 +599,7 @@ public class NetServer implements ApplicationListener{
|
|||||||
byte[] stateBytes = syncStream.toByteArray();
|
byte[] stateBytes = syncStream.toByteArray();
|
||||||
|
|
||||||
//write basic state data.
|
//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);
|
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){
|
if(syncStream.size() > maxSnapshotSize){
|
||||||
dataStream.close();
|
dataStream.close();
|
||||||
byte[] syncBytes = syncStream.toByteArray();
|
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;
|
sent = 0;
|
||||||
syncStream.reset();
|
syncStream.reset();
|
||||||
}
|
}
|
||||||
@@ -640,7 +640,7 @@ public class NetServer implements ApplicationListener{
|
|||||||
dataStream.close();
|
dataStream.close();
|
||||||
|
|
||||||
byte[] syncBytes = syncStream.toByteArray();
|
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);
|
Vars.ui.chatfrag.addMessage(text, null);
|
||||||
}
|
}
|
||||||
}else{
|
}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);
|
Vars.ui.chatfrag.addMessage(text, fromName);
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
Call.sendMessage(con.id, text, fromName, from);
|
Call.sendMessage(con, text, fromName, from);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ public class Net{
|
|||||||
*/
|
*/
|
||||||
public void closeServer(){
|
public void closeServer(){
|
||||||
for(NetConnection con : getConnections()){
|
for(NetConnection con : getConnections()){
|
||||||
Call.onKick(con.id, KickReason.serverClose);
|
Call.onKick(con, KickReason.serverClose);
|
||||||
}
|
}
|
||||||
|
|
||||||
provider.closeServer();
|
provider.closeServer();
|
||||||
@@ -176,43 +176,24 @@ public class Net{
|
|||||||
return (Iterable<NetConnection>)provider.getConnections();
|
return (Iterable<NetConnection>)provider.getConnections();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Send an object to all connected clients, or to the server if this is a client.*/
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
public void send(Object object, SendMode mode){
|
public void send(Object object, SendMode mode){
|
||||||
if(server){
|
if(server){
|
||||||
provider.sendServer(object, mode);
|
for(NetConnection con : provider.getConnections()){
|
||||||
|
con.send(object, mode);
|
||||||
|
}
|
||||||
}else{
|
}else{
|
||||||
provider.sendClient(object, mode);
|
provider.sendClient(object, mode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Send an object to everyone EXCEPT a certain client. Server-side only.*/
|
||||||
* Send an object to a certain client. Server-side only
|
public void sendExcept(NetConnection except, Object object, SendMode mode){
|
||||||
*/
|
for(NetConnection con : getConnections()){
|
||||||
public void sendTo(int id, Object object, SendMode mode){
|
if(con != except){
|
||||||
provider.sendServerTo(id, object, mode);
|
con.send(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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -329,7 +310,7 @@ public class Net{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Discover servers. This should run the callback regardless of whether any servers are found. Should not block.
|
* 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.
|
* @param done is the callback that should run after discovery.
|
||||||
*/
|
*/
|
||||||
void discoverServers(Consumer<Host> callback, Runnable done);
|
void discoverServers(Consumer<Host> callback, Runnable done);
|
||||||
@@ -340,65 +321,6 @@ public class Net{
|
|||||||
/** Host a server at specified port. */
|
/** Host a server at specified port. */
|
||||||
void hostServer(int port) throws IOException;
|
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. */
|
/** Return all connected users. */
|
||||||
Iterable<? extends NetConnection> getConnections();
|
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.Net.*;
|
||||||
import io.anuke.mindustry.net.Packets.*;
|
import io.anuke.mindustry.net.Packets.*;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
import static io.anuke.mindustry.Vars.netServer;
|
import static io.anuke.mindustry.Vars.netServer;
|
||||||
|
|
||||||
public abstract class NetConnection{
|
public abstract class NetConnection{
|
||||||
private static int lastID;
|
|
||||||
|
|
||||||
public final int id;
|
|
||||||
public final String address;
|
public final String address;
|
||||||
|
public boolean mobile, modclient;
|
||||||
public boolean modclient;
|
|
||||||
public boolean mobile;
|
|
||||||
public @Nullable Player player;
|
public @Nullable Player player;
|
||||||
|
|
||||||
/** ID of last recieved client snapshot. */
|
/** ID of last recieved client snapshot. */
|
||||||
@@ -25,18 +22,16 @@ public abstract class NetConnection{
|
|||||||
/** Timestamp of last recieved snapshot. */
|
/** Timestamp of last recieved snapshot. */
|
||||||
public long lastRecievedClientTime;
|
public long lastRecievedClientTime;
|
||||||
|
|
||||||
public boolean hasConnected = false;
|
public boolean hasConnected, hasBegunConnecting;
|
||||||
public boolean hasBegunConnecting = false;
|
|
||||||
public float viewWidth, viewHeight, viewX, viewY;
|
public float viewWidth, viewHeight, viewX, viewY;
|
||||||
|
|
||||||
/** Assigns this connection a unique ID. No two connections will ever have the same ID.*/
|
/** Assigns this connection a unique ID. No two connections will ever have the same ID.*/
|
||||||
public NetConnection(String address){
|
public NetConnection(String address){
|
||||||
this.id = lastID++;
|
|
||||||
this.address = address;
|
this.address = address;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void kick(KickReason reason){
|
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){
|
if(player != null && (reason == KickReason.kick || reason == KickReason.banned || reason == KickReason.vote) && player.uuid != null){
|
||||||
PlayerInfo info = netServer.admins.getInfo(player.uuid);
|
PlayerInfo info = netServer.admins.getInfo(player.uuid);
|
||||||
@@ -44,7 +39,7 @@ public abstract class NetConnection{
|
|||||||
info.lastKicked = Math.max(Time.millis(), info.lastKicked);
|
info.lastKicked = Math.max(Time.millis(), info.lastKicked);
|
||||||
}
|
}
|
||||||
|
|
||||||
Call.onKick(id, reason);
|
Call.onKick(this, reason);
|
||||||
|
|
||||||
Time.runTask(2f, this::close);
|
Time.runTask(2f, this::close);
|
||||||
|
|
||||||
@@ -55,6 +50,29 @@ public abstract class NetConnection{
|
|||||||
return true;
|
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 send(Object object, SendMode mode);
|
||||||
|
|
||||||
public abstract void close();
|
public abstract void close();
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ public class Packets{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static class Connect implements Packet{
|
public static class Connect implements Packet{
|
||||||
public int id;
|
|
||||||
public String addressTCP;
|
public String addressTCP;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -51,7 +50,6 @@ public class Packets{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static class Disconnect implements Packet{
|
public static class Disconnect implements Packet{
|
||||||
public int id;
|
|
||||||
public String reason;
|
public String reason;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ public class SteamCoreNetImpl implements SteamNetworkingCallback, SteamMatchmaki
|
|||||||
final ByteBuffer readBuffer = ByteBuffer.allocateDirect(1024 * 4);
|
final ByteBuffer readBuffer = ByteBuffer.allocateDirect(1024 * 4);
|
||||||
|
|
||||||
final CopyOnWriteArrayList<SteamConnection> connections = new CopyOnWriteArrayList<>();
|
final CopyOnWriteArrayList<SteamConnection> connections = new CopyOnWriteArrayList<>();
|
||||||
|
final CopyOnWriteArrayList<NetConnection> connectionsOut = new CopyOnWriteArrayList<>();
|
||||||
final IntMap<SteamConnection> steamConnections = new IntMap<>(); //maps steam ID -> valid net connection
|
final IntMap<SteamConnection> steamConnections = new IntMap<>(); //maps steam ID -> valid net connection
|
||||||
final ObjectMap<String, SteamID> lobbyIDs = new ObjectMap<>();
|
final ObjectMap<String, SteamID> lobbyIDs = new ObjectMap<>();
|
||||||
|
|
||||||
@@ -149,11 +150,12 @@ public class SteamCoreNetImpl implements SteamNetworkingCallback, SteamMatchmaki
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void pingHost(String address, int port, Consumer<Host> valid, Consumer<Exception> failed){
|
public void pingHost(String address, int port, Consumer<Host> valid, Consumer<Exception> failed){
|
||||||
//no
|
provider.pingHost(address, port, valid, failed);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void hostServer(int port) throws IOException{
|
public void hostServer(int port) throws IOException{
|
||||||
|
provider.hostServer(port);
|
||||||
smat.createLobby(Core.settings.getBool("publichost") ? LobbyType.Public : LobbyType.FriendsOnly, 32);
|
smat.createLobby(Core.settings.getBool("publichost") ? LobbyType.Public : LobbyType.FriendsOnly, 32);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,6 +167,8 @@ public class SteamCoreNetImpl implements SteamNetworkingCallback, SteamMatchmaki
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void closeServer(){
|
public void closeServer(){
|
||||||
|
provider.closeServer();
|
||||||
|
|
||||||
if(currentLobby != null){
|
if(currentLobby != null){
|
||||||
smat.leaveLobby(currentLobby);
|
smat.leaveLobby(currentLobby);
|
||||||
for(SteamConnection con : steamConnections.values()){
|
for(SteamConnection con : steamConnections.values()){
|
||||||
@@ -178,7 +182,11 @@ public class SteamCoreNetImpl implements SteamNetworkingCallback, SteamMatchmaki
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterable<? extends NetConnection> getConnections(){
|
public Iterable<? extends NetConnection> getConnections(){
|
||||||
return connections;
|
//merge provider connections
|
||||||
|
connectionsOut.clear();
|
||||||
|
connectionsOut.addAll(connections);
|
||||||
|
for(NetConnection c : provider.getConnections()) connectionsOut.add(c);
|
||||||
|
return connectionsOut;
|
||||||
}
|
}
|
||||||
|
|
||||||
void disconnectSteamUser(SteamID steamid){
|
void disconnectSteamUser(SteamID steamid){
|
||||||
@@ -351,7 +359,6 @@ public class SteamCoreNetImpl implements SteamNetworkingCallback, SteamMatchmaki
|
|||||||
if(!steamConnections.containsKey(steamIDRemote.getAccountID())){
|
if(!steamConnections.containsKey(steamIDRemote.getAccountID())){
|
||||||
SteamConnection con = new SteamConnection(steamIDRemote);
|
SteamConnection con = new SteamConnection(steamIDRemote);
|
||||||
Connect c = new Connect();
|
Connect c = new Connect();
|
||||||
c.id = con.id;
|
|
||||||
c.addressTCP = "steam:" + steamIDRemote.getAccountID();
|
c.addressTCP = "steam:" + steamIDRemote.getAccountID();
|
||||||
|
|
||||||
Log.info("&bRecieved connection: {0}", c.addressTCP);
|
Log.info("&bRecieved connection: {0}", c.addressTCP);
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ public class ArcNetImpl implements NetProvider{
|
|||||||
public void connected(Connection connection){
|
public void connected(Connection connection){
|
||||||
Connect c = new Connect();
|
Connect c = new Connect();
|
||||||
c.addressTCP = connection.getRemoteAddressTCP().getAddress().getHostAddress();
|
c.addressTCP = connection.getRemoteAddressTCP().getAddress().getHostAddress();
|
||||||
c.id = connection.getID();
|
|
||||||
if(connection.getRemoteAddressTCP() != null) c.addressTCP = connection.getRemoteAddressTCP().toString();
|
if(connection.getRemoteAddressTCP() != null) c.addressTCP = connection.getRemoteAddressTCP().toString();
|
||||||
|
|
||||||
Core.app.post(() -> net.handleClientReceived(c));
|
Core.app.post(() -> net.handleClientReceived(c));
|
||||||
@@ -83,7 +82,6 @@ public class ArcNetImpl implements NetProvider{
|
|||||||
ArcConnection kn = new ArcConnection(ip, connection);
|
ArcConnection kn = new ArcConnection(ip, connection);
|
||||||
|
|
||||||
Connect c = new Connect();
|
Connect c = new Connect();
|
||||||
c.id = kn.id;
|
|
||||||
c.addressTCP = ip;
|
c.addressTCP = ip;
|
||||||
|
|
||||||
Log.debug("&bRecieved connection: {0}", c.addressTCP);
|
Log.debug("&bRecieved connection: {0}", c.addressTCP);
|
||||||
@@ -98,7 +96,6 @@ public class ArcNetImpl implements NetProvider{
|
|||||||
if(k == null) return;
|
if(k == null) return;
|
||||||
|
|
||||||
Disconnect c = new Disconnect();
|
Disconnect c = new Disconnect();
|
||||||
c.id = k.id;
|
|
||||||
c.reason = reason.toString();
|
c.reason = reason.toString();
|
||||||
|
|
||||||
Core.app.post(() -> {
|
Core.app.post(() -> {
|
||||||
@@ -228,10 +225,11 @@ public class ArcNetImpl implements NetProvider{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void dispose(){
|
public void dispose(){
|
||||||
|
disconnectClient();
|
||||||
|
closeServer();
|
||||||
try{
|
try{
|
||||||
client.dispose();
|
client.dispose();
|
||||||
}catch(IOException e){
|
}catch(IOException ignored){
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,34 +238,6 @@ public class ArcNetImpl implements NetProvider{
|
|||||||
return connections;
|
return connections;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void sendServerStream(int id, Streamable stream){
|
|
||||||
ArcConnection connection = (ArcConnection)getConnection(id);
|
|
||||||
if(connection == null) return;
|
|
||||||
|
|
||||||
connection.connection.addListener(new InputStreamSender(stream.stream, 512){
|
|
||||||
int id;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void start(){
|
|
||||||
//send an object so the receiving side knows how to handle the following chunks
|
|
||||||
StreamBegin begin = new StreamBegin();
|
|
||||||
begin.total = stream.stream.available();
|
|
||||||
begin.type = Registrator.getID(stream.getClass());
|
|
||||||
connection.connection.sendTCP(begin);
|
|
||||||
id = begin.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Object next(byte[] bytes){
|
|
||||||
StreamChunk chunk = new StreamChunk();
|
|
||||||
chunk.id = id;
|
|
||||||
chunk.data = bytes;
|
|
||||||
return chunk; //wrap the byte[] with an object so the receiving side knows how to handle it.
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void hostServer(int port) throws IOException{
|
public void hostServer(int port) throws IOException{
|
||||||
connections.clear();
|
connections.clear();
|
||||||
@@ -314,6 +284,31 @@ public class ArcNetImpl implements NetProvider{
|
|||||||
return connection.isConnected();
|
return connection.isConnected();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendStream(Streamable stream){
|
||||||
|
connection.addListener(new InputStreamSender(stream.stream, 512){
|
||||||
|
int id;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void start(){
|
||||||
|
//send an object so the receiving side knows how to handle the following chunks
|
||||||
|
StreamBegin begin = new StreamBegin();
|
||||||
|
begin.total = stream.stream.available();
|
||||||
|
begin.type = Registrator.getID(stream.getClass());
|
||||||
|
connection.sendTCP(begin);
|
||||||
|
id = begin.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Object next(byte[] bytes){
|
||||||
|
StreamChunk chunk = new StreamChunk();
|
||||||
|
chunk.id = id;
|
||||||
|
chunk.data = bytes;
|
||||||
|
return chunk; //wrap the byte[] with an object so the receiving side knows how to handle it.
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void send(Object object, SendMode mode){
|
public void send(Object object, SendMode mode){
|
||||||
try{
|
try{
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ public class PacketSerializer implements NetSerializer{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void writeFramework(ByteBuffer buffer, FrameworkMessage message){
|
public static void writeFramework(ByteBuffer buffer, FrameworkMessage message){
|
||||||
if(message instanceof Ping){
|
if(message instanceof Ping){
|
||||||
Ping p = (Ping)message;
|
Ping p = (Ping)message;
|
||||||
|
|||||||
@@ -816,7 +816,7 @@ public class ServerControl implements ApplicationListener{
|
|||||||
if(state.rules.pvp){
|
if(state.rules.pvp){
|
||||||
p.setTeam(netServer.assignTeam(p, new ArrayIterable<>(players)));
|
p.setTeam(netServer.assignTeam(p, new ArrayIterable<>(players)));
|
||||||
}
|
}
|
||||||
netServer.sendWorldData(p, p.con.id);
|
netServer.sendWorldData(p);
|
||||||
}
|
}
|
||||||
inExtraRound = false;
|
inExtraRound = false;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user