| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4ca967f commit a5bbd2a
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -45,6 +45,9 @@ | |||
| 45 | 45 | * @see_external LIB_net/clientEvent | |
| 46 | 46 | */ | |
| 47 | 47 | public class Client implements Runnable { | |
| 48 | + | ||
| 49 | + protected static final int MAX_BUFFER_SIZE = 2 << 27; // 128 MB | ||
| 50 | + | ||
| 48 | 51 | PApplet parent; | |
| 49 | 52 | Method clientEventMethod; | |
| 50 | 53 | Method disconnectEventMethod; | |
@@ -234,8 +237,6 @@ public void run() { | |||
| 234 | 237 | } | |
| 235 | 238 | ||
| 236 | 239 | synchronized (bufferLock) { | |
| 237 | - // todo: at some point buffer should stop increasing in size, | ||
| 238 | - // otherwise it could use up all the memory. | ||
| 239 | 240 | int freeBack = buffer.length - bufferLast; | |
| 240 | 241 | if (readCount > freeBack) { | |
| 241 | 242 | // not enough space at the back | |
@@ -245,6 +246,13 @@ public void run() { | |||
| 245 | 246 | // can't fit even after compacting, resize the buffer | |
| 246 | 247 | // find the next power of two which can fit everything in | |
| 247 | 248 | int newSize = Integer.highestOneBit(bufferLength + readCount - 1) << 1; | |
| 249 | + if (newSize > MAX_BUFFER_SIZE) { | ||
| 250 | + // buffer is full because client is not reading (fast enough) | ||
| 251 | + System.err.println("Client: can't receive more data, buffer is full. " + | ||
| 252 | + "Make sure you read the data from the client."); | ||
| 253 | + stop(); | ||
| 254 | + return; | ||
| 255 | + } | ||
| 248 | 256 | targetBuffer = new byte[newSize]; | |
| 249 | 257 | } | |
| 250 | 258 | // compact the buffer (either in-place or into the new bigger buffer) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments