Skip to content

Commit a5bbd2a

Browse files
committed
Net-client: set max buffer size
1 parent 4ca967f commit a5bbd2a

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
* @see_external LIB_net/clientEvent
4646
*/
4747
public class Client implements Runnable {
48+
49+
protected static final int MAX_BUFFER_SIZE = 2 << 27; // 128 MB
50+
4851
PApplet parent;
4952
Method clientEventMethod;
5053
Method disconnectEventMethod;
@@ -234,8 +237,6 @@ public void run() {
234237
}
235238

236239
synchronized (bufferLock) {
237-
// todo: at some point buffer should stop increasing in size,
238-
// otherwise it could use up all the memory.
239240
int freeBack = buffer.length - bufferLast;
240241
if (readCount > freeBack) {
241242
// not enough space at the back
@@ -245,6 +246,13 @@ public void run() {
245246
// can't fit even after compacting, resize the buffer
246247
// find the next power of two which can fit everything in
247248
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+
}
248256
targetBuffer = new byte[newSize];
249257
}
250258
// compact the buffer (either in-place or into the new bigger buffer)

0 commit comments

Comments
 (0)