notify remote when BT game deleted

This commit is contained in:
Eric House 2016-12-05 07:12:22 -08:00
parent ef0eeaae2e
commit aa420ce4d1

View file

@ -465,7 +465,11 @@ public class BTService extends XWService {
receiveInvitation( proto, inStream, socket ); receiveInvitation( proto, inStream, socket );
break; break;
case MESG_SEND: case MESG_SEND:
receiveMessage( inStream, socket ); receiveMessage( cmd, inStream, socket );
break;
case MESG_GAMEGONE:
receiveMessage( cmd, inStream, socket );
break; break;
default: default:
@ -561,10 +565,13 @@ public class BTService extends XWService {
socket.close(); socket.close();
} // receiveInvitation } // receiveInvitation
private void receiveMessage( DataInputStream dis, BluetoothSocket socket ) private void receiveMessage( BTCmd cmd, DataInputStream dis, BluetoothSocket socket )
{ {
try { try {
BTCmd result = null;
int gameID = dis.readInt(); int gameID = dis.readInt();
switch ( cmd ) {
case MESG_SEND:
short len = dis.readShort(); short len = dis.readShort();
byte[] buffer = new byte[len]; byte[] buffer = new byte[len];
int nRead = dis.read( buffer, 0, len ); int nRead = dis.read( buffer, 0, len );
@ -575,21 +582,31 @@ public class BTService extends XWService {
CommsAddrRec addr = new CommsAddrRec( host.getName(), CommsAddrRec addr = new CommsAddrRec( host.getName(),
host.getAddress() ); host.getAddress() );
ReceiveResult rslt ReceiveResult rslt
= BTService.this.receiveMessage( BTService.this, gameID, = BTService.this.receiveMessage( BTService.this,
m_btMsgSink, buffer, addr ); gameID, m_btMsgSink,
buffer, addr );
BTCmd result = rslt == ReceiveResult.GAME_GONE ? result = rslt == ReceiveResult.GAME_GONE ?
BTCmd.MESG_GAMEGONE : BTCmd.MESG_ACCPT; BTCmd.MESG_GAMEGONE : BTCmd.MESG_ACCPT;
} else {
DbgUtils.loge( TAG, "receiveMessage(): read only "
+ "%d of %d bytes", nRead, len );
}
break;
case MESG_GAMEGONE:
sendResult( MultiEvent.MESSAGE_NOGAME, gameID );
result = BTCmd.MESG_ACCPT;
break;
default:
result = BTCmd.BAD_PROTO;
break;
}
DataOutputStream os = DataOutputStream os =
new DataOutputStream( socket.getOutputStream() ); new DataOutputStream( socket.getOutputStream() );
os.writeByte( result.ordinal() ); os.writeByte( result.ordinal() );
os.flush(); os.flush();
socket.close(); socket.close();
} else {
DbgUtils.loge( TAG, "receiveMessages: read only %d of %d bytes",
nRead, len );
}
} catch ( IOException ioe ) { } catch ( IOException ioe ) {
logIOE( ioe ); logIOE( ioe );
} }
@ -716,12 +733,17 @@ public class BTService extends XWService {
break; break;
case MESG_SEND: case MESG_SEND:
boolean success = doAnyResends( elem.m_btAddr ) boolean success = doAnyResends( elem.m_btAddr )
&& sendMsg( elem ); && sendElem( elem );
if ( !success ) { if ( !success ) {
addToResends( elem ); addToResends( elem );
} }
updateStatusOut( success ); updateStatusOut( success );
break; break;
case MESG_GAMEGONE:
sendElem( elem );
break;
default: default:
Assert.fail(); Assert.fail();
break; break;
@ -850,7 +872,7 @@ public class BTService extends XWService {
} }
} // sendInvite } // sendInvite
private boolean sendMsg( BTQueueElem elem ) private boolean sendElem( BTQueueElem elem )
{ {
boolean success = false; boolean success = false;
// synchronized( m_deadGames ) { // synchronized( m_deadGames ) {
@ -859,7 +881,7 @@ public class BTService extends XWService {
MultiEvent evt; MultiEvent evt;
if ( success ) { if ( success ) {
evt = MultiEvent.MESSAGE_DROPPED; evt = MultiEvent.MESSAGE_DROPPED;
DbgUtils.logw( TAG, "sendMsg: dropping message %s because game %X dead", DbgUtils.logw( TAG, "sendElem: dropping message %s because game %X dead",
elem.m_cmd, elem.m_gameID ); elem.m_cmd, elem.m_gameID );
} else { } else {
evt = MultiEvent.MESSAGE_REFUSED; evt = MultiEvent.MESSAGE_REFUSED;
@ -872,13 +894,22 @@ public class BTService extends XWService {
createRfcommSocketToServiceRecord( XWApp.getAppUUID() ); createRfcommSocketToServiceRecord( XWApp.getAppUUID() );
if ( null != socket ) { if ( null != socket ) {
DataOutputStream outStream = DataOutputStream outStream =
connect( socket, BTCmd.MESG_SEND ); connect( socket, elem.m_cmd );
if ( null != outStream ) { if ( null != outStream ) {
outStream.writeInt( elem.m_gameID ); outStream.writeInt( elem.m_gameID );
switch ( elem.m_cmd ) {
case MESG_SEND:
short len = (short)elem.m_msg.length; short len = (short)elem.m_msg.length;
outStream.writeShort( len ); outStream.writeShort( len );
outStream.write( elem.m_msg, 0, elem.m_msg.length ); outStream.write( elem.m_msg, 0, elem.m_msg.length );
break;
case MESG_GAMEGONE:
// gameID's all we need
break;
default:
Assert.fail();
}
outStream.flush(); outStream.flush();
Thread killer = killSocketIn( socket ); Thread killer = killSocketIn( socket );
@ -920,7 +951,7 @@ public class BTService extends XWService {
} }
} }
return success; return success;
} // sendMsg } // sendElem
private boolean doAnyResends( LinkedList<BTQueueElem> resends ) private boolean doAnyResends( LinkedList<BTQueueElem> resends )
{ {
@ -930,7 +961,7 @@ public class BTService extends XWService {
ListIterator<BTQueueElem> iter = resends.listIterator(); ListIterator<BTQueueElem> iter = resends.listIterator();
while ( iter.hasNext() && success ) { while ( iter.hasNext() && success ) {
BTQueueElem elem = iter.next(); BTQueueElem elem = iter.next();
success = sendMsg( elem ); success = sendElem( elem );
if ( success ) { if ( success ) {
iter.remove(); iter.remove();
} else if ( elem.failCountExceeded() ) { } else if ( elem.failCountExceeded() ) {