Inheritance: IDynLoader
Ejemplo n.º 1
0
        public static void Init(string basePath = null)
        {
            var platform = Environment.OSVersion.Platform;

            if (platform == PlatformID.Unix && File.Exists("/usr/lib/libSystem.dylib"))
            {
                platform = PlatformID.MacOSX;
            }

            IDynLoader loader;

            switch (platform)
            {
            case PlatformID.Win32NT:
                WSAStartup(514, new byte[512]);
                loader = new Win32Loader();
                break;

            case PlatformID.Unix:
                loader = new LinuxLoader();
                break;

            case PlatformID.MacOSX:
                loader = new OsxLoader();
                break;

            default:
                throw new NotSupportedException("Platform " + platform + " is not supported.");
            }

            var core  = loader.LoadLibrary(basePath, "libevent_core");
            var extra = loader.LoadLibrary(basePath, "libevent_extra");

            foreach (var fieldInfo in typeof(Event).GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                if (!typeof(Delegate).IsAssignableFrom(fieldInfo.FieldType))
                {
                    continue;
                }
                var import = fieldInfo.GetCustomAttributes(typeof(EvImportAttribute), true).Cast <EvImportAttribute> ().First();

                var lib     = import.Dll == EvDll.Core ? core : extra;
                var funcPtr = loader.GetProcAddress(lib, import.Name ?? fieldInfo.FieldType.Name);
                fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(funcPtr, fieldInfo.FieldType));
            }

            EventSetLogCallback(LogCallbackDelegate);
            var selectorPtr = platform == PlatformID.Win32NT
                                  ? loader.GetProcAddress(core, "evthread_use_windows_threads")
                                  : loader.GetProcAddress(loader.LoadLibrary(basePath, "libevent_pthreads"), "evthread_use_pthreads");

            ((D.thread_model_selector)Marshal.GetDelegateForFunctionPointer(selectorPtr, typeof(D.thread_model_selector)))();
        }
Ejemplo n.º 2
0
        public static void Init(string basePath = null)
        {
            var platform = Environment.OSVersion.Platform;

            if (platform == PlatformID.Unix && File.Exists("/usr/lib/libSystem.dylib"))
                platform = PlatformID.MacOSX;

            IDynLoader loader;
            switch (platform)
            {
                case PlatformID.Win32NT:
                    WSAStartup(514, new byte[512]);
                    loader = new Win32Loader();
                    break;
                case PlatformID.Unix:
                    loader = new LinuxLoader();
                    break;
                case PlatformID.MacOSX:
                    loader = new OsxLoader();
                    break;
                default:
                    throw new NotSupportedException("Platform " + platform + " is not supported.");
            }

            var core = loader.LoadLibrary(basePath, "libevent_core");
            var extra = loader.LoadLibrary(basePath, "libevent_extra");
            foreach (var fieldInfo in typeof (Event).GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                if (!typeof (Delegate).IsAssignableFrom(fieldInfo.FieldType))
                    continue;
                var import = fieldInfo.GetCustomAttributes (typeof (EvImportAttribute), true).Cast<EvImportAttribute> ().First ();

                var lib = import.Dll == EvDll.Core ? core : extra;
                var funcPtr = loader.GetProcAddress(lib, import.Name ?? fieldInfo.FieldType.Name);
                fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(funcPtr, fieldInfo.FieldType));
            }

            EventSetLogCallback(LogCallbackDelegate);
            var selectorPtr = platform == PlatformID.Win32NT
                                  ? loader.GetProcAddress(core, "evthread_use_windows_threads")
                                  : loader.GetProcAddress(loader.LoadLibrary(basePath, "libevent_pthreads"), "evthread_use_pthreads");
            ((D.thread_model_selector) Marshal.GetDelegateForFunctionPointer(selectorPtr, typeof (D.thread_model_selector)))();
        }