More commands added to server, new logging
This commit is contained in:
@@ -12,8 +12,8 @@ import io.anuke.mindustry.net.Packet;
|
||||
import io.anuke.mindustry.net.Packets.Connect;
|
||||
import io.anuke.mindustry.net.Packets.Disconnect;
|
||||
import io.anuke.mindustry.net.Registrator;
|
||||
import io.anuke.ucore.UCore;
|
||||
import io.anuke.ucore.function.Consumer;
|
||||
import io.anuke.ucore.util.Log;
|
||||
import org.java_websocket.client.WebSocketClient;
|
||||
import org.java_websocket.drafts.Draft_6455;
|
||||
import org.java_websocket.handshake.ServerHandshake;
|
||||
@@ -32,7 +32,7 @@ public class JavaWebsocketClient implements ClientProvider {
|
||||
public void connect(String ip, int port) throws IOException {
|
||||
try {
|
||||
URI i = new URI("ws://" + ip + ":" + Vars.webPort);
|
||||
UCore.log("Connecting: " + i);
|
||||
Log.info("Connecting: {0}", i);
|
||||
socket = new WebSocketClient(i, new Draft_6455(), null, 5000) {
|
||||
Thread thread;
|
||||
|
||||
@@ -47,14 +47,13 @@ public class JavaWebsocketClient implements ClientProvider {
|
||||
|
||||
@Override
|
||||
public void onOpen(ServerHandshake handshakedata) {
|
||||
UCore.log("Connected!");
|
||||
Log.info("Connected!");
|
||||
Connect connect = new Connect();
|
||||
Net.handleClientReceived(connect);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(String message) {
|
||||
if(debug) UCore.log("Got message: " + message);
|
||||
try {
|
||||
byte[] bytes = Base64Coder.decode(message);
|
||||
ByteBuffer buffer = ByteBuffer.wrap(bytes);
|
||||
@@ -63,7 +62,6 @@ public class JavaWebsocketClient implements ClientProvider {
|
||||
//this is a framework message... do nothing yet?
|
||||
} else {
|
||||
Class<?> type = Registrator.getByID(id);
|
||||
if(debug) UCore.log("Got class ID: " + type);
|
||||
Packet packet = (Packet) ClassReflection.newInstance(type);
|
||||
packet.read(buffer);
|
||||
Net.handleClientReceived(packet);
|
||||
@@ -76,7 +74,6 @@ public class JavaWebsocketClient implements ClientProvider {
|
||||
|
||||
@Override
|
||||
public void onClose(int code, String reason, boolean remote) {
|
||||
if(debug) UCore.log("Closed.");
|
||||
Disconnect disconnect = new Disconnect();
|
||||
Net.handleClientReceived(disconnect);
|
||||
}
|
||||
@@ -105,7 +102,6 @@ public class JavaWebsocketClient implements ClientProvider {
|
||||
byte[] out = new byte[pos];
|
||||
buffer.get(out);
|
||||
String string = new String(Base64Coder.encode(out));
|
||||
if(debug) UCore.log("Sending string: " + string);
|
||||
socket.send(string);
|
||||
}
|
||||
|
||||
|
||||
@@ -12,17 +12,19 @@ import io.anuke.mindustry.net.Net.ClientProvider;
|
||||
import io.anuke.mindustry.net.Net.SendMode;
|
||||
import io.anuke.mindustry.net.Packets.Connect;
|
||||
import io.anuke.mindustry.net.Packets.Disconnect;
|
||||
import io.anuke.ucore.UCore;
|
||||
import io.anuke.ucore.function.Consumer;
|
||||
import io.anuke.ucore.util.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.ClosedSelectorException;
|
||||
import java.util.List;
|
||||
|
||||
import static io.anuke.mindustry.Vars.*;
|
||||
import static io.anuke.mindustry.Vars.netClient;
|
||||
import static io.anuke.mindustry.Vars.port;
|
||||
|
||||
public class KryoClient implements ClientProvider{
|
||||
Client client;
|
||||
@@ -41,7 +43,7 @@ public class KryoClient implements ClientProvider{
|
||||
ByteBuffer buffer = ByteBuffer.wrap(datagramPacket.getData());
|
||||
Host address = KryoRegistrator.readServerData(datagramPacket.getAddress(), buffer);
|
||||
addresses.put(datagramPacket.getAddress(), address);
|
||||
UCore.log("Host data found: " + buffer.capacity() + " bytes.");
|
||||
Log.info("Host data found: {0} bytes.", buffer.capacity());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -106,7 +108,7 @@ public class KryoClient implements ClientProvider{
|
||||
try{
|
||||
client.run();
|
||||
}catch (Exception e){
|
||||
handleException(e);
|
||||
if(!(e instanceof ClosedSelectorException)) handleException(e);
|
||||
}
|
||||
}, "Kryonet Client");
|
||||
updateThread.setDaemon(true);
|
||||
|
||||
@@ -1,29 +1,57 @@
|
||||
package io.anuke.kryonet;
|
||||
|
||||
import com.esotericsoftware.kryo.Kryo;
|
||||
import com.esotericsoftware.minlog.Log;
|
||||
import com.esotericsoftware.minlog.Log.Logger;
|
||||
import io.anuke.mindustry.Vars;
|
||||
import io.anuke.mindustry.net.Host;
|
||||
import io.anuke.mindustry.net.Net;
|
||||
import io.anuke.ucore.util.ColorCodes;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.net.InetAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import static io.anuke.mindustry.Vars.headless;
|
||||
import static io.anuke.mindustry.Vars.playerGroup;
|
||||
|
||||
public class KryoRegistrator {
|
||||
public static boolean fakeLag = false;
|
||||
public static final int fakeLagAmount = 500;
|
||||
|
||||
public static void register(Kryo kryo){
|
||||
//TODO register stuff?
|
||||
//Log.set(Log.LEVEL_DEBUG);
|
||||
static{
|
||||
Log.setLogger(new Logger(){
|
||||
public void log (int level, String category, String message, Throwable ex) {
|
||||
StringBuilder builder = new StringBuilder(256);
|
||||
|
||||
if(headless)
|
||||
builder.append(ColorCodes.BLUE);
|
||||
|
||||
builder.append("Net: ");
|
||||
|
||||
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.ucore.util.Log.info("&b" + builder.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static ByteBuffer writeServerData(){
|
||||
String host = Vars.headless ? "Server" : Vars.player.name;
|
||||
String host = headless ? "Server" : Vars.player.name;
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1 + host.getBytes().length + 4);
|
||||
buffer.put((byte)host.getBytes().length);
|
||||
buffer.put(host.getBytes());
|
||||
buffer.putInt(Net.getConnections().size + 1);
|
||||
buffer.putInt(playerGroup.size());
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import io.anuke.mindustry.net.Streamable;
|
||||
import io.anuke.mindustry.net.Streamable.StreamBegin;
|
||||
import io.anuke.mindustry.net.Streamable.StreamChunk;
|
||||
import io.anuke.ucore.UCore;
|
||||
import io.anuke.ucore.util.Log;
|
||||
import org.java_websocket.WebSocket;
|
||||
import org.java_websocket.exceptions.WebsocketNotConnectedException;
|
||||
import org.java_websocket.handshake.ClientHandshake;
|
||||
@@ -32,7 +33,7 @@ import java.io.IOException;
|
||||
import java.net.BindException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Arrays;
|
||||
import java.nio.channels.ClosedSelectorException;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
public class KryoServer implements ServerProvider {
|
||||
@@ -50,7 +51,7 @@ public class KryoServer implements ServerProvider {
|
||||
server = new Server(4096*2, 2048, connection -> new ByteSerializer()); //TODO tweak
|
||||
server.setDiscoveryHandler((datagramChannel, fromAddress) -> {
|
||||
ByteBuffer buffer = KryoRegistrator.writeServerData();
|
||||
UCore.log("Replying to discover request with buffer of size " + buffer.capacity());
|
||||
Log.info("Replying to discover request with buffer of size {0}.", buffer.capacity());
|
||||
buffer.position(0);
|
||||
datagramChannel.send(buffer, fromAddress);
|
||||
return true;
|
||||
@@ -67,7 +68,6 @@ public class KryoServer implements ServerProvider {
|
||||
c.addressTCP = connection.getRemoteAddressTCP().toString();
|
||||
|
||||
connections.add(kn);
|
||||
UCore.log("Adding connection #" + kn.id + " to list.");
|
||||
Gdx.app.postRunnable(() -> Net.handleServerReceived(kn.id, c));
|
||||
}
|
||||
|
||||
@@ -136,7 +136,7 @@ public class KryoServer implements ServerProvider {
|
||||
try{
|
||||
server.run();
|
||||
}catch (Exception e){
|
||||
handleException(e);
|
||||
if(!(e instanceof ClosedSelectorException)) handleException(e);
|
||||
}
|
||||
}, "Kryonet Server");
|
||||
thread.setDaemon(true);
|
||||
@@ -152,7 +152,7 @@ public class KryoServer implements ServerProvider {
|
||||
Thread thread = new Thread(() ->{
|
||||
try {
|
||||
server.close();
|
||||
UCore.log("Killing web server...");
|
||||
Log.info("Killing web server...");
|
||||
try {
|
||||
if (webServer != null) webServer.stop(1); //please die, right now
|
||||
}catch(Exception e){
|
||||
@@ -164,7 +164,7 @@ public class KryoServer implements ServerProvider {
|
||||
worker.interrupt();
|
||||
}
|
||||
}
|
||||
UCore.log("Killed web server.");
|
||||
Log.info("Killed web server.");
|
||||
}catch (Exception e){
|
||||
Gdx.app.postRunnable(() -> {throw new RuntimeException(e);});
|
||||
}
|
||||
@@ -260,7 +260,7 @@ public class KryoServer implements ServerProvider {
|
||||
}
|
||||
|
||||
try {
|
||||
UCore.log("Disposing web server...");
|
||||
Log.info("Disposing web server...");
|
||||
|
||||
if(webServer != null) webServer.stop(1);
|
||||
//kill them all
|
||||
@@ -269,7 +269,7 @@ public class KryoServer implements ServerProvider {
|
||||
thread.interrupt();
|
||||
}
|
||||
}
|
||||
UCore.log("Killed web server.");
|
||||
Log.info("Killed web server.");
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -334,14 +334,14 @@ public class KryoServer implements ServerProvider {
|
||||
try {
|
||||
synchronized (buffer) {
|
||||
buffer.position(0);
|
||||
if(debug) UCore.log("Sending object with ID " + Registrator.getID(object.getClass()));
|
||||
if(debug) Log.info("Sending object with ID {0}", Registrator.getID(object.getClass()));
|
||||
serializer.write(buffer, object);
|
||||
int pos = buffer.position();
|
||||
buffer.position(0);
|
||||
byte[] out = new byte[pos];
|
||||
buffer.get(out);
|
||||
String string = new String(Base64Coder.encode(out));
|
||||
if(debug) UCore.log("Sending string: " + string);
|
||||
if(debug) Log.info("Sending string: {0}", string);
|
||||
socket.send(string);
|
||||
}
|
||||
}catch (WebsocketNotConnectedException e){
|
||||
@@ -360,7 +360,7 @@ public class KryoServer implements ServerProvider {
|
||||
}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
UCore.log("Disconnecting invalid client!");
|
||||
Log.info("Disconnecting invalid client!");
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
@@ -387,7 +387,7 @@ public class KryoServer implements ServerProvider {
|
||||
public void onOpen(WebSocket conn, ClientHandshake handshake) {
|
||||
Connect connect = new Connect();
|
||||
connect.addressTCP = conn.getRemoteSocketAddress().toString();
|
||||
UCore.log("Websocket connection recieved: " + connect.addressTCP);
|
||||
Log.info("Websocket connection recieved: {0}", connect.addressTCP);
|
||||
KryoConnection kn = new KryoConnection(lastconnection ++, connect.addressTCP, conn);
|
||||
connections.add(kn);
|
||||
}
|
||||
@@ -412,23 +412,20 @@ public class KryoServer implements ServerProvider {
|
||||
conn.send("---" + connections.size() + "|" + Vars.player.name);
|
||||
connections.remove(k);
|
||||
}else {
|
||||
if (debug) UCore.log("Got message: " + message);
|
||||
|
||||
byte[] out = Base64Coder.decode(message);
|
||||
if (debug) UCore.log("Decoded: " + Arrays.toString(out));
|
||||
ByteBuffer buffer = ByteBuffer.wrap(out);
|
||||
Object o = serializer.read(buffer);
|
||||
Net.handleServerReceived(k.id, o);
|
||||
}
|
||||
}catch (Exception e){
|
||||
UCore.log("Error reading message!");
|
||||
e.printStackTrace();
|
||||
Log.err(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(WebSocket conn, Exception ex) {
|
||||
UCore.log("WS error:");
|
||||
Log.info("WS error:");
|
||||
ex.printStackTrace();
|
||||
if(ex instanceof BindException){
|
||||
Net.closeServer();
|
||||
@@ -441,7 +438,7 @@ public class KryoServer implements ServerProvider {
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
UCore.log("Web server started.");
|
||||
Log.info("Web server started.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user