13
0

Add FightServerConnection

Dieser Commit ist enthalten in:
jojo 2020-08-21 21:41:00 +02:00
Ursprung 05ffa3f1b4
Commit 89691e0c24
2 geänderte Dateien mit 183 neuen und 4 gelöschten Zeilen

Datei anzeigen

@ -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;
}
}
}

Datei anzeigen

@ -1,4 +0,0 @@
package de.steamwar.spectatesystem;
public class FightserverConnection {
}