2022-07-05 19:49:04 +00:00
|
|
|
package nl.andrewl.aos2_server;
|
|
|
|
|
|
|
|
import nl.andrewl.aos_core.Net;
|
2022-07-06 18:20:15 +00:00
|
|
|
import nl.andrewl.aos_core.model.Player;
|
|
|
|
import nl.andrewl.aos_core.net.ConnectAcceptMessage;
|
2022-07-05 21:34:54 +00:00
|
|
|
import nl.andrewl.aos_core.net.ConnectRejectMessage;
|
|
|
|
import nl.andrewl.aos_core.net.ConnectRequestMessage;
|
2022-07-06 18:20:15 +00:00
|
|
|
import nl.andrewl.aos_core.net.TcpReceiver;
|
2022-07-05 19:49:04 +00:00
|
|
|
import nl.andrewl.record_net.Message;
|
|
|
|
import nl.andrewl.record_net.util.ExtendedDataInputStream;
|
|
|
|
import nl.andrewl.record_net.util.ExtendedDataOutputStream;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2022-07-06 18:20:15 +00:00
|
|
|
import java.net.DatagramPacket;
|
|
|
|
import java.net.DatagramSocket;
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.Socket;
|
2022-07-05 19:49:04 +00:00
|
|
|
|
2022-07-06 18:20:15 +00:00
|
|
|
public class ClientCommunicationHandler {
|
2022-07-05 19:49:04 +00:00
|
|
|
private final Server server;
|
|
|
|
private final Socket socket;
|
|
|
|
private final DatagramSocket datagramSocket;
|
|
|
|
private final ExtendedDataInputStream in;
|
|
|
|
private final ExtendedDataOutputStream out;
|
|
|
|
|
|
|
|
private InetAddress clientAddress;
|
|
|
|
private int clientUdpPort;
|
2022-07-06 18:20:15 +00:00
|
|
|
private Player player;
|
2022-07-05 19:49:04 +00:00
|
|
|
|
2022-07-06 18:20:15 +00:00
|
|
|
public ClientCommunicationHandler(Server server, Socket socket, DatagramSocket datagramSocket) throws IOException {
|
2022-07-05 19:49:04 +00:00
|
|
|
this.server = server;
|
|
|
|
this.socket = socket;
|
|
|
|
this.datagramSocket = datagramSocket;
|
|
|
|
this.in = Net.getInputStream(socket.getInputStream());
|
|
|
|
this.out = Net.getOutputStream(socket.getOutputStream());
|
2022-07-06 18:20:15 +00:00
|
|
|
establishConnection();
|
|
|
|
new Thread(new TcpReceiver(in, this::handleTcpMessage)).start();
|
2022-07-05 19:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void shutdown() {
|
2022-07-06 18:20:15 +00:00
|
|
|
try {
|
|
|
|
socket.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2022-07-05 19:49:04 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 18:20:15 +00:00
|
|
|
public void setClientUdpPort(int port) {
|
|
|
|
this.clientUdpPort = port;
|
2022-07-05 19:49:04 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 18:20:15 +00:00
|
|
|
private void handleTcpMessage(Message msg) {
|
|
|
|
System.out.println("Message received from client " + player.getUsername() + ": " + msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void establishConnection() throws IOException {
|
|
|
|
socket.setSoTimeout(1000);
|
2022-07-05 19:49:04 +00:00
|
|
|
boolean connectionEstablished = false;
|
|
|
|
int attempts = 0;
|
|
|
|
while (!connectionEstablished && attempts < 100) {
|
|
|
|
try {
|
|
|
|
Message msg = Net.read(in);
|
2022-07-05 21:34:54 +00:00
|
|
|
if (msg instanceof ConnectRequestMessage connectMsg) {
|
2022-07-06 18:20:15 +00:00
|
|
|
// Try to set the TCP timeout back to 0 now that we've got the correct request.
|
|
|
|
socket.setSoTimeout(0);
|
2022-07-05 19:49:04 +00:00
|
|
|
this.clientAddress = socket.getInetAddress();
|
|
|
|
System.out.println("Player connected: " + connectMsg.username());
|
|
|
|
connectionEstablished = true;
|
2022-07-06 18:20:15 +00:00
|
|
|
this.player = server.registerPlayer(this, connectMsg.username());
|
|
|
|
Net.write(new ConnectAcceptMessage(player.getId()), out);
|
2022-07-05 19:49:04 +00:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
attempts++;
|
|
|
|
}
|
|
|
|
if (!connectionEstablished) {
|
|
|
|
try {
|
2022-07-05 21:34:54 +00:00
|
|
|
Net.write(new ConnectRejectMessage("Too many connect attempts failed."), out);
|
2022-07-05 19:49:04 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
System.out.println("Player couldn't connect after " + attempts + " attempts. Aborting.");
|
2022-07-06 18:20:15 +00:00
|
|
|
socket.close();
|
2022-07-05 19:49:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 18:20:15 +00:00
|
|
|
public void sendDatagramPacket(Message msg) {
|
2022-07-05 19:49:04 +00:00
|
|
|
try {
|
|
|
|
sendDatagramPacket(Net.write(msg));
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 18:20:15 +00:00
|
|
|
public void sendDatagramPacket(byte[] data) {
|
2022-07-05 19:49:04 +00:00
|
|
|
DatagramPacket packet = new DatagramPacket(data, data.length, clientAddress, clientUdpPort);
|
|
|
|
sendDatagramPacket(packet);
|
|
|
|
}
|
|
|
|
|
2022-07-06 18:20:15 +00:00
|
|
|
public void sendDatagramPacket(DatagramPacket packet) {
|
2022-07-05 19:49:04 +00:00
|
|
|
try {
|
|
|
|
packet.setAddress(clientAddress);
|
|
|
|
packet.setPort(clientUdpPort);
|
|
|
|
datagramSocket.send(packet);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|