|
24 | 24 | import java.io.PrintWriter;
|
25 | 25 | import java.net.Socket;
|
26 | 26 | import java.text.ParseException;
|
27 |
| -import java.util.Arrays; |
28 |
| -import java.util.Calendar; |
29 |
| -import java.util.List; |
30 |
| -import java.util.Locale; |
| 27 | +import java.util.*; |
31 | 28 | import java.util.concurrent.CopyOnWriteArrayList;
|
| 29 | +import java.util.concurrent.LinkedBlockingQueue; |
32 | 30 | import javax.swing.Timer;
|
33 | 31 | import jsgl.math.vector.Vec3f;
|
34 | 32 | import org.apache.logging.log4j.LogManager;
|
@@ -69,40 +67,79 @@ public MessageReceiver(String host, int port)
|
69 | 67 | @Override
|
70 | 68 | public void run()
|
71 | 69 | {
|
72 |
| - try { |
73 |
| - socket = new Socket(host, port); |
74 |
| - out = new PrintWriter(socket.getOutputStream(), true); |
75 |
| - in = new DataInputStream(socket.getInputStream()); |
76 |
| - |
77 |
| - setConnected(true); |
78 |
| - if (recordLogs) |
79 |
| - setupNewLogfile(); |
80 |
| - |
81 |
| - String message; |
82 |
| - do { |
83 |
| - message = readMessage(); |
84 |
| - if (message != null) { |
85 |
| - try { |
86 |
| - parser.parse(message); |
87 |
| - if (logfileOutput != null) |
88 |
| - writeToLogfile(message); |
89 |
| - } catch (ParseException e) { |
90 |
| - LOGGER.error("Unable to parse server message", e); |
91 |
| - } |
| 70 | + // Receive messages in a separate thread and put them in a queue |
| 71 | + LinkedBlockingQueue<Optional<String>> messages = new LinkedBlockingQueue<>(3000); |
| 72 | + new Thread(() -> { |
| 73 | + try { |
| 74 | + socket = new Socket(host, port); |
| 75 | + out = new PrintWriter(socket.getOutputStream(), true); |
| 76 | + in = new DataInputStream(socket.getInputStream()); |
| 77 | + setConnected(true); |
| 78 | + String message; |
| 79 | + do { |
| 80 | + message = readMessage(); |
| 81 | + messages.put(Optional.ofNullable(message)); |
| 82 | + } while (message != null); |
| 83 | + } catch (IOException | InterruptedException e) { |
| 84 | + // This is fine, just leave. |
| 85 | + // The surrounding thread will quit when receiving an |
| 86 | + // empty optional. |
| 87 | + try { |
| 88 | + messages.put(Optional.empty()); |
| 89 | + } catch (InterruptedException ignored) { |
92 | 90 | }
|
93 |
| - } while (message != null); |
| 91 | + } |
94 | 92 |
|
95 | 93 | // If the thread gets to this point the server has stopped
|
96 | 94 | // sending messages by closing the connection
|
97 | 95 | // DebugInfo.println(getClass(), "rcssserver3d closed TCP connection");
|
98 | 96 | disconnect();
|
99 | 97 | if (autoConnectTimer != null)
|
100 | 98 | autoConnectTimer.start();
|
101 |
| - } catch (IOException e) { |
102 |
| - disconnect(); |
103 |
| - if (autoConnectTimer != null) |
104 |
| - autoConnectTimer.start(); |
| 99 | + }).start(); |
| 100 | + |
| 101 | + if (recordLogs) { |
| 102 | + setupNewLogfile(); |
105 | 103 | }
|
| 104 | + |
| 105 | + Optional<String> message = Optional.empty(); |
| 106 | + long lastUpdateTimestamp = 0; |
| 107 | + long monitorStepMillis = Math.round(Networking.INSTANCE.getMonitorStep() * 1000.0); |
| 108 | + do { |
| 109 | + // Retrieve a message from the queue |
| 110 | + try { |
| 111 | + message = messages.take(); |
| 112 | + } catch (InterruptedException e) { |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + if (Networking.INSTANCE.getUseBuffer()) { |
| 117 | + // Wait until the time for one monitor frame elapsed. |
| 118 | + // This ensures that two messages are not applied immediately after each |
| 119 | + // other. That may be the case with a bad network connection. |
| 120 | + long elapsed = System.currentTimeMillis() - lastUpdateTimestamp; |
| 121 | + long timeLeft = monitorStepMillis - elapsed; |
| 122 | + if (timeLeft > 0) { |
| 123 | + try { |
| 124 | + Thread.sleep(timeLeft); |
| 125 | + } catch (InterruptedException e) { |
| 126 | + e.printStackTrace(); |
| 127 | + } |
| 128 | + } |
| 129 | + lastUpdateTimestamp = System.currentTimeMillis(); |
| 130 | + } |
| 131 | + |
| 132 | + // Process message |
| 133 | + message.ifPresent(msg -> { |
| 134 | + try { |
| 135 | + parser.parse(msg); |
| 136 | + if (logfileOutput != null) |
| 137 | + writeToLogfile(msg); |
| 138 | + } catch (ParseException e) { |
| 139 | + LOGGER.error("Unable to parse server message", e); |
| 140 | + } |
| 141 | + }); |
| 142 | + } while (message.isPresent()); |
106 | 143 | }
|
107 | 144 |
|
108 | 145 | private String readMessage() throws IOException
|
|
0 commit comments