Skip to content

Commit 83d136a

Browse files
authored
Merge branch 'develop' into bugfix/5992-broken-fow
2 parents 426e6fe + ebd9e82 commit 83d136a

35 files changed

Lines changed: 1246 additions & 301 deletions

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ ext {
9393
}
9494

9595
def javaArgs = [
96-
"-Xss8M", "-Dsun.java2d.d3d=false", "-Dfile.encoding=UTF-8",
96+
"-Xss8M", "-Dsun.java2d.d3d=false",
9797
"-Dpolyglot.engine.WarnInterpreterOnly=false",
9898
"-Djava.util.Arrays.useLegacyMergeSort=true",
99-
"-DMAPTOOL_DATADIR=.maptool-" + vendor.toLowerCase(), "-XX:+ShowCodeDetailsInExceptionMessages",
99+
"-DMAPTOOL_DATADIR=.maptool-" + vendor.toLowerCase(),
100100
"--add-opens=java.desktop/java.awt=ALL-UNNAMED", "--add-opens=java.desktop/java.awt.geom=ALL-UNNAMED",
101101
"--add-opens=java.desktop/sun.awt.geom=ALL-UNNAMED", "--add-opens=java.base/java.util=ALL-UNNAMED",
102102
"--add-opens=javafx.web/javafx.scene.web=ALL-UNNAMED", "--add-opens=javafx.web/com.sun.webkit=ALL-UNNAMED",

clientserver/src/main/java/net/rptools/clientserver/simple/connection/AbstractConnection.java

Lines changed: 10 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package net.rptools.clientserver.simple.connection;
1616

1717
import java.io.*;
18-
import java.nio.ByteBuffer;
1918
import java.util.List;
2019
import java.util.concurrent.BlockingQueue;
2120
import java.util.concurrent.CopyOnWriteArrayList;
@@ -33,13 +32,23 @@
3332
public abstract class AbstractConnection implements Connection {
3433
private static final Logger log = LogManager.getLogger(AbstractConnection.class);
3534

35+
private final String id;
3636
private final AtomicBoolean closed = new AtomicBoolean(false);
3737
private final BlockingQueue<byte[]> outQueue = new LinkedBlockingQueue<>();
3838

3939
private final List<DisconnectHandler> disconnectHandlers = new CopyOnWriteArrayList<>();
4040
private final List<ActivityListener> listeners = new CopyOnWriteArrayList<>();
4141
private final List<MessageHandler> messageHandlers = new CopyOnWriteArrayList<>();
4242

43+
protected AbstractConnection(String id) {
44+
this.id = id;
45+
}
46+
47+
@Override
48+
public final String getId() {
49+
return id;
50+
}
51+
4352
@Override
4453
public final void close() {
4554
if (closed.compareAndSet(false, true)) {
@@ -115,95 +124,6 @@ protected final void dispatchCompressedMessage(byte[] compressedMessage) {
115124
dispatchMessage(message);
116125
}
117126

118-
protected final void writeMessage(OutputStream out, byte[] message) throws IOException {
119-
int length = message.length;
120-
121-
notifyListeners(ActivityListener.Direction.Outbound, ActivityListener.State.Start, length, 0);
122-
123-
out.write(length >> 24);
124-
out.write(length >> 16);
125-
out.write(length >> 8);
126-
out.write(length);
127-
128-
for (int i = 0; i < message.length; i++) {
129-
out.write(message[i]);
130-
131-
if (i != 0 && i % ActivityListener.CHUNK_SIZE == 0) {
132-
notifyListeners(
133-
ActivityListener.Direction.Outbound, ActivityListener.State.Progress, length, i);
134-
}
135-
}
136-
out.flush();
137-
notifyListeners(
138-
ActivityListener.Direction.Outbound, ActivityListener.State.Complete, length, length);
139-
}
140-
141-
protected final byte[] readMessage(InputStream in) throws IOException {
142-
int b32 = in.read();
143-
int b24 = in.read();
144-
int b16 = in.read();
145-
int b8 = in.read();
146-
147-
if (b32 < 0) {
148-
throw new IOException("Stream closed");
149-
}
150-
int length = (b32 << 24) + (b24 << 16) + (b16 << 8) + b8;
151-
152-
notifyListeners(ActivityListener.Direction.Inbound, ActivityListener.State.Start, length, 0);
153-
154-
byte[] ret = new byte[length];
155-
for (int i = 0; i < length; i++) {
156-
ret[i] = (byte) in.read();
157-
158-
if (i != 0 && i % ActivityListener.CHUNK_SIZE == 0) {
159-
notifyListeners(
160-
ActivityListener.Direction.Inbound, ActivityListener.State.Progress, length, i);
161-
}
162-
}
163-
notifyListeners(
164-
ActivityListener.Direction.Inbound, ActivityListener.State.Complete, length, length);
165-
return ret;
166-
}
167-
168-
private ByteBuffer messageBuffer = null;
169-
170-
protected final byte[] readMessage(ByteBuffer part) {
171-
if (messageBuffer == null) {
172-
int length = part.getInt();
173-
notifyListeners(ActivityListener.Direction.Inbound, ActivityListener.State.Start, length, 0);
174-
175-
if (part.remaining() == length) {
176-
var ret = new byte[length];
177-
part.get(ret);
178-
notifyListeners(
179-
ActivityListener.Direction.Inbound, ActivityListener.State.Complete, length, length);
180-
return ret;
181-
}
182-
183-
messageBuffer = ByteBuffer.allocate(length);
184-
}
185-
186-
messageBuffer.put(part);
187-
notifyListeners(
188-
ActivityListener.Direction.Inbound,
189-
ActivityListener.State.Progress,
190-
messageBuffer.capacity(),
191-
messageBuffer.position());
192-
193-
if (messageBuffer.capacity() == messageBuffer.position()) {
194-
notifyListeners(
195-
ActivityListener.Direction.Inbound,
196-
ActivityListener.State.Complete,
197-
messageBuffer.capacity(),
198-
messageBuffer.capacity());
199-
var ret = messageBuffer.array();
200-
messageBuffer = null;
201-
return ret;
202-
}
203-
204-
return null;
205-
}
206-
207127
protected final void fireDisconnect() {
208128
for (DisconnectHandler handler : disconnectHandlers) {
209129
handler.handleDisconnect(this);

clientserver/src/main/java/net/rptools/clientserver/simple/connection/DirectConnection.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public static Pair create(String id) {
4040
}
4141

4242
private final AtomicBoolean sharedClosedFlag;
43-
private final String id;
4443
private final BlockingQueue<byte[]> writeQueue;
4544
private final ReceiveThread receiveThread;
4645

@@ -49,8 +48,8 @@ private DirectConnection(
4948
String id,
5049
BlockingQueue<byte[]> writeQueue,
5150
BlockingQueue<byte[]> readQueue) {
51+
super(id);
5252
this.sharedClosedFlag = sharedClosedFlag;
53-
this.id = id;
5453
this.writeQueue = writeQueue;
5554
this.receiveThread = new ReceiveThread(readQueue);
5655
}
@@ -89,11 +88,6 @@ public boolean isAlive() {
8988
return !sharedClosedFlag.get();
9089
}
9190

92-
@Override
93-
public String getId() {
94-
return id;
95-
}
96-
9791
@Override
9892
public String getError() {
9993
return null;

clientserver/src/main/java/net/rptools/clientserver/simple/connection/SocketConnection.java

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.io.*;
1818
import java.net.Socket;
1919
import java.net.SocketTimeoutException;
20+
import net.rptools.clientserver.ActivityListener;
2021
import org.apache.logging.log4j.LogManager;
2122
import org.apache.logging.log4j.Logger;
2223

@@ -27,32 +28,29 @@ public class SocketConnection extends AbstractConnection implements Connection {
2728
/** Instance used for log messages. */
2829
private static final Logger log = LogManager.getLogger(SocketConnection.class);
2930

30-
private final String id;
31+
// Only valid for open connections.
3132
private SendThread send;
3233
private ReceiveThread receive;
3334
private Socket socket;
35+
36+
// Only valid for pending connections before #open() is called.
3437
private String hostName;
3538
private int port;
3639

3740
public SocketConnection(String id, String hostName, int port) {
38-
this.id = id;
41+
super(id);
3942
this.hostName = hostName;
4043
this.port = port;
4144
}
4245

4346
public SocketConnection(String id, Socket socket) {
44-
this.id = id;
45-
this.socket = socket;
46-
47-
initialize(socket);
47+
super(id);
48+
connect(socket);
4849
}
4950

50-
@Override
51-
public String getId() {
52-
return id;
53-
}
51+
private void connect(Socket socket) {
52+
assert this.socket != null : "Should only call #connect() not already open";
5453

55-
private void initialize(Socket socket) {
5654
this.socket = socket;
5755
this.send = new SendThread(socket);
5856
this.receive = new ReceiveThread(socket);
@@ -63,7 +61,11 @@ private void initialize(Socket socket) {
6361

6462
@Override
6563
public void open() throws IOException {
66-
initialize(new Socket(hostName, port));
64+
if (this.socket != null) {
65+
throw new IOException("The connection has already been opened.");
66+
}
67+
68+
connect(new Socket(hostName, port));
6769
}
6870

6971
@Override
@@ -73,6 +75,11 @@ public void sendMessage(Object channel, byte[] message) {
7375

7476
@Override
7577
protected void onClose() {
78+
if (socket == null) {
79+
// Not open, so nothing to do.
80+
return;
81+
}
82+
7683
receive.interrupt();
7784
send.interrupt();
7885

@@ -85,7 +92,7 @@ protected void onClose() {
8592

8693
@Override
8794
public boolean isAlive() {
88-
return !socket.isClosed();
95+
return socket != null && !socket.isClosed();
8996
}
9097

9198
@Override
@@ -124,7 +131,7 @@ public void run() {
124131
}
125132

126133
try {
127-
SocketConnection.this.writeMessage(out, message);
134+
writeMessage(out, message);
128135
} catch (IOException e) {
129136
log.error("Error while writing message. Closing connection.", e);
130137
return;
@@ -134,6 +141,29 @@ public void run() {
134141
SocketConnection.this.close();
135142
}
136143
}
144+
145+
protected final void writeMessage(OutputStream out, byte[] message) throws IOException {
146+
int length = message.length;
147+
148+
notifyListeners(ActivityListener.Direction.Outbound, ActivityListener.State.Start, length, 0);
149+
150+
out.write(length >> 24);
151+
out.write(length >> 16);
152+
out.write(length >> 8);
153+
out.write(length);
154+
155+
for (int i = 0; i < message.length; i++) {
156+
out.write(message[i]);
157+
158+
if (i != 0 && i % ActivityListener.CHUNK_SIZE == 0) {
159+
notifyListeners(
160+
ActivityListener.Direction.Outbound, ActivityListener.State.Progress, length, i);
161+
}
162+
}
163+
out.flush();
164+
notifyListeners(
165+
ActivityListener.Direction.Outbound, ActivityListener.State.Complete, length, length);
166+
}
137167
}
138168

139169
// /////////////////////////////////////////////////////////////////////////
@@ -160,7 +190,7 @@ public void run() {
160190

161191
while (!SocketConnection.this.isClosed() && SocketConnection.this.isAlive()) {
162192
try {
163-
byte[] message = SocketConnection.this.readMessage(in);
193+
byte[] message = readMessage(in);
164194
SocketConnection.this.dispatchCompressedMessage(message);
165195
} catch (SocketTimeoutException e) {
166196
log.warn("Lost client {}", SocketConnection.this.getId(), e);
@@ -178,5 +208,32 @@ public void run() {
178208
fireDisconnect();
179209
}
180210
}
211+
212+
private byte[] readMessage(InputStream in) throws IOException {
213+
int b32 = in.read();
214+
int b24 = in.read();
215+
int b16 = in.read();
216+
int b8 = in.read();
217+
218+
if (b32 < 0) {
219+
throw new IOException("Stream closed");
220+
}
221+
int length = (b32 << 24) + (b24 << 16) + (b16 << 8) + b8;
222+
223+
notifyListeners(ActivityListener.Direction.Inbound, ActivityListener.State.Start, length, 0);
224+
225+
byte[] ret = new byte[length];
226+
for (int i = 0; i < length; i++) {
227+
ret[i] = (byte) in.read();
228+
229+
if (i != 0 && i % ActivityListener.CHUNK_SIZE == 0) {
230+
notifyListeners(
231+
ActivityListener.Direction.Inbound, ActivityListener.State.Progress, length, i);
232+
}
233+
}
234+
notifyListeners(
235+
ActivityListener.Direction.Inbound, ActivityListener.State.Complete, length, length);
236+
return ret;
237+
}
181238
}
182239
}

0 commit comments

Comments
 (0)