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.net.ServerSocket;
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.TimerTask;
import java.util.Map.Entry;
@ -124,18 +129,6 @@ class Client {
// takes input from terminal
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
out = new DataOutputStream(
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){
switch (rot){
case SCHOLAR:{
@ -368,39 +391,11 @@ class Server
}
}
// 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 = "";
// reads message from client until "Over" is sent
while (!line.equals("Over"))
{
try
{
line = in.readUTF();
System.out.println("Received: "+line);
switch (line){
@Override
public void run() {
while (true){
for (String command:commandQueue){
switch (command){
case "FOLLOW":{
PressKeyWithModifier(KeyEvent.VK_CONTROL,KeyEvent.VK_7);
}break;
@ -455,14 +450,10 @@ class Server
}break;
default:{
System.out.println("Unknown command: "+line);
System.out.println("Unknown command: "+command);
}
}
}
catch(IOException i)
{
System.out.println(i);
break;
throw new UnsupportedOperationException("Unimplemented method 'run'");
}
if (queueUpSprint&&nextActionTimer==0){
@ -484,6 +475,28 @@ class Server
whisperingDawnTimer=Math.max(0,whisperingDawnTimer-difference);
feyIlluminationTimer=Math.max(0,feyIlluminationTimer-difference);
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");

Loading…
Cancel
Save