Successful desktop compilation
This commit is contained in:
57
net/src/io/anuke/kryonet/ByteSerializer.java
Normal file
57
net/src/io/anuke/kryonet/ByteSerializer.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package io.anuke.kryonet;
|
||||
|
||||
import com.esotericsoftware.kryonet.FrameworkMessage;
|
||||
import com.esotericsoftware.kryonet.serialization.Serialization;
|
||||
import io.anuke.arc.function.Supplier;
|
||||
import io.anuke.arc.util.pooling.Pools;
|
||||
import io.anuke.mindustry.net.Packet;
|
||||
import io.anuke.mindustry.net.Registrator;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class ByteSerializer implements Serialization{
|
||||
|
||||
@Override
|
||||
public void write(ByteBuffer byteBuffer, Object o){
|
||||
if(o instanceof FrameworkMessage){
|
||||
byteBuffer.put((byte) -2); //code for framework message
|
||||
FrameworkSerializer.write(byteBuffer, (FrameworkMessage) o);
|
||||
}else{
|
||||
if(!(o instanceof Packet))
|
||||
throw new RuntimeException("All sent objects must implement be Packets! Class: " + o.getClass());
|
||||
byte id = Registrator.getID(o.getClass());
|
||||
if(id == -1)
|
||||
throw new RuntimeException("Unregistered class: " + o.getClass());
|
||||
byteBuffer.put(id);
|
||||
((Packet) o).write(byteBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(ByteBuffer byteBuffer){
|
||||
byte id = byteBuffer.get();
|
||||
if(id == -2){
|
||||
return FrameworkSerializer.read(byteBuffer);
|
||||
}else{
|
||||
Packet packet = Pools.obtain((Class<Packet>) Registrator.getByID(id).type, (Supplier<Packet>) Registrator.getByID(id).constructor);
|
||||
packet.read(byteBuffer);
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLengthLength(){
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeLength(ByteBuffer byteBuffer, int i){
|
||||
byteBuffer.putShort((short) i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readLength(ByteBuffer byteBuffer){
|
||||
return byteBuffer.getShort();
|
||||
}
|
||||
}
|
||||
67
net/src/io/anuke/kryonet/FrameworkSerializer.java
Normal file
67
net/src/io/anuke/kryonet/FrameworkSerializer.java
Normal file
@@ -0,0 +1,67 @@
|
||||
package io.anuke.kryonet;
|
||||
|
||||
import com.esotericsoftware.kryonet.FrameworkMessage;
|
||||
import com.esotericsoftware.kryonet.FrameworkMessage.*;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class FrameworkSerializer{
|
||||
|
||||
public static void write(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){
|
||||
DiscoverHost p = (DiscoverHost) message;
|
||||
|
||||
buffer.put((byte) 1);
|
||||
}else if(message instanceof KeepAlive){
|
||||
KeepAlive p = (KeepAlive) message;
|
||||
|
||||
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 read(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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
241
net/src/io/anuke/kryonet/KryoClient.java
Normal file
241
net/src/io/anuke/kryonet/KryoClient.java
Normal file
@@ -0,0 +1,241 @@
|
||||
package io.anuke.kryonet;
|
||||
|
||||
import com.esotericsoftware.kryonet.*;
|
||||
import io.anuke.arc.Core;
|
||||
import io.anuke.arc.collection.Array;
|
||||
import io.anuke.arc.function.Consumer;
|
||||
import io.anuke.arc.util.pooling.Pools;
|
||||
import io.anuke.mindustry.net.Host;
|
||||
import io.anuke.mindustry.net.Net;
|
||||
import io.anuke.mindustry.net.Net.ClientProvider;
|
||||
import io.anuke.mindustry.net.Net.SendMode;
|
||||
import io.anuke.mindustry.net.NetworkIO;
|
||||
import io.anuke.mindustry.net.Packets.Connect;
|
||||
import io.anuke.mindustry.net.Packets.Disconnect;
|
||||
import net.jpountz.lz4.LZ4Factory;
|
||||
import net.jpountz.lz4.LZ4FastDecompressor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.ClosedSelectorException;
|
||||
|
||||
import static io.anuke.mindustry.Vars.netClient;
|
||||
import static io.anuke.mindustry.Vars.port;
|
||||
|
||||
public class KryoClient implements ClientProvider{
|
||||
final Client client;
|
||||
final Array<InetAddress> foundAddresses = new Array<>();
|
||||
final ClientDiscoveryHandler handler;
|
||||
final LZ4FastDecompressor decompressor = LZ4Factory.fastestInstance().fastDecompressor();
|
||||
Consumer<Host> lastCallback;
|
||||
|
||||
public KryoClient(){
|
||||
KryoCore.init();
|
||||
|
||||
handler = new ClientDiscoveryHandler(){
|
||||
@Override
|
||||
public DatagramPacket onRequestNewDatagramPacket(){
|
||||
return new DatagramPacket(new byte[128], 128);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDiscoveredHost(DatagramPacket datagramPacket){
|
||||
ByteBuffer buffer = ByteBuffer.wrap(datagramPacket.getData());
|
||||
Host host = NetworkIO.readServerData(datagramPacket.getAddress().getHostAddress(), buffer);
|
||||
for(InetAddress address : foundAddresses){
|
||||
if(address.equals(datagramPacket.getAddress()) || (isLocal(address) && isLocal(datagramPacket.getAddress()))){
|
||||
return;
|
||||
}
|
||||
}
|
||||
Core.app.post(() -> lastCallback.accept(host));
|
||||
foundAddresses.add(datagramPacket.getAddress());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFinally(){
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
client = new Client(8192, 4096, connection -> new ByteSerializer());
|
||||
client.setDiscoveryHandler(handler);
|
||||
|
||||
Listener listener = new Listener(){
|
||||
@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);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
if(KryoCore.fakeLag){
|
||||
client.addListener(new Listener.LagListener(KryoCore.fakeLagMin, KryoCore.fakeLagMax, listener));
|
||||
}else{
|
||||
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 byte[] decompressSnapshot(byte[] input, int size){
|
||||
byte[] result = new byte[size];
|
||||
decompressor.decompress(input, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect(String ip, int port, Runnable success){
|
||||
runAsync(() -> {
|
||||
try{
|
||||
//just in case
|
||||
client.stop();
|
||||
|
||||
Thread updateThread = new Thread(() -> {
|
||||
try{
|
||||
client.run();
|
||||
}catch(Exception e){
|
||||
if(!(e instanceof ClosedSelectorException)) handleException(e);
|
||||
}
|
||||
}, "Kryonet Client");
|
||||
updateThread.setDaemon(true);
|
||||
updateThread.start();
|
||||
|
||||
client.connect(5000, ip, port, port);
|
||||
success.run();
|
||||
}catch(Exception e){
|
||||
handleException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect(){
|
||||
client.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(Object object, SendMode mode){
|
||||
if(mode == SendMode.tcp){
|
||||
client.sendTCP(object);
|
||||
}else{
|
||||
client.sendUDP(object);
|
||||
}
|
||||
|
||||
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){
|
||||
runAsync(() -> {
|
||||
synchronized(handler){
|
||||
try{
|
||||
DatagramSocket socket = new DatagramSocket();
|
||||
socket.send(new DatagramPacket(new byte[]{-2, 1}, 2, InetAddress.getByName(address), port));
|
||||
|
||||
socket.setSoTimeout(2000);
|
||||
|
||||
lastCallback = valid;
|
||||
|
||||
DatagramPacket packet = handler.onRequestNewDatagramPacket();
|
||||
|
||||
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){
|
||||
runAsync(() -> {
|
||||
synchronized(handler){
|
||||
foundAddresses.clear();
|
||||
lastCallback = callback;
|
||||
client.discoverHosts(port, 3000);
|
||||
Core.app.post(done);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose(){
|
||||
try{
|
||||
client.dispose();
|
||||
}catch(IOException e){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
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 KryoNetException){
|
||||
Core.app.post(() -> Net.showError(new IOException("mismatch")));
|
||||
}else{
|
||||
Core.app.post(() -> Net.showError(e));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
66
net/src/io/anuke/kryonet/KryoCore.java
Normal file
66
net/src/io/anuke/kryonet/KryoCore.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package io.anuke.kryonet;
|
||||
|
||||
import com.esotericsoftware.kryonet.util.Log;
|
||||
import com.esotericsoftware.kryonet.util.Log.Logger;
|
||||
import io.anuke.arc.util.ColorCodes;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
import static io.anuke.mindustry.Vars.headless;
|
||||
|
||||
/** Utilities and configs for kryo module. */
|
||||
public class KryoCore{
|
||||
public static boolean fakeLag = false;
|
||||
public static final int fakeLagMax = 500;
|
||||
public static final int fakeLagMin = 0;
|
||||
public static final float fakeLagDrop = 0.1f;
|
||||
public static final float fakeLagDuplicate = 0.1f;
|
||||
|
||||
public static boolean lastUDP;
|
||||
|
||||
private static ScheduledExecutorService threadPool;
|
||||
|
||||
public static void init(){
|
||||
Log.set(fakeLag ? Log.LEVEL_DEBUG : Log.LEVEL_WARN);
|
||||
|
||||
Log.setLogger(new Logger(){
|
||||
public void log(int level, String category, String message, Throwable ex){
|
||||
if(fakeLag){
|
||||
if(message.contains("UDP")){
|
||||
lastUDP = true;
|
||||
}else if(message.contains("TCP")){
|
||||
lastUDP = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder builder = new StringBuilder(256);
|
||||
|
||||
if(headless)
|
||||
builder.append(ColorCodes.BLUE);
|
||||
|
||||
builder.append("Net Error: ");
|
||||
|
||||
builder.append(message);
|
||||
|
||||
if(ex != null){
|
||||
StringWriter writer = new StringWriter(256);
|
||||
ex.printStackTrace(new PrintWriter(writer));
|
||||
builder.append('\n');
|
||||
builder.append(writer.toString().trim());
|
||||
}
|
||||
|
||||
if(headless)
|
||||
builder.append(ColorCodes.RESET);
|
||||
|
||||
io.anuke.arc.util.Log.info("&b" + builder.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static int calculateLag(){
|
||||
return fakeLagMin + (int) (Math.random() * (fakeLagMax - fakeLagMin));
|
||||
}
|
||||
}
|
||||
316
net/src/io/anuke/kryonet/KryoServer.java
Normal file
316
net/src/io/anuke/kryonet/KryoServer.java
Normal file
@@ -0,0 +1,316 @@
|
||||
package io.anuke.kryonet;
|
||||
|
||||
import com.dosse.upnp.UPnP;
|
||||
import com.esotericsoftware.kryonet.Connection;
|
||||
import com.esotericsoftware.kryonet.FrameworkMessage;
|
||||
import com.esotericsoftware.kryonet.Listener;
|
||||
import com.esotericsoftware.kryonet.Listener.LagListener;
|
||||
import com.esotericsoftware.kryonet.Server;
|
||||
import com.esotericsoftware.kryonet.util.InputStreamSender;
|
||||
import io.anuke.arc.Core;
|
||||
import io.anuke.arc.collection.Array;
|
||||
import io.anuke.arc.util.Log;
|
||||
import io.anuke.arc.util.Time;
|
||||
import io.anuke.mindustry.Vars;
|
||||
import io.anuke.mindustry.net.*;
|
||||
import io.anuke.mindustry.net.Net.SendMode;
|
||||
import io.anuke.mindustry.net.Net.ServerProvider;
|
||||
import io.anuke.mindustry.net.Packets.Connect;
|
||||
import io.anuke.mindustry.net.Packets.Disconnect;
|
||||
import io.anuke.mindustry.net.Packets.StreamBegin;
|
||||
import io.anuke.mindustry.net.Packets.StreamChunk;
|
||||
import net.jpountz.lz4.LZ4Compressor;
|
||||
import net.jpountz.lz4.LZ4Factory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.ClosedSelectorException;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
public class KryoServer implements ServerProvider{
|
||||
final Server server;
|
||||
final CopyOnWriteArrayList<KryoConnection> connections = new CopyOnWriteArrayList<>();
|
||||
final CopyOnWriteArraySet<Integer> missing = new CopyOnWriteArraySet<>();
|
||||
final Array<KryoConnection> array = new Array<>();
|
||||
final LZ4Compressor compressor = LZ4Factory.fastestInstance().fastCompressor();
|
||||
Thread serverThread;
|
||||
|
||||
int lastconnection = 0;
|
||||
|
||||
public KryoServer(){
|
||||
KryoCore.init();
|
||||
|
||||
server = new Server(4096 * 2, 4096, connection -> new ByteSerializer());
|
||||
server.setDiscoveryHandler((datagramChannel, fromAddress) -> {
|
||||
ByteBuffer buffer = NetworkIO.writeServerData();
|
||||
buffer.position(0);
|
||||
datagramChannel.send(buffer, fromAddress);
|
||||
return true;
|
||||
});
|
||||
|
||||
Listener listener = new Listener(){
|
||||
|
||||
@Override
|
||||
public void connected(Connection connection){
|
||||
String ip = connection.getRemoteAddressTCP().getAddress().getHostAddress();
|
||||
|
||||
KryoConnection kn = new KryoConnection(lastconnection++, ip, connection);
|
||||
|
||||
Connect c = new Connect();
|
||||
c.id = kn.id;
|
||||
c.addressTCP = ip;
|
||||
|
||||
Log.info("&bRecieved connection: {0}", c.addressTCP);
|
||||
|
||||
connections.add(kn);
|
||||
Core.app.post(() -> Net.handleServerReceived(kn.id, c));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnected(Connection connection){
|
||||
KryoConnection 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){
|
||||
KryoConnection k = getByKryoID(connection.getID());
|
||||
if(object instanceof FrameworkMessage || k == null) return;
|
||||
|
||||
Core.app.post(() -> {
|
||||
try{
|
||||
Net.handleServerReceived(k.id, object);
|
||||
}catch(ValidateException e){
|
||||
Log.err("Validate failed: {0} ({1})", e.player.name, e.getMessage());
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if(KryoCore.fakeLag){
|
||||
server.addListener(new LagListener(KryoCore.fakeLagMin, KryoCore.fakeLagMax, listener));
|
||||
}else{
|
||||
server.addListener(listener);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] compressSnapshot(byte[] input){
|
||||
return compressor.compress(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Array<KryoConnection> getConnections(){
|
||||
array.clear();
|
||||
for(KryoConnection c : connections){
|
||||
array.add(c);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KryoConnection getByID(int id){
|
||||
for(int i = 0; i < connections.size(); i++){
|
||||
KryoConnection con = connections.get(i);
|
||||
if(con.id == id){
|
||||
return con;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
serverThread = new Thread(() -> {
|
||||
try{
|
||||
server.run();
|
||||
}catch(Throwable e){
|
||||
if(!(e instanceof ClosedSelectorException)) handleException(e);
|
||||
}
|
||||
}, "Kryonet Server");
|
||||
serverThread.setDaemon(true);
|
||||
serverThread.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(){
|
||||
connections.clear();
|
||||
lastconnection = 0;
|
||||
|
||||
async(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){
|
||||
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();
|
||||
Log.info("Disposed server.");
|
||||
}
|
||||
|
||||
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);
|
||||
if(con.connection != null && con.connection.getID() == id){
|
||||
return con;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
void async(Runnable run){
|
||||
Thread thread = new Thread(run);
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
class KryoConnection extends NetConnection{
|
||||
public final Connection connection;
|
||||
|
||||
public KryoConnection(int id, String address, Connection connection){
|
||||
super(id, address);
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConnected(){
|
||||
return connection.isConnected();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(Object object, SendMode mode){
|
||||
try{
|
||||
if(mode == SendMode.tcp){
|
||||
connection.sendTCP(object);
|
||||
}else{
|
||||
connection.sendUDP(object);
|
||||
}
|
||||
}catch(Exception e){
|
||||
Log.err(e);
|
||||
Log.info("Disconnecting invalid client!");
|
||||
connection.close();
|
||||
|
||||
KryoConnection k = getByKryoID(connection.getID());
|
||||
if(k != null) connections.remove(k);
|
||||
Log.info("Connection removed {0}", k);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(){
|
||||
if(connection.isConnected()) connection.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user