Skip to content

Commit 1ee6059

Browse files
authored
Merge pull request #500 from marci4/master
Making WebSocket.send() thread-safe
2 parents a14541f + c355e3e commit 1ee6059

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

pom.xml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@
1616
<scm>
1717
<url>https://github.com/TooTallNate/Java-WebSocket</url>
1818
</scm>
19-
<distributionManagement>
20-
<snapshotRepository>
21-
<id>ossrh</id>
22-
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
23-
</snapshotRepository>
24-
</distributionManagement>
2519
<build>
2620
<sourceDirectory>src/main/java</sourceDirectory>
2721
<plugins>
@@ -62,6 +56,20 @@
6256
</execution>
6357
</executions>
6458
</plugin>
59+
<plugin>
60+
<groupId>org.apache.maven.plugins</groupId>
61+
<artifactId>maven-gpg-plugin</artifactId>
62+
<version>1.5</version>
63+
<executions>
64+
<execution>
65+
<id>sign-artifacts</id>
66+
<phase>verify</phase>
67+
<goals>
68+
<goal>sign</goal>
69+
</goals>
70+
</execution>
71+
</executions>
72+
</plugin>
6573
</plugins>
6674
</build>
6775
<dependencies>

src/main/java/org/java_websocket/WebSocketImpl.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.nio.channels.SelectionKey;
2626
import java.util.ArrayList;
2727
import java.util.Collection;
28+
import java.util.Collections;
2829
import java.util.List;
2930
import java.util.concurrent.BlockingQueue;
3031
import java.util.concurrent.LinkedBlockingQueue;
@@ -110,6 +111,11 @@ public class WebSocketImpl implements WebSocket {
110111
*/
111112
private long lastPong = System.currentTimeMillis();
112113

114+
/**
115+
* Attribut to synchronize the write
116+
*/
117+
private static final Object synchronizeWriteObject = new Object();
118+
113119
/**
114120
* Creates a websocket with server role
115121
*
@@ -209,8 +215,8 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) {
209215
HandshakeState isflashedgecase = isFlashEdgeCase( socketBuffer );
210216
if( isflashedgecase == HandshakeState.MATCHED ) {
211217
try {
212-
write( ByteBuffer.wrap( Charsetfunctions.utf8Bytes( wsl.getFlashPolicy( this ) ) ) );
213-
close( CloseFrame.FLASHPOLICY, "" );
218+
write( Collections.singletonList( ByteBuffer.wrap(Charsetfunctions.utf8Bytes(wsl.getFlashPolicy(this)))));
219+
close(CloseFrame.FLASHPOLICY, "");
214220
} catch ( InvalidDataException e ) {
215221
close( CloseFrame.ABNORMAL_CLOSE, "remote peer closed connection before flashpolicy could be transmitted", true );
216222
}
@@ -623,9 +629,13 @@ public void send( byte[] bytes ) throws IllegalArgumentException, WebsocketNotCo
623629
private void send( Collection<Framedata> frames ) {
624630
if( !isOpen() )
625631
throw new WebsocketNotConnectedException();
626-
for( Framedata f : frames ) {
627-
sendFrame( f );
632+
ArrayList<ByteBuffer> outgoingFrames = new ArrayList<ByteBuffer>();
633+
for (Framedata f : frames) {
634+
if( DEBUG )
635+
System.out.println( "send frame: " + f );
636+
outgoingFrames.add( draft.createBinaryFrame( f ) );
628637
}
638+
write( outgoingFrames );
629639
}
630640

631641
@Override
@@ -635,9 +645,7 @@ public void sendFragmentedFrame( Opcode op, ByteBuffer buffer, boolean fin ) {
635645

636646
@Override
637647
public void sendFrame( Framedata framedata ) {
638-
if( DEBUG )
639-
System.out.println( "send frame: " + framedata );
640-
write( draft.createBinaryFrame( framedata ) );
648+
send ( Collections.singletonList( framedata ) );
641649
}
642650

643651
public void sendPing() throws NotYetConnectedException {
@@ -707,8 +715,10 @@ private void write( ByteBuffer buf ) {
707715
}
708716

709717
private void write( List<ByteBuffer> bufs ) {
710-
for( ByteBuffer b : bufs ) {
711-
write( b );
718+
synchronized ( synchronizeWriteObject ) {
719+
for (ByteBuffer b : bufs) {
720+
write(b);
721+
}
712722
}
713723
}
714724

0 commit comments

Comments
 (0)