3434 * The Class IOConnection.
3535 */
3636public class IOConnection {
37+ private static final int STATE_INIT = 0 ;
38+ private static final int STATE_HANDSHAKE = 1 ;
39+ private static final int STATE_CONNECTING = 2 ;
40+ private static final int STATE_READY = 3 ;
41+ private static final int STATE_INTERRUPTED = 4 ;
42+ private static final int STATE_DISCONNECTING = 5 ;
43+ private static final int STATE_INVALID = 6 ;
44+ private int state = STATE_INIT ;
3745
3846 /** Socket.io path. */
3947 public static final String SOCKET_IO_1 = "/socket.io/1/" ;
@@ -71,12 +79,6 @@ public class IOConnection {
7179 /** The connect thread handling authentication. */
7280 private Thread connectThread = null ;
7381
74- /** Indicates if a connection is currently available. */
75- private boolean connected = false ;
76-
77- /** true if the connection should be shut down immediately. */
78- private boolean wantToDisconnect = false ;
79-
8082 /**
8183 * The first socket to be connected. the socket.io server does not send a
8284 * connected response to this one.
@@ -127,7 +129,7 @@ private final class ReconnectTimeoutTask extends TimerTask {
127129 */
128130 @ Override
129131 public void run () {
130- wantToDisconnect = true ;
132+ state = STATE_DISCONNECTING ;
131133 if (reconnectTimer != null ) {
132134 reconnectTimer .cancel ();
133135 reconnectTimer = null ;
@@ -163,31 +165,16 @@ public void run() {
163165 * The Class ConnectThread. Handles connecting to the server with an
164166 * {@link IOTransport}
165167 */
166- private class ConnectThread extends Thread {
168+ private class BackgroundThread extends Thread {
167169
168170 /**
169171 * Tries handshaking if necessary and connects with corresponding
170172 * transport afterwards.
171173 */
172174 public void run () {
173- if (transport != null )
174- return ;
175-
176- try {
177- if (sessionId == null ) {
178- System .out .println ("Handshaking" );
179- handshake ();
180- } else
181- System .out .println ("Try to reconnect" );
182- connectTransport ();
183- System .out .println ("Transport connected." );
184-
185- } catch (IOException e ) {
186- error (new SocketIOException (
187- "Error while handshaking. This could be caused by insufficient rights or network problems." ,
188- e ));
189- }
190- connectThread = null ;
175+ if (state == STATE_INIT )
176+ handshake ();
177+ connectTransport ();
191178 }
192179
193180 /**
@@ -196,35 +183,45 @@ public void run() {
196183 * @throws IOException
197184 * Signals that an I/O exception has occurred.
198185 */
199- private void handshake () throws IOException {
200- URL url = new URL ( IOConnection . this . url . toString () + SOCKET_IO_1 ) ;
186+ private void handshake () {
187+ URL url ;
201188 String response ;
202- URLConnection connection = url .openConnection ();
203- connection .setConnectTimeout (connectTimeout );
204- connection .setReadTimeout (connectTimeout );
205-
206- InputStream stream = connection .getInputStream ();
207- Scanner in = new Scanner (stream );
208- response = in .nextLine ();
209- if (response .contains (":" )) {
210- String [] data = response .split (":" );
211- heartbeatTimeout = Long .parseLong (data [1 ]) * 1000 ;
212- closingTimeout = Long .parseLong (data [2 ]) * 1000 ;
213- protocols = Arrays .asList (data [3 ].split ("," ));
214- sessionId = data [0 ];
189+ URLConnection connection ;
190+ try {
191+ state = STATE_HANDSHAKE ;
192+ url = new URL (IOConnection .this .url .toString () + SOCKET_IO_1 );
193+ connection = url .openConnection ();
194+ connection .setConnectTimeout (connectTimeout );
195+ connection .setReadTimeout (connectTimeout );
196+
197+ InputStream stream = connection .getInputStream ();
198+ Scanner in = new Scanner (stream );
199+ response = in .nextLine ();
200+ if (response .contains (":" )) {
201+ String [] data = response .split (":" );
202+ heartbeatTimeout = Long .parseLong (data [1 ]) * 1000 ;
203+ closingTimeout = Long .parseLong (data [2 ]) * 1000 ;
204+ protocols = Arrays .asList (data [3 ].split ("," ));
205+ sessionId = data [0 ];
206+ }
207+ } catch (IOException e ) {
208+ state = STATE_INVALID ;
209+ error (new SocketIOException ("Error while handshaking" , e ));
215210 }
216211 }
217212
218213 /**
219214 * Connect transport.
220215 */
221216 private void connectTransport () {
217+ if (state == STATE_INVALID )
218+ return ;
219+ state = STATE_CONNECTING ;
222220 if (protocols .contains (WebsocketTransport .TRANSPORT_NAME ))
223221 transport = WebsocketTransport .create (url , IOConnection .this );
224222 else
225223 error (new SocketIOException (
226224 "Server supports no available transports. You should reconfigure the server to support a available transport" ));
227-
228225 transport .connect ();
229226 }
230227
@@ -273,8 +270,8 @@ public void ack(Object... args) {
273270 }
274271
275272 }
276- IOMessage ackMsg = new IOMessage (IOMessage .TYPE_ACK , endPoint , id
277- + array .toString ());
273+ IOMessage ackMsg = new IOMessage (IOMessage .TYPE_ACK , endPoint ,
274+ id + array .toString ());
278275 sendPlain (ackMsg .toString ());
279276 }
280277 };
@@ -327,7 +324,7 @@ protected void error(SocketIOException e) {
327324 */
328325 private void connect () {
329326 if (connectThread == null ) {
330- connectThread = new ConnectThread ();
327+ connectThread = new BackgroundThread ();
331328 connectThread .start ();
332329 }
333330 }
@@ -363,7 +360,8 @@ public void connect(SocketIO socket) {
363360 */
364361 private void cleanup () {
365362 System .out .println ("Clean up" );
366- wantToDisconnect = true ;
363+ if (state != STATE_INVALID )
364+ state = STATE_DISCONNECTING ;
367365 if (transport != null )
368366 transport .disconnect ();
369367 sockets .clear ();
@@ -403,7 +401,7 @@ public void disconnect(SocketIO socket) {
403401 */
404402 private void sendPlain (String text ) {
405403 synchronized (outputBuffer ) {
406- if (connected )
404+ if (state == STATE_READY )
407405 try {
408406 System .out .println ("> " + text );
409407 transport .send (text );
@@ -430,7 +428,7 @@ private void invalidateTransport() {
430428 * {@link IOTransport} calls this when a connection is established.
431429 */
432430 public void transportConnected () {
433- connected = true ;
431+ state = STATE_READY ;
434432 if (reconnectTimeoutTimer != null ) {
435433 reconnectTimeoutTimer .cancel ();
436434 }
@@ -461,7 +459,7 @@ public void transportConnected() {
461459 public void transportDisconnected () {
462460 System .out .println ("Disconnect" );
463461 this .lastException = null ;
464- connected = false ;
462+ state = STATE_INTERRUPTED ;
465463 reconnect ();
466464 }
467465
@@ -475,7 +473,7 @@ public void transportDisconnected() {
475473 public void transportError (Exception error ) {
476474 System .out .println ("Error" );
477475 this .lastException = error ;
478- connected = false ;
476+ state = STATE_INTERRUPTED ;
479477 reconnect ();
480478 }
481479
@@ -585,9 +583,8 @@ public void transportMessage(String text) {
585583 } catch (JSONException e ) {
586584 warning ("Received malformated Acknowledge data!" );
587585 }
588- }
589- else if (data .length == 1 ){
590- sendPlain ("6:::" +data [0 ]);
586+ } else if (data .length == 1 ) {
587+ sendPlain ("6:::" + data [0 ]);
591588 }
592589 break ;
593590 case IOMessage .TYPE_ERROR :
@@ -617,11 +614,11 @@ else if(data.length == 1){
617614 * do not shut down TCP-connections when switching from HSDPA to Wifi
618615 */
619616 public void reconnect () {
620- if (wantToDisconnect == false ) {
617+ if (state != STATE_INVALID ) {
621618 if (transport != null )
622619 invalidateTransport ();
623620 transport = null ;
624- connected = false ;
621+ state = STATE_INTERRUPTED ;
625622 if (reconnectTimeoutTimer == null ) {
626623 reconnectTimeoutTimer = new Timer ("reconnectTimeoutTimer" );
627624 reconnectTimeoutTimer .schedule (new ReconnectTimeoutTask (),
@@ -775,6 +772,6 @@ public void emit(SocketIO socket, String event, IOAcknowledge ack,
775772 * @return true, if is connected
776773 */
777774 public boolean isConnected () {
778- return connected ;
775+ return state == STATE_READY ;
779776 }
780777}
0 commit comments