track time since last game-related packet, not any packet from relay.

This commit is contained in:
Eric House 2013-08-15 20:05:10 -07:00
parent 10ef75113f
commit 8c56f55445

View file

@ -85,7 +85,7 @@ public class RelayService extends XWService
private Handler m_handler; private Handler m_handler;
private Runnable m_onInactivity; private Runnable m_onInactivity;
private short m_maxIntervalSeconds = 5; // give time to get real value private short m_maxIntervalSeconds = 5; // give time to get real value
private long m_lastPacketReceived; private long m_lastGamePacketReceived;
// These must match the enum XWRelayReg in xwrelay.h // These must match the enum XWRelayReg in xwrelay.h
private static final int XWPDEV_PROTO_VERSION = 0; private static final int XWPDEV_PROTO_VERSION = 0;
@ -187,7 +187,7 @@ public class RelayService extends XWService
public void onCreate() public void onCreate()
{ {
super.onCreate(); super.onCreate();
m_lastPacketReceived = m_lastGamePacketReceived =
XWPrefs.getPrefsLong( this, R.string.key_last_packet, 0 ); XWPrefs.getPrefsLong( this, R.string.key_last_packet, 0 );
m_handler = new Handler(); m_handler = new Handler();
@ -250,7 +250,7 @@ public class RelayService extends XWService
break; break;
case TIMER_FIRED: case TIMER_FIRED:
if ( !startFetchThreadIf() ) { if ( !startFetchThreadIf() ) {
requestMessages(); sendKeepAlive();
} }
break; break;
default: default:
@ -272,7 +272,7 @@ public class RelayService extends XWService
{ {
DbgUtils.logf( "RelayService.onDestroy() called" ); DbgUtils.logf( "RelayService.onDestroy() called" );
XWPrefs.setPrefsLong( this, R.string.key_last_packet, XWPrefs.setPrefsLong( this, R.string.key_last_packet,
m_lastPacketReceived ); m_lastGamePacketReceived );
if ( shouldMaintainConnection() ) { if ( shouldMaintainConnection() ) {
long interval_millis = m_maxIntervalSeconds * 1000; long interval_millis = m_maxIntervalSeconds * 1000;
@ -355,7 +355,6 @@ public class RelayService extends XWService
+ "on receive" ); + "on receive" );
m_UDPSocket.receive( packet ); m_UDPSocket.receive( packet );
resetExitTimer(); resetExitTimer();
m_lastPacketReceived = Utils.getCurSeconds();
DbgUtils.logf( "UPD read thread: " DbgUtils.logf( "UPD read thread: "
+ "receive returned" ); + "receive returned" );
} catch( java.io.IOException ioe ) { } catch( java.io.IOException ioe ) {
@ -511,6 +510,8 @@ public class RelayService extends XWService
requestMessages(); requestMessages();
break; break;
case XWPDEV_MSG: case XWPDEV_MSG:
// game-related packets only count
m_lastGamePacketReceived = Utils.getCurSeconds();
int token = dis.readInt(); int token = dis.readInt();
byte[] msg = new byte[dis.available()]; byte[] msg = new byte[dis.available()];
Assert.assertTrue( packet.getLength() >= msg.length ); Assert.assertTrue( packet.getLength() >= msg.length );
@ -549,13 +550,12 @@ public class RelayService extends XWService
} }
} }
private void requestMessages() private void requestMessagesImpl( XWRelayReg reg )
{ {
DbgUtils.logf( "requestMessages" ); DbgUtils.logf( "requestMessagesImpl" );
ByteArrayOutputStream bas = new ByteArrayOutputStream(); ByteArrayOutputStream bas = new ByteArrayOutputStream();
try { try {
DataOutputStream out = DataOutputStream out = addProtoAndCmd( bas, reg );
addProtoAndCmd( bas, XWRelayReg.XWPDEV_RQSTMSGS );
String devid = getDevID( null ); String devid = getDevID( null );
out.writeShort( devid.length() ); out.writeShort( devid.length() );
out.writeBytes( devid ); out.writeBytes( devid );
@ -565,6 +565,16 @@ public class RelayService extends XWService
} }
} }
private void requestMessages()
{
requestMessagesImpl( XWRelayReg.XWPDEV_RQSTMSGS );
}
private void sendKeepAlive()
{
requestMessagesImpl( XWRelayReg.XWPDEV_KEEPALIVE );
}
private void sendMessage( long rowid, byte[] msg ) private void sendMessage( long rowid, byte[] msg )
{ {
ByteArrayOutputStream bas = new ByteArrayOutputStream(); ByteArrayOutputStream bas = new ByteArrayOutputStream();
@ -951,7 +961,7 @@ public class RelayService extends XWService
{ {
boolean result = !s_gcmWorking; boolean result = !s_gcmWorking;
if ( result ) { if ( result ) {
long interval = Utils.getCurSeconds() - m_lastPacketReceived; long interval = Utils.getCurSeconds() - m_lastGamePacketReceived;
result = interval < MAX_KEEPALIVE_SECS; result = interval < MAX_KEEPALIVE_SECS;
} }
DbgUtils.logf( "RelayService.shouldMaintainConnection=>%b", result ); DbgUtils.logf( "RelayService.shouldMaintainConnection=>%b", result );