start game stuff - both online and offline #13
|
@ -1,27 +1,86 @@
|
|||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
public class AsyncSocket {
|
||||
private Socket socket;
|
||||
private Thread checkerThread;
|
||||
private Thread connectorThread;
|
||||
private AsyncSocketListener handler;
|
||||
private boolean shouldStop = false;
|
||||
|
||||
private String sendBuffer = "";
|
||||
|
||||
private BufferedReader in;
|
||||
private PrintWriter out;
|
||||
private BufferedWriter out;
|
||||
|
||||
public AsyncSocket(int port, AsyncSocketListener handler) {
|
||||
this.setHandler(handler);
|
||||
|
||||
// start server in new thread
|
||||
this.connectorThread = new Thread(() -> {
|
||||
try {
|
||||
ServerSocket serverSocket = new ServerSocket(port);
|
||||
|
||||
System.out.println("Waiting for client connection on port " + port);
|
||||
|
||||
Socket socket = serverSocket.accept();
|
||||
|
||||
System.out.println("Socket connected.");
|
||||
|
||||
serverSocket.close();
|
||||
|
||||
this.initSocket(socket);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
// TODO: proper error handling
|
||||
}
|
||||
});
|
||||
this.connectorThread.start();
|
||||
}
|
||||
|
||||
public AsyncSocket(InetSocketAddress address, AsyncSocketListener handler) {
|
||||
this.setHandler(handler);
|
||||
|
||||
// start client in new thread
|
||||
this.connectorThread = new Thread(() -> {
|
||||
System.out.println("Connecting to " + address.toString());
|
||||
|
||||
Socket socket = new Socket();
|
||||
try {
|
||||
socket.connect(address, 10);
|
||||
System.out.println("Socket connected.");
|
||||
|
||||
this.initSocket(socket);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("Connection timed out");
|
||||
}
|
||||
|
||||
});
|
||||
this.connectorThread.start();
|
||||
}
|
||||
|
||||
public AsyncSocket(Socket socket, AsyncSocketListener handler) throws IOException {
|
||||
this.setHandler(handler);
|
||||
this.initSocket(socket);
|
||||
}
|
||||
|
||||
private void initSocket(Socket socket) throws IOException {
|
||||
System.out.println("Initialising sockets");
|
||||
this.socket = socket;
|
||||
|
||||
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
|
||||
this.out = new PrintWriter(new OutputStreamWriter(this.socket.getOutputStream()));
|
||||
|
||||
this.handler = handler;
|
||||
this.out = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
|
||||
|
||||
this.shouldStop = false;
|
||||
this.checkerThread = new Thread(() -> {
|
||||
while (!this.shouldStop) {
|
||||
try {
|
||||
|
@ -29,21 +88,38 @@ public class AsyncSocket {
|
|||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (!this.connectorThread.isAlive()) {
|
||||
try {
|
||||
this.connectorThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (!this.in.ready()) continue;
|
||||
if (this.handler == null) continue;
|
||||
if (!this.in.ready()) {
|
||||
continue;
|
||||
}
|
||||
if (this.handler == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String message = this.in.readLine();
|
||||
if (message.length() <= 0) continue;
|
||||
|
||||
|
||||
message = message.strip();
|
||||
System.out.println("RECEIVED - " + message);
|
||||
this.handler.receive(message);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
System.out.println("starting checker thread");
|
||||
this.checkerThread.start();
|
||||
this.flushBuffer();
|
||||
}
|
||||
|
||||
public void setHandler(AsyncSocketListener handler) {
|
||||
|
@ -51,21 +127,36 @@ public class AsyncSocket {
|
|||
}
|
||||
|
||||
|
||||
public void send(SocketPackage socketPackage) {
|
||||
public synchronized void send(SocketPackage socketPackage) {
|
||||
this.sendLine(socketPackage.toString());
|
||||
}
|
||||
public void send(String packageName) {
|
||||
public synchronized void send(String packageName) {
|
||||
this.send(packageName, "");
|
||||
}
|
||||
public void send(String packageName, String packageContent) {
|
||||
public synchronized void send(String packageName, String packageContent) {
|
||||
if (packageContent.length() > 0) {
|
||||
packageContent = " " + packageContent;
|
||||
}
|
||||
this.sendLine(packageName + packageContent);
|
||||
}
|
||||
|
||||
public void sendLine(String message) {
|
||||
this.out.print(message + "\r\n");
|
||||
public synchronized void sendLine(String message) {
|
||||
sendBuffer = sendBuffer + message + "\r\n";
|
||||
this.flushBuffer();
|
||||
}
|
||||
|
||||
private synchronized void flushBuffer() {
|
||||
if (!this.sendBuffer.isEmpty() && this.out != null) {
|
||||
try {
|
||||
this.out.write(sendBuffer);
|
||||
System.out.println("SENT - " + sendBuffer);
|
||||
sendBuffer = "";
|
||||
this.out.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
// TODO: handle writing error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
|
@ -73,8 +164,11 @@ public class AsyncSocket {
|
|||
|
||||
try {
|
||||
this.socket.close();
|
||||
this.checkerThread.join();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -36,34 +36,15 @@ public class GameController {
|
|||
public static void startOnlineGame(Class<? extends LocalPlayer> localPlayerClass, String localPlayerName, InetSocketAddress address, int size) throws IOException {
|
||||
AsyncSocket clientSocket;
|
||||
|
||||
boolean localPlayerIsServer = address.getHostName() == null;
|
||||
boolean localPlayerIsServer = address.getHostName() == null || address.getHostName().isEmpty() || address.getHostName().equals("0.0.0.0");
|
||||
|
||||
if (localPlayerIsServer) {
|
||||
// SERVER MODE
|
||||
clientSocket = new AsyncSocket(address.getPort(), null);
|
||||
|
||||
ServerSocket serverSocket = new ServerSocket(address.getPort());
|
||||
|
||||
System.out.println("Waiting for client connection...");
|
||||
|
||||
clientSocket = new AsyncSocket(serverSocket.accept(), null);
|
||||
|
||||
serverSocket.close();
|
||||
} else {
|
||||
// CLIENT MODE
|
||||
|
||||
Socket socket = new Socket();
|
||||
|
||||
try {
|
||||
socket.connect(address, CONNECTION_TIMEOUT);
|
||||
|
||||
clientSocket = new AsyncSocket(socket, null);
|
||||
} catch (SocketTimeoutException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("Connection timed out");
|
||||
} finally {
|
||||
socket.close();
|
||||
}
|
||||
|
||||
clientSocket = new AsyncSocket(address, null);
|
||||
}
|
||||
|
||||
clientSocket.send("VERSION", "Gruppe03 " + String.join(" ", supportedVersions.keySet()));
|
||||
|
|
Loading…
Reference in New Issue