]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
First attempt at making CLR delegate stuff work
authorMichael Giagnocavo <mgg@giagnocavo.net>
Thu, 2 Oct 2008 18:10:48 +0000 (18:10 +0000)
committerMichael Giagnocavo <mgg@giagnocavo.net>
Thu, 2 Oct 2008 18:10:48 +0000 (18:10 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@9795 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/mod/languages/mod_managed/freeswitch_managed.cpp
src/mod/languages/mod_managed/freeswitch_managed.h
src/mod/languages/mod_managed/mod_managed.cpp
src/mod/languages/mod_managed/mod_managed.vcproj

index 83c78ab6260ac8759b2cd1c1fa79665ad78b195d..119fb3a728e417eb8ab9b34672995d2a41c2d063 100644 (file)
@@ -74,6 +74,9 @@ ManagedSession::~ManagedSession()
                // Don't let any callbacks use this CoreSession anymore\r
                switch_channel_set_private(channel, "CoreSession", NULL);\r
        }\r
+       // Free delegates\r
+       hangupDelegateHandle.Free();\r
+       dtmfDelegateHandle.Free();\r
 }\r
 \r
 bool ManagedSession::begin_allow_threads() \r
@@ -88,20 +91,20 @@ bool ManagedSession::end_allow_threads()
 \r
 void ManagedSession::check_hangup_hook() \r
 {\r
-       if (!hangupDelegateHandle) {\r
+       if (!hangupDelegateHandle.IsAllocated) {\r
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "hangupDelegateHandle didn't get an object.");\r
                return;\r
        }\r
-       hangupDelegateHandle();\r
+       ((HangupDelegate^)hangupDelegateHandle.Target)();\r
 }\r
 \r
 switch_status_t ManagedSession::run_dtmf_callback(void *input, switch_input_type_t itype) \r
 {\r
-       if (!dtmfDelegateHandle) {\r
+       if (!dtmfDelegateHandle.IsAllocated) {\r
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "dtmfDelegateHandle didn't get an object.");\r
                return SWITCH_STATUS_FALSE;;\r
        }\r
-       char* result = dtmfDelegateHandle(input, itype);\r
+       char *result = ((InputDelegate^)dtmfDelegateHandle.Target)(input, itype);\r
 \r
        switch_status_t status = process_callback_result(result);\r
        return status;\r
index 7de5ab4b97b9b6ddd3534a3c54eaf97c54b80462..97b6d02fde6c4989d25ef3739fea51567d6b1ac1 100644 (file)
@@ -38,7 +38,7 @@ SWITCH_BEGIN_EXTERN_C
 #include <switch.h>\r
 #include <switch_cpp.h>\r
 \r
-#ifdef _MANAGED\r
+#ifndef _MANAGED\r
 // this section remove linker error LNK4248 for these opaque structures\r
        struct switch_core_session {char foo[];};\r
        struct apr_pool_t {char foo[];};\r
@@ -69,11 +69,6 @@ SWITCH_BEGIN_EXTERN_C
        struct apr_socket_t {char foo[];};\r
 // LNK Error\r
 \r
-typedef char* (CALLBACK* inputtype)(void * input, switch_input_type_t type);\r
-typedef void (CALLBACK* hanguptype)();\r
-\r
-#else\r
-\r
 #include <mono/jit/jit.h>\r
 #include <mono/metadata/assembly.h>\r
 \r
@@ -104,7 +99,8 @@ using namespace System;
 using namespace System::Reflection;\r
 using namespace System::Runtime::InteropServices;\r
 \r
-delegate void HangupMethod();\r
+delegate void HangupDelegate(void);\r
+delegate char* InputDelegate(void* input, switch_input_type_t type);\r
 \r
 public ref class FreeSwitchManaged\r
 {\r
@@ -134,8 +130,8 @@ public:
        virtual switch_status_t run_dtmf_callback(void *input, switch_input_type_t itype);\r
 \r
 #ifdef _MANAGED\r
-       inputtype dtmfDelegateHandle; // GCHandle to the input delegate \r
-       hanguptype hangupDelegateHandle; // GCHandle to the hangup delegate\r
+       GCHandle dtmfDelegateHandle; // GCHandle to the input delegate \r
+       GCHandle hangupDelegateHandle; // GCHandle to the hangup delegate\r
 #else\r
        guint32 dtmfDelegateHandle; // GCHandle to the input delegate \r
        guint32 hangupDelegateHandle; // GCHandle to the hangup delegate\r
index 12d0595555137f42ee91fb17c2d5fa55778b243d..cca9fe7248b433a121d40be20027df3ee55d816c 100644 (file)
@@ -448,10 +448,12 @@ struct dotnet_conf_t {
     //char *cor_version;\r
 } globals;\r
 \r
+\r
 // Sets up delegates (and anything else needed) on the ManagedSession object\r
 // Called from ManagedSession.Initialize Managed -> this is Unmanaged code so all pointers are marshalled and prevented from GC\r
 // Exported method.\r
-SWITCH_MOD_DECLARE(void) InitManagedSession(ManagedSession * session, inputtype dtmfDelegate, hanguptype hangupDelegate) \r
+\r
+SWITCH_MOD_DECLARE(void) InitManagedSession(ManagedSession *session, void *dtmfDelegate, void *hangupDelegate) \r
 {\r
        switch_assert(session);\r
        if (!session) {\r
@@ -459,8 +461,8 @@ SWITCH_MOD_DECLARE(void) InitManagedSession(ManagedSession * session, inputtype
        }\r
        session->setDTMFCallback(NULL, "");\r
        session->setHangupHook(NULL);\r
-       session->dtmfDelegateHandle = dtmfDelegate;\r
-       session->hangupDelegateHandle = hangupDelegate;\r
+       session->dtmfDelegateHandle = GCHandle::Alloc(Marshal::GetDelegateForFunctionPointer(IntPtr(dtmfDelegate), InputDelegate::typeid));\r
+       session->hangupDelegateHandle = GCHandle::Alloc(Marshal::GetDelegateForFunctionPointer(IntPtr(hangupDelegate), HangupDelegate::typeid));\r
 }\r
 \r
 switch_status_t loadModDotnetManaged() \r
@@ -469,50 +471,6 @@ switch_status_t loadModDotnetManaged()
        char filename[256];\r
        switch_snprintf(filename, 256, "%s%s%s", SWITCH_GLOBAL_dirs.mod_dir, SWITCH_PATH_SEPARATOR, MOD_MANAGED_DLL);\r
 \r
-       //HRESULT hr;\r
-       //wchar_t wCORVersion[256];\r
-       //if (globals.cor_version) {\r
-       //  MultiByteToWideChar(CP_UTF8, 0, globals.cor_version, -1,\r
-       //                                                       wCORVersion, sizeof(wCORVersion) / sizeof(wchar_t));\r
-       //}\r
-       //else {\r
-       //  DWORD bytes;\r
-       //  hr = GetCORVersion(wCORVersion, sizeof(wCORVersion) \r
-       //                                                                                              / sizeof(wchar_t) - 1, &bytes);\r
-       //  if (FAILED(hr)) {\r
-       //              switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, \r
-       //                                              "mod_dotnet: GetCORVersion failed to return "\r
-       //                                              "the .NET CLR engine version.");\r
-       //              return SWITCH_STATUS_FALSE;\r
-       //  }\r
-       //  int len = WideCharToMultiByte(CP_UTF8, 0, wCORVersion, -1, \r
-       //                                                                                NULL, 0, NULL, NULL);\r
-       //  globals.cor_version = (char *)apr_palloc(globals.pool, len);\r
-       //  len = WideCharToMultiByte(CP_UTF8, 0, wCORVersion, -1, \r
-       //                                                                       globals.cor_version, len, NULL, NULL);\r
-       //}\r
-\r
-       ////verify that the clr is already loaded - because this dll is a clr enabled dll it will be loaded but lets get its info anyway\r
-       //hr = CorBindToRuntimeEx(wCORVersion, \r
-       //                                                       L"wks",  // Or "svr" \r
-       //                                                       STARTUP_LOADER_OPTIMIZATION_MULTI_DOMAIN_HOST | \r
-       //                                                       STARTUP_CONCURRENT_GC, \r
-       //                                                       CLSID_CorRuntimeHost, \r
-       //                                                       IID_ICorRuntimeHost, \r
-       //                                                       (void **)&globals.pCorRuntime);\r
-       //if (FAILED(hr)) {\r
-       //  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, \r
-       //                                      "mod_dotnet: Could not CorBindToRuntimeEx version "\r
-       //                                      "%s for the .NET CLR engine.", globals.cor_version);\r
-       //  return SWITCH_STATUS_FALSE;\r
-       //}\r
-\r
-       //if (FAILED(hr)) { // a value of one here means that the specified clr is already loaded and good.\r
-       //      switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,\r
-       //                                      "mod_dotnet: Could not start the "\r
-       //                                      ".NET CLR engine.");\r
-       //      return SWITCH_STATUS_FALSE;\r
-       //}\r
        wchar_t modpath[256];\r
        mbstowcs(modpath, filename, 255);\r
        try {\r
index 04c947811933b5f74d3a0ef902b065ddfe9ef177..13218cd8fc02826f1b0502a20d0bf59b1581bb23 100644 (file)
@@ -44,6 +44,7 @@
                                Name="VCCLCompilerTool"\r
                                Optimization="0"\r
                                AdditionalIncludeDirectories=""\r
+                               AdditionalUsingDirectories=""\r
                                PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;MOD_EXPORTS"\r
                                MinimalRebuild="false"\r
                                BasicRuntimeChecks="0"\r
                                Optimization="3"\r
                                EnableIntrinsicFunctions="true"\r
                                AdditionalIncludeDirectories=""\r
+                               AdditionalUsingDirectories=""\r
                                PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;MOD_MONO_EXPORTS"\r
                                RuntimeLibrary="2"\r
                                EnableFunctionLevelLinking="true"\r