From 886b02710e37f97ff3f1d15f259ce2e71354b005 Mon Sep 17 00:00:00 2001 From: Michael Giagnocavo Date: Thu, 2 Oct 2008 20:02:31 +0000 Subject: [PATCH] The difference now, is that this actually works git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@9802 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- .../mod_managed/freeswitch_managed.cpp | 39 +------------------ .../mod_managed/freeswitch_managed.h | 12 ++---- .../mod_managed/managed/ManagedSession.cs | 6 ++- src/mod/languages/mod_managed/mod_managed.cpp | 8 ++-- 4 files changed, 12 insertions(+), 53 deletions(-) diff --git a/src/mod/languages/mod_managed/freeswitch_managed.cpp b/src/mod/languages/mod_managed/freeswitch_managed.cpp index 583051cf1c..da3b601d0a 100644 --- a/src/mod/languages/mod_managed/freeswitch_managed.cpp +++ b/src/mod/languages/mod_managed/freeswitch_managed.cpp @@ -128,14 +128,6 @@ ManagedSession::ManagedSession(switch_core_session_t *session):CoreSession(sessi ManagedSession::~ManagedSession() { mono_thread_attach(globals.domain); - - if (dtmfDelegateHandle) { - mono_gchandle_free(dtmfDelegateHandle); - } - - if (hangupDelegateHandle) { - mono_gchandle_free(hangupDelegateHandle); - } // Do auto-hangup ourselves because CoreSession can't call check_hangup_hook // after ManagedSession destruction (cause at point it's pure virtual) @@ -163,46 +155,19 @@ bool ManagedSession::end_allow_threads() void ManagedSession::check_hangup_hook() { mono_thread_attach(globals.domain); - if (!hangupDelegateHandle) { - return; - } - - MonoObject * hangupDelegate = mono_gchandle_get_target(hangupDelegateHandle); if (!hangupDelegate) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "hangupDelegateHandle didn't get an object."); return; } - - MonoObject * ex = NULL; - mono_runtime_delegate_invoke(hangupDelegate, NULL, &ex); - if (ex) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "hangupDelegate threw an exception."); - } + hangupDelegate(); } switch_status_t ManagedSession::run_dtmf_callback(void *input, switch_input_type_t itype) { mono_thread_attach(globals.domain); - if (!dtmfDelegateHandle) { - return SWITCH_STATUS_SUCCESS; - } - MonoObject * dtmfDelegate = mono_gchandle_get_target(dtmfDelegateHandle); if (!dtmfDelegate) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "dtmfDelegateHandle didn't get an object."); return SWITCH_STATUS_SUCCESS; } - - void *args[2]; - args[0] = &input; - args[1] = &itype; - MonoObject * ex = NULL; - MonoObject * res = mono_runtime_delegate_invoke(dtmfDelegate, args, &ex); - if (ex) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "dtmfDelegate threw an exception."); - return SWITCH_STATUS_FALSE; - } - - char *resPtr = mono_string_to_utf8((MonoString *) res); + char *resPtr = dtmfDelegate(input, itype); switch_status_t status = process_callback_result(resPtr); g_free(resPtr); return status; diff --git a/src/mod/languages/mod_managed/freeswitch_managed.h b/src/mod/languages/mod_managed/freeswitch_managed.h index b5f64c8d5f..331a50a9e9 100644 --- a/src/mod/languages/mod_managed/freeswitch_managed.h +++ b/src/mod/languages/mod_managed/freeswitch_managed.h @@ -38,6 +38,9 @@ SWITCH_BEGIN_EXTERN_C #include #include +typedef void (*hangupFunction)(void); +typedef char* (*inputFunction)(void*, switch_input_type_t); + #ifndef _MANAGED // this section remove linker error LNK4248 for these opaque structures struct switch_core_session {char foo[];}; @@ -99,9 +102,6 @@ using namespace System; using namespace System::Reflection; using namespace System::Runtime::InteropServices; -typedef void (*hangupFunction)(void); -typedef char* (*inputFunction)(void*, switch_input_type_t); - public ref class FreeSwitchManaged { public: @@ -129,15 +129,9 @@ public: virtual switch_status_t run_dtmf_callback(void *input, switch_input_type_t itype); -#ifdef _MANAGED // P/Invoke function pointer to delegates inputFunction dtmfDelegate; hangupFunction hangupDelegate; -#else - guint32 dtmfDelegateHandle; // GCHandle to the input delegate - guint32 hangupDelegateHandle; // GCHandle to the hangup delegate -#endif - }; #endif \ No newline at end of file diff --git a/src/mod/languages/mod_managed/managed/ManagedSession.cs b/src/mod/languages/mod_managed/managed/ManagedSession.cs index 5561673aef..a557728220 100644 --- a/src/mod/languages/mod_managed/managed/ManagedSession.cs +++ b/src/mod/languages/mod_managed/managed/ManagedSession.cs @@ -32,16 +32,18 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Runtime.InteropServices; namespace FreeSWITCH.Native { // switch_status_t ManagedSession::run_dtmf_callback(void *input, switch_input_type_t itype) // But, process_callback_result is used to turn a string into a switch_status_t - using DtmfCallback = Func; + //using DtmfCallback = Func; + delegate string DtmfCallback(IntPtr input, Native.switch_input_type_t itype); public partial class ManagedSession { // SWITCH_DECLARE(void) InitManagedSession(ManagedSession *session, MonoObject *dtmfDelegate, MonoObject *hangupDelegate) - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)] + [DllImport("mod_managed.dll", CharSet = CharSet.Ansi)] static extern void InitManagedSession(IntPtr sessionPtr, DtmfCallback dtmfDelegate, Action hangupDelegate); /// Initializes the native ManagedSession. Must be called after Originate. diff --git a/src/mod/languages/mod_managed/mod_managed.cpp b/src/mod/languages/mod_managed/mod_managed.cpp index 1f19e4a105..8425d3db70 100644 --- a/src/mod/languages/mod_managed/mod_managed.cpp +++ b/src/mod/languages/mod_managed/mod_managed.cpp @@ -80,7 +80,7 @@ mod_mono_globals globals = // Sets up delegates (and anything else needed) on the ManagedSession object // Called via internalcall -SWITCH_MOD_DECLARE(void) InitManagedSession(ManagedSession * session, MonoObject * dtmfDelegate, MonoObject * hangupDelegate) +SWITCH_MOD_DECLARE(void) InitManagedSession(ManagedSession * session, inputFunction dtmfDelegate, hangupFunction hangupDelegate) { switch_assert(session); if (!session) { @@ -88,8 +88,8 @@ SWITCH_MOD_DECLARE(void) InitManagedSession(ManagedSession * session, MonoObject } session->setDTMFCallback(NULL, ""); session->setHangupHook(NULL); - session->dtmfDelegateHandle = mono_gchandle_new(dtmfDelegate, FALSE); - session->hangupDelegateHandle = mono_gchandle_new(hangupDelegate, FALSE); + session->dtmfDelegate = dtmfDelegate; + session->hangupDelegate = hangupDelegate; } switch_status_t setMonoDirs() @@ -283,8 +283,6 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_managed_load) /* Not sure if this is necesary on the loading thread */ mono_thread_attach(globals.domain); - mono_add_internal_call("FreeSWITCH.Native.ManagedSession::InitManagedSession", (void *)InitManagedSession); - /* Run loader */ MonoObject * objResult; MonoObject * exception = NULL;