Do not bog down the socket with updates. Instead run actions in separate thread.

master
sigonasr2 2 years ago
parent 11261a9b73
commit 32f057dc88
  1. 115
      sig/App.java

@ -5,6 +5,11 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -124,18 +129,6 @@ class Client {
// takes input from terminal // takes input from terminal
input = new DataInputStream(System.in); input = new DataInputStream(System.in);
Timer updateTimer=new Timer();
updateTimer.schedule(new TimerTask(){
@Override
public void run() {
try {
out.writeUTF("UPDATE");
} catch (IOException e) {
e.printStackTrace();
}
}
},100,100);
// sends output to the socket // sends output to the socket
out = new DataOutputStream( out = new DataOutputStream(
socket.getOutputStream()); socket.getOutputStream());
@ -299,6 +292,36 @@ class Server
} }
} }
Collection<String>commandQueue=Collections.synchronizedCollection(new ArrayList<>());
// constructor with port
public Server(int port)
{
try {
r = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
// starts server and waits for a connection
try
{
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("Client accepted");
// takes input from the client socket
in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
String line = "";
Thread t = new Thread(new Runnable(){
void run(Rotation rot){ void run(Rotation rot){
switch (rot){ switch (rot){
case SCHOLAR:{ case SCHOLAR:{
@ -368,39 +391,11 @@ class Server
} }
} }
// constructor with port @Override
public Server(int port) public void run() {
{ while (true){
try { for (String command:commandQueue){
r = new Robot(); switch (command){
} catch (AWTException e) {
e.printStackTrace();
}
// starts server and waits for a connection
try
{
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("Client accepted");
// takes input from the client socket
in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
String line = "";
// reads message from client until "Over" is sent
while (!line.equals("Over"))
{
try
{
line = in.readUTF();
System.out.println("Received: "+line);
switch (line){
case "FOLLOW":{ case "FOLLOW":{
PressKeyWithModifier(KeyEvent.VK_CONTROL,KeyEvent.VK_7); PressKeyWithModifier(KeyEvent.VK_CONTROL,KeyEvent.VK_7);
}break; }break;
@ -455,14 +450,10 @@ class Server
}break; }break;
default:{ default:{
System.out.println("Unknown command: "+line); System.out.println("Unknown command: "+command);
} }
} }
} throw new UnsupportedOperationException("Unimplemented method 'run'");
catch(IOException i)
{
System.out.println(i);
break;
} }
if (queueUpSprint&&nextActionTimer==0){ if (queueUpSprint&&nextActionTimer==0){
@ -484,6 +475,28 @@ class Server
whisperingDawnTimer=Math.max(0,whisperingDawnTimer-difference); whisperingDawnTimer=Math.max(0,whisperingDawnTimer-difference);
feyIlluminationTimer=Math.max(0,feyIlluminationTimer-difference); feyIlluminationTimer=Math.max(0,feyIlluminationTimer-difference);
lastFrame=System.currentTimeMillis(); lastFrame=System.currentTimeMillis();
r.delay(100);
}
}
});
t.start();
// reads message from client until "Over" is sent
while (!line.equals("Over"))
{
try
{
line = in.readUTF();
System.out.println("Received: "+line);
commandQueue.add(line);
}
catch(IOException i)
{
System.out.println(i);
break;
}
} }
System.out.println("Closing connection"); System.out.println("Closing connection");

Loading…
Cancel
Save