From fc3d26d626b7a82681e00b4dfe06b46022327b6f Mon Sep 17 00:00:00 2001 From: chentao Date: Thu, 25 Feb 2016 00:04:57 +0800 Subject: [PATCH] Fix binder ipc issue when process died unexpected --- .../com/github/shadowsocks/ServiceBoundContext.scala | 8 ++++---- src/main/scala/com/github/shadowsocks/Shadowsocks.scala | 6 +++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/scala/com/github/shadowsocks/ServiceBoundContext.scala b/src/main/scala/com/github/shadowsocks/ServiceBoundContext.scala index 34ffe537..6cba8d7d 100644 --- a/src/main/scala/com/github/shadowsocks/ServiceBoundContext.scala +++ b/src/main/scala/com/github/shadowsocks/ServiceBoundContext.scala @@ -4,16 +4,15 @@ import android.content.{ComponentName, Context, Intent, ServiceConnection} import android.os.{RemoteException, IBinder} import com.github.shadowsocks.aidl.{IShadowsocksServiceCallback, IShadowsocksService} import com.github.shadowsocks.utils.Action -import com.github.shadowsocks.utils.Utils /** * @author Mygod */ -trait ServiceBoundContext extends Context { - +trait ServiceBoundContext extends Context { outer => class ShadowsocksServiceConnection extends ServiceConnection { override def onServiceConnected(name: ComponentName, service: IBinder) { bgService = IShadowsocksService.Stub.asInterface(service) + bgService.asBinder().linkToDeath(new ShadowsocksDeathRecipient(outer), 0) registerCallback ServiceBoundContext.this.onServiceConnected() } @@ -33,11 +32,12 @@ trait ServiceBoundContext extends Context { } catch { case ignored: RemoteException => // Nothing } + def unregisterCallback = if (bgService != null && callback != null && callbackRegistered) try { bgService.unregisterCallback(callback) callbackRegistered = false } catch { - case ignored: RemoteException => // Nothing + case ignored: RemoteException => callbackRegistered = false } def onServiceConnected() = () diff --git a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala index 84a8602f..bcc2b6cc 100644 --- a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala +++ b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala @@ -377,7 +377,11 @@ class Shadowsocks }) updateTraffic(0, 0, 0, 0) - // Bind to the service + bindToService() + } + + // Bind to the service + def bindToService() { handler.post(() => attachService) }