Removed all websocket code

This commit is contained in:
Anuken 2018-07-30 23:43:11 -04:00
parent 54a4c95a1b
commit 43be3259c9
5 changed files with 18 additions and 351 deletions

View File

@ -117,8 +117,6 @@ project(":html") {
compile "com.badlogicgames.gdx:gdx-controllers:$gdxVersion:sources"
compile "com.badlogicgames.gdx:gdx-controllers-gwt:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-controllers-gwt:$gdxVersion:sources"
compile "com.sksamuel.gwt:gwt-websockets:1.0.4"
compile "com.sksamuel.gwt:gwt-websockets:1.0.4:sources"
}
compileJava.options.compilerArgs = [
@ -215,7 +213,6 @@ project(":kryonet") {
dependencies {
compile project(":core")
compile 'com.github.crykn:kryonet:2.22.1'
compile "org.java-websocket:Java-WebSocket:1.3.7"
}
}

View File

@ -32,8 +32,6 @@ public class Vars{
public static final float respawnduration = 60 * 4;
//time between waves in frames (on normal mode)
public static final float wavespace = 60 * 60 * 2f;
//waves can last no longer than 3 minutes, otherwise the next one spawns
public static final float maxwavespace = 60 * 60 * 4f;
//set ridiculously high for now
public static final float coreBuildRange = 800999f;
//discord group URL
@ -41,8 +39,6 @@ public class Vars{
public static final String releasesURL = "https://api.github.com/repos/Anuken/Mindustry/releases";
public static final int maxTextLength = 150;
public static final int maxNameLength = 40;
//public static final int maxCharNameLength = 20;
// public static final int saveSlots = 64;
public static final float itemSize = 5f;
public static final int tilesize = 8;
public static final int sectorSize = 300;
@ -69,7 +65,6 @@ public class Vars{
};
//server port
public static final int port = 6567;
public static final int webPort = 6568;
public static boolean testMobile;
//shorthand for whether or not this is running on android or ios
public static boolean mobile;

View File

@ -18,7 +18,6 @@ import com.google.gwt.i18n.shared.DateTimeFormat;
import com.google.gwt.user.client.ui.*;
import io.anuke.mindustry.Mindustry;
import io.anuke.mindustry.core.Platform;
import io.anuke.mindustry.net.Net;
import io.anuke.ucore.function.Consumer;
import java.io.ByteArrayInputStream;
@ -94,8 +93,6 @@ public class HtmlLauncher extends GwtApplication {
setupResizeHook();
}
});
Net.setClientProvider(new WebsocketClient());
Platform.instance = new Platform(){
DateTimeFormat format = DateTimeFormat.getFormat("EEE, dd MMM yyyy HH:mm:ss");

View File

@ -1,150 +0,0 @@
package io.anuke.mindustry.client;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Base64Coder;
import com.badlogic.gdx.utils.reflect.ClassReflection;
import com.badlogic.gdx.utils.reflect.ReflectionException;
import com.sksamuel.gwt.websockets.Websocket;
import com.sksamuel.gwt.websockets.WebsocketListener;
import io.anuke.mindustry.core.Platform;
import io.anuke.mindustry.net.*;
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.core.Timers;
import io.anuke.ucore.function.Consumer;
import java.io.IOException;
import java.nio.ByteBuffer;
import static io.anuke.mindustry.Vars.webPort;
public class WebsocketClient implements ClientProvider {
Websocket socket;
ByteBuffer buffer = ByteBuffer.allocate(1024);
@Override
public void connect(String ip, int port){
socket = new Websocket("ws://" + ip + ":" + webPort);
socket.addListener(new WebsocketListener() {
public void onMessage(byte[] bytes) {
try {
ByteBuffer buffer = ByteBuffer.wrap(bytes);
byte id = buffer.get();
if(id != -2){ //ignore framework messages
Class<?> type = Registrator.getByID(id);
Packet packet = (Packet) ClassReflection.newInstance(type);
packet.read(buffer);
Net.handleClientReceived(packet);
}
}catch (ReflectionException e){
throw new RuntimeException(e);
}
}
@Override
public void onClose() {
Disconnect disconnect = new Disconnect();
Net.handleClientReceived(disconnect);
}
@Override
public void onMessage(String msg) {
onMessage(Base64Coder.decode(msg));
}
@Override
public void onOpen() {
Connect connect = new Connect();
connect.addressTCP = ip;
Net.handleClientReceived(connect);
}
});
socket.open();
}
@Override
public void send(Object object, SendMode mode) {
if(!(object instanceof Packet)) throw new RuntimeException("All sent objects must be packets!");
Packet p = (Packet)object;
buffer.position(0);
buffer.put(Registrator.getID(object.getClass()));
p.write(buffer);
int pos = buffer.position();
buffer.position(0);
byte[] out = new byte[pos];
buffer.get(out);
String string = new String(Base64Coder.encode(out));
socket.send(string);
}
@Override
public void updatePing() {
}
@Override
public int getPing() {
return 0;
}
@Override
public void disconnect() {
socket.close();
}
@Override
public void discover(Consumer<Array<Host>> callback){
callback.accept(new Array<>());
}
@Override
public void pingHost(String address, int port, Consumer<Host> valid, Consumer<Exception> failed) {
try {
if (!Platform.instance.canJoinGame()) {
failed.accept(new IOException());
} else {
Websocket socket = new Websocket("ws://" + address + ":" + webPort);
final boolean[] accepted = {false};
socket.addListener(new WebsocketListener() {
@Override
public void onClose() {
if (!accepted[0]) failed.accept(new IOException("Failed to connect to host."));
}
@Override
public void onMessage(String msg) {
byte[] bytes = Base64Coder.decode(msg);
Host host = NetworkIO.readServerData(address, ByteBuffer.wrap(bytes));
if(bytes.length != 128)
valid.accept(new Host("Unknown", address, "Unknown", 0, 0, 0));
else
valid.accept(host);
accepted[0] = true;
socket.close();
}
@Override
public void onOpen() {
socket.send("ping");
}
});
socket.open();
Timers.runTask(60f * 5, () -> {
if (!accepted[0]) {
failed.accept(new IOException("Failed to connect to host."));
socket.close();
}
});
}
}catch (Exception e){
failed.accept(new IOException("Failed to connect to host."));
}
}
@Override
public void dispose() {
socket.close();
}
}

View File

@ -2,48 +2,38 @@ package io.anuke.kryonet;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Base64Coder;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.FrameworkMessage;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;
import com.esotericsoftware.kryonet.util.InputStreamSender;
import io.anuke.kryonet.CustomListeners.UnreliableListener;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.net.Net.SendMode;
import io.anuke.mindustry.net.Net.ServerProvider;
import io.anuke.mindustry.net.NetConnection;
import io.anuke.mindustry.net.NetworkIO;
import io.anuke.mindustry.net.Packets.*;
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 io.anuke.mindustry.net.Streamable;
import io.anuke.ucore.UCore;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.util.Log;
import org.java_websocket.WebSocket;
import org.java_websocket.exceptions.WebsocketNotConnectedException;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import java.io.IOException;
import java.net.BindException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedSelectorException;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import static io.anuke.mindustry.Vars.headless;
public class KryoServer implements ServerProvider {
final boolean tcpOnly = System.getProperty("java.version") == null;
final Server server;
final ByteSerializer serializer = new ByteSerializer();
final ByteBuffer buffer = ByteBuffer.allocate(4096);
final CopyOnWriteArrayList<KryoConnection> connections = new CopyOnWriteArrayList<>();
final CopyOnWriteArraySet<Integer> missing = new CopyOnWriteArraySet<>();
final Array<KryoConnection> array = new Array<>();
SocketServer webServer;
Thread serverThread;
int lastconnection = 0;
@ -147,8 +137,6 @@ public class KryoServer implements ServerProvider {
}else{
server.bind(port, port);
}
webServer = new SocketServer(Vars.webPort);
webServer.start();
serverThread = new Thread(() -> {
try{
@ -168,27 +156,6 @@ public class KryoServer implements ServerProvider {
lastconnection = 0;
async(server::close);
//kill them all
for (Thread worker : Thread.getAllStackTraces().keySet()) {
if (worker.getName().contains("WebSocketWorker")) {
worker.interrupt();
}
}
try {
if (webServer != null) webServer.stop(1);
}catch (NullPointerException e){
try {
synchronized (webServer) {
((Thread) UCore.getPrivate(WebSocketServer.class, webServer, "selectorthread")).join(1);
}
}catch (InterruptedException j){
handleException(j);
}
}catch (InterruptedException e){
handleException(e);
}
}
@Override
@ -289,17 +256,6 @@ public class KryoServer implements ServerProvider {
return null;
}
KryoConnection getBySocket(WebSocket socket){
for(int i = 0; i < connections.size(); i ++){
KryoConnection con = connections.get(i);
if(con.socket == socket){
return con;
}
}
return null;
}
void async(Runnable run){
Thread thread = new Thread(run);
thread.setDaemon(true);
@ -307,169 +263,41 @@ public class KryoServer implements ServerProvider {
}
class KryoConnection extends NetConnection{
public final WebSocket socket;
public final Connection connection;
public KryoConnection(int id, String address, WebSocket socket) {
super(id, address);
this.socket = socket;
this.connection = null;
}
public KryoConnection(int id, String address, Connection connection) {
super(id, address);
this.socket = null;
this.connection = connection;
}
@Override
public boolean isConnected(){
return connection == null ? !socket.isClosed() : connection.isConnected();
return connection.isConnected();
}
@Override
public void send(Object object, SendMode mode){
if(socket != null){
try {
synchronized (buffer) {
buffer.position(0);
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));
socket.send(string);
}
}catch (WebsocketNotConnectedException e){
//don't log anything, it's not important
connections.remove(this);
}catch (Exception e){
connections.remove(this);
e.printStackTrace();
try {
if (mode == SendMode.tcp) {
connection.sendTCP(object);
} else {
connection.sendUDP(object);
}
}else if (connection != null) {
try {
if (mode == SendMode.tcp) {
connection.sendTCP(object);
} else {
connection.sendUDP(object);
}
}catch (Exception e){
Log.err(e);
Log.info("Disconnecting invalid client!");
}catch (Exception e){
Log.err(e);
Log.info("Disconnecting invalid client!");
connection.close();
try{
//send error packet here
/*
NetErrorPacket packet = new NetErrorPacket();
packet.message = Strings.parseException(e, true);
Timers.runTask(5f, connection::close);*/
}catch (Exception e2){
Log.err(e2);
connection.close();
}
connection.close();
KryoConnection k = getByKryoID(connection.getID());
if(k != null) connections.remove(k);
Log.info("Connection removed {0}", k);
}
KryoConnection k = getByKryoID(connection.getID());
if(k != null) connections.remove(k);
Log.info("Connection removed {0}", k);
}
}
@Override
public void close(){
if(socket != null){
if(socket.isOpen()) socket.close();
}else if (connection != null) {
if(connection.isConnected()) connection.close();
}
if(connection.isConnected()) connection.close();
}
}
class SocketServer extends WebSocketServer {
public SocketServer(int port) {
super(new InetSocketAddress(port));
}
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
if (conn == null) return;
KryoConnection k = getBySocket(conn);
if(k == null) return;
Disconnect disconnect = new Disconnect();
disconnect.id = k.id;
Log.info("&bLost web connection: {0}", k.id);
Gdx.app.postRunnable(() -> Net.handleServerReceived(k.id, disconnect));
}
@Override
public void onMessage(WebSocket conn, String message) {
try {
if(message.equals("ping")){
ByteBuffer ping = NetworkIO.writeServerData();
conn.send(new String(Base64Coder.encode(ping.array())));
}else {
KryoConnection k = getBySocket(conn);
if (k == null){
Connect connect = new Connect();
connect.addressTCP = conn.getRemoteSocketAddress().getAddress().getHostAddress();
k = new KryoConnection(lastconnection ++, connect.addressTCP, conn);
Log.info("&bRecieved web connection: {0} {1}", k.id, connect.addressTCP);
connections.add(k);
int id = k.id;
Gdx.app.postRunnable(() -> Net.handleServerReceived(id, connect));
}
int id = k.id;
byte[] out = Base64Coder.decode(message);
ByteBuffer buffer = ByteBuffer.wrap(out);
Object o = serializer.read(buffer);
Gdx.app.postRunnable(() -> {
try {
Net.handleServerReceived(id, o);
}catch (Exception e){
e.printStackTrace();
}
});
}
}catch (Exception e){
Log.err(e);
}
}
@Override
public void onError(WebSocket conn, Exception ex) {
Log.info("WS error: ");
Log.err(ex);
if(ex instanceof BindException){
Net.closeServer();
if(!headless) {
Net.showError("$text.server.addressinuse");
}else{
Log.err("Web address in use!");
}
}else if(ex.getMessage().equals("Permission denied")){
Net.closeServer();
Net.showError("Permission denied.");
}
}
@Override
public void onStart() {}
}
}