From 930146cf11714798635cbe3772009b41252ca38a Mon Sep 17 00:00:00 2001 From: Eric House Date: Thu, 17 Nov 2016 23:22:28 -0800 Subject: [PATCH] wifi tweaks Show net status as failing when no sockets available, and notice when they're closed. --- .../org/eehouse/android/xw4/BiDiSockWrap.java | 5 ++-- .../org/eehouse/android/xw4/WiDirService.java | 24 ++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BiDiSockWrap.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BiDiSockWrap.java index 465c9c4e0..088f96653 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BiDiSockWrap.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BiDiSockWrap.java @@ -133,7 +133,7 @@ public class BiDiSockWrap { DbgUtils.logex( ioe ); } mIface.connectStateChanged( this, false ); - mQueue.add(null); + mQueue.add( new byte[0] ); } private void startThreads() @@ -150,7 +150,8 @@ public class BiDiSockWrap { DbgUtils.logd( BiDiSockWrap.class, "write thread got packet of len %d", packet.length ); - if ( null == packet || 0 == packet.length ) { + Assert.assertNotNull( packet ); + if ( 0 == packet.length ) { closeSocket(); break; } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/WiDirService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/WiDirService.java index 52bd82d8d..f55ea61a9 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/WiDirService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/WiDirService.java @@ -192,9 +192,10 @@ public class WiDirService extends XWService { public static String formatNetStateInfo() { - return String.format( "name: %s; mac: %s; role: %s", + return String.format( "name: %s; mac: %s; role: %s; nThreads: %d", sDeviceName, getMyMacAddress(), - sAmServer ? "group owner" : "guest" ); + sAmServer ? "owner" : "guest", + Thread.activeCount() ); } private static String getMyMacAddress() { return getMyMacAddress(null); } @@ -232,6 +233,7 @@ public class WiDirService extends XWService { public static int sendPacket( Context context, String macAddr, int gameID, byte[] buf ) { + DbgUtils.logd( CLAZZ, "sendPacket(len=%d,addr=%s)", buf.length, macAddr ); int nSent = -1; boolean[] forwarding = { false }; @@ -314,6 +316,7 @@ public class WiDirService extends XWService { public void connectStateChanged( BiDiSockWrap wrap, boolean nowConnected ) { + DbgUtils.logd( CLAZZ, "connectStateChanged(con=%b)", nowConnected ); if ( nowConnected ) { try { wrap.send( new JSONObject() @@ -323,6 +326,15 @@ public class WiDirService extends XWService { } catch ( JSONException jse ) { DbgUtils.logex( jse ); } + } else { + int sizeBefore = sSocketWrapMap.size(); + sSocketWrapMap.values().remove( wrap ); + DbgUtils.logd( CLAZZ, "removed wrap; had %d, now have %d", + sizeBefore, sSocketWrapMap.size() ); + if ( 0 == sSocketWrapMap.size() ) { + updateStatusIn( false ); + updateStatusOut( false ); + } } } @@ -357,6 +369,7 @@ public class WiDirService extends XWService { } } } + DbgUtils.logd( CLAZZ, "thread count: %d", Thread.activeCount() ); new Handler().postDelayed( new Runnable() { @Override public void run() { @@ -470,7 +483,9 @@ public class WiDirService extends XWService { discoverServices(); } @Override - public void onFailure(int code) { Assert.fail(); } + public void onFailure(int code) { + tryAgain( "discoverPeers", code ); + } } ); } @@ -788,6 +803,9 @@ public class WiDirService extends XWService { forwarding[0] = true; } + if ( null == wrap ) { + updateStatusOut( false ); + } return wrap; }