From cd3ff71efbd48fbd0ca1a15b1a4262f6fe2c39dd Mon Sep 17 00:00:00 2001 From: Andy2 Date: Mon, 13 Dec 2010 18:30:22 -0800 Subject: [PATCH] notify comms layer when unable to establish connection. This allows comms logic to retry and fixes failure on device to detect when network comes up after being down when game was opened. --- .../eehouse/android/xw4/CommsTransport.java | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/CommsTransport.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/CommsTransport.java index c15996e56..a5f11006d 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/CommsTransport.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/CommsTransport.java @@ -100,6 +100,7 @@ public class CommsTransport implements TransportProcs { @Override public void run() { + boolean failed = true; try { if ( Build.PRODUCT.contains("sdk") ) { System.setProperty("java.net.preferIPv6Addresses", "false"); @@ -107,7 +108,7 @@ public class CommsTransport implements TransportProcs { m_selector = Selector.open(); - loop(); + failed = loop(); closeSocket(); } catch ( java.io.IOException ioe ) { @@ -117,11 +118,16 @@ public class CommsTransport implements TransportProcs { m_addr.ip_relay_hostName, m_addr.ip_relay_port, uae.toString() ); } + m_thread = null; + if ( failed ) { + m_jniThread.handle( JNICmd.CMD_TRANSFAIL ); + } } - private void loop() + private boolean loop() { + boolean failed = false; outer_loop: while ( !m_done ) { try { @@ -136,12 +142,14 @@ public class CommsTransport implements TransportProcs { Utils.logf( "connecting to %s:%d", m_addr.ip_relay_hostName, m_addr.ip_relay_port ); - InetSocketAddress isa - = new InetSocketAddress( m_addr.ip_relay_hostName, - m_addr.ip_relay_port ); + InetSocketAddress isa = new + InetSocketAddress(m_addr.ip_relay_hostName, + m_addr.ip_relay_port ); m_socketChannel.connect( isa ); } catch ( java.io.IOException ioe ) { Utils.logf( ioe.toString() ); + failed = true; + break outer_loop; } } @@ -154,7 +162,7 @@ public class CommsTransport implements TransportProcs { m_selector.select(); } catch ( ClosedChannelException cce ) { // we get this when relay goes down. Need to notify! - m_jniThread.handle( JNICmd.CMD_TRANSFAIL ); + failed = true; closeSocket(); Utils.logf( "exiting: " + cce.toString() ); break; // don't try again @@ -162,6 +170,10 @@ public class CommsTransport implements TransportProcs { closeSocket(); Utils.logf( "exiting: " + ioe.toString() ); Utils.logf( ioe.toString() ); + } catch ( java.nio.channels.NoConnectionPendingException ncp ) { + Utils.logf( "%s", ncp.toString() ); + closeSocket(); + break; } Iterator iter = m_selector.selectedKeys().iterator(); @@ -196,12 +208,17 @@ public class CommsTransport implements TransportProcs { } catch ( java.io.IOException ioe ) { Utils.logf( "%s: cancelling key", ioe.toString() ); key.cancel(); + failed = true; + break outer_loop; + } catch ( java.nio.channels. + NoConnectionPendingException ncp ) { + Utils.logf( "%s", ncp.toString() ); break outer_loop; } } } + return failed; } // loop - } public void setReceiver( JNIThread jnit )