Multiplayer: Smooth building + rotation / Disabled UDP / Cleanup
This commit is contained in:
@@ -4,6 +4,7 @@ import io.anuke.arc.*;
|
||||
import io.anuke.arc.collection.*;
|
||||
import io.anuke.arc.function.*;
|
||||
import io.anuke.arc.net.*;
|
||||
import io.anuke.arc.util.async.*;
|
||||
import io.anuke.arc.util.pooling.*;
|
||||
import io.anuke.mindustry.net.Net.*;
|
||||
import io.anuke.mindustry.net.Packets.*;
|
||||
@@ -84,7 +85,7 @@ public class ArcNetClient implements ClientProvider{
|
||||
|
||||
@Override
|
||||
public void connect(String ip, int port, Runnable success){
|
||||
runAsync(() -> {
|
||||
Threads.daemon(() -> {
|
||||
try{
|
||||
//just in case
|
||||
client.stop();
|
||||
@@ -99,7 +100,7 @@ public class ArcNetClient implements ClientProvider{
|
||||
updateThread.setDaemon(true);
|
||||
updateThread.start();
|
||||
|
||||
client.connect(5000, ip, port, port);
|
||||
client.connect(5000, ip, port);
|
||||
success.run();
|
||||
}catch(Exception e){
|
||||
handleException(e);
|
||||
@@ -115,11 +116,7 @@ public class ArcNetClient implements ClientProvider{
|
||||
@Override
|
||||
public void send(Object object, SendMode mode){
|
||||
try{
|
||||
if(mode == SendMode.tcp){
|
||||
client.sendTCP(object);
|
||||
}else{
|
||||
client.sendUDP(object);
|
||||
}
|
||||
client.sendTCP(object);
|
||||
//sending things can cause an under/overflow, catch it and disconnect instead of crashing
|
||||
}catch(BufferOverflowException | BufferUnderflowException e){
|
||||
Net.showError(e);
|
||||
@@ -140,7 +137,7 @@ public class ArcNetClient implements ClientProvider{
|
||||
|
||||
@Override
|
||||
public void pingHost(String address, int port, Consumer<Host> valid, Consumer<Exception> invalid){
|
||||
runAsync(() -> {
|
||||
Threads.daemon(() -> {
|
||||
try{
|
||||
DatagramSocket socket = new DatagramSocket();
|
||||
socket.send(new DatagramPacket(new byte[]{-2, 1}, 2, InetAddress.getByName(address), port));
|
||||
@@ -189,12 +186,6 @@ public class ArcNetClient implements ClientProvider{
|
||||
}
|
||||
}
|
||||
|
||||
private void runAsync(Runnable run){
|
||||
Thread thread = new Thread(run, "Client Async Run");
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
private void handleException(Exception e){
|
||||
if(e instanceof ArcNetException){
|
||||
Core.app.post(() -> Net.showError(new IOException("mismatch")));
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package io.anuke.mindustry.net;
|
||||
|
||||
import com.dosse.upnp.*;
|
||||
import io.anuke.arc.*;
|
||||
import io.anuke.arc.collection.*;
|
||||
import io.anuke.arc.net.*;
|
||||
import io.anuke.arc.util.*;
|
||||
import io.anuke.mindustry.*;
|
||||
import io.anuke.arc.util.async.*;
|
||||
import io.anuke.mindustry.net.Net.*;
|
||||
import io.anuke.mindustry.net.Packets.*;
|
||||
import net.jpountz.lz4.*;
|
||||
@@ -19,14 +17,10 @@ import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
public class ArcNetServer implements ServerProvider{
|
||||
final Server server;
|
||||
final CopyOnWriteArrayList<KryoConnection> connections = new CopyOnWriteArrayList<>();
|
||||
final CopyOnWriteArraySet<Integer> missing = new CopyOnWriteArraySet<>();
|
||||
final Array<KryoConnection> array = new Array<>();
|
||||
final CopyOnWriteArrayList<ArcConnection> connections = new CopyOnWriteArrayList<>();
|
||||
final LZ4Compressor compressor = LZ4Factory.fastestInstance().fastCompressor();
|
||||
Thread serverThread;
|
||||
|
||||
int lastconnection = 0;
|
||||
|
||||
public ArcNetServer(){
|
||||
server = new Server(4096 * 2, 4096, new PacketSerializer());
|
||||
server.setMulticast(multicastGroup, multicastPort);
|
||||
@@ -42,7 +36,7 @@ public class ArcNetServer implements ServerProvider{
|
||||
public void connected(Connection connection){
|
||||
String ip = connection.getRemoteAddressTCP().getAddress().getHostAddress();
|
||||
|
||||
KryoConnection kn = new KryoConnection(lastconnection++, ip, connection);
|
||||
ArcConnection kn = new ArcConnection(ip, connection);
|
||||
|
||||
Connect c = new Connect();
|
||||
c.id = kn.id;
|
||||
@@ -56,7 +50,7 @@ public class ArcNetServer implements ServerProvider{
|
||||
|
||||
@Override
|
||||
public void disconnected(Connection connection){
|
||||
KryoConnection k = getByKryoID(connection.getID());
|
||||
ArcConnection k = getByKryoID(connection.getID());
|
||||
if(k == null) return;
|
||||
|
||||
Disconnect c = new Disconnect();
|
||||
@@ -70,7 +64,7 @@ public class ArcNetServer implements ServerProvider{
|
||||
|
||||
@Override
|
||||
public void received(Connection connection, Object object){
|
||||
KryoConnection k = getByKryoID(connection.getID());
|
||||
ArcConnection k = getByKryoID(connection.getID());
|
||||
if(object instanceof FrameworkMessage || k == null) return;
|
||||
|
||||
Core.app.post(() -> {
|
||||
@@ -99,18 +93,14 @@ public class ArcNetServer implements ServerProvider{
|
||||
}
|
||||
|
||||
@Override
|
||||
public Array<KryoConnection> getConnections(){
|
||||
array.clear();
|
||||
for(KryoConnection c : connections){
|
||||
array.add(c);
|
||||
}
|
||||
return array;
|
||||
public Iterable<ArcConnection> getConnections(){
|
||||
return connections;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KryoConnection getByID(int id){
|
||||
public ArcConnection getByID(int id){
|
||||
for(int i = 0; i < connections.size(); i++){
|
||||
KryoConnection con = connections.get(i);
|
||||
ArcConnection con = connections.get(i);
|
||||
if(con.id == id){
|
||||
return con;
|
||||
}
|
||||
@@ -121,28 +111,14 @@ public class ArcNetServer implements ServerProvider{
|
||||
|
||||
@Override
|
||||
public void host(int port) throws IOException{
|
||||
//attempt to open default ports if they're not already open
|
||||
//this only opens the default port due to security concerns (?)
|
||||
if(port == Vars.port){
|
||||
async(() -> {
|
||||
try{
|
||||
if(!UPnP.isMappedTCP(port)) UPnP.openPortTCP(port);
|
||||
if(!UPnP.isMappedUDP(port)) UPnP.openPortUDP(port);
|
||||
}catch(Throwable ignored){
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
lastconnection = 0;
|
||||
connections.clear();
|
||||
missing.clear();
|
||||
server.bind(port, port);
|
||||
server.bind(port);
|
||||
|
||||
serverThread = new Thread(() -> {
|
||||
try{
|
||||
server.run();
|
||||
}catch(Throwable e){
|
||||
if(!(e instanceof ClosedSelectorException)) handleException(e);
|
||||
if(!(e instanceof ClosedSelectorException)) Threads.throwAppException(e);
|
||||
}
|
||||
}, "Net Server");
|
||||
serverThread.setDaemon(true);
|
||||
@@ -152,102 +128,12 @@ public class ArcNetServer implements ServerProvider{
|
||||
@Override
|
||||
public void close(){
|
||||
connections.clear();
|
||||
lastconnection = 0;
|
||||
|
||||
async(server::stop);
|
||||
Threads.daemon(server::stop);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendStream(int id, Streamable stream){
|
||||
KryoConnection connection = getByID(id);
|
||||
if(connection == null) return;
|
||||
try{
|
||||
|
||||
if(connection.connection != null){
|
||||
|
||||
connection.connection.addListener(new InputStreamSender(stream.stream, 512){
|
||||
int id;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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.
|
||||
}
|
||||
});
|
||||
}else{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(Object object, SendMode mode){
|
||||
ArcConnection getByKryoID(int id){
|
||||
for(int i = 0; i < connections.size(); i++){
|
||||
connections.get(i).send(object, mode);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendTo(int id, Object object, SendMode mode){
|
||||
NetConnection conn = getByID(id);
|
||||
if(conn == null){
|
||||
if(!missing.contains(id))
|
||||
Log.err("Failed to find connection with ID {0}.", id);
|
||||
missing.add(id);
|
||||
return;
|
||||
}
|
||||
conn.send(object, mode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendExcept(int id, Object object, SendMode mode){
|
||||
for(int i = 0; i < connections.size(); i++){
|
||||
KryoConnection conn = connections.get(i);
|
||||
if(conn.id != id) conn.send(object, mode);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose(){
|
||||
close();
|
||||
}
|
||||
|
||||
private void handleException(Throwable e){
|
||||
Time.run(0f, () -> {
|
||||
throw new RuntimeException(e);
|
||||
});
|
||||
}
|
||||
|
||||
KryoConnection getByKryoID(int id){
|
||||
for(int i = 0; i < connections.size(); i++){
|
||||
KryoConnection con = connections.get(i);
|
||||
ArcConnection con = connections.get(i);
|
||||
if(con.connection != null && con.connection.getID() == id){
|
||||
return con;
|
||||
}
|
||||
@@ -256,17 +142,11 @@ public class ArcNetServer implements ServerProvider{
|
||||
return null;
|
||||
}
|
||||
|
||||
void async(Runnable run){
|
||||
Thread thread = new Thread(run);
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
class KryoConnection extends NetConnection{
|
||||
class ArcConnection extends NetConnection{
|
||||
public final Connection connection;
|
||||
|
||||
public KryoConnection(int id, String address, Connection connection){
|
||||
super(id, address);
|
||||
public ArcConnection(String address, Connection connection){
|
||||
super(address);
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
@@ -278,17 +158,13 @@ public class ArcNetServer implements ServerProvider{
|
||||
@Override
|
||||
public void send(Object object, SendMode mode){
|
||||
try{
|
||||
if(mode == SendMode.tcp){
|
||||
connection.sendTCP(object);
|
||||
}else{
|
||||
connection.sendUDP(object);
|
||||
}
|
||||
connection.sendTCP(object);
|
||||
}catch(Exception e){
|
||||
Log.err(e);
|
||||
Log.info("Error sending packet. Disconnecting invalid client!");
|
||||
connection.close();
|
||||
|
||||
KryoConnection k = getByKryoID(connection.getID());
|
||||
ArcConnection k = getByKryoID(connection.getID());
|
||||
if(k != null) connections.remove(k);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user