| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c9eab5c commit bae41fa
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -49,7 +49,7 @@ public class Client implements Runnable { | |||
| 49 | 49 | Method clientEventMethod; | |
| 50 | 50 | Method disconnectEventMethod; | |
| 51 | 51 | ||
| 52 | - Thread thread; | ||
| 52 | + volatile Thread thread; | ||
| 53 | 53 | Socket socket; | |
| 54 | 54 | int port; | |
| 55 | 55 | String host; | |
@@ -308,7 +308,9 @@ public String ip() { | |||
| 308 | 308 | * @brief Returns the number of bytes in the buffer waiting to be read | |
| 309 | 309 | */ | |
| 310 | 310 | public int available() { | |
| 311 | - return (bufferLast - bufferIndex); | ||
| 311 | + synchronized (bufferLock) { | ||
| 312 | + return (bufferLast - bufferIndex); | ||
| 313 | + } | ||
| 312 | 314 | } | |
| 313 | 315 | ||
| 314 | 316 | ||
@@ -323,8 +325,10 @@ public int available() { | |||
| 323 | 325 | * @brief Clears the buffer | |
| 324 | 326 | */ | |
| 325 | 327 | public void clear() { | |
| 326 | - bufferLast = 0; | ||
| 327 | - bufferIndex = 0; | ||
| 328 | + synchronized (bufferLock) { | ||
| 329 | + bufferLast = 0; | ||
| 330 | + bufferIndex = 0; | ||
| 331 | + } | ||
| 328 | 332 | } | |
| 329 | 333 | ||
| 330 | 334 | ||
@@ -341,9 +345,9 @@ public void clear() { | |||
| 341 | 345 | * @brief Returns a value from the buffer | |
| 342 | 346 | */ | |
| 343 | 347 | public int read() { | |
| 344 | - if (bufferIndex == bufferLast) return -1; | ||
| 345 | - | ||
| 346 | 348 | synchronized (bufferLock) { | |
| 349 | + if (bufferIndex == bufferLast) return -1; | ||
| 350 | + | ||
| 347 | 351 | int outgoing = buffer[bufferIndex++] & 0xff; | |
| 348 | 352 | if (bufferIndex == bufferLast) { // rewind | |
| 349 | 353 | bufferIndex = 0; | |
@@ -366,8 +370,10 @@ public int read() { | |||
| 366 | 370 | * @brief Returns the next byte in the buffer as a char | |
| 367 | 371 | */ | |
| 368 | 372 | 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 | + } | ||
| 371 | 377 | } | |
| 372 | 378 | ||
| 373 | 379 | ||
@@ -394,9 +400,9 @@ public char readChar() { | |||
| 394 | 400 | * @brief Reads everything in the buffer | |
| 395 | 401 | */ | |
| 396 | 402 | public byte[] readBytes() { | |
| 397 | - if (bufferIndex == bufferLast) return null; | ||
| 398 | - | ||
| 399 | 403 | synchronized (bufferLock) { | |
| 404 | + if (bufferIndex == bufferLast) return null; | ||
| 405 | + | ||
| 400 | 406 | int length = bufferLast - bufferIndex; | |
| 401 | 407 | byte outgoing[] = new byte[length]; | |
| 402 | 408 | System.arraycopy(buffer, bufferIndex, outgoing, 0, length); | |
@@ -419,9 +425,9 @@ public byte[] readBytes() { | |||
| 419 | 425 | * @param max the maximum number of bytes to read | |
| 420 | 426 | */ | |
| 421 | 427 | public byte[] readBytes(int max) { | |
| 422 | - if (bufferIndex == bufferLast) return null; | ||
| 423 | - | ||
| 424 | 428 | synchronized (bufferLock) { | |
| 429 | + if (bufferIndex == bufferLast) return null; | ||
| 430 | + | ||
| 425 | 431 | int length = bufferLast - bufferIndex; | |
| 426 | 432 | if (length > max) length = max; | |
| 427 | 433 | byte outgoing[] = new byte[length]; | |
@@ -451,9 +457,9 @@ public byte[] readBytes(int max) { | |||
| 451 | 457 | * @param bytebuffer passed in byte array to be altered | |
| 452 | 458 | */ | |
| 453 | 459 | public int readBytes(byte bytebuffer[]) { | |
| 454 | - if (bufferIndex == bufferLast) return 0; | ||
| 455 | - | ||
| 456 | 460 | synchronized (bufferLock) { | |
| 461 | + if (bufferIndex == bufferLast) return 0; | ||
| 462 | + | ||
| 457 | 463 | int length = bufferLast - bufferIndex; | |
| 458 | 464 | if (length > bytebuffer.length) length = bytebuffer.length; | |
| 459 | 465 | System.arraycopy(buffer, bufferIndex, bytebuffer, 0, length); | |
@@ -489,10 +495,11 @@ public int readBytes(byte bytebuffer[]) { | |||
| 489 | 495 | * @param interesting character designated to mark the end of the data | |
| 490 | 496 | */ | |
| 491 | 497 | public byte[] readBytesUntil(int interesting) { | |
| 492 | - if (bufferIndex == bufferLast) return null; | ||
| 493 | 498 | byte what = (byte)interesting; | |
| 494 | 499 | ||
| 495 | 500 | synchronized (bufferLock) { | |
| 501 | + if (bufferIndex == bufferLast) return null; | ||
| 502 | + | ||
| 496 | 503 | int found = -1; | |
| 497 | 504 | for (int k = bufferIndex; k < bufferLast; k++) { | |
| 498 | 505 | if (buffer[k] == what) { | |
@@ -530,10 +537,11 @@ public byte[] readBytesUntil(int interesting) { | |||
| 530 | 537 | * @param byteBuffer passed in byte array to be altered | |
| 531 | 538 | */ | |
| 532 | 539 | public int readBytesUntil(int interesting, byte byteBuffer[]) { | |
| 533 | - if (bufferIndex == bufferLast) return 0; | ||
| 534 | 540 | byte what = (byte)interesting; | |
| 535 | 541 | ||
| 536 | 542 | synchronized (bufferLock) { | |
| 543 | + if (bufferIndex == bufferLast) return 0; | ||
| 544 | + | ||
| 537 | 545 | int found = -1; | |
| 538 | 546 | for (int k = bufferIndex; k < bufferLast; k++) { | |
| 539 | 547 | if (buffer[k] == what) { | |
@@ -578,8 +586,10 @@ public int readBytesUntil(int interesting, byte byteBuffer[]) { | |||
| 578 | 586 | * @brief Returns the buffer as a String | |
| 579 | 587 | */ | |
| 580 | 588 | 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 | + } | ||
| 583 | 593 | } | |
| 584 | 594 | ||
| 585 | 595 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments