Ensure native session is disconnected before resource free.
@@ -13,6 +13,7 @@ package com.freerdp.freerdpcore.services;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
+import android.support.v4.util.LongSparseArray;
import android.util.Log;
import com.freerdp.freerdpcore.application.GlobalApp;
@@ -28,6 +29,8 @@ public class LibFreeRDP {
private static EventListener listener;
private static boolean mHasH264 = true;
+ private static final LongSparseArray<Boolean> mInstanceState = new LongSparseArray<>();
+
static {
final String h264 = "openh264";
final String[] libraries = {
@@ -92,20 +95,47 @@ public class LibFreeRDP {
}
public static void freeInstance(long inst) {
+ synchronized (mInstanceState) {
+ if (mInstanceState.get(inst, false)) {
+ freerdp_disconnect(inst);
+ }
+ while(mInstanceState.get(inst)) {
+ try {
+ mInstanceState.wait();
+ } catch (InterruptedException e) {
+ throw new RuntimeException();
+ }
+ }
+ }
freerdp_free(inst);
}
public static boolean connect(long inst) {
+ synchronized (mInstanceState) {
+ if (mInstanceState.get(inst, false)) {
+ throw new RuntimeException("instance already connected");
+ }
+ }
return freerdp_connect(inst);
}
public static boolean disconnect(long inst) {
+ synchronized (mInstanceState) {
+ if (mInstanceState.get(inst)) {
return freerdp_disconnect(inst);
}
+ return true;
+ }
+ }
public static boolean cancelConnection(long inst) {
+ synchronized (mInstanceState) {
+ if (mInstanceState.get(inst)) {
return freerdp_disconnect(inst);
}
+ return true;
+ }
+ }
private static String addFlag(String name, boolean enabled) {
if (enabled) {
@@ -337,6 +367,10 @@ public class LibFreeRDP {
private static void OnConnectionSuccess(long inst) {
if (listener != null)
listener.OnConnectionSuccess(inst);
+ synchronized (mInstanceState) {
+ mInstanceState.append(inst, true);
+ mInstanceState.notifyAll();
+ }
}
private static void OnConnectionFailure(long inst) {
@@ -352,6 +386,10 @@ public class LibFreeRDP {
private static void OnDisconnecting(long inst) {
if (listener != null)
listener.OnDisconnecting(inst);
+ synchronized (mInstanceState) {
+ mInstanceState.remove(inst);
+ mInstanceState.notifyAll();
+ }
}
private static void OnDisconnected(long inst) {
