From 7d61e09f7a8b607a9c236b3270da0ad5b0c890f7 Mon Sep 17 00:00:00 2001 From: chleun-moon <32117100+chleun-moon@users.noreply.github.com> Date: Tue, 27 Oct 2020 11:02:37 +0900 Subject: [PATCH] [Tizen.Network.Connection] Make callbacks to member variable (#1608) (#2121) --- .../ConnectionInternalManager.cs | 103 ++++++++++++++------- 1 file changed, 67 insertions(+), 36 deletions(-) diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionInternalManager.cs b/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionInternalManager.cs index 42301a5..7d015e9 100644 --- a/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionInternalManager.cs +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionInternalManager.cs @@ -85,6 +85,10 @@ namespace Tizen.Network.Connection private TizenSynchronizationContext context = new TizenSynchronizationContext(); + private Dictionary _callback_map = + new Dictionary(); + private int _requestId = 0; + internal static ConnectionInternalManager Instance { get @@ -867,26 +871,35 @@ namespace Tizen.Network.Connection if (profile != null) { TaskCompletionSource task = new TaskCompletionSource(); - Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) => + IntPtr id; + lock (_callback_map) { - Log.Info(Globals.LogTag, "SetDefaultCellularProfile done " + profile.Name); - if (Result != ConnectionError.None) + id = (IntPtr)_requestId++; + _callback_map[id] = (error, key) => { - Log.Error(Globals.LogTag, "Error occurs during set default cellular profile, " + Result); - task.SetException(new InvalidOperationException("Error occurs during set default cellular profile, " + Result)); - } - else - { - task.SetResult(true); - } - }; + Log.Info(Globals.LogTag, "SetDefaultCellularProfile done " + profile.Name); + if (error != ConnectionError.None) + { + Log.Error(Globals.LogTag, "Error occurs during set default cellular profile, " + error); + task.SetException(new InvalidOperationException("Error occurs during set default cellular profile, " + error)); + } + else + { + task.SetResult(true); + } + lock (_callback_map) + { + _callback_map.Remove(key); + } + }; + } context.Post((x) => { Log.Info(Globals.LogTag, "Interop.Connection.SetDefaultCellularServiceProfileAsync " + profile.Name); try { - int ret = Interop.Connection.SetDefaultCellularServiceProfileAsync(GetHandle(), (int)type, profile.ProfileHandle, Callback, (IntPtr)0); + int ret = Interop.Connection.SetDefaultCellularServiceProfileAsync(GetHandle(), (int)type, profile.ProfileHandle, _callback_map[id], id); if ((ConnectionError)ret != ConnectionError.None) { @@ -965,26 +978,35 @@ namespace Tizen.Network.Connection { Log.Debug(Globals.LogTag, "OpenProfileAsync " + profile.Name); TaskCompletionSource task = new TaskCompletionSource(); - Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) => + IntPtr id; + lock (_callback_map) { - Log.Info(Globals.LogTag, "OpenProfileAsync done " + profile.Name); - if (Result != ConnectionError.None) - { - Log.Error(Globals.LogTag, "Error occurs during connecting profile, " + Result); - task.SetException(new InvalidOperationException("Error occurs during connecting profile, " + Result)); - } - else + id = (IntPtr)_requestId++; + _callback_map[id] = (error, key) => { - task.SetResult(true); - } - }; + Log.Info(Globals.LogTag, "OpenProfileAsync done " + profile.Name); + if (error != ConnectionError.None) + { + Log.Error(Globals.LogTag, "Error occurs during connecting profile, " + error); + task.SetException(new InvalidOperationException("Error occurs during connecting profile, " + error)); + } + else + { + task.SetResult(true); + } + lock (_callback_map) + { + _callback_map.Remove(key); + } + }; + } context.Post((x) => { Log.Info(Globals.LogTag, "Interop.Connection.OpenProfile " + profile.Name); try { - int ret = Interop.Connection.OpenProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero); + int ret = Interop.Connection.OpenProfile(GetHandle(), profile.ProfileHandle, _callback_map[id], id); if ((ConnectionError)ret != ConnectionError.None) { Log.Error(Globals.LogTag, "It failed to connect profile, " + (ConnectionError)ret); @@ -1016,26 +1038,35 @@ namespace Tizen.Network.Connection { Log.Info(Globals.LogTag, "CloseProfileAsync " + profile.Name); TaskCompletionSource task = new TaskCompletionSource(); - Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) => + IntPtr id; + lock (_callback_map) { - Log.Info(Globals.LogTag, "CloseProfileAsync done " + profile.Name); - if (Result != ConnectionError.None) - { - Log.Error(Globals.LogTag, "Error occurs during disconnecting profile, " + Result); - task.SetException(new InvalidOperationException("Error occurs during disconnecting profile, " + Result)); - } - else + id = (IntPtr)_requestId++; + _callback_map[id] = (error, key) => { - task.SetResult(true); - } - }; + Log.Info(Globals.LogTag, "CloseProfileAsync done " + profile.Name); + if (error!= ConnectionError.None) + { + Log.Error(Globals.LogTag, "Error occurs during disconnecting profile, " + error); + task.SetException(new InvalidOperationException("Error occurs during disconnecting profile, " + error)); + } + else + { + task.SetResult(true); + } + lock (_callback_map) + { + _callback_map.Remove(key); + } + }; + } context.Post((x) => { Log.Info(Globals.LogTag, "Interop.Connection.CloseProfile " + profile.Name); try { - int ret = Interop.Connection.CloseProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero); + int ret = Interop.Connection.CloseProfile(GetHandle(), profile.ProfileHandle, _callback_map[id], id); if ((ConnectionError)ret != ConnectionError.None) { Log.Error(Globals.LogTag, "It failed to disconnect profile, " + (ConnectionError)ret); -- 2.7.4