Skip to content

Commit bae41fa

Browse files
committed
Net-client: synchronize access to all concurrently modified fields
1 parent c9eab5c commit bae41fa

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

java/libraries/net/src/processing/net/Client.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class Client implements Runnable {
4949
Method clientEventMethod;
5050
Method disconnectEventMethod;
5151

52-
Thread thread;
52+
volatile Thread thread;
5353
Socket socket;
5454
int port;
5555
String host;
@@ -308,7 +308,9 @@ public String ip() {
308308
* @brief Returns the number of bytes in the buffer waiting to be read
309309
*/
310310
public int available() {
311-
return (bufferLast - bufferIndex);
311+
synchronized (bufferLock) {
312+
return (bufferLast - bufferIndex);
313+
}
312314
}
313315

314316

@@ -323,8 +325,10 @@ public int available() {
323325
* @brief Clears the buffer
324326
*/
325327
public void clear() {
326-
bufferLast = 0;
327-
bufferIndex = 0;
328+
synchronized (bufferLock) {
329+
bufferLast = 0;
330+
bufferIndex = 0;
331+
}
328332
}
329333

330334

@@ -341,9 +345,9 @@ public void clear() {
341345
* @brief Returns a value from the buffer
342346
*/
343347
public int read() {
344-
if (bufferIndex == bufferLast) return -1;
345-
346348
synchronized (bufferLock) {
349+
if (bufferIndex == bufferLast) return -1;
350+
347351
int outgoing = buffer[bufferIndex++] & 0xff;
348352
if (bufferIndex == bufferLast) { // rewind
349353
bufferIndex = 0;
@@ -366,8 +370,10 @@ public int read() {
366370
* @brief Returns the next byte in the buffer as a char
367371
*/
368372
public char readChar() {
369-
if (bufferIndex == bufferLast) return (char)(-1);
370-
return (char) read();
373+
synchronized (bufferLock) {
374+
if (bufferIndex == bufferLast) return (char) (-1);
375+
return (char) read();
376+
}
371377
}
372378

373379

@@ -394,9 +400,9 @@ public char readChar() {
394400
* @brief Reads everything in the buffer
395401
*/
396402
public byte[] readBytes() {
397-
if (bufferIndex == bufferLast) return null;
398-
399403
synchronized (bufferLock) {
404+
if (bufferIndex == bufferLast) return null;
405+
400406
int length = bufferLast - bufferIndex;
401407
byte outgoing[] = new byte[length];
402408
System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
@@ -419,9 +425,9 @@ public byte[] readBytes() {
419425
* @param max the maximum number of bytes to read
420426
*/
421427
public byte[] readBytes(int max) {
422-
if (bufferIndex == bufferLast) return null;
423-
424428
synchronized (bufferLock) {
429+
if (bufferIndex == bufferLast) return null;
430+
425431
int length = bufferLast - bufferIndex;
426432
if (length > max) length = max;
427433
byte outgoing[] = new byte[length];
@@ -451,9 +457,9 @@ public byte[] readBytes(int max) {
451457
* @param bytebuffer passed in byte array to be altered
452458
*/
453459
public int readBytes(byte bytebuffer[]) {
454-
if (bufferIndex == bufferLast) return 0;
455-
456460
synchronized (bufferLock) {
461+
if (bufferIndex == bufferLast) return 0;
462+
457463
int length = bufferLast - bufferIndex;
458464
if (length > bytebuffer.length) length = bytebuffer.length;
459465
System.arraycopy(buffer, bufferIndex, bytebuffer, 0, length);
@@ -489,10 +495,11 @@ public int readBytes(byte bytebuffer[]) {
489495
* @param interesting character designated to mark the end of the data
490496
*/
491497
public byte[] readBytesUntil(int interesting) {
492-
if (bufferIndex == bufferLast) return null;
493498
byte what = (byte)interesting;
494499

495500
synchronized (bufferLock) {
501+
if (bufferIndex == bufferLast) return null;
502+
496503
int found = -1;
497504
for (int k = bufferIndex; k < bufferLast; k++) {
498505
if (buffer[k] == what) {
@@ -530,10 +537,11 @@ public byte[] readBytesUntil(int interesting) {
530537
* @param byteBuffer passed in byte array to be altered
531538
*/
532539
public int readBytesUntil(int interesting, byte byteBuffer[]) {
533-
if (bufferIndex == bufferLast) return 0;
534540
byte what = (byte)interesting;
535541

536542
synchronized (bufferLock) {
543+
if (bufferIndex == bufferLast) return 0;
544+
537545
int found = -1;
538546
for (int k = bufferIndex; k < bufferLast; k++) {
539547
if (buffer[k] == what) {
@@ -578,8 +586,10 @@ public int readBytesUntil(int interesting, byte byteBuffer[]) {
578586
* @brief Returns the buffer as a String
579587
*/
580588
public String readString() {
581-
if (bufferIndex == bufferLast) return null;
582-
return new String(readBytes());
589+
synchronized (bufferLock) {
590+
if (bufferIndex == bufferLast) return null;
591+
return new String(readBytes());
592+
}
583593
}
584594

585595

0 commit comments

Comments
 (0)