Skip to content

Commit 9ef3c1c

Browse files
committed
beautification and documentation and fixed typo in enum Opcode
1 parent abde700 commit 9ef3c1c

5 files changed

Lines changed: 61 additions & 79 deletions

File tree

src/org/java_websocket/WebSocketImpl.java

Lines changed: 48 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,27 @@
5050
*/
5151
public class WebSocketImpl extends WebSocket {
5252

53+
public SelectionKey key;
54+
55+
/* only used to optain the socket addresses*/
56+
public final Socket socket;
57+
/** the possibly wrapped channel object whose selection is controlled by {@link #key} */
58+
public ByteChannel channel;
59+
/**
60+
* Queue of buffers that need to be sent to the client.
61+
*/
62+
public final BlockingQueue<ByteBuffer> outQueue;
63+
/**
64+
* Queue of buffers that need to be processed
65+
*/
66+
public final BlockingQueue<ByteBuffer> inQueue;
67+
68+
/**
69+
* Helper variable ment to store the thread which ( exclusively ) triggers this objects decode method.
70+
**/
71+
public volatile WebSocketWorker workerThread; // TODO reset worker?
72+
73+
5374
/**
5475
* Determines whether to receive data as part of the
5576
* handshake, or as part of text/data frame transmitted over the websocket.
@@ -60,40 +81,31 @@ public class WebSocketImpl extends WebSocket {
6081
*/
6182
private volatile boolean closeHandshakeSent = false;
6283
/**
63-
* Determines wheter the connection is open or not
84+
* Determines whether the connection is open or not
6485
*/
6586
private volatile boolean connectionClosed = false;
6687

88+
6789
/**
68-
* The listener to notify of WebSocketImpl events.
90+
* The listener to notify of WebSocket events.
6991
*/
7092
private final WebSocketListener wsl;
71-
/**
72-
* Queue of buffers that need to be sent to the client.
73-
*/
74-
public final BlockingQueue<ByteBuffer> outQueue;
93+
94+
private List<Draft> knownDrafts;
7595

7696
private Draft draft = null;
7797

7898
private Role role;
7999

80-
private Framedata currentframe;
81-
82-
private ClientHandshake handshakerequest = null;
83-
84-
private List<Draft> known_drafts;
100+
/** used to join continuous frames */
101+
private Framedata tempContiniousFrame;// FIXME out of mem risk
85102

103+
/** the bytes of an incomplete received handshake */
86104
private ByteBuffer tmpHandshakeBytes;
87105

88-
public final BlockingQueue<ByteBuffer> in;
89-
90-
public volatile WebSocketWorker worker;
91-
92-
public SelectionKey key;
93-
94-
public final Socket socket;
106+
/** stores the handshake sent by this websocket ( Role.CLIENT only ) */
107+
private ClientHandshake handshakerequest = null;
95108

96-
public ByteChannel ioobject;
97109

98110
// CONSTRUCTOR /////////////////////////////////////////////////////////////
99111
/**
@@ -111,54 +123,26 @@ public class WebSocketImpl extends WebSocket {
111123
public WebSocketImpl( WebSocketListener listener , List<Draft> drafts , Socket sock ) {
112124
this( listener, (Draft) null, sock );
113125
this.role = Role.SERVER;
114-
if( known_drafts == null || known_drafts.isEmpty() ) {
115-
known_drafts = new ArrayList<Draft>( 1 );
116-
known_drafts.add( new Draft_17() );
117-
known_drafts.add( new Draft_10() );
118-
known_drafts.add( new Draft_76() );
119-
known_drafts.add( new Draft_75() );
126+
if( knownDrafts == null || knownDrafts.isEmpty() ) {
127+
knownDrafts = new ArrayList<Draft>( 1 );
128+
knownDrafts.add( new Draft_17() );
129+
knownDrafts.add( new Draft_10() );
130+
knownDrafts.add( new Draft_76() );
131+
knownDrafts.add( new Draft_75() );
120132
} else {
121-
known_drafts = drafts;
133+
knownDrafts = drafts;
122134
}
123135
}
124136

125137
public WebSocketImpl( WebSocketListener listener , Draft draft , Socket sock ) {
126138
this.outQueue = new LinkedBlockingQueue<ByteBuffer>();
127-
in = new LinkedBlockingQueue<ByteBuffer>();
139+
inQueue = new LinkedBlockingQueue<ByteBuffer>();
128140
this.wsl = listener;
129141
this.role = Role.CLIENT;
130142
this.draft = draft;
131143
this.socket = sock;
132144
}
133145

134-
/**
135-
* Returns whether the buffer has been filled.
136-
*
137-
* @throws IOException
138-
**/
139-
/*public boolean read( final ByteBuffer buf ) throws IOException {
140-
buf.clear();
141-
int read;
142-
if(sockchannel != null) read = sockchannel.read( buf );
143-
else read = sockchannel2.read(buf);
144-
buf.flip();
145-
if( read == -1 ) {
146-
if( draft == null ) {
147-
closeConnection( CloseFrame.ABNORMAL_CLOSE, true );
148-
} else if( draft.getCloseHandshakeType() == CloseHandshakeType.NONE ) {
149-
closeConnection( CloseFrame.NORMAL, true );
150-
} else if( draft.getCloseHandshakeType() == CloseHandshakeType.ONEWAY ) {
151-
if( role == Role.SERVER )
152-
closeConnection( CloseFrame.ABNORMAL_CLOSE, true );
153-
else
154-
closeConnection( CloseFrame.NORMAL, true );
155-
} else {
156-
closeConnection( CloseFrame.ABNORMAL_CLOSE, true );
157-
}
158-
return false;
159-
}
160-
return read != 0;
161-
}*/
162146
/**
163147
* Should be called when a Selector has a key that is writable for this
164148
* WebSocketImpl's SocketChannel connection.
@@ -219,7 +203,7 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) throws IOException
219203
try {
220204
if( role == Role.SERVER ) {
221205
if( draft == null ) {
222-
for( Draft d : known_drafts ) {
206+
for( Draft d : knownDrafts ) {
223207
try {
224208
d.setParseMode( role );
225209
socketBuffer.reset();
@@ -349,21 +333,21 @@ private void decodeFrames( ByteBuffer socketBuffer ) {
349333
continue;
350334
} else {
351335
// process non control frames
352-
if( currentframe == null ) {
353-
if( f.getOpcode() == Opcode.CONTINIOUS ) {
336+
if( tempContiniousFrame == null ) {
337+
if( f.getOpcode() == Opcode.CONTINUOUS ) {
354338
throw new InvalidFrameException( "unexpected continious frame" );
355339
} else if( f.isFin() ) {
356340
// receive normal onframe message
357341
deliverMessage( f );
358342
} else {
359343
// remember the frame whose payload is about to be continued
360-
currentframe = f;
344+
tempContiniousFrame = f;
361345
}
362-
} else if( f.getOpcode() == Opcode.CONTINIOUS ) {
363-
currentframe.append( f );
346+
} else if( f.getOpcode() == Opcode.CONTINUOUS ) {
347+
tempContiniousFrame.append( f );
364348
if( f.isFin() ) {
365-
deliverMessage( currentframe );
366-
currentframe = null;
349+
deliverMessage( tempContiniousFrame );
350+
tempContiniousFrame = null;
367351
}
368352
} else {
369353
throw new InvalidDataException( CloseFrame.PROTOCOL_ERROR, "non control or continious frame expected" );
@@ -435,7 +419,7 @@ protected synchronized void closeConnection( int code, String message, boolean r
435419
this.wsl.onWebsocketClose( this, code, message, remote );
436420
if( draft != null )
437421
draft.reset();
438-
currentframe = null;
422+
tempContiniousFrame = null;
439423
handshakerequest = null;
440424
}
441425

src/org/java_websocket/drafts/Draft_10.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public List<Framedata> createFrames( String text, boolean mask ) {
158158
}
159159

160160
private byte fromOpcode( Opcode opcode ) {
161-
if( opcode == Opcode.CONTINIOUS )
161+
if( opcode == Opcode.CONTINUOUS )
162162
return 0;
163163
else if( opcode == Opcode.TEXT )
164164
return 1;
@@ -222,7 +222,7 @@ private byte[] toByteArray( long val, int bytecount ) {
222222
private Opcode toOpcode( byte opcode ) throws InvalidFrameException {
223223
switch ( opcode ) {
224224
case 0:
225-
return Opcode.CONTINIOUS;
225+
return Opcode.CONTINUOUS;
226226
case 1:
227227
return Opcode.TEXT;
228228
case 2:

src/org/java_websocket/drafts/Draft_75.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected List<Framedata> translateRegularFrame( ByteBuffer buffer ) throws Inva
138138
FramedataImpl1 curframe = new FramedataImpl1();
139139
curframe.setPayload( currentFrame );
140140
curframe.setFin( true );
141-
curframe.setOptcode( inframe ? Opcode.CONTINIOUS : Opcode.TEXT );
141+
curframe.setOptcode( inframe ? Opcode.CONTINUOUS : Opcode.TEXT );
142142
readyframes.add( curframe );
143143
this.currentFrame = null;
144144
buffer.mark();
@@ -161,7 +161,7 @@ protected List<Framedata> translateRegularFrame( ByteBuffer buffer ) throws Inva
161161
currentFrame.flip();
162162
curframe.setPayload( currentFrame );
163163
curframe.setFin( false );
164-
curframe.setOptcode( inframe ? Opcode.CONTINIOUS : Opcode.TEXT );
164+
curframe.setOptcode( inframe ? Opcode.CONTINUOUS : Opcode.TEXT );
165165
inframe = true;
166166
readyframes.add( curframe );
167167
}

src/org/java_websocket/framing/Framedata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public interface Framedata {
88
public enum Opcode {
9-
CONTINIOUS , TEXT , BINARY , PING , PONG , CLOSING
9+
CONTINUOUS, TEXT, BINARY, PING, PONG, CLOSING
1010
// more to come
1111
}
1212
public boolean isFin();

src/org/java_websocket/server/WebSocketServer.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public void run() {
265265
channel.configureBlocking( false );
266266
WebSocketImpl c = wsf.createWebSocket( this, drafts, channel.socket() );
267267
c.key = channel.register( selector, SelectionKey.OP_READ, c );
268-
c.ioobject = wsf.wrapChannel( channel );
268+
c.channel = wsf.wrapChannel( channel );
269269
i.remove();
270270
allocateBuffers( c );
271271
continue;
@@ -275,8 +275,8 @@ public void run() {
275275
conn = (WebSocketImpl) key.attachment();
276276
ByteBuffer buf = takeBuffer();
277277
try {
278-
if( SocketChannelIOHelper.read( buf, conn, (ByteChannel) conn.ioobject ) ) {
279-
conn.in.put( buf );
278+
if( SocketChannelIOHelper.read( buf, conn, (ByteChannel) conn.channel ) ) {
279+
conn.inQueue.put( buf );
280280
queue( conn );
281281
i.remove();
282282
} else {
@@ -292,7 +292,7 @@ public void run() {
292292
}
293293
if( key.isWritable() ) {
294294
conn = (WebSocketImpl) key.attachment();
295-
if( SocketChannelIOHelper.batch( conn, (ByteChannel) conn.ioobject ) ) {
295+
if( SocketChannelIOHelper.batch( conn, (ByteChannel) conn.channel ) ) {
296296
if( key.isValid() )
297297
key.channel().register( selector, SelectionKey.OP_READ, key.attachment() );
298298
}
@@ -314,8 +314,6 @@ public void run() {
314314
}
315315
}
316316

317-
318-
319317
protected void allocateBuffers( WebSocket c ) throws InterruptedException {
320318
if( queuesize.get() >= 2 * decoders.size() + 1 ) {
321319
return;
@@ -334,11 +332,11 @@ public ByteBuffer createBuffer() {
334332
}
335333

336334
private void queue( WebSocketImpl ws ) throws InterruptedException {
337-
if( ws.worker == null ) {
338-
ws.worker = decoders.get( queueinvokes % decoders.size() );
335+
if( ws.workerThread == null ) {
336+
ws.workerThread = decoders.get( queueinvokes % decoders.size() );
339337
queueinvokes++;
340338
}
341-
ws.worker.put( ws );
339+
ws.workerThread.put( ws );
342340
}
343341
private ByteBuffer takeBuffer() throws InterruptedException {
344342
return buffers.take();
@@ -497,7 +495,7 @@ public void run() {
497495
while ( true ) {
498496
ByteBuffer buf = null;
499497
ws = iqueue.take();
500-
buf = ws.in.poll();
498+
buf = ws.inQueue.poll();
501499
assert ( buf != null );
502500
try {
503501
ws.decode( buf );

0 commit comments

Comments
 (0)