mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-08 05:24:39 +01:00
kill service after 3 minutes of no socket activity
This commit is contained in:
parent
d436232800
commit
bf39f9194c
1 changed files with 26 additions and 3 deletions
|
@ -24,6 +24,7 @@ import android.app.Service;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
import android.os.Handler;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
@ -72,6 +73,8 @@ public class RelayService extends XWService {
|
||||||
private DatagramSocket m_UDPSocket;
|
private DatagramSocket m_UDPSocket;
|
||||||
private LinkedBlockingQueue<DatagramPacket> m_queue =
|
private LinkedBlockingQueue<DatagramPacket> m_queue =
|
||||||
new LinkedBlockingQueue<DatagramPacket>();
|
new LinkedBlockingQueue<DatagramPacket>();
|
||||||
|
private Handler m_handler;
|
||||||
|
private Runnable m_killer;
|
||||||
|
|
||||||
// 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;
|
||||||
|
@ -155,6 +158,13 @@ public class RelayService extends XWService {
|
||||||
{
|
{
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
startFetchThreadIf();
|
startFetchThreadIf();
|
||||||
|
m_handler = new Handler();
|
||||||
|
m_killer = new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
// DbgUtils.logf( "RelayService: m_killer fired" );
|
||||||
|
stopSelf();
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -211,7 +221,8 @@ public class RelayService extends XWService {
|
||||||
result = Service.START_STICKY;
|
result = Service.START_STICKY;
|
||||||
} else {
|
} else {
|
||||||
result = Service.START_STICKY_COMPATIBILITY;
|
result = Service.START_STICKY_COMPATIBILITY;
|
||||||
}
|
}
|
||||||
|
resetExitTimer();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -278,6 +289,7 @@ public class RelayService extends XWService {
|
||||||
DbgUtils.logf( "UPD read thread blocking "
|
DbgUtils.logf( "UPD read thread blocking "
|
||||||
+ "on receive" );
|
+ "on receive" );
|
||||||
m_UDPSocket.receive( packet );
|
m_UDPSocket.receive( packet );
|
||||||
|
resetExitTimer();
|
||||||
DbgUtils.logf( "UPD read thread: "
|
DbgUtils.logf( "UPD read thread: "
|
||||||
+ "receive returned" );
|
+ "receive returned" );
|
||||||
} catch( java.io.IOException ioe ) {
|
} catch( java.io.IOException ioe ) {
|
||||||
|
@ -296,9 +308,8 @@ public class RelayService extends XWService {
|
||||||
DbgUtils.logf( "m_UDPReadThread not null and assumed to "
|
DbgUtils.logf( "m_UDPReadThread not null and assumed to "
|
||||||
+ "be running" );
|
+ "be running" );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
} // startUDPThreadsIfNot
|
||||||
|
|
||||||
// Some of this must not be done on main (UI) thread
|
// Some of this must not be done on main (UI) thread
|
||||||
private void connectSocket()
|
private void connectSocket()
|
||||||
|
@ -347,6 +358,7 @@ public class RelayService extends XWService {
|
||||||
outPacket.getLength() );
|
outPacket.getLength() );
|
||||||
try {
|
try {
|
||||||
m_UDPSocket.send( outPacket );
|
m_UDPSocket.send( outPacket );
|
||||||
|
resetExitTimer();
|
||||||
} catch ( java.io.IOException ioe ) {
|
} catch ( java.io.IOException ioe ) {
|
||||||
DbgUtils.loge( ioe );
|
DbgUtils.loge( ioe );
|
||||||
}
|
}
|
||||||
|
@ -814,6 +826,17 @@ public class RelayService extends XWService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Called from any thread
|
||||||
|
private void resetExitTimer()
|
||||||
|
{
|
||||||
|
// DbgUtils.logf( "RelayService.resetExitTimer()" );
|
||||||
|
m_handler.removeCallbacks( m_killer );
|
||||||
|
|
||||||
|
// UDP socket's no good as a return address after 2 minutes of
|
||||||
|
// in activity, so take down the service after that time.
|
||||||
|
m_handler.postDelayed( m_killer, 3 * 60 * 1000 );
|
||||||
|
}
|
||||||
|
|
||||||
private class PacketHeader {
|
private class PacketHeader {
|
||||||
public int m_packetID;
|
public int m_packetID;
|
||||||
public XWRelayReg m_cmd;
|
public XWRelayReg m_cmd;
|
||||||
|
|
Loading…
Reference in a new issue