[Connection][WiFi]Apply TizenSynchronizationContext for event handling (#138)
authorchleun-moon <32117100+chleun-moon@users.noreply.github.com>
Wed, 7 Mar 2018 07:05:51 +0000 (16:05 +0900)
committerGitHub <noreply@github.com>
Wed, 7 Mar 2018 07:05:51 +0000 (16:05 +0900)
src/Tizen.Network.Connection/Tizen.Network.Connection.csproj
src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionInternalManager.cs
src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionProfile.cs
src/Tizen.Network.WiFi/Tizen.Network.WiFi.csproj
src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManager.cs
src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManagerImpl.cs
src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiNetworkChange.cs

index 7f4584f..4942b30 100644 (file)
@@ -7,6 +7,7 @@
   <ItemGroup>
     <ProjectReference Include="..\Tizen\Tizen.csproj" />
     <ProjectReference Include="..\Tizen.Log\Tizen.Log.csproj" />
+    <ProjectReference Include="..\Tizen.Applications.Common\Tizen.Applications.Common.csproj" />
   </ItemGroup>
 
 </Project>
index a4ef319..c8fd585 100755 (executable)
@@ -22,19 +22,20 @@ using System.Threading.Tasks;
 using System.Runtime.InteropServices;
 using System.Collections;
 using System.Threading;
+using Tizen.Applications;
 
 namespace Tizen.Network.Connection
 {
     class HandleHolder
     {
         private IntPtr Handle;
-        private int tid;
+        private int _tid;
 
         public HandleHolder()
         {
-            tid = Thread.CurrentThread.ManagedThreadId;
-            Log.Info(Globals.LogTag, "PInvoke connection_destroy for Thread " + tid);
-            int ret = Interop.Connection.Create(tid, out Handle);
+            _tid = Thread.CurrentThread.ManagedThreadId;
+            Log.Info(Globals.LogTag, "PInvoke connection_destroy for Thread " + _tid);
+            int ret = Interop.Connection.Create(_tid, out Handle);
             Log.Info(Globals.LogTag, "Handle: " + Handle);
             if(ret != (int)ConnectionError.None)
             {
@@ -57,8 +58,9 @@ namespace Tizen.Network.Connection
 
         private void Destroy()
         {
-            Log.Info(Globals.LogTag, "PInvoke connection_destroy for Thread " + tid);
-            Interop.Connection.Destroy(tid, Handle);
+
+            Log.Info(Globals.LogTag, "PInvoke connection_destroy for Thread " + _tid);
+            Interop.Connection.Destroy(_tid, Handle);
             if (Handle != IntPtr.Zero)
             {
                 Handle = IntPtr.Zero;
@@ -81,6 +83,8 @@ namespace Tizen.Network.Connection
         private Interop.Connection.ConnectionAddressChangedCallback _proxyAddressChangedCallback;
         private Interop.Connection.EthernetCableStateChangedCallback _ethernetCableStateChangedCallback;
 
+        private TizenSynchronizationContext context = new TizenSynchronizationContext();
+
         internal static ConnectionInternalManager Instance
         {
             get
@@ -102,6 +106,7 @@ namespace Tizen.Network.Connection
 
         ~ConnectionInternalManager()
         {
+            UnregisterEvents();
         }
 
         internal IntPtr GetHandle()
@@ -113,20 +118,25 @@ namespace Tizen.Network.Connection
         {
             add
             {
-                if (_ConnectionTypeChanged == null)
-                {
-                    ConnectionTypeChangedStart();
-                }
-
-                _ConnectionTypeChanged += value;
+                context.Post((x) =>
+                        {
+                            if (_ConnectionTypeChanged == null)
+                            {
+                                ConnectionTypeChangedStart();
+                            }
+                            _ConnectionTypeChanged += value;
+                        }, null);
             }
             remove
             {
-                _ConnectionTypeChanged -= value;
-                if (_ConnectionTypeChanged == null)
-                {
-                    ConnectionTypeChangedStop();
-                }
+                context.Post((x) =>
+                        {
+                            _ConnectionTypeChanged -= value;
+                            if (_ConnectionTypeChanged == null)
+                            {
+                                ConnectionTypeChangedStop();
+                            }
+                        }, null);
             }
         }
 
@@ -162,19 +172,25 @@ namespace Tizen.Network.Connection
         {
             add
             {
-                if (_EthernetCableStateChanged == null)
-                {
-                    EthernetCableStateChangedStart();
-                }
-                _EthernetCableStateChanged += value;
+                context.Post((x) =>
+                        {
+                            if (_EthernetCableStateChanged == null)
+                            {
+                                EthernetCableStateChangedStart();
+                            }
+                            _EthernetCableStateChanged += value;
+                        }, null);
             }
             remove
             {
-                _EthernetCableStateChanged -= value;
-                if (_EthernetCableStateChanged == null)
-                {
-                    EthernetCableStateChangedStop();
-                }
+                context.Post((x) =>
+                        {
+                            _EthernetCableStateChanged -= value;
+                            if (_EthernetCableStateChanged == null)
+                            {
+                                EthernetCableStateChangedStop();
+                            }
+                        }, null);
             }
         }
 
@@ -214,20 +230,26 @@ namespace Tizen.Network.Connection
         {
             add
             {
-                if (_IPAddressChanged == null)
-                {
-                    IPAddressChangedStart();
-                }
-                _IPAddressChanged += value;
+                context.Post((x) =>
+                        {
+                            if (_IPAddressChanged == null)
+                            {
+                                IPAddressChangedStart();
+                            }
+                            _IPAddressChanged += value;
+                        }, null);
             }
 
             remove
             {
-                _IPAddressChanged -= value;
-                if (_IPAddressChanged == null)
-                {
-                    IPAddressChangedStop();
-                }
+                context.Post((x) =>
+                        {
+                            _IPAddressChanged -= value;
+                            if (_IPAddressChanged == null)
+                            {
+                                IPAddressChangedStop();
+                            }
+                        }, null);
             }
         }
 
@@ -267,22 +289,25 @@ namespace Tizen.Network.Connection
         {
             add
             {
-                //Console.WriteLine("ProxyAddressChanged Add **");
-                if (_ProxyAddressChanged == null)
-                {
-                    ProxyAddressChangedStart();
-                }
-
-                _ProxyAddressChanged += value;
+                context.Post((x) =>
+                        {
+                            if (_ProxyAddressChanged == null)
+                            {
+                               ProxyAddressChangedStart();
+                            }
+                            _ProxyAddressChanged += value;
+                        }, null);
             }
             remove
             {
-                //Console.WriteLine("ProxyAddressChanged Remove");
-                _ProxyAddressChanged -= value;
-                if (_ProxyAddressChanged == null)
-                {
-                    ProxyAddressChangedStop();
-                }
+                context.Post((x) =>
+                        {
+                            _ProxyAddressChanged -= value;
+                            if (_ProxyAddressChanged == null)
+                            {
+                                ProxyAddressChangedStop();
+                            }
+                        }, null);
             }
         }
 
index 3215777..f0512f1 100755 (executable)
@@ -19,6 +19,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Runtime.InteropServices;
+using Tizen.Applications;
 
 namespace Tizen.Network.Connection
 {
@@ -36,6 +37,8 @@ namespace Tizen.Network.Connection
 
         private Interop.ConnectionProfile.ProfileStateChangedCallback _profileChangedCallback;
 
+        private TizenSynchronizationContext context = new TizenSynchronizationContext();
+
         internal IntPtr GetHandle()
         {
             return ProfileHandle;
@@ -55,20 +58,26 @@ namespace Tizen.Network.Connection
             add
             {
                 Log.Debug(Globals.LogTag, "ProfileStateChanged add");
-                if (_ProfileStateChanged == null)
-                {
-                    ProfileStateChangedStart();
-                }
-                _ProfileStateChanged += value;
+                context.Post((x) =>
+                        {
+                            if (_ProfileStateChanged == null)
+                            {
+                                ProfileStateChangedStart();
+                            }
+                            _ProfileStateChanged += value;
+                        }, null);
             }
             remove
             {
                 Log.Debug(Globals.LogTag, "ProfileStateChanged remove");
-                _ProfileStateChanged -= value;
-                if (_ProfileStateChanged == null)
-                {
-                    ProfileStateChangedStop();
-                }
+                context.Post((x) =>
+                        {
+                            _ProfileStateChanged -= value;
+                            if (_ProfileStateChanged == null)
+                            {
+                                ProfileStateChangedStop();
+                            }
+                        }, null);
             }
         }
 
@@ -131,12 +140,9 @@ namespace Tizen.Network.Connection
             if (disposed)
                 return;
 
-            if (disposing)
-            {
-                // Free managed objects.
-                UnregisterEvents();
-                Destroy();
-            }
+            // Free unmanaged objects
+            UnregisterEvents();
+            Destroy();
             disposed = true;
         }
 
index b3b1ca1..52d22a1 100644 (file)
@@ -6,6 +6,7 @@
 
   <ItemGroup>
     <ProjectReference Include="..\Tizen.Network.Connection\Tizen.Network.Connection.csproj" />
+    <ProjectReference Include="..\Tizen.Applications.Common\Tizen.Applications.Common.csproj" />
   </ItemGroup>
 
 </Project>
index 96d613c..5cbdad5 100755 (executable)
@@ -29,7 +29,7 @@ namespace Tizen.Network.WiFi
     [EditorBrowsable(EditorBrowsableState.Never)]
     public sealed class SafeWiFiManagerHandle : SafeHandle
     {
-        private int tid;
+        private int _tid;
 
         internal SafeWiFiManagerHandle() : base(IntPtr.Zero, true)
         {
@@ -53,18 +53,15 @@ namespace Tizen.Network.WiFi
         /// </summary>
         protected override bool ReleaseHandle()
         {
-            Interop.WiFi.Deinitialize(tid, this.handle);
+            Interop.WiFi.Deinitialize(_tid, this.handle);
             this.SetHandle(IntPtr.Zero);
             return true;
         }
 
-        internal int TID
+        internal void SetTID(int id)
         {
-            set
-            {
-                tid = value;
-                Log.Info(Globals.LogTag, "New Handle for Thread " + tid);
-            }
+            _tid = id;
+            Log.Info(Globals.LogTag, "New Handle for Thread " + _tid);
         }
     }
 
index 053949a..ada882e 100755 (executable)
@@ -122,7 +122,7 @@ namespace Tizen.Network.WiFi
         {
             get
             {
-                return _instance.Value; 
+               return _instance.Value;
             }
         }
 
@@ -153,7 +153,7 @@ namespace Tizen.Network.WiFi
                 Log.Error(Globals.LogTag, "Failed to initialize wifi, Error - " + (WiFiError)ret);
                 WiFiErrorFactory.ThrowWiFiException(ret, "http://tizen.org/privilege/network.get");
             }
-            handle.TID = tid;
+            handle.SetTID(tid);
             return handle;
         }
 
index ac1eabf..256f61f 100755 (executable)
@@ -16,6 +16,8 @@
 
 using System;
 using System.Threading.Tasks;
+using System.Threading;
+using Tizen.Applications;
 
 namespace Tizen.Network.WiFi
 {
@@ -52,23 +54,30 @@ namespace Tizen.Network.WiFi
         private Interop.WiFi.RssiLevelChangedCallback _rssiChangedCallback;
         private Interop.WiFi.VoidCallback _backgroundScanFinishedCallback;
 
+        private TizenSynchronizationContext context = new TizenSynchronizationContext();
         internal event EventHandler<DeviceStateChangedEventArgs> DeviceStateChanged
         {
             add
             {
-                if (_deviceStateChanged == null)
-                {
-                    RegisterDeviceStateChangedEvent();
-                }
-                _deviceStateChanged += value;
+                context.Post((x) =>
+                        {
+                            if (_deviceStateChanged == null)
+                            {
+                                RegisterDeviceStateChangedEvent(); 
+                            }
+                            _deviceStateChanged += value;
+                        }, null);
             }
             remove
             {
-                _deviceStateChanged -= value;
-                if (_deviceStateChanged == null)
-                {
-                    UnregisterDeviceStateChangedEvent();
-                }
+                context.Post((x) =>
+                        {
+                            _deviceStateChanged -= value;
+                            if (_deviceStateChanged == null)
+                            {
+                                UnregisterDeviceStateChangedEvent();
+                            }
+                        }, null);
             }
         }
 
@@ -76,19 +85,25 @@ namespace Tizen.Network.WiFi
         {
             add
             {
-                if (_connectionStateChanged == null)
-                {
-                    RegisterConnectionStateChangedEvent();
-                }
-                _connectionStateChanged += value;
+                context.Post((x) =>
+                        {
+                            if (_connectionStateChanged == null)
+                            {
+                                RegisterConnectionStateChangedEvent();
+                            }
+                            _connectionStateChanged += value;
+                        }, null);
             }
             remove
             {
-                _connectionStateChanged -= value;
-                if (_connectionStateChanged == null)
-                {
-                    UnregisterConnectionStateChangedEvent();
-                }
+                context.Post((x) =>
+                        {
+                            _connectionStateChanged -= value;
+                            if (_connectionStateChanged == null)
+                            {
+                                UnregisterConnectionStateChangedEvent();
+                            }
+                        }, null);
             }
         }
 
@@ -96,19 +111,25 @@ namespace Tizen.Network.WiFi
         {
             add
             {
-                if (_rssiLevelChanged == null)
-                {
-                    RegisterRssiLevelChangedEvent();
-                }
-                _rssiLevelChanged += value;
+                context.Post((x) =>
+                        {
+                            if (_rssiLevelChanged == null)
+                            {
+                                RegisterRssiLevelChangedEvent();
+                            }
+                            _rssiLevelChanged += value;
+                        }, null);
             }
             remove
             {
-                _rssiLevelChanged -= value;
-                if (_rssiLevelChanged == null)
-                {
-                    UnregisterRssiLevelChangedEvent();
-                }
+                context.Post((x) =>
+                        {
+                            _rssiLevelChanged -= value;
+                            if (_rssiLevelChanged == null)
+                            {
+                                UnregisterRssiLevelChangedEvent();
+                            }
+                        }, null);
             }
         }
 
@@ -116,24 +137,31 @@ namespace Tizen.Network.WiFi
         {
             add
             {
-                if (_backgroundScanFinished == null)
-                {
-                    RegisterBackgroundScanFinishedEvent();
-                }
-                _backgroundScanFinished += value;
+                context.Post((x) =>
+                        {
+                            if (_backgroundScanFinished == null)
+                            {
+                                RegisterBackgroundScanFinishedEvent();
+                            }
+                            _backgroundScanFinished += value;
+                        }, null);
             }
             remove
             {
-                _backgroundScanFinished -= value;
-                if (_backgroundScanFinished == null)
-                {
-                    UnregisterBackgroundScanFinishedEvent();
-                }
+                context.Post((x) =>
+                        {
+                            _backgroundScanFinished -= value;
+                            if (_backgroundScanFinished == null)
+                            {
+                                UnregisterBackgroundScanFinishedEvent();
+                            }
+                        }, null);
             }
         }
 
         private void RegisterDeviceStateChangedEvent()
         {
+            Log.Info(Globals.LogTag, "RegisterDeviceStateChangedEvent in Thread " + Thread.CurrentThread.ManagedThreadId);
             _deviceChangedCallback = (int deviceState, IntPtr userDate) =>
             {
                 WiFiDeviceState state = (WiFiDeviceState)deviceState;
@@ -149,6 +177,7 @@ namespace Tizen.Network.WiFi
 
         private void UnregisterDeviceStateChangedEvent()
         {
+            Log.Info(Globals.LogTag, "UnregisterDeviceStateChangedEvent in Thread " + Thread.CurrentThread.ManagedThreadId);
             int ret = Interop.WiFi.UnsetDeviceStateChangedCallback(GetSafeHandle());
             if (ret != (int)WiFiError.None)
             {