Example #1
0
        public static void Startup(ENetAllocator allocator = null)
        {
            if (Started)
            {
                return;
            }
            Started = true;

            Allocator = (allocator == null) ? new ENetManagedAllocator() : allocator;

            LibENet.Load();

            Native.ENetCallbacks callbacks = new Native.ENetCallbacks();
            callbacks.Malloc   = Marshal.GetFunctionPointerForDelegate(MemAllocDelegate);
            callbacks.Free     = Marshal.GetFunctionPointerForDelegate(MemFreeDelegate);
            callbacks.NoMemory = Marshal.GetFunctionPointerForDelegate(NoMemoryDelegate);

            var linkedVer = LibENet.LinkedVersion();

            if (LibENet.InitializeWithCallbacks(linkedVer, &callbacks) != 0)
            {
                throw new Exception("ENet library initialization failed.");
            }

            LinkedVersion = new Version((int)(((linkedVer) >> 16) & 0xFF),
                                        (int)(((linkedVer) >> 8) & 0xFF),
                                        (int)((linkedVer) & 0xFF));
        }
Example #2
0
        /// <summary>
        /// Initializes ENet with given startup options.
        /// </summary>
        /// <param name="startupOptions">The startup options.</param>
        public static void Startup(ENetStartupOptions startupOptions)
        {
            ThrowHelper.ThrowIfArgumentNull(startupOptions, nameof(startupOptions));
            startupOptions.CheckValues();

            var allocator = startupOptions.Allocator;

            if (Started)
            {
                return;
            }
            Started = true;

            if (startupOptions.ModulePath != null)
            {
                LibENet.Load(startupOptions.ModulePath);
            }
            else if (startupOptions.ModuleHandle != IntPtr.Zero)
            {
                LibENet.Load(startupOptions.ModuleHandle);
            }
            else
            {
                // load from native dependencies.
                LibENet.Load();
            }

            var linkedVer = LibENet.LinkedVersion();

            s_LinkedVersion = new Version((int)(((linkedVer) >> 16) & 0xFF),
                                          (int)(((linkedVer) >> 8) & 0xFF),
                                          (int)((linkedVer) & 0xFF));

            if (allocator == null)
            {
                if (LibENet.Initialize() != 0)
                {
                    ThrowHelper.ThrowENetInitializationFailed();
                }
            }
            else
            {
                s_Allocator = allocator;

                NativeENetCallbacks callbacks = new NativeENetCallbacks
                {
                    Malloc   = Marshal.GetFunctionPointerForDelegate(s_MemAllocDelegate),
                    Free     = Marshal.GetFunctionPointerForDelegate(s_MemFreeDelegate),
                    NoMemory = Marshal.GetFunctionPointerForDelegate(s_NoMemoryDelegate)
                };

                if (LibENet.InitializeWithCallbacks(linkedVer, &callbacks) != 0)
                {
                    ThrowHelper.ThrowENetInitializationFailed();
                }
            }
        }