Skip to content

Commit 8fc4922

Browse files
committed
merged TooTallNate/trunk with Davidiusdadi/trunk
2 parents 31dcbc1 + 92c48b9 commit 8fc4922

14 files changed

Lines changed: 78 additions & 26 deletions

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Minimum Required JDK
7373

7474
`Java-WebSocket` is known to work with:
7575

76-
* Java 1.4 (aka SE 6)
76+
* Java 1.5 (aka SE 6)
7777
* Android 1.6 (API 4)
7878

7979
Other JRE implementations may work as well, but haven't been tested.

src/org/java_websocket/WebSocket.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.List;
1111
import java.util.concurrent.BlockingQueue;
1212
import java.util.concurrent.LinkedBlockingQueue;
13+
import java.util.concurrent.atomic.AtomicLong;
1314

1415
import org.java_websocket.drafts.Draft;
1516
import org.java_websocket.drafts.Draft.CloseHandshakeType;
@@ -90,12 +91,13 @@ public enum Role {
9091
* Queue of buffers that need to be sent to the client.
9192
*/
9293
private BlockingQueue<ByteBuffer> bufferQueue;
94+
9395
/**
9496
* The amount of bytes still in queue to be sent, at every given time.
9597
* It's updated at every send/sent operation.
9698
*/
97-
private Long bufferQueueTotalAmount = (long) 0;
98-
99+
private AtomicLong bufferQueueTotalAmount = new AtomicLong(0l);
100+
99101
private Draft draft = null;
100102

101103
private Role role;
@@ -499,7 +501,7 @@ boolean hasBufferedData() {
499501
* @return Amount of Data still in Queue and not sent yet of the socket
500502
*/
501503
long bufferedDataAmount() {
502-
return bufferQueueTotalAmount;
504+
return bufferQueueTotalAmount.get();
503505
}
504506

505507
/**
@@ -512,10 +514,9 @@ public void flush() throws IOException {
512514
if( buffer.remaining() > 0 ) {
513515
continue;
514516
} else {
515-
synchronized ( bufferQueueTotalAmount ) {
516-
// subtract this amount of data from the total queued (synchronized over this object)
517-
bufferQueueTotalAmount -= written;
518-
}
517+
// subtract this amount of data from the total queued (synchronized over this object)
518+
bufferQueueTotalAmount.addAndGet( -written );
519+
519520
this.bufferQueue.poll(); // Buffer finished. Remove it.
520521
buffer = this.bufferQueue.peek();
521522
}
@@ -562,10 +563,10 @@ public void startHandshake( ClientHandshakeBuilder handshakedata ) throws Invali
562563
private void channelWrite( ByteBuffer buf ) throws InterruptedException {
563564
if( DEBUG )
564565
System.out.println( "write(" + buf.remaining() + "): {" + ( buf.remaining() > 1000 ? "too big to display" : new String( buf.array() ) ) + "}" );
565-
synchronized ( bufferQueueTotalAmount ) {
566-
// add up the number of bytes to the total queued (synchronized over this object)
567-
bufferQueueTotalAmount += buf.remaining();
568-
}
566+
567+
// add up the number of bytes to the total queued (synchronized over this object)
568+
bufferQueueTotalAmount.addAndGet(buf.remaining());
569+
569570
if( !bufferQueue.offer( buf ) ) {
570571
try {
571572
flush();

src/org/java_websocket/WebSocketClient.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,12 @@ public void close() {
108108
if( thread != null ) {
109109
thread.interrupt();
110110
closelock.lock();
111-
if( selector != null )
112-
selector.wakeup();
113-
closelock.unlock();
111+
try {
112+
if( selector != null )
113+
selector.wakeup();
114+
} finally {
115+
closelock.unlock();
116+
}
114117
}
115118

116119
}

src/org/java_websocket/drafts/Draft_10.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
public class Draft_10 extends Draft {
3232

3333
private class IncompleteException extends Throwable {
34+
35+
/**
36+
* It's Serializable.
37+
*/
38+
private static final long serialVersionUID = 7330519489840500997L;
39+
40+
3441
private int preferedsize;
3542
public IncompleteException( int preferedsize ) {
3643
this.preferedsize = preferedsize;
@@ -56,6 +63,8 @@ public static int readVersion( Handshakedata handshakedata ) {
5663

5764
private ByteBuffer incompleteframe;
5865
private Framedata fragmentedframe = null;
66+
67+
private final Random reuseableRandom = new Random();
5968

6069
@Override
6170
public HandshakeState acceptHandshakeAsClient( ClientHandshake request, ServerHandshake response ) throws InvalidHandshakeException {
@@ -106,7 +115,7 @@ public ByteBuffer createBinaryFrame( Framedata framedata ) {
106115

107116
if( mask ) {
108117
ByteBuffer maskkey = ByteBuffer.allocate( 4 );
109-
maskkey.putInt( new Random().nextInt() );
118+
maskkey.putInt( reuseableRandom.nextInt() );
110119
buf.put( maskkey.array() );
111120
for( int i = 0 ; i < mes.limit() ; i++ ) {
112121
buf.put( (byte) ( mes.get() ^ maskkey.get( i % 4 ) ) );
@@ -183,7 +192,7 @@ public ClientHandshakeBuilder postProcessHandshakeRequestAsClient( ClientHandsha
183192
request.put( "Sec-WebSocket-Version", "8" );
184193

185194
byte[] random = new byte[ 16 ];
186-
new Random().nextBytes( random );
195+
reuseableRandom.nextBytes( random );
187196
request.put( "Sec-WebSocket-Key", Base64.encodeBytes( random ) );
188197

189198
return request;

src/org/java_websocket/drafts/Draft_75.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public class Draft_75 extends Draft {
4444
private boolean inframe = false;
4545
protected List<Framedata> readyframes = new LinkedList<Framedata>();
4646
protected ByteBuffer currentFrame;
47+
48+
49+
private final Random reuseableRandom = new Random();
50+
4751

4852
@Override
4953
public HandshakeState acceptHandshakeAsClient( ClientHandshake request, ServerHandshake response ) {
@@ -99,7 +103,7 @@ public ClientHandshakeBuilder postProcessHandshakeRequestAsClient( ClientHandsha
99103
request.put( "Upgrade", "WebSocket" );
100104
request.put( "Connection", "Upgrade" );
101105
if( !request.hasFieldValue( "Origin" ) ) {
102-
request.put( "Origin", "random" + new Random().nextInt() );
106+
request.put( "Origin", "random" + reuseableRandom.nextInt() );
103107
}
104108

105109
return request;

src/org/java_websocket/drafts/Draft_76.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
public class Draft_76 extends Draft_75 {
2929
private boolean failed = false;
3030
private static final byte[] closehandshake = { -1, 0 };
31+
32+
private final Random reuseableRandom = new Random();
33+
3134

3235
public static byte[] createChallenge( String key1, String key2, byte[] key3 ) throws InvalidHandshakeException {
3336
byte[] part1 = getPart( key1 );
@@ -137,15 +140,15 @@ public HandshakeState acceptHandshakeAsServer( ClientHandshake handshakedata ) {
137140
public ClientHandshakeBuilder postProcessHandshakeRequestAsClient( ClientHandshakeBuilder request ) {
138141
request.put( "Upgrade", "WebSocket" );
139142
request.put( "Connection", "Upgrade" );
140-
request.put( "Sec-WebSocket-Key1", this.generateKey() );
141-
request.put( "Sec-WebSocket-Key2", this.generateKey() );
143+
request.put( "Sec-WebSocket-Key1", generateKey() );
144+
request.put( "Sec-WebSocket-Key2", generateKey() );
142145

143146
if( !request.hasFieldValue( "Origin" ) ) {
144-
request.put( "Origin", "random" + new Random().nextInt() );
147+
request.put( "Origin", "random" + reuseableRandom.nextInt() );
145148
}
146149

147150
byte[] key3 = new byte[ 8 ];
148-
new Random().nextBytes( key3 );
151+
reuseableRandom.nextBytes( key3 );
149152
request.setContent( key3 );
150153
return request;
151154

src/org/java_websocket/exeptions/IncompleteHandshakeException.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
public class IncompleteHandshakeException extends RuntimeException {
44

5+
/**
6+
* Serializable
7+
*/
8+
private static final long serialVersionUID = 7906596804233893092L;
9+
510
public IncompleteHandshakeException() {
611
super();
712
}

src/org/java_websocket/exeptions/InvalidDataException.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package org.java_websocket.exeptions;
22

33
public class InvalidDataException extends Exception {
4+
/**
5+
* Serializable
6+
*/
7+
private static final long serialVersionUID = 3731842424390998726L;
8+
49
private int closecode;
10+
511
public InvalidDataException( int closecode ) {
612
this.closecode = closecode;
713
}
@@ -14,14 +20,14 @@ public InvalidDataException( int closecode , String s ) {
1420
public InvalidDataException( int closecode , Throwable t ) {
1521
super( t );
1622
if( t instanceof InvalidDataException ) {
17-
closecode = ( (InvalidDataException) t ).getCloseCode();
23+
this.closecode = ( (InvalidDataException) t ).getCloseCode();
1824
}
1925
}
2026

2127
public InvalidDataException( int closecode , String s , Throwable t ) {
2228
super( s, t );
2329
if( t instanceof InvalidDataException ) {
24-
closecode = ( (InvalidDataException) t ).getCloseCode();
30+
this.closecode = ( (InvalidDataException) t ).getCloseCode();
2531
}
2632
}
2733

src/org/java_websocket/exeptions/InvalidFrameException.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
public class InvalidFrameException extends InvalidDataException {
66

7+
/**
8+
* Serializable
9+
*/
10+
private static final long serialVersionUID = -9016496369828887591L;
11+
712
public InvalidFrameException() {
813
super( CloseFrame.PROTOCOL_ERROR );
914
}

src/org/java_websocket/exeptions/InvalidHandshakeException.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
public class InvalidHandshakeException extends InvalidDataException {
66

7+
/**
8+
* Serializable
9+
*/
10+
private static final long serialVersionUID = -1426533877490484964L;
11+
712
public InvalidHandshakeException() {
813
super( CloseFrame.PROTOCOL_ERROR );
914
}

0 commit comments

Comments
 (0)