Skip to content

Commit dbb9f73

Browse files
committed
More improvement
Reduced complexity Removed found issues Fixed wrong checks in tests
1 parent 27e2675 commit dbb9f73

File tree

9 files changed

+281
-154
lines changed

9 files changed

+281
-154
lines changed

src/main/java/org/java_websocket/AbstractWrappedByteChannel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@
3030
import java.nio.channels.ByteChannel;
3131
import java.nio.channels.SocketChannel;
3232

33-
/*
33+
/**
3434
* @deprecated
3535
*/
3636
@Deprecated
3737
public class AbstractWrappedByteChannel implements WrappedByteChannel {
3838

3939
private final ByteChannel channel;
4040

41-
/*
41+
/**
4242
* @deprecated
4343
*/
4444
@Deprecated
4545
public AbstractWrappedByteChannel( ByteChannel towrap ) {
4646
this.channel = towrap;
4747
}
4848

49-
/*
49+
/**
5050
* @deprecated
5151
*/
5252
@Deprecated

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,15 @@ public class WebSocketImpl implements WebSocket {
9898
private final WebSocketListener wsl;
9999

100100
private SelectionKey key;
101+
101102
/**
102103
* the possibly wrapped channel object whose selection is controlled by {@link #key}
103104
*/
104-
public ByteChannel channel;
105+
private ByteChannel channel;
105106
/**
106107
* Helper variable meant to store the thread which ( exclusively ) triggers this objects decode method.
107108
**/
108-
public volatile WebSocketWorker workerThread; // TODO reset worker?
109+
private volatile WebSocketWorker workerThread;
109110
/**
110111
* When true no further frames may be submitted to be sent
111112
*/
@@ -282,7 +283,7 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) {
282283
closeConnectionDueToInternalServerError( e );
283284
return false;
284285
}
285-
write( d.createHandshake( d.postProcessHandshakeResponseAsServer( handshake, response ), role ) );
286+
write( d.createHandshake( d.postProcessHandshakeResponseAsServer( handshake, response ) ) );
286287
draft = d;
287288
open( handshake );
288289
return true;
@@ -686,7 +687,7 @@ public void startHandshake( ClientHandshakeBuilder handshakedata ) throws Invali
686687
}
687688

688689
// Send
689-
write( draft.createHandshake( this.handshakerequest, role ) );
690+
write( draft.createHandshake( this.handshakerequest ) );
690691
}
691692

692693
private void write( ByteBuffer buf ) {
@@ -824,4 +825,21 @@ public <T> void setAttachment(T attachment) {
824825
this.attachment = attachment;
825826
}
826827

828+
public ByteChannel getChannel() {
829+
return channel;
830+
}
831+
832+
public void setChannel(ByteChannel channel) {
833+
this.channel = channel;
834+
}
835+
836+
public WebSocketWorker getWorkerThread() {
837+
return workerThread;
838+
}
839+
840+
public void setWorkerThread(WebSocketWorker workerThread) {
841+
this.workerThread = workerThread;
842+
}
843+
844+
827845
}

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -655,19 +655,7 @@ private class WebsocketWriteThread implements Runnable {
655655
public void run() {
656656
Thread.currentThread().setName( "WebSocketWriteThread-" + Thread.currentThread().getId() );
657657
try {
658-
try {
659-
while( !Thread.interrupted() ) {
660-
ByteBuffer buffer = engine.outQueue.take();
661-
ostream.write( buffer.array(), 0, buffer.limit() );
662-
ostream.flush();
663-
}
664-
} catch ( InterruptedException e ) {
665-
for (ByteBuffer buffer : engine.outQueue) {
666-
ostream.write( buffer.array(), 0, buffer.limit() );
667-
ostream.flush();
668-
}
669-
Thread.currentThread().interrupt();
670-
}
658+
runWriteData();
671659
} catch ( IOException e ) {
672660
handleIOException( e );
673661
} finally {
@@ -676,6 +664,26 @@ public void run() {
676664
}
677665
}
678666

667+
/**
668+
* Write the data into the outstream
669+
* @throws IOException if write or flush did not work
670+
*/
671+
private void runWriteData() throws IOException {
672+
try {
673+
while( !Thread.interrupted() ) {
674+
ByteBuffer buffer = engine.outQueue.take();
675+
ostream.write( buffer.array(), 0, buffer.limit() );
676+
ostream.flush();
677+
}
678+
} catch ( InterruptedException e ) {
679+
for (ByteBuffer buffer : engine.outQueue) {
680+
ostream.write( buffer.array(), 0, buffer.limit() );
681+
ostream.flush();
682+
}
683+
Thread.currentThread().interrupt();
684+
}
685+
}
686+
679687
/**
680688
* Closing the socket
681689
*/

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ public static HandshakeBuilder translateHandshakeHttp( ByteBuffer buf, Role role
9797
throw new InvalidHandshakeException();
9898
}
9999
if( role == Role.CLIENT ) {
100-
handshake = prvTranslateHandshakeHttpClient(firstLineTokens, line);
100+
handshake = translateHandshakeHttpClient(firstLineTokens, line);
101101
} else {
102-
handshake = prvTranslateHandshakeHttpServer(firstLineTokens, line);
102+
handshake = translateHandshakeHttpServer(firstLineTokens, line);
103103
}
104104
line = readStringLine( buf );
105105
while ( line != null && line.length() > 0 ) {
@@ -125,7 +125,7 @@ public static HandshakeBuilder translateHandshakeHttp( ByteBuffer buf, Role role
125125
* @param firstLineTokens the token of the first line split as as an string array
126126
* @param line the whole line
127127
*/
128-
private static HandshakeBuilder prvTranslateHandshakeHttpServer(String[] firstLineTokens, String line) throws InvalidHandshakeException {
128+
private static HandshakeBuilder translateHandshakeHttpServer(String[] firstLineTokens, String line) throws InvalidHandshakeException {
129129
// translating/parsing the request from the CLIENT
130130
if (!"GET".equalsIgnoreCase(firstLineTokens[0])) {
131131
throw new InvalidHandshakeException( String.format("Invalid request method received: %s Status line: %s", firstLineTokens[0],line));
@@ -144,7 +144,7 @@ private static HandshakeBuilder prvTranslateHandshakeHttpServer(String[] firstLi
144144
* @param firstLineTokens the token of the first line split as as an string array
145145
* @param line the whole line
146146
*/
147-
private static HandshakeBuilder prvTranslateHandshakeHttpClient(String[] firstLineTokens, String line) throws InvalidHandshakeException {
147+
private static HandshakeBuilder translateHandshakeHttpClient(String[] firstLineTokens, String line) throws InvalidHandshakeException {
148148
// translating/parsing the response from the SERVER
149149
if (!"101".equals(firstLineTokens[1])) {
150150
throw new InvalidHandshakeException( String.format("Invalid status code received: %s Status line: %s", firstLineTokens[1], line));
@@ -214,11 +214,27 @@ public List<Framedata> continuousFrame(Opcode op, ByteBuffer buffer, boolean fin
214214

215215
public abstract void reset();
216216

217+
/**
218+
* @deprecated use createHandshake without the role
219+
*/
220+
@Deprecated
217221
public List<ByteBuffer> createHandshake( Handshakedata handshakedata, Role ownrole ) {
218-
return createHandshake( handshakedata, ownrole, true );
222+
return createHandshake(handshakedata);
223+
}
224+
225+
public List<ByteBuffer> createHandshake( Handshakedata handshakedata) {
226+
return createHandshake( handshakedata, true );
219227
}
220228

229+
/**
230+
* @deprecated use createHandshake without the role since it does not have any effect
231+
*/
232+
@Deprecated
221233
public List<ByteBuffer> createHandshake( Handshakedata handshakedata, Role ownrole, boolean withcontent ) {
234+
return createHandshake(handshakedata, withcontent);
235+
}
236+
237+
public List<ByteBuffer> createHandshake( Handshakedata handshakedata, boolean withcontent ) {
222238
StringBuilder bui = new StringBuilder( 100 );
223239
if( handshakedata instanceof ClientHandshake ) {
224240
bui.append( "GET " );

0 commit comments

Comments
 (0)