mirror of
https://github.com/Anuken/Mindustry.git
synced 2025-01-03 13:30:25 +07:00
Removed arcnet as dependency
This commit is contained in:
parent
da336ea412
commit
8b9be6eafe
@ -232,7 +232,6 @@ project(":core"){
|
||||
compile "org.lz4:lz4-java:1.4.1"
|
||||
compile arcModule("arc-core")
|
||||
compile arcModule("extensions:freetype")
|
||||
compile arcModule("extensions:arcnet")
|
||||
compile arcModule("extensions:mnet")
|
||||
if(localArc() && debugged()) compile arcModule("extensions:recorder")
|
||||
|
||||
|
@ -1,187 +0,0 @@
|
||||
package io.anuke.mindustry.net;
|
||||
|
||||
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.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.*;
|
||||
import java.nio.channels.*;
|
||||
|
||||
import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
public class ArcNetClient implements ClientProvider{
|
||||
final Client client;
|
||||
final Supplier<DatagramPacket> packetSupplier = () -> new DatagramPacket(new byte[256], 256);
|
||||
|
||||
public ArcNetClient(){
|
||||
client = new Client(8192, 4096, new PacketSerializer());
|
||||
client.setDiscoveryPacket(packetSupplier);
|
||||
|
||||
NetListener listener = new NetListener(){
|
||||
@Override
|
||||
public void connected(Connection connection){
|
||||
Connect c = new Connect();
|
||||
c.addressTCP = connection.getRemoteAddressTCP().getAddress().getHostAddress();
|
||||
c.id = connection.getID();
|
||||
if(connection.getRemoteAddressTCP() != null) c.addressTCP = connection.getRemoteAddressTCP().toString();
|
||||
|
||||
Core.app.post(() -> Net.handleClientReceived(c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnected(Connection connection){
|
||||
if(connection.getLastProtocolError() != null){
|
||||
netClient.setQuiet();
|
||||
}
|
||||
|
||||
Disconnect c = new Disconnect();
|
||||
Core.app.post(() -> Net.handleClientReceived(c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void received(Connection connection, Object object){
|
||||
if(object instanceof FrameworkMessage) return;
|
||||
|
||||
Core.app.post(() -> {
|
||||
try{
|
||||
Net.handleClientReceived(object);
|
||||
}catch(Exception e){
|
||||
handleException(e);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
client.addListener(listener);
|
||||
}
|
||||
|
||||
private static boolean isLocal(InetAddress addr){
|
||||
if(addr.isAnyLocalAddress() || addr.isLoopbackAddress()) return true;
|
||||
|
||||
try{
|
||||
return NetworkInterface.getByInetAddress(addr) != null;
|
||||
}catch(Exception e){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect(String ip, int port, Runnable success){
|
||||
Threads.daemon(() -> {
|
||||
try{
|
||||
//just in case
|
||||
client.stop();
|
||||
|
||||
Thread updateThread = new Thread(() -> {
|
||||
try{
|
||||
client.run();
|
||||
}catch(Exception e){
|
||||
if(!(e instanceof ClosedSelectorException)) handleException(e);
|
||||
}
|
||||
}, "Net Client");
|
||||
updateThread.setDaemon(true);
|
||||
updateThread.start();
|
||||
|
||||
client.connect(5000, ip, port);
|
||||
success.run();
|
||||
}catch(Exception e){
|
||||
handleException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect(){
|
||||
client.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(Object object, SendMode mode){
|
||||
try{
|
||||
client.sendTCP(object);
|
||||
//sending things can cause an under/overflow, catch it and disconnect instead of crashing
|
||||
}catch(BufferOverflowException | BufferUnderflowException e){
|
||||
Net.showError(e);
|
||||
}
|
||||
|
||||
Pools.free(object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updatePing(){
|
||||
client.updateReturnTripTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPing(){
|
||||
return client.getReturnTripTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pingHost(String address, int port, Consumer<Host> valid, Consumer<Exception> invalid){
|
||||
Threads.daemon(() -> {
|
||||
try{
|
||||
DatagramSocket socket = new DatagramSocket();
|
||||
socket.send(new DatagramPacket(new byte[]{-2, 1}, 2, InetAddress.getByName(address), port));
|
||||
socket.setSoTimeout(2000);
|
||||
|
||||
DatagramPacket packet = packetSupplier.get();
|
||||
socket.receive(packet);
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.wrap(packet.getData());
|
||||
Host host = NetworkIO.readServerData(packet.getAddress().getHostAddress(), buffer);
|
||||
|
||||
Core.app.post(() -> valid.accept(host));
|
||||
}catch(Exception e){
|
||||
Core.app.post(() -> invalid.accept(e));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void discover(Consumer<Host> callback, Runnable done){
|
||||
Array<InetAddress> foundAddresses = new Array<>();
|
||||
client.discoverHosts(port, multicastGroup, multicastPort, 3000, packet -> {
|
||||
Core.app.post(() -> {
|
||||
try{
|
||||
if(foundAddresses.contains(address -> address.equals(packet.getAddress()) || (isLocal(address) && isLocal(packet.getAddress())))){
|
||||
return;
|
||||
}
|
||||
ByteBuffer buffer = ByteBuffer.wrap(packet.getData());
|
||||
Host host = NetworkIO.readServerData(packet.getAddress().getHostAddress(), buffer);
|
||||
callback.accept(host);
|
||||
foundAddresses.add(packet.getAddress());
|
||||
}catch(Exception e){
|
||||
//don't crash when there's an error pinging a a server or parsing data
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}, () -> Core.app.post(done));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose(){
|
||||
try{
|
||||
client.dispose();
|
||||
}catch(IOException e){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleException(Exception e){
|
||||
if(e instanceof ArcNetException){
|
||||
Core.app.post(() -> Net.showError(new IOException("mismatch")));
|
||||
}else{
|
||||
Core.app.post(() -> Net.showError(e));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,171 +0,0 @@
|
||||
package io.anuke.mindustry.net;
|
||||
|
||||
import io.anuke.arc.*;
|
||||
import io.anuke.arc.net.*;
|
||||
import io.anuke.arc.util.*;
|
||||
import io.anuke.arc.util.async.*;
|
||||
import io.anuke.mindustry.net.Net.*;
|
||||
import io.anuke.mindustry.net.Packets.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.*;
|
||||
import java.nio.channels.*;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
public class ArcNetServer implements ServerProvider{
|
||||
final Server server;
|
||||
final CopyOnWriteArrayList<ArcConnection> connections = new CopyOnWriteArrayList<>();
|
||||
Thread serverThread;
|
||||
|
||||
public ArcNetServer(){
|
||||
server = new Server(4096 * 2, 4096, new PacketSerializer());
|
||||
server.setMulticast(multicastGroup, multicastPort);
|
||||
server.setDiscoveryHandler((address, handler) -> {
|
||||
ByteBuffer buffer = NetworkIO.writeServerData();
|
||||
buffer.position(0);
|
||||
handler.respond(buffer);
|
||||
});
|
||||
|
||||
NetListener listener = new NetListener(){
|
||||
|
||||
@Override
|
||||
public void connected(Connection connection){
|
||||
String ip = connection.getRemoteAddressTCP().getAddress().getHostAddress();
|
||||
|
||||
ArcConnection kn = new ArcConnection(ip, connection);
|
||||
|
||||
Connect c = new Connect();
|
||||
c.id = kn.id;
|
||||
c.addressTCP = ip;
|
||||
|
||||
Log.debug("&bRecieved connection: {0}", c.addressTCP);
|
||||
|
||||
connections.add(kn);
|
||||
Core.app.post(() -> Net.handleServerReceived(kn.id, c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnected(Connection connection){
|
||||
ArcConnection k = getByKryoID(connection.getID());
|
||||
if(k == null) return;
|
||||
|
||||
Disconnect c = new Disconnect();
|
||||
c.id = k.id;
|
||||
|
||||
Core.app.post(() -> {
|
||||
Net.handleServerReceived(k.id, c);
|
||||
connections.remove(k);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void received(Connection connection, Object object){
|
||||
ArcConnection k = getByKryoID(connection.getID());
|
||||
if(object instanceof FrameworkMessage || k == null) return;
|
||||
|
||||
Core.app.post(() -> {
|
||||
try{
|
||||
Net.handleServerReceived(k.id, object);
|
||||
}catch(RuntimeException e){
|
||||
if(e.getCause() instanceof ValidateException){
|
||||
ValidateException v = (ValidateException)e.getCause();
|
||||
Log.err("Validation failed: {0} ({1})", v.player.name, v.getMessage());
|
||||
}else{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
server.addListener(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<ArcConnection> getConnections(){
|
||||
return connections;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArcConnection getByID(int id){
|
||||
for(int i = 0; i < connections.size(); i++){
|
||||
ArcConnection con = connections.get(i);
|
||||
if(con.id == id){
|
||||
return con;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void host(int port) throws IOException{
|
||||
connections.clear();
|
||||
server.bind(port);
|
||||
|
||||
serverThread = new Thread(() -> {
|
||||
try{
|
||||
server.run();
|
||||
}catch(Throwable e){
|
||||
if(!(e instanceof ClosedSelectorException)) Threads.throwAppException(e);
|
||||
}
|
||||
}, "Net Server");
|
||||
serverThread.setDaemon(true);
|
||||
serverThread.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(){
|
||||
connections.clear();
|
||||
Threads.daemon(server::stop);
|
||||
}
|
||||
|
||||
ArcConnection getByKryoID(int id){
|
||||
for(int i = 0; i < connections.size(); i++){
|
||||
ArcConnection con = connections.get(i);
|
||||
if(con.connection != null && con.connection.getID() == id){
|
||||
return con;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
class ArcConnection extends NetConnection{
|
||||
public final Connection connection;
|
||||
|
||||
public ArcConnection(String address, Connection connection){
|
||||
super(address);
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConnected(){
|
||||
return connection.isConnected();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(Object object, SendMode mode){
|
||||
try{
|
||||
connection.sendTCP(object);
|
||||
}catch(Exception e){
|
||||
Log.err(e);
|
||||
Log.info("Error sending packet. Disconnecting invalid client!");
|
||||
connection.close();
|
||||
|
||||
ArcConnection k = getByKryoID(connection.getID());
|
||||
if(k != null) connections.remove(k);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(){
|
||||
if(connection.isConnected()) connection.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -30,7 +30,9 @@ public class MClient implements ClientProvider, ApplicationListener{
|
||||
if(response.getType() == ResponseType.ACCEPTED){
|
||||
Core.app.post(() -> {
|
||||
success.run();
|
||||
Net.handleClientReceived(new Connect());
|
||||
Connect c = new Connect();
|
||||
c.addressTCP = ip;
|
||||
Net.handleClientReceived(c);
|
||||
});
|
||||
}else if(response.getType() == ResponseType.WRONG_STATE){
|
||||
Core.app.post(() -> Net.showError(new IOException("alreadyconnected")));
|
||||
|
@ -1,8 +1,6 @@
|
||||
package io.anuke.mindustry.net;
|
||||
|
||||
import io.anuke.arc.function.*;
|
||||
import io.anuke.arc.net.*;
|
||||
import io.anuke.arc.net.FrameworkMessage.*;
|
||||
import io.anuke.arc.util.pooling.*;
|
||||
import io.anuke.mnet.*;
|
||||
|
||||
@ -10,10 +8,9 @@ import java.nio.*;
|
||||
import java.util.*;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class PacketSerializer implements NetSerializer, MSerializer{
|
||||
public class PacketSerializer implements MSerializer{
|
||||
private ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024 * 8);
|
||||
|
||||
@Override
|
||||
public void write(ByteBuffer byteBuffer, Object o){
|
||||
if(o == null){
|
||||
byteBuffer.put((byte)-1);
|
||||
@ -29,7 +26,6 @@ public class PacketSerializer implements NetSerializer, MSerializer{
|
||||
((Packet) o).write(byteBuffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(ByteBuffer byteBuffer){
|
||||
byte id = byteBuffer.get();
|
||||
if(id == -1){
|
||||
@ -81,51 +77,4 @@ public class PacketSerializer implements NetSerializer, MSerializer{
|
||||
buffer.position(0);
|
||||
return read(buffer);
|
||||
}
|
||||
|
||||
|
||||
public static void writeFramework(ByteBuffer buffer, FrameworkMessage message){
|
||||
if(message instanceof Ping){
|
||||
Ping p = (Ping)message;
|
||||
buffer.put((byte)0);
|
||||
buffer.putInt(p.id);
|
||||
buffer.put(p.isReply ? 1 : (byte)0);
|
||||
}else if(message instanceof DiscoverHost){
|
||||
buffer.put((byte)1);
|
||||
}else if(message instanceof KeepAlive){
|
||||
buffer.put((byte)2);
|
||||
}else if(message instanceof RegisterUDP){
|
||||
RegisterUDP p = (RegisterUDP)message;
|
||||
buffer.put((byte)3);
|
||||
buffer.putInt(p.connectionID);
|
||||
}else if(message instanceof RegisterTCP){
|
||||
RegisterTCP p = (RegisterTCP)message;
|
||||
buffer.put((byte)4);
|
||||
buffer.putInt(p.connectionID);
|
||||
}
|
||||
}
|
||||
|
||||
public static FrameworkMessage readFramework(ByteBuffer buffer){
|
||||
byte id = buffer.get();
|
||||
|
||||
if(id == 0){
|
||||
Ping p = new Ping();
|
||||
p.id = buffer.getInt();
|
||||
p.isReply = buffer.get() == 1;
|
||||
return p;
|
||||
}else if(id == 1){
|
||||
return new DiscoverHost();
|
||||
}else if(id == 2){
|
||||
return new KeepAlive();
|
||||
}else if(id == 3){
|
||||
RegisterUDP p = new RegisterUDP();
|
||||
p.connectionID = buffer.getInt();
|
||||
return p;
|
||||
}else if(id == 4){
|
||||
RegisterTCP p = new RegisterTCP();
|
||||
p.connectionID = buffer.getInt();
|
||||
return p;
|
||||
}else{
|
||||
throw new RuntimeException("Unknown framework message!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,6 @@ if(!hasProperty("release")){
|
||||
use(':Arc:extensions')
|
||||
use(':Arc:extensions:freetype')
|
||||
use(':Arc:extensions:recorder')
|
||||
use(':Arc:extensions:arcnet')
|
||||
use(':Arc:extensions:mnet')
|
||||
use(':Arc:extensions:packer')
|
||||
use(':Arc:backends')
|
||||
|
Loading…
Reference in New Issue
Block a user