]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
MODLANG-165 Added wrapper for switch_event_bind for .net
authorJeff Lenk <jeff@jefflenk.com>
Tue, 1 Jun 2010 15:23:04 +0000 (10:23 -0500)
committerJeff Lenk <jeff@jefflenk.com>
Tue, 1 Jun 2010 15:23:04 +0000 (10:23 -0500)
src/mod/languages/mod_managed/managed/EventBinding.cs [new file with mode: 0644]
src/mod/languages/mod_managed/managed/FreeSWITCH.Managed.csproj

diff --git a/src/mod/languages/mod_managed/managed/EventBinding.cs b/src/mod/languages/mod_managed/managed/EventBinding.cs
new file mode 100644 (file)
index 0000000..f8de889
--- /dev/null
@@ -0,0 +1,119 @@
+/* \r
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application - mod_managed\r
+ * Copyright (C) 2008, Michael Giagnocavo <mgg@giagnocavo.net>\r
+ *\r
+ * Version: MPL 1.1\r
+ *\r
+ * The contents of this file are subject to the Mozilla Public License Version\r
+ * 1.1 (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ * http://www.mozilla.org/MPL/\r
+ *\r
+ * Software distributed under the License is distributed on an "AS IS" basis,\r
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License\r
+ * for the specific language governing rights and limitations under the\r
+ * License.\r
+ *\r
+ * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application - mod_managed\r
+ *\r
+ * The Initial Developer of the Original Code is\r
+ * Michael Giagnocavo <mgg@giagnocavo.net>\r
+ * Portions created by the Initial Developer are Copyright (C)\r
+ * the Initial Developer. All Rights Reserved.\r
+ *\r
+ * Contributor(s):\r
+ * \r
+ * Michael Giagnocavo <mgg@giagnocavo.net>\r
+ * \r
+ * EventBinding.cs - Helpers for switch_event_bind function\r
+ *\r
+ */\r
+using System;\r
+using System.Collections.Generic;\r
+using System.Linq;\r
+using System.Text;\r
+using System.Runtime.InteropServices;\r
+using System.Reflection;\r
+using FreeSWITCH.Native;\r
+\r
+namespace FreeSWITCH\r
+{\r
+\r
+    public class EventBinding : IDisposable\r
+    {\r
+\r
+        public class EventBindingArgs : EventArgs\r
+        {\r
+            public switch_event EventObj { get; set; }\r
+        }\r
+\r
+        //typedef void (*switch_event_callback_t) (switch_event_t *);\r
+\r
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]\r
+        delegate void switch_event_callback_delegate(IntPtr event_data);\r
+\r
+        readonly switch_event_callback_delegate del; // Prevent GC\r
+        readonly SWIGTYPE_p_f_p_switch_event__void function;\r
+\r
+        private EventBinding(SWIGTYPE_p_f_p_switch_event__void function, switch_event_callback_delegate origDelegate)\r
+        {\r
+            this.function = function;\r
+            this.del = origDelegate;\r
+        }\r
+        bool disposed;\r
+        public void Dispose()\r
+        {\r
+            dispose();\r
+            GC.SuppressFinalize(this);\r
+        }\r
+        void dispose()\r
+        {\r
+            if (disposed) return;\r
+            // HACK: FS crashes if we unbind after shutdown is pretty complete. This is still a race condition.\r
+            if (freeswitch.switch_core_ready() == switch_bool_t.SWITCH_FALSE) return;\r
+            freeswitch.switch_event_unbind_callback(this.function);\r
+            disposed = true;\r
+        }\r
+        ~EventBinding()\r
+        {\r
+            dispose();\r
+        }\r
+        public static switch_event SwitchEventDupe(switch_event evt)\r
+        {\r
+            IntPtr clone_ptr_ptr = Marshal.AllocCoTaskMem(IntPtr.Size);\r
+            freeswitch.switch_event_dup(new SWIGTYPE_p_p_switch_event(clone_ptr_ptr, false), evt);\r
+            IntPtr event_ptr = (IntPtr)Marshal.PtrToStructure(clone_ptr_ptr, typeof(IntPtr));\r
+            switch_event dupe_evt = new switch_event(event_ptr, false);\r
+            Marshal.FreeCoTaskMem(clone_ptr_ptr);\r
+            return dupe_evt;\r
+        }\r
+        public static IDisposable Bind(string id, switch_event_types_t event_types, string subclass_name, Action<EventBindingArgs> f, bool dupe)\r
+        {\r
+            switch_event_callback_delegate boundFunc;\r
+            if (dupe)\r
+            {\r
+                boundFunc = (eventObj) =>\r
+                {\r
+                    var args = new EventBindingArgs { EventObj = SwitchEventDupe(new switch_event(eventObj,false)) };\r
+                    f(args);\r
+                };\r
+            }\r
+            else\r
+            {\r
+                boundFunc = (eventObj) =>\r
+                {\r
+                    var args = new EventBindingArgs { EventObj = new switch_event(eventObj, false) };\r
+                    f(args);\r
+                };\r
+            }\r
+            var fp = Marshal.GetFunctionPointerForDelegate(boundFunc);\r
+            var swigFp = new SWIGTYPE_p_f_p_switch_event__void(fp, false);\r
+            var res = freeswitch.switch_event_bind(id, event_types, subclass_name, swigFp, null);\r
+            if (res != switch_status_t.SWITCH_STATUS_SUCCESS)\r
+            {\r
+                throw new InvalidOperationException("Call to switch_event_bind failed, result: " + res + ".");\r
+            }\r
+            return new EventBinding(swigFp, boundFunc);\r
+        }\r
+    }\r
+}
\ No newline at end of file
index c9947dd82c6e126408945b49a07b970a31910848..cffb967a5453b373ecafdc684a7ec9e584c492cf 100644 (file)
@@ -49,6 +49,7 @@
   <ItemGroup>\r
     <Compile Include="AssemblyInfo.cs" />\r
     <Compile Include="ChannelVariables.cs" />\r
+    <Compile Include="EventBinding.cs" />\r
     <Compile Include="ManagedSession.cs" />\r
     <Compile Include="Loader.cs" />\r
     <Compile Include="Extensions.cs" />\r