From 89691e0c245193b91ad306762dd8627d76648bba Mon Sep 17 00:00:00 2001 From: jojo Date: Fri, 21 Aug 2020 21:41:00 +0200 Subject: [PATCH] Add FightServerConnection --- .../spectatesystem/FightServerConnection.java | 183 ++++++++++++++++++ .../spectatesystem/FightserverConnection.java | 4 - 2 files changed, 183 insertions(+), 4 deletions(-) create mode 100644 src/de/steamwar/spectatesystem/FightServerConnection.java delete mode 100644 src/de/steamwar/spectatesystem/FightserverConnection.java diff --git a/src/de/steamwar/spectatesystem/FightServerConnection.java b/src/de/steamwar/spectatesystem/FightServerConnection.java new file mode 100644 index 0000000..5c91431 --- /dev/null +++ b/src/de/steamwar/spectatesystem/FightServerConnection.java @@ -0,0 +1,183 @@ +package de.steamwar.spectatesystem; + +import org.bukkit.Bukkit; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; + +public class FightServerConnection { + + public static FightServerConnection getInstance() { + return fightServerConnection; + } + + public static FightServerConnection build(int port) { + fightServerConnection = new FightServerConnection(port); + return fightServerConnection; + } + + public static void cleanUp() { + if (fightServerConnection == null) { + return; + } + fightServerConnection.internalCleanUp(); + fightServerConnection = null; + } + + private static FightServerConnection fightServerConnection = null; + + private void internalCleanUp() { + internalPreCleanUp(); + try { + serverSocket.close(); + } catch (IOException e) { + errorCode = 2; + } + } + + private void internalPreCleanUp() { + if (socket != null) { + try { + socket.close(); + } catch (IOException e) { + errorCode = 3; + } + } + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + errorCode = 3; + } + } + if (outputStream != null) { + try { + outputStream.close(); + } catch (IOException e) { + errorCode = 3; + } + } + } + + /** + * 0 - OK + * 1 - No Connect + * 2 - Error while cleanup + * 3 - Error while pre cleanup + */ + private int errorCode = 0; + + private ServerSocket serverSocket; + private Socket socket = null; + private InputStream inputStream = null; + private OutputStream outputStream = null; + + private FightServerConnection(int port) { + try { + this.serverSocket = new ServerSocket(port); + Bukkit.getScheduler().runTaskAsynchronously(SpectateSystem.get(), () -> { + while (serverSocket != null) { + while (socket == null) { + try { + socket = serverSocket.accept(); + } catch (IOException e) { + + } + } + try { + inputStream = socket.getInputStream(); + outputStream = socket.getOutputStream(); + } catch (IOException e) { + socket = null; + } + if (socket == null) continue; + while (true) { + try { + outputStream.write(0); + } catch (IOException e) { + break; + } + } + internalPreCleanUp(); + socket = null; + inputStream = null; + outputStream = null; + } + }); + } catch (IOException e) { + errorCode = 1; + } + } + + public int getErrorCode() { + return errorCode; + } + + public InputStream getInputStream() { + return inputStream; + } + + public OutputStream getOutputStream() { + return outputStream; + } + + public int read() { + try { + return inputStream.read(); + } catch (IOException e) { + errorCode = 3; + return -2; + } + } + + public byte[] lazyRead(int length) { + try { + byte[] bytes = new byte[length]; + inputStream.read(bytes); + return bytes; + } catch (IOException e) { + errorCode = 3; + return new byte[length]; + } + } + + public byte[] read(int length) { + byte[] bytes = new byte[length]; + for (int i = 0; i < length; i++) { + bytes[i] = (byte) read(); + if (errorCode != 0) { + break; + } + } + return bytes; + } + + public void write(byte b) { + try { + outputStream.write(b); + } catch (IOException e) { + errorCode = 4; + } + } + + public void write(byte... bytes) { + try { + outputStream.write(bytes); + } catch (IOException e) { + errorCode = 4; + } + flush(); + } + + public void flush() { + try { + outputStream.flush(); + } catch (IOException e) { + errorCode = 5; + } + } + +} diff --git a/src/de/steamwar/spectatesystem/FightserverConnection.java b/src/de/steamwar/spectatesystem/FightserverConnection.java deleted file mode 100644 index 7b70a9d..0000000 --- a/src/de/steamwar/spectatesystem/FightserverConnection.java +++ /dev/null @@ -1,4 +0,0 @@ -package de.steamwar.spectatesystem; - -public class FightserverConnection { -}