Skip to content

Commit 8fc4922

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

14 files changed

+78
-26
lines changed

‎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
importjava.util.List;
1111
importjava.util.concurrent.BlockingQueue;
1212
importjava.util.concurrent.LinkedBlockingQueue;
13+
importjava.util.concurrent.atomic.AtomicLong;
1314

1415
importorg.java_websocket.drafts.Draft;
1516
importorg.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
privateBlockingQueue<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-
privateLongbufferQueueTotalAmount = (long) 0;
98-
99+
privateAtomicLongbufferQueueTotalAmount = newAtomicLong(0l);
100+
99101
privateDraftdraft = null;
100102

101103
privateRolerole;
@@ -499,7 +501,7 @@ boolean hasBufferedData(){
499501
* @return Amount of Data still in Queue and not sent yet of the socket
500502
*/
501503
longbufferedDataAmount(){
502-
returnbufferQueueTotalAmount;
504+
returnbufferQueueTotalAmount.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
privatevoidchannelWrite( ByteBufferbuf ) throwsInterruptedException{
563564
if( DEBUG )
564565
System.out.println( "write(" + buf.remaining() + "):{" + ( buf.remaining() > 1000 ? "too big to display" : newString( 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
publicclassDraft_10extendsDraft{
3232

3333
privateclassIncompleteExceptionextendsThrowable{
34+
35+
/**
36+
* It's Serializable.
37+
*/
38+
privatestaticfinallongserialVersionUID = 7330519489840500997L;
39+
40+
3441
privateintpreferedsize;
3542
publicIncompleteException( intpreferedsize ){
3643
this.preferedsize = preferedsize;
@@ -56,6 +63,8 @@ public static int readVersion( Handshakedata handshakedata ){
5663

5764
privateByteBufferincompleteframe;
5865
privateFramedatafragmentedframe = null;
66+
67+
privatefinalRandomreuseableRandom = newRandom();
5968

6069
@Override
6170
publicHandshakeStateacceptHandshakeAsClient( ClientHandshakerequest, ServerHandshakeresponse ) throwsInvalidHandshakeException{
@@ -106,7 +115,7 @@ public ByteBuffer createBinaryFrame( Framedata framedata ){
106115

107116
if( mask ){
108117
ByteBuffermaskkey = ByteBuffer.allocate( 4 );
109-
maskkey.putInt( newRandom().nextInt() );
118+
maskkey.putInt( reuseableRandom.nextInt() );
110119
buf.put( maskkey.array() );
111120
for( inti = 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 = newbyte[ 16 ];
186-
newRandom().nextBytes( random );
195+
reuseableRandom.nextBytes( random );
187196
request.put( "Sec-WebSocket-Key", Base64.encodeBytes( random ) );
188197

189198
returnrequest;

‎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
privatebooleaninframe = false;
4545
protectedList<Framedata> readyframes = newLinkedList<Framedata>();
4646
protectedByteBuffercurrentFrame;
47+
48+
49+
privatefinalRandomreuseableRandom = newRandom();
50+
4751

4852
@Override
4953
publicHandshakeStateacceptHandshakeAsClient( ClientHandshakerequest, ServerHandshakeresponse ){
@@ -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" + newRandom().nextInt() );
106+
request.put( "Origin", "random" + reuseableRandom.nextInt() );
103107
}
104108

105109
returnrequest;

‎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
publicclassDraft_76extendsDraft_75{
2929
privatebooleanfailed = false;
3030
privatestaticfinalbyte[] closehandshake ={-1, 0 };
31+
32+
privatefinalRandomreuseableRandom = newRandom();
33+
3134

3235
publicstaticbyte[] createChallenge( Stringkey1, Stringkey2, byte[] key3 ) throwsInvalidHandshakeException{
3336
byte[] part1 = getPart( key1 );
@@ -137,15 +140,15 @@ public HandshakeState acceptHandshakeAsServer( ClientHandshake handshakedata ){
137140
publicClientHandshakeBuilderpostProcessHandshakeRequestAsClient( ClientHandshakeBuilderrequest ){
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" + newRandom().nextInt() );
147+
request.put( "Origin", "random" + reuseableRandom.nextInt() );
145148
}
146149

147150
byte[] key3 = newbyte[ 8 ];
148-
newRandom().nextBytes( key3 );
151+
reuseableRandom.nextBytes( key3 );
149152
request.setContent( key3 );
150153
returnrequest;
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
publicclassIncompleteHandshakeExceptionextendsRuntimeException{
44

5+
/**
6+
* Serializable
7+
*/
8+
privatestaticfinallongserialVersionUID = 7906596804233893092L;
9+
510
publicIncompleteHandshakeException(){
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
packageorg.java_websocket.exeptions;
22

33
publicclassInvalidDataExceptionextendsException{
4+
/**
5+
* Serializable
6+
*/
7+
privatestaticfinallongserialVersionUID = 3731842424390998726L;
8+
49
privateintclosecode;
10+
511
publicInvalidDataException( intclosecode ){
612
this.closecode = closecode;
713
}
@@ -14,14 +20,14 @@ public InvalidDataException( int closecode , String s ){
1420
publicInvalidDataException( intclosecode , Throwablet ){
1521
super( t );
1622
if( tinstanceofInvalidDataException ){
17-
closecode = ( (InvalidDataException) t ).getCloseCode();
23+
this.closecode = ( (InvalidDataException) t ).getCloseCode();
1824
}
1925
}
2026

2127
publicInvalidDataException( intclosecode , Strings , Throwablet ){
2228
super( s, t );
2329
if( tinstanceofInvalidDataException ){
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
publicclassInvalidFrameExceptionextendsInvalidDataException{
66

7+
/**
8+
* Serializable
9+
*/
10+
privatestaticfinallongserialVersionUID = -9016496369828887591L;
11+
712
publicInvalidFrameException(){
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
publicclassInvalidHandshakeExceptionextendsInvalidDataException{
66

7+
/**
8+
* Serializable
9+
*/
10+
privatestaticfinallongserialVersionUID = -1426533877490484964L;
11+
712
publicInvalidHandshakeException(){
813
super( CloseFrame.PROTOCOL_ERROR );
914
}

0 commit comments

Comments
(0)