[Freeswitch-svn] [commit] r9802 - in freeswitch/trunk/src/mod/languages/mod_managed: . managed

Freeswitch SVN michaelgg at freeswitch.org
Thu Oct 2 16:02:32 EDT 2008


Author: michaelgg
Date: Thu Oct  2 16:02:31 2008
New Revision: 9802

Modified:
   freeswitch/trunk/src/mod/languages/mod_managed/freeswitch_managed.cpp
   freeswitch/trunk/src/mod/languages/mod_managed/freeswitch_managed.h
   freeswitch/trunk/src/mod/languages/mod_managed/managed/ManagedSession.cs
   freeswitch/trunk/src/mod/languages/mod_managed/mod_managed.cpp

Log:
The difference now, is that this actually works

Modified: freeswitch/trunk/src/mod/languages/mod_managed/freeswitch_managed.cpp
==============================================================================
--- freeswitch/trunk/src/mod/languages/mod_managed/freeswitch_managed.cpp	(original)
+++ freeswitch/trunk/src/mod/languages/mod_managed/freeswitch_managed.cpp	Thu Oct  2 16:02:31 2008
@@ -128,14 +128,6 @@
 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 @@
 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;

Modified: freeswitch/trunk/src/mod/languages/mod_managed/freeswitch_managed.h
==============================================================================
--- freeswitch/trunk/src/mod/languages/mod_managed/freeswitch_managed.h	(original)
+++ freeswitch/trunk/src/mod/languages/mod_managed/freeswitch_managed.h	Thu Oct  2 16:02:31 2008
@@ -38,6 +38,9 @@
 #include <switch.h>
 #include <switch_cpp.h>
 
+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::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 @@
 
 	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

Modified: freeswitch/trunk/src/mod/languages/mod_managed/managed/ManagedSession.cs
==============================================================================
--- freeswitch/trunk/src/mod/languages/mod_managed/managed/ManagedSession.cs	(original)
+++ freeswitch/trunk/src/mod/languages/mod_managed/managed/ManagedSession.cs	Thu Oct  2 16:02:31 2008
@@ -32,16 +32,18 @@
 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<IntPtr, Native.switch_input_type_t, string>;
+    //using DtmfCallback = Func<IntPtr, Native.switch_input_type_t, string>;
+    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);
 
         /// <summary>Initializes the native ManagedSession. Must be called after Originate.</summary>

Modified: freeswitch/trunk/src/mod/languages/mod_managed/mod_managed.cpp
==============================================================================
--- freeswitch/trunk/src/mod/languages/mod_managed/mod_managed.cpp	(original)
+++ freeswitch/trunk/src/mod/languages/mod_managed/mod_managed.cpp	Thu Oct  2 16:02:31 2008
@@ -80,7 +80,7 @@
 
 // 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 @@
 	}
 	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 @@
 	/* 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;



More information about the Freeswitch-svn mailing list