use AutoCloseable rather than a hack of my own

This commit is contained in:
Eric House 2018-12-11 15:14:51 -08:00
parent f7258f29e8
commit c2f5949623

View file

@ -1076,22 +1076,20 @@ public class BTService extends XWService {
if ( null != os ) { if ( null != os ) {
os.writeInt( gameID ); os.writeInt( gameID );
os.flush(); os.flush();
Thread killer = killSocketIn( socket, 5 ); try ( KillerIn killer = new KillerIn( socket, 5 ) ) {
DataInputStream is =
DataInputStream is = new DataInputStream( socket.getInputStream() );
new DataInputStream( socket.getInputStream() ); BTCmd reply = BTCmd.values()[is.readByte()];
BTCmd reply = BTCmd.values()[is.readByte()]; if ( BTCmd.BAD_PROTO == reply ) {
if ( BTCmd.BAD_PROTO == reply ) { sendBadProto( socket );
sendBadProto( socket ); } else {
} else { gotReply = BTCmd.PONG == reply;
gotReply = BTCmd.PONG == reply; if ( gotReply && is.readBoolean() ) {
if ( gotReply && is.readBoolean() ) { mHelper.postEvent( MultiEvent.MESSAGE_NOGAME, gameID );
mHelper.postEvent( MultiEvent.MESSAGE_NOGAME, gameID ); }
} }
} }
receiveWorking = true; receiveWorking = true;
killer.interrupt();
sendWorking = true; sendWorking = true;
} }
socket.close(); socket.close();
@ -1210,12 +1208,13 @@ public class BTService extends XWService {
} }
outStream.flush(); outStream.flush();
Thread killer = killSocketIn( socket );
DataInputStream inStream = BTCmd reply;
new DataInputStream( socket.getInputStream() ); try ( KillerIn killer = new KillerIn( socket, 10 ) ) {
BTCmd reply = BTCmd.values()[inStream.readByte()]; DataInputStream inStream =
killer.interrupt(); new DataInputStream( socket.getInputStream() );
reply = BTCmd.values()[inStream.readByte()];
}
success = true; success = true;
switch ( reply ) { switch ( reply ) {
@ -1466,21 +1465,16 @@ public class BTService extends XWService {
.updateStatusIn( this, null, CommsConnType.COMMS_CONN_BT, success ); .updateStatusIn( this, null, CommsConnType.COMMS_CONN_BT, success );
} }
private Thread killSocketIn( final BluetoothSocket socket ) private class KillerIn implements AutoCloseable {
{ private final Thread mThread;
return killSocketIn( socket, 10 ); KillerIn( final BluetoothSocket socket, final int seconds )
} {
mThread = new Thread( new Runnable() {
private Thread killSocketIn( final BluetoothSocket socket, int seconds )
{
final int millis = seconds * 1000;
Thread thread = new Thread( new Runnable() {
public void run() { public void run() {
try { try {
Thread.sleep( millis ); Thread.sleep( 1000 * seconds );
} catch ( InterruptedException ie ) { } catch ( InterruptedException ie ) {
Log.w( TAG, "killSocketIn: killed by owner" ); Log.w( TAG, "KillerIn: killed by owner" );
return;
} }
try { try {
socket.close(); socket.close();
@ -1488,9 +1482,15 @@ public class BTService extends XWService {
Log.ex( TAG, ioe ); Log.ex( TAG, ioe );
} }
} }
} ); });
thread.start(); mThread.start();
return thread; }
@Override
public void close()
{
mThread.interrupt();
}
} }
private class BTMsgSink extends MultiMsgSink { private class BTMsgSink extends MultiMsgSink {