This commit is contained in:
Anuken
2019-12-22 22:04:41 -05:00
parent adb12dcbb8
commit b806a22a0a
@@ -55,6 +55,7 @@ public class ServerControl implements ApplicationListener{
private @Nullable Map nextMapOverride; private @Nullable Map nextMapOverride;
private Thread socketThread; private Thread socketThread;
private ServerSocket serverSocket;
private PrintWriter socketOutput; private PrintWriter socketOutput;
public ServerControl(String[] args){ public ServerControl(String[] args){
@@ -964,33 +965,39 @@ public class ServerControl implements ApplicationListener{
if(on && socketThread == null){ if(on && socketThread == null){
socketThread = new Thread(() -> { socketThread = new Thread(() -> {
try{ try{
try(ServerSocket socket = new ServerSocket()){ serverSocket = new ServerSocket();
socket.bind(new InetSocketAddress("localhost", commandSocketPort)); serverSocket.bind(new InetSocketAddress("localhost", commandSocketPort));
while(true){ while(true){
Socket client = socket.accept(); Socket client = serverSocket.accept();
info("&lmRecieved command socket connection: &lb{0}", socket.getLocalSocketAddress()); info("&lmRecieved command socket connection: &lb{0}", serverSocket.getLocalSocketAddress());
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
socketOutput = new PrintWriter(client.getOutputStream(), true); socketOutput = new PrintWriter(client.getOutputStream(), true);
String line; String line;
while(client.isConnected() && (line = in.readLine()) != null){ while(client.isConnected() && (line = in.readLine()) != null){
String result = line; String result = line;
Core.app.post(() -> handleCommandString(result)); Core.app.post(() -> handleCommandString(result));
}
info("&lmLost command socket connection: &lb{0}", socket.getLocalSocketAddress());
socketOutput = null;
} }
info("&lmLost command socket connection: &lb{0}", serverSocket.getLocalSocketAddress());
socketOutput = null;
} }
}catch(BindException b){ }catch(BindException b){
err("Command input socket already in use. Is another instance of the server running?"); err("Command input socket already in use. Is another instance of the server running?");
}catch(IOException e){ }catch(IOException e){
err("Terminating socket server."); if(!e.getMessage().equals("Socket closed")){
e.printStackTrace(); err("Terminating socket server.");
e.printStackTrace();
}
} }
}); });
socketThread.setDaemon(true); socketThread.setDaemon(true);
socketThread.start(); socketThread.start();
}else if(socketThread != null){ }else if(socketThread != null){
socketThread.interrupt(); socketThread.interrupt();
try{
serverSocket.close();
}catch(IOException e){
e.printStackTrace();
}
socketThread = null; socketThread = null;
socketOutput = null; socketOutput = null;
} }