Skip to content

Commit 527311c

Browse files
committed
Restoring "getBufferedAmount" in WSClient.
Synchronize access to the "bufferedTotalQueueAmount" variable.
1 parent ce3ede1 commit 527311c

3 files changed

Lines changed: 40 additions & 8 deletions

File tree

‎src/org/java_websocket/WebSocket.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ public enum Role {
8989
* Queue of buffers that need to be sent to the client.
9090
*/
9191
private BlockingQueue<ByteBuffer> bufferQueue;
92+
/**
93+
* The amount of bytes still in queue to be sent, at every given time.
94+
* It's updated at every send/sent operation.
95+
*/
96+
private long bufferQueueTotalAmount = 0;
9297

9398
private Draft draft = null;
9499

@@ -470,6 +475,15 @@ boolean hasBufferedData() {
470475
return !this.bufferQueue.isEmpty();
471476
}
472477

478+
/**
479+
* The amount of data in Queue, ready to be sent.
480+
*
481+
* @return Amount of Data still in Queue and not sent yet of the socket
482+
*/
483+
long bufferedDataAmount() {
484+
return bufferQueueTotalAmount;
485+
}
486+
473487
/**
474488
* Empty the internal buffer, sending all the pending data before continuing.
475489
*/
@@ -480,6 +494,10 @@ public void flush() throws IOException {
480494
if( buffer.remaining() > 0 ) {
481495
continue;
482496
} else {
497+
synchronized (this) {
498+
// subtract this amount of data from the total queued (synchronized over this object)
499+
bufferQueueTotalAmount -= buffer.limit();
500+
}
483501
this.bufferQueue.poll(); // Buffer finished. Remove it.
484502
buffer = this.bufferQueue.peek();
485503
}
@@ -527,6 +545,10 @@ private void channelWrite( ByteBuffer buf ) throws InterruptedException {
527545
if( DEBUG )
528546
System.out.println( "write(" + buf.limit() + "): {" + ( buf.limit() > 1000 ? "too big to display" : new String( buf.array() ) ) + "}" );
529547
buf.rewind();
548+
synchronized (this) {
549+
// add up the number of bytes to the total queued (synchronized over this object)
550+
bufferQueueTotalAmount += buf.limit();
551+
}
530552
bufferQueue.put( buf );
531553
wsl.onWriteDemand( this );
532554
}

‎src/org/java_websocket/WebSocketClient.java‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
import java.io.IOException;
44
import java.net.InetSocketAddress;
55
import java.net.URI;
6-
import java.nio.channels.ClosedByInterruptException;
7-
import java.nio.channels.NotYetConnectedException;
8-
import java.nio.channels.SelectionKey;
9-
import java.nio.channels.Selector;
10-
import java.nio.channels.SocketChannel;
11-
import java.nio.channels.UnresolvedAddressException;
6+
import java.nio.channels.*;
127
import java.util.Iterator;
138
import java.util.Set;
149
import java.util.concurrent.locks.Lock;
@@ -279,6 +274,19 @@ public int getReadyState() {
279274
return conn.getReadyState();
280275
}
281276

277+
/**
278+
* Amount of data buffered/queued but not sent yet.
279+
*
280+
* In details, it returns the number of bytes of application data (UTF-8 text and binary data)
281+
* that have been queued using send() but that, as of the last time the event loop
282+
* started executing a task, had not yet been transmitted to the network.
283+
*
284+
* @return Amount still buffered/queued but not sent yet.
285+
*/
286+
public long getBufferedAmount() {
287+
return conn.bufferedDataAmount();
288+
}
289+
282290
/**
283291
* Calls subclass' implementation of <var>onMessage</var>.
284292
*

‎src/org/java_websocket/drafts/Draft.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,14 @@ public static HandshakeBuilder translateHandshakeHttp( ByteBuffer buf, Role role
8181
throw new InvalidHandshakeException();
8282
}
8383

84-
if( role == Role.CLIENT ) {// translating/parsing the response from the SERVER
84+
if( role == Role.CLIENT ) {
85+
// translating/parsing the response from the SERVER
8586
handshake = new HandshakeImpl1Server();
8687
ServerHandshakeBuilder serverhandshake = (ServerHandshakeBuilder) handshake;
8788
serverhandshake.setHttpStatus( Short.parseShort( firstLineTokens[ 1 ] ) );
8889
serverhandshake.setHttpStatusMessage( firstLineTokens[ 2 ] );
89-
} else {// translating/parsing the request from the CLIENT
90+
} else {
91+
// translating/parsing the request from the CLIENT
9092
ClientHandshakeBuilder clienthandshake = new HandshakeImpl1Client();
9193
clienthandshake.setResourceDescriptor( firstLineTokens[ 1 ] );
9294
handshake = clienthandshake;

0 commit comments

Comments
 (0)