[Freeswitch-svn] [commit] r9824 - freeswitch/trunk/src/mod/languages/mod_managed

Freeswitch SVN mikej at freeswitch.org
Sat Oct 4 02:35:32 EDT 2008


Author: mikej
Date: Sat Oct  4 02:35:32 2008
New Revision: 9824

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

Log:
a little more cleanup

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	Sat Oct  4 02:35:32 2008
@@ -41,19 +41,21 @@
 typedef void (*hangupFunction)(void);
 typedef char* (*inputFunction)(void*, switch_input_type_t);
 
-#ifdef _MANAGED
-#define ATTACH_THREADS
-#else
+#ifndef _MANAGED
 #include <glib.h>
 #include <mono/jit/jit.h>
 #include <mono/metadata/assembly.h>
+#include <mono/metadata/environment.h>
 #include <mono/metadata/threads.h>
+#include <mono/metadata/debug-helpers.h>
+#endif
 
 #ifndef SWIG
-struct mod_mono_globals {
+struct mod_managed_globals {
+	switch_memory_pool_t *pool;
+#ifndef _MANAGED
 	MonoDomain *domain;
 	MonoAssembly *mod_mono_asm;
-	switch_memory_pool_t *pool;
 	switch_bool_t embedded;
 
 	MonoMethod *loadMethod;
@@ -61,11 +63,15 @@
 	MonoMethod *runMethod;
 	MonoMethod *executeMethod;
 	MonoMethod *executeBackgroundMethod;
+#endif
 };
-
-typedef struct mod_mono_globals mod_mono_globals;
-extern mod_mono_globals globals;
+typedef struct mod_managed_globals mod_managed_globals;
+extern mod_managed_globals globals;
 #endif
+
+#ifdef _MANAGED
+#define ATTACH_THREADS
+#else
 #define ATTACH_THREADS mono_thread_attach(globals.domain);
 #endif
 

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	Sat Oct  4 02:35:32 2008
@@ -36,9 +36,16 @@
 
 #include <switch.h>
 
-SWITCH_BEGIN_EXTERN_C 
 #include "freeswitch_managed.h" 
 
+#ifdef _MANAGED
+#include <mscoree.h>
+using namespace System;
+using namespace System::Runtime::InteropServices;
+#endif
+
+SWITCH_BEGIN_EXTERN_C 
+
 SWITCH_MODULE_LOAD_FUNCTION(mod_managed_load);
 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_managed_shutdown);
 SWITCH_MODULE_DEFINITION(mod_managed, mod_managed_load, mod_managed_shutdown, NULL);
@@ -47,36 +54,16 @@
 SWITCH_STANDARD_API(managed_api_function);	/* Execute */
 SWITCH_STANDARD_APP(managed_app_function);	/* Run */
 
-SWITCH_END_EXTERN_C
+#define MOD_MANAGED_ASM_NAME "mod_managed_lib"
+#define MOD_MANAGED_ASM_V1 1
+#define MOD_MANAGED_ASM_V2 0
+#define MOD_MANAGED_ASM_V3 0
+#define MOD_MANAGED_ASM_V4 0
+#define MOD_MANAGED_DLL MOD_MANAGED_ASM_NAME ".dll"
 
-#define MOD_MANAGED_DLL "mod_managed_lib.dll"
+mod_managed_globals globals = { 0 };
 
 #ifndef _MANAGED	
-	
-SWITCH_BEGIN_EXTERN_C 
-
-#include <glib.h>
-#include <mono/jit/jit.h>
-#include <mono/metadata/assembly.h>
-#include <mono/metadata/environment.h>
-#include <mono/metadata/threads.h>
-#include <mono/metadata/debug-helpers.h>
-	
-#ifdef WIN32
-#include <shlobj.h>
-#define EXPORT __declspec(dllexport)
-#else
-#define EXPORT 
-#endif	
-
-#define MOD_MONO_MANAGED_ASM_NAME "mod_managed_lib"
-#define MOD_MONO_MANAGED_ASM_V1 1
-#define MOD_MONO_MANAGED_ASM_V2 0
-#define MOD_MONO_MANAGED_ASM_V3 0
-#define MOD_MONO_MANAGED_ASM_V4 0
-
-mod_mono_globals globals = 
-{ 0 };
 
 // Sets up delegates (and anything else needed) on the ManagedSession object
 // Called via internalcall
@@ -92,6 +79,10 @@
 	session->hangupDelegate = hangupDelegate;
 }
 
+#ifdef WIN32
+#include <shlobj.h>
+#endif	
+
 switch_status_t setMonoDirs() 
 {
 #ifdef WIN32	
@@ -187,11 +178,11 @@
 
 	/* Already loaded? */
 	MonoAssemblyName name;
-	name.name = MOD_MONO_MANAGED_ASM_NAME;
-	name.major = MOD_MONO_MANAGED_ASM_V1;
-	name.minor = MOD_MONO_MANAGED_ASM_V2;
-	name.revision = MOD_MONO_MANAGED_ASM_V3;
-	name.build = MOD_MONO_MANAGED_ASM_V4;
+	name.name = MOD_MANAGED_ASM_NAME;
+	name.major = MOD_MANAGED_ASM_V1;
+	name.minor = MOD_MANAGED_ASM_V2;
+	name.revision = MOD_MANAGED_ASM_V3;
+	name.build = MOD_MANAGED_ASM_V4;
 	name.culture = "";
 	name.hash_value = "";
 
@@ -313,116 +304,6 @@
 	return SWITCH_STATUS_SUCCESS;
 }
 
-SWITCH_STANDARD_API(managedrun_api_function) 
-{
-	// TODO: Should we be detaching after all this?
-	mono_thread_attach(globals.domain);
-
-	if (switch_strlen_zero(cmd)) {
-		stream->write_function(stream, "-ERR no args specified!\n");
-		return SWITCH_STATUS_SUCCESS;
-	}
-
-	// ExecuteBackground(string command)
-	void *args[1];
-
-	args[0] = mono_string_new(globals.domain, cmd);
-	MonoObject * exception = NULL;
-	MonoObject * objResult = mono_runtime_invoke(globals.executeBackgroundMethod, NULL, args, &exception);
-
-	if (exception) {
-		stream->write_function(stream, "-ERR FreeSWITCH.Loader.ExecuteBackground threw an exception.\n");
-		mono_print_unhandled_exception(exception);
-		return SWITCH_STATUS_SUCCESS;
-	}
-
-	if (*(int *) mono_object_unbox(objResult)) {
-		stream->write_function(stream, "+OK\n");
-	} else {
-		stream->write_function(stream, "-ERR ExecuteBackground returned false (unknown module?).\n");
-	}
-
-	return SWITCH_STATUS_SUCCESS;
-}
-
-SWITCH_STANDARD_API(managed_api_function) 
-{
-	mono_thread_attach(globals.domain);
-
-	if (switch_strlen_zero(cmd)) {
-		stream->write_function(stream, "-ERR no args specified!\n");
-		return SWITCH_STATUS_SUCCESS;
-	}
-
-	// Method is: Execute(string command, IntPtr streamPtr, IntPtr eventPtr)
-	void *args[3];
-
-	args[0] = mono_string_new(globals.domain, cmd);
-	args[1] = &stream;			// Address of the arguments
-	args[2] = &(stream->param_event);
-
-	MonoObject * exception = NULL;
-	MonoObject * objResult = mono_runtime_invoke(globals.executeMethod, NULL, args, &exception);
-
-	if (exception) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Exception trying to execute mono %s.\n", cmd);
-		mono_print_unhandled_exception(exception);
-	}
-
-	if (!(*(int *) mono_object_unbox(objResult))) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Execute failed for %s (unknown module?).\n", cmd);
-	}
-
-	return SWITCH_STATUS_SUCCESS;
-}
-
-SWITCH_STANDARD_APP(managed_app_function) 
-{
-	mono_thread_attach(globals.domain);
-
-	if (switch_strlen_zero(data)) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No args specified!\n");
-	}
-
-	// bool Run(string command, IntPtr sessionHandle)
-	void *args[2];
-
-	args[0] = mono_string_new(globals.domain, data);
-	args[1] = &session;
-
-	MonoObject * exception = NULL;
-	MonoObject * objResult = mono_runtime_invoke(globals.runMethod, NULL, args, &exception);
-
-	if (exception) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Exception trying to execute application mono %s.\n", data);
-		mono_print_unhandled_exception(exception);
-	}
-
-	if (!(*(int *) mono_object_unbox(objResult))) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Application run failed for %s (unknown module?).\n", data);
-	}
-} 
-
-SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_managed_shutdown) 
-{
-	MonoObject * ex;
-
-	mono_thread_attach(globals.domain);
-	mono_runtime_invoke(globals.unloadMethod, NULL, NULL, &ex);
-
-	if (ex) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Exception occurred in Loader::Unload.\n");
-		mono_print_unhandled_exception(ex);
-	}
-
-	if (!globals.embedded) {
-		mono_jit_cleanup(globals.domain);
-	}
-
-	return SWITCH_STATUS_SUCCESS;
-}
-
-SWITCH_END_EXTERN_C
 #endif
 
 /**********************************************************
@@ -430,23 +311,6 @@
 **********************************************************/
 
 #ifdef _MANAGED
-
-#include <mscoree.h>
-
-using namespace System;
-using namespace System::Runtime::InteropServices;
-
-SWITCH_BEGIN_EXTERN_C 
-
-struct dotnet_conf_t {
-    switch_memory_pool_t *pool;
-    //ICLRRuntimeHost *pCorRuntime;
-    //HMODULE lock_module;
-	//OSVERSIONINFO osver;
-    //char *cor_version;
-} globals;
-
-
 // Sets up delegates (and anything else needed) on the ManagedSession object
 // Called from ManagedSession.Initialize Managed -> this is Unmanaged code so all pointers are marshalled and prevented from GC
 // Exported method.
@@ -543,15 +407,16 @@
 	SWITCH_ADD_APP(app_interface, "managed", "Run CLI App", "Run an App on a channel", managed_app_function, "<modulename> [<args>]", SAF_NONE);
 	return SWITCH_STATUS_SUCCESS;
 }
-
+#endif
 
 SWITCH_STANDARD_API(managedrun_api_function) 
 {
+	int success;
 	if (switch_strlen_zero(cmd)) {
 		stream->write_function(stream, "-ERR no args specified!\n");	
 		return SWITCH_STATUS_SUCCESS;
 	}
-
+#ifdef _MANAGED
 	Object ^objResult;
 	try {
 		objResult = FreeSwitchManaged::executeBackgroundMethod->Invoke(nullptr, gcnew array<Object^> { gcnew String(cmd) } );
@@ -563,22 +428,40 @@
 		Marshal::FreeHGlobal(msg);
 		return SWITCH_STATUS_FALSE;
 	}
+	success = *reinterpret_cast<bool^>(objResult);
+#else
+	mono_thread_attach(globals.domain);
+	void *args[1];
 
-	if (*reinterpret_cast<bool^>(objResult)) {
+	args[0] = mono_string_new(globals.domain, cmd);
+	MonoObject * exception = NULL;
+	MonoObject * objResult = mono_runtime_invoke(globals.executeBackgroundMethod, NULL, args, &exception);
+
+	if (exception) {
+		stream->write_function(stream, "-ERR FreeSWITCH.Loader.ExecuteBackground threw an exception.\n");
+		mono_print_unhandled_exception(exception);
+		return SWITCH_STATUS_SUCCESS;
+	}
+	success = *(int *) mono_object_unbox(objResult);
+#endif
+	if (success) {
 		stream->write_function(stream, "+OK\n");
 	} else {	
 		stream->write_function(stream, "-ERR ExecuteBackground returned false (unknown module?).\n");
 	}
+
 	return SWITCH_STATUS_SUCCESS;
 }
 
+
 SWITCH_STANDARD_API(managed_api_function) 
 {
+	int success;
 	if (switch_strlen_zero(cmd)) {
 		stream->write_function(stream, "-ERR no args specified!\n");	
 		return SWITCH_STATUS_SUCCESS;
 	}
-	
+#ifdef _MANAGED
 	Object ^objResult;
 	try {
 		objResult = FreeSwitchManaged::executeMethod->Invoke(nullptr, gcnew array<Object^>{gcnew String(cmd),gcnew IntPtr(stream), gcnew IntPtr(stream->param_event)});
@@ -589,20 +472,38 @@
 		Marshal::FreeHGlobal(msg);
 		return SWITCH_STATUS_FALSE;
 	}
-	
-	if (!*reinterpret_cast<bool^>(objResult)) {
+	success = *reinterpret_cast<bool^>(objResult);
+#else
+	mono_thread_attach(globals.domain);
+	void *args[3];
+
+	args[0] = mono_string_new(globals.domain, cmd);
+	args[1] = &stream;			// Address of the arguments
+	args[2] = &(stream->param_event);
+
+	MonoObject * exception = NULL;
+	MonoObject * objResult = mono_runtime_invoke(globals.executeMethod, NULL, args, &exception);
+
+	if (exception) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Exception trying to execute mono %s.\n", cmd);
+		mono_print_unhandled_exception(exception);
+	}
+	success = *(int *) mono_object_unbox(objResult);
+#endif
+	if (!success) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Execute failed for %s (unknown module?).\n", cmd);
 	}
-	
 	return SWITCH_STATUS_SUCCESS;
 }
 
+
 SWITCH_STANDARD_APP(managed_app_function) 
 {
+	int success;
 	if (switch_strlen_zero(data)) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No args specified!\n");
 	}
-	
+#ifdef _MANAGED
 	Object ^objResult;
 	try {
 		objResult = FreeSwitchManaged::runMethod->Invoke(nullptr, gcnew array<Object^>{gcnew String(data),gcnew IntPtr(session)});
@@ -614,8 +515,24 @@
 		Marshal::FreeHGlobal(msg);
 		return;
 	}
-	
-	if (!*reinterpret_cast<bool^>(objResult)) {	
+	success = *reinterpret_cast<bool^>(objResult);
+#else
+	mono_thread_attach(globals.domain);
+	void *args[2];
+
+	args[0] = mono_string_new(globals.domain, data);
+	args[1] = &session;
+
+	MonoObject * exception = NULL;
+	MonoObject * objResult = mono_runtime_invoke(globals.runMethod, NULL, args, &exception);
+
+	if (exception) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Exception trying to execute application mono %s.\n", data);
+		mono_print_unhandled_exception(exception);
+	}
+	success = *(int *) mono_object_unbox(objResult);
+#endif
+	if (!success) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Application run failed for %s (unknown module?).\n", data);
 	}
 }
@@ -623,6 +540,7 @@
 
 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_managed_shutdown) 
 {
+#ifdef _MANAGED
 	Object ^objResult;
 	try {
 		objResult = FreeSwitchManaged::unloadMethod->Invoke(nullptr, nullptr);
@@ -634,9 +552,22 @@
 		Marshal::FreeHGlobal(msg);
 		return SWITCH_STATUS_FALSE;;
 	}
+#else
+	MonoObject * ex;
+
+	mono_thread_attach(globals.domain);
+	mono_runtime_invoke(globals.unloadMethod, NULL, NULL, &ex);
+
+	if (ex) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Exception occurred in Loader::Unload.\n");
+		mono_print_unhandled_exception(ex);
+	}
+
+	if (!globals.embedded) {
+		mono_jit_cleanup(globals.domain);
+	}
+#endif
 	return SWITCH_STATUS_SUCCESS;
 }
 
 SWITCH_END_EXTERN_C
-
-#endif
\ No newline at end of file



More information about the Freeswitch-svn mailing list