[Freeswitch-svn] [commit] r9576 - in freeswitch/trunk/src/mod/languages: mod_mono mod_mono_managed

Freeswitch SVN mikej at freeswitch.org
Tue Sep 16 19:54:24 EDT 2008


Author: mikej
Date: Tue Sep 16 19:54:23 2008
New Revision: 9576

Added:
   freeswitch/trunk/src/mod/languages/mod_mono/Makefile
   freeswitch/trunk/src/mod/languages/mod_mono/runswig.sh
   freeswitch/trunk/src/mod/languages/mod_mono_managed/Makefile
Modified:
   freeswitch/trunk/src/mod/languages/mod_mono/freeswitch.i
   freeswitch/trunk/src/mod/languages/mod_mono/freeswitch_mono.h
   freeswitch/trunk/src/mod/languages/mod_mono/mod_mono.cpp
   freeswitch/trunk/src/mod/languages/mod_mono/switch_platform.i
   freeswitch/trunk/src/mod/languages/mod_mono_managed/Loader.cs

Log:
(MODLANG-83) mod_mono Linux build & embedding

Added: freeswitch/trunk/src/mod/languages/mod_mono/Makefile
==============================================================================
--- (empty file)
+++ freeswitch/trunk/src/mod/languages/mod_mono/Makefile	Tue Sep 16 19:54:23 2008
@@ -0,0 +1,16 @@
+#MOD_CFLAGS=`pkg-config --cflags --libs mono`
+MOD_CFLAGS=-D_REENTRANT -pthread -I/opt/mono-1.9/lib/pkgconfig/../../include/mono-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -lmono
+#LDFLAGS=`pkg-config --libs mono`
+BASE=../../../..
+VERBOSE=1
+include $(BASE)/build/modmake.rules
+LOCAL_OBJS=freeswitch_mono.o freeswitch_wrap.o
+local_depend: $(LOCAL_OBJS)
+
+freeswitch_mono.o: freeswitch_mono.h freeswitch_mono.cpp
+
+freeswitch_wrap.o: freeswitch_wrap.cpp
+
+freeswitch_wrap.cpp: freeswitch_wrap.cxx
+       cp freeswitch_wrap.cxx freeswitch_wrap.cpp
+

Modified: freeswitch/trunk/src/mod/languages/mod_mono/freeswitch.i
==============================================================================
--- freeswitch/trunk/src/mod/languages/mod_mono/freeswitch.i	(original)
+++ freeswitch/trunk/src/mod/languages/mod_mono/freeswitch.i	Tue Sep 16 19:54:23 2008
@@ -107,6 +107,7 @@
 %ignore switch_core_session_get_event_hooks;
 %ignore switch_inet_pton;
 %ignore switch_xml_idx;
+%ignore switch_xml_pi;
 
 // Real header includes now
 %import switch_platform.i // This will give us all the macros we need to compile the other stuff

Modified: freeswitch/trunk/src/mod/languages/mod_mono/freeswitch_mono.h
==============================================================================
--- freeswitch/trunk/src/mod/languages/mod_mono/freeswitch_mono.h	(original)
+++ freeswitch/trunk/src/mod/languages/mod_mono/freeswitch_mono.h	Tue Sep 16 19:54:23 2008
@@ -44,6 +44,7 @@
 	MonoDomain *domain;
 	MonoAssembly *mod_mono_asm;
 	switch_memory_pool_t *pool;
+	switch_bool_t embedded;
 
 	MonoMethod *loadMethod;
 	MonoMethod *unloadMethod;
@@ -76,4 +77,4 @@
 	guint32 hangupDelegateHandle; // GCHandle to the hangup delegate
 };
 
-#endif
\ No newline at end of file
+#endif

Modified: freeswitch/trunk/src/mod/languages/mod_mono/mod_mono.cpp
==============================================================================
--- freeswitch/trunk/src/mod/languages/mod_mono/mod_mono.cpp	(original)
+++ freeswitch/trunk/src/mod/languages/mod_mono/mod_mono.cpp	Tue Sep 16 19:54:23 2008
@@ -47,12 +47,18 @@
 #ifdef WIN32
 #include <shlobj.h>
 #define EXPORT __declspec(dllexport)
-#elif
+#else
 #define EXPORT 
 #endif	
 
 #define MOD_MONO_MANAGED_DLL "mod_mono_managed.dll"
-	
+
+#define MOD_MONO_MANAGED_ASM_NAME "mod_mono_managed"
+#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 };
 
@@ -120,7 +126,7 @@
 		return SWITCH_STATUS_SUCCESS;
 	}
 
-#elif
+#else
 	// On other platforms, it should just work if it hasn't been relocated
 	mono_set_dirs(NULL, NULL);
 	return SWITCH_STATUS_SUCCESS;
@@ -137,20 +143,41 @@
 	switch_snprintf(filename, 256, "%s%s%s", SWITCH_GLOBAL_dirs.mod_dir, SWITCH_PATH_SEPARATOR, MOD_MONO_MANAGED_DLL);
 	globals.domain = mono_jit_init(filename);
 
-	if (!globals.domain) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mono_jit_init failed.\n");
-		return SWITCH_STATUS_FALSE;
+	/* Already got a Mono domain? */
+	if ((globals.domain = mono_get_root_domain())) {
+		mono_thread_attach(globals.domain);
+		globals.embedded = SWITCH_TRUE;
+	} else {
+		if (!(globals.domain = mono_jit_init(filename))) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mono_jit_init failed.\n");
+			return SWITCH_STATUS_FALSE;
+		}
 	}
 
-	/* Open the assembly */ 
-	globals.mod_mono_asm = mono_domain_assembly_open(globals.domain, filename);
-
-	if (!globals.mod_mono_asm) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mono_domain_assembly_open failed.\n");
-		return SWITCH_STATUS_FALSE;
+	/* 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.culture = "";
+	name.hash_value = "";
+
+	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Calling mono_assembly_loaded");
+
+	if (!(globals.mod_mono_asm = mono_assembly_loaded(&name))) {
+		/* Open the assembly */ 
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Calling mono_domain_assembly_open");
+		globals.mod_mono_asm = mono_domain_assembly_open(globals.domain, filename);
+		if (!globals.mod_mono_asm) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mono_domain_assembly_open failed.\n");
+			return SWITCH_STATUS_FALSE;
+		}
 	}
 
 	return SWITCH_STATUS_SUCCESS;
+
 }
 
 MonoMethod * getMethod(const char *name, MonoClass * klass) 
@@ -226,7 +253,7 @@
 	/* Not sure if this is necesary on the loading thread */ 
 	mono_thread_attach(globals.domain);
 
-	mono_add_internal_call("FreeSWITCH.Native.MonoSession::InitMonoSession", InitMonoSession);
+	mono_add_internal_call("FreeSWITCH.Native.MonoSession::InitMonoSession", (void *)InitMonoSession);
 
 	/* Run loader */ 
 	MonoObject * objResult;
@@ -360,9 +387,9 @@
 		mono_print_unhandled_exception(ex);
 	}
 
-	mono_runtime_set_shutting_down();
-	mono_runtime_cleanup(globals.domain);
-	mono_runtime_quit();
+	if (!globals.embedded) {
+		mono_jit_cleanup(globals.domain);
+	}
 
 	return SWITCH_STATUS_SUCCESS;
 }

Added: freeswitch/trunk/src/mod/languages/mod_mono/runswig.sh
==============================================================================
--- (empty file)
+++ freeswitch/trunk/src/mod/languages/mod_mono/runswig.sh	Tue Sep 16 19:54:23 2008
@@ -0,0 +1,5 @@
+#!/bin/bash
+swig -I../../../include -v -O -c++ -csharp -namespace FreeSWITCH.Native -dllimport mod_mono freeswitch.i
+rm -f ../mod_mono_managed/swig.cs
+cat *.cs > ../mod_mono_managed/swig.cs
+rm -f *.cs

Modified: freeswitch/trunk/src/mod/languages/mod_mono/switch_platform.i
==============================================================================
--- freeswitch/trunk/src/mod/languages/mod_mono/switch_platform.i	(original)
+++ freeswitch/trunk/src/mod/languages/mod_mono/switch_platform.i	Tue Sep 16 19:54:23 2008
@@ -14,6 +14,20 @@
 //		we define char as byte.
 // TODO: Possible? It'd be nice to do the whole char*->IntPtr->Marshal/Free thing here instead of swigStringFix
 
+%typemap(imtype, out="string") char **   "ref string"
+%typemap(cstype, out="string") char **   "ref string"
+%typemap(csin) char **     "ref $csinput"
+%typemap(csvarin) char **
+%{
+  set { $imcall; }
+%}
+%typemap(csvarout) char **
+%{
+  get {
+    return $imcall;
+  }
+%}
+
 #define SWITCH_DECLARE(type) type
 #define SWITCH_DECLARE_NONSTD(type) type
 #define SWITCH_MOD_DECLARE(type) type
@@ -42,4 +56,4 @@
 #define _Out_cap_(x)
 #define _Out_z_cap_(x)
 #define _Out_ptrdiff_cap_(x)
-#define _Out_opt_ptrdiff_cap_(x)
\ No newline at end of file
+#define _Out_opt_ptrdiff_cap_(x)

Modified: freeswitch/trunk/src/mod/languages/mod_mono_managed/Loader.cs
==============================================================================
--- freeswitch/trunk/src/mod/languages/mod_mono_managed/Loader.cs	(original)
+++ freeswitch/trunk/src/mod/languages/mod_mono_managed/Loader.cs	Tue Sep 16 19:54:23 2008
@@ -100,22 +100,22 @@
             } 
         }
 
-        static List<Assembly> loadAssemblies(string managedDir)
+        static Assembly[] loadAssemblies(string managedDir)
         {
-            return Directory.GetFiles(managedDir, "*.dll", SearchOption.AllDirectories)
-                  .Select(f => Path.Combine(managedDir, f))
-                  .Select(f => {
-                      try {
-                          return System.Reflection.Assembly.LoadFile(f);
-                      }
-                      catch (Exception ex) {
-                          Log.WriteLine(LogLevel.Notice, "Assembly.LoadFile failed; skipping {0} ({1})", f, ex.Message);
-                          return null;
-                      }
-                  })
-                  .Where(a => a != null)
-                  .Concat(new[] { System.Reflection.Assembly.GetExecutingAssembly() }) // Add in our own to load Demo or built-in things if added
-                  .ToList();
+            // load the modules in the mod/mono directory
+            Log.WriteLine(LogLevel.Notice, "loadAssemblies: {0}", managedDir);
+            foreach (string s in Directory.GetFiles(managedDir, "*.dll", SearchOption.AllDirectories))
+            {
+                string f = Path.Combine(managedDir, s);
+                try {
+                    System.Reflection.Assembly.LoadFile(f);
+                }
+                catch (Exception ex) {
+                    Log.WriteLine(LogLevel.Notice, "Assembly.LoadFile failed; skipping {0} ({1})", f, ex.Message);
+                }
+            }
+
+            return AppDomain.CurrentDomain.GetAssemblies();  // Includes anything else already loaded
         }
 
         public static void Unload()

Added: freeswitch/trunk/src/mod/languages/mod_mono_managed/Makefile
==============================================================================
--- (empty file)
+++ freeswitch/trunk/src/mod/languages/mod_mono_managed/Makefile	Tue Sep 16 19:54:23 2008
@@ -0,0 +1,13 @@
+all: Debug Debug/mod_mono_managed.dll
+
+clean:
+       rm -fr Debug
+
+Debug:
+       mkdir Debug
+
+Debug/mod_mono_managed.dll: Loader.cs MonoSession.cs ApiFunction.cs AppFunction.cs Extensions.cs Log.cs Demo.cs swig.cs
+       gmcs -target:library -out:Debug/mod_mono_managed.dll -d:DEBUG Loader.cs MonoSession.cs ApiFunction.cs AppFunction.cs Extensions.cs Log.cs Demo.cs swig.cs
+
+install: Debug/mod_mono_managed.dll
+       install Debug/mod_mono_managed.dll /usr/local/freeswitch/mod



More information about the Freeswitch-svn mailing list