Beispiel #1
0
    public BaseSharedChannelBenchmark()
    {
        globalChannelMemoryRegionView = SharedMemoryRegionView.CreateNew <MlosProxyInternal.GlobalMemoryRegion>(GlobalMemoryMapName, SharedMemorySize);
        sharedChannelMemoryMapView    = SharedMemoryMapView.CreateNew(SharedChannelMemoryMapName, SharedMemorySize);

        MlosProxyInternal.GlobalMemoryRegion globalMemoryRegion = globalChannelMemoryRegionView.MemoryRegion();

        sharedChannel = new TestSharedChannel(sharedChannelMemoryMapView, globalMemoryRegion.ControlChannelSynchronization);

        settingsAssemblyManager.RegisterAssembly(typeof(MlosContext).Assembly);
        settingsAssemblyManager.RegisterAssembly(typeof(AssemblyInitializer).Assembly);

        // Preallocate class type messages to avoid heap allocations.
        //
        stringViewArray    = new StringViewArray();
        stringViewArray.Id = 1;
        stringViewArray.Strings[0].Value = "12345";
        stringViewArray.Strings[1].Value = "54321";
        stringViewArray.Strings[2].Value = "abc";
        stringViewArray.Strings[3].Value = "cba";
        stringViewArray.Strings[4].Value = "sbs";

        wideStringMultiMessage = new WideStringMultiMessage();
        wideStringMultiMessage.StringMessages[0].Id = 4;
        wideStringMultiMessage.StringMessages[0].Strings[0].Value = "Test_Name9876";
        wideStringMultiMessage.StringMessages[0].Strings[1].Value = "Test_Name19876";
        wideStringMultiMessage.StringMessages[0].Strings[2].Value = "Test_Name1239876";
        wideStringMultiMessage.StringMessages[0].Strings[3].Value = "Test_Name45659876";
        wideStringMultiMessage.StringMessages[0].Strings[4].Value = "Test_Name901239876";

        pointMessage.X = 4;
        pointMessage.Y = 5;
    }
Beispiel #2
0
        /// <summary>
        /// #TODO remove, this is not required.
        /// set the terminate channel in sync object and signal.
        /// </summary>
        /// <param name="msg"></param>
        private static void TerminateReaderThreadRequestMessageCallback(TerminateReaderThreadRequestMessage msg)
        {
            // Terminate the channel.
            //
            MlosProxyInternal.GlobalMemoryRegion globalMemoryRegion = globalMemoryRegionView.MemoryRegion();
            ChannelSynchronization controlChannelSync = globalMemoryRegion.ControlChannelSynchronization;

            controlChannelSync.TerminateChannel.Store(true);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AnonymousMemoryMlosContext"/> class.
        /// </summary>
        /// <param name="globalMemoryRegionView"></param>
        /// <param name="controlChannelMemoryMapView"></param>
        /// <param name="feedbackChannelMemoryMapView"></param>
        /// <param name="sharedConfigMemoryRegionView"></param>
        /// <param name="controlChannelNamedEvent"></param>
        /// <param name="feedbackChannelNamedEvent"></param>
        /// <param name="fileDescriptorExchangeServer"></param>
        private AnonymousMemoryMlosContext(
            SharedMemoryRegionView <MlosProxyInternal.GlobalMemoryRegion> globalMemoryRegionView,
            SharedMemoryMapView controlChannelMemoryMapView,
            SharedMemoryMapView feedbackChannelMemoryMapView,
            SharedMemoryRegionView <MlosProxyInternal.SharedConfigMemoryRegion> sharedConfigMemoryRegionView,
            NamedEvent controlChannelNamedEvent,
            NamedEvent feedbackChannelNamedEvent,
            FileDescriptorExchangeServer fileDescriptorExchangeServer)
        {
            this.globalMemoryRegionView       = globalMemoryRegionView ?? throw new ArgumentNullException(nameof(globalMemoryRegionView));
            this.controlChannelMemoryMapView  = controlChannelMemoryMapView ?? throw new ArgumentNullException(nameof(controlChannelMemoryMapView));
            this.feedbackChannelMemoryMapView = feedbackChannelMemoryMapView ?? throw new ArgumentNullException(nameof(feedbackChannelMemoryMapView));

            this.controlChannelNamedEvent  = controlChannelNamedEvent ?? throw new ArgumentNullException(nameof(controlChannelNamedEvent));
            this.feedbackChannelNamedEvent = feedbackChannelNamedEvent ?? throw new ArgumentNullException(nameof(feedbackChannelNamedEvent));

            this.fileDescriptorExchangeServer = fileDescriptorExchangeServer;

            // Create the shared config manager.
            //
            SharedConfigManager = new SharedConfigManager();

            // Register shared config memory region.
            //
            SharedConfigManager.RegisterSharedConfigMemoryRegion(sharedConfigMemoryRegionView);

            MlosProxyInternal.GlobalMemoryRegion globalMemoryRegion = globalMemoryRegionView.MemoryRegion();

            // Increase the usage counter. When closing global shared memory, we will decrease the counter.
            // If there is no process using the shared memory, we will clean the OS resources. On Windows OS,
            // this is no-op; on Linux, we unlink created files.
            //
            globalMemoryRegion.AttachedProcessesCount.FetchAdd(1);

            // Create the control channel instance.
            //
            ControlChannel = new SharedChannel <InterProcessSharedChannelPolicy, SharedChannelSpinPolicy>(
                buffer: controlChannelMemoryMapView.Buffer,
                size: (uint)controlChannelMemoryMapView.MemSize,
                sync: globalMemoryRegion.ControlChannelSynchronization)
            {
                ChannelPolicy = { NotificationEvent = controlChannelNamedEvent },
            };

            // Create the feedback channel instance.
            //
            FeedbackChannel = new SharedChannel <InterProcessSharedChannelPolicy, SharedChannelSpinPolicy>(
                buffer: feedbackChannelMemoryMapView.Buffer,
                size: (uint)feedbackChannelMemoryMapView.MemSize,
                sync: globalMemoryRegion.FeedbackChannelSynchronization)
            {
                ChannelPolicy = { NotificationEvent = feedbackChannelNamedEvent },
            };
        }
Beispiel #4
0
        public SharedChannelTests()
        {
            // Load the registry settings assemblies.
            //
            _ = SettingsAssemblyInitializer.GetGlobalDispatchTable();

            // Initialize shared channel.
            //
            globalChannelMemoryRegionView = SharedMemoryRegionView.Create <MlosProxyInternal.GlobalMemoryRegion>(GlobalMemoryMapName, SharedMemorySize);
            sharedChannelMemoryMapView    = SharedMemoryMapView.Create(SharedChannelMemoryMapName, SharedMemorySize);

            MlosProxyInternal.GlobalMemoryRegion globalMemoryRegion = globalChannelMemoryRegionView.MemoryRegion();

            sharedChannel = new TestSharedChannel(sharedChannelMemoryMapView, globalMemoryRegion.ControlChannelSynchronization);
        }
Beispiel #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="InterProcessMlosContext"/> class.
        /// </summary>
        /// <param name="globalMemoryRegionView"></param>
        /// <param name="controlChannelMemoryMapView"></param>
        /// <param name="feedbackChannelMemoryMapView"></param>
        /// <param name="controlChannelNamedEvent"></param>
        /// <param name="feedbackChannelNamedEvent"></param>
        /// <param name="targetProcessNamedEvent"></param>
        internal InterProcessMlosContext(
            SharedMemoryRegionView <MlosProxyInternal.GlobalMemoryRegion> globalMemoryRegionView,
            SharedMemoryMapView controlChannelMemoryMapView,
            SharedMemoryMapView feedbackChannelMemoryMapView,
            NamedEvent controlChannelNamedEvent,
            NamedEvent feedbackChannelNamedEvent,
            NamedEvent targetProcessNamedEvent)
        {
            this.globalMemoryRegionView       = globalMemoryRegionView;
            this.controlChannelMemoryMapView  = controlChannelMemoryMapView;
            this.feedbackChannelMemoryMapView = feedbackChannelMemoryMapView;

            this.controlChannelNamedEvent  = controlChannelNamedEvent;
            this.feedbackChannelNamedEvent = feedbackChannelNamedEvent;
            this.targetProcessNamedEvent   = targetProcessNamedEvent;

            // Create the shared config manager.
            //
            SharedConfigManager = new SharedConfigManager();

            MlosProxyInternal.GlobalMemoryRegion globalMemoryRegion = globalMemoryRegionView.MemoryRegion();

            // Increase the usage counter. When closing global shared memory, we will decrease the counter.
            // If there is no process using the shared memory, we will clean the OS resources. On Windows OS,
            // this is no-op; on Linux, we unlink created files.
            //
            globalMemoryRegion.AttachedProcessesCount.FetchAdd(1);

            // Create the control channel instance.
            //
            ControlChannel = new SharedChannel <InterProcessSharedChannelPolicy, SharedChannelSpinPolicy>(
                buffer: controlChannelMemoryMapView.Buffer,
                size: (uint)controlChannelMemoryMapView.MemSize,
                sync: globalMemoryRegion.ControlChannelSynchronization)
            {
                ChannelPolicy = { NotificationEvent = controlChannelNamedEvent },
            };

            // Create the feedback channel instance.
            //
            FeedbackChannel = new SharedChannel <InterProcessSharedChannelPolicy, SharedChannelSpinPolicy>(
                buffer: feedbackChannelMemoryMapView.Buffer,
                size: (uint)feedbackChannelMemoryMapView.MemSize,
                sync: globalMemoryRegion.FeedbackChannelSynchronization)
            {
                ChannelPolicy = { NotificationEvent = feedbackChannelNamedEvent },
            };
        }
Beispiel #6
0
        /// <summary>
        /// Initialize shared channel.
        /// </summary>
        public static void InitializeSharedChannel()
        {
            // Create or open the memory mapped files.
            //
            globalMemoryRegionView       = SharedMemoryRegionView.CreateOrOpen <MlosProxyInternal.GlobalMemoryRegion>(GlobalMemoryMapName, SharedMemorySize);
            controlChannelMemoryMapView  = SharedMemoryMapView.CreateOrOpen(ControlChannelMemoryMapName, SharedMemorySize);
            feedbackChannelMemoryMapView = SharedMemoryMapView.CreateOrOpen(FeedbackChannelMemoryMapName, SharedMemorySize);
            sharedConfigMemoryMapView    = SharedMemoryRegionView.CreateOrOpen <MlosProxyInternal.SharedConfigMemoryRegion>(SharedConfigMemoryMapName, SharedMemorySize);

            // Create channel synchronization primitives.
            //
            controlChannelNamedEvent  = NamedEvent.CreateOrOpen(ControlChannelSemaphoreName);
            feedbackChannelNamedEvent = NamedEvent.CreateOrOpen(FeedbackChannelSemaphoreName);

            // Setup feedback channel.
            //
            MlosProxyInternal.GlobalMemoryRegion globalMemoryRegion = globalMemoryRegionView.MemoryRegion();

            // Enable channels.
            //
            globalMemoryRegion.ControlChannelSynchronization.TerminateChannel.Store(false);
            globalMemoryRegion.FeedbackChannelSynchronization.TerminateChannel.Store(false);

            var feedbackChannel = new SharedChannel <InterProcessSharedChannelPolicy, SharedChannelSpinPolicy>(
                buffer: feedbackChannelMemoryMapView.Buffer,
                size: (uint)feedbackChannelMemoryMapView.MemSize,
                sync: globalMemoryRegion.FeedbackChannelSynchronization);

            feedbackChannel.ChannelPolicy.NotificationEvent = feedbackChannelNamedEvent;

            // Set SharedConfig memory region.
            //
            SharedConfigManager.SetMemoryRegion(new MlosProxyInternal.SharedConfigMemoryRegion()
            {
                Buffer = sharedConfigMemoryMapView.MemoryRegion().Buffer
            });

            // Setup MlosContext.
            //
            MlosContext.FeedbackChannel     = feedbackChannel;
            MlosContext.SharedConfigManager = SharedConfigManager;

            // Initialize callbacks.
            //
            MlosProxyInternal.RegisterAssemblyRequestMessage.Callback                 = RegisterAssemblyCallback;
            MlosProxyInternal.RegisterMemoryRegionRequestMessage.Callback             = RegisterMemoryRegionMessageCallback;
            MlosProxyInternal.RegisterSharedConfigMemoryRegionRequestMessage.Callback = RegisterSharedConfigMemoryRegionRequestMessageCallback;
            MlosProxy.TerminateReaderThreadRequestMessage.Callback = TerminateReaderThreadRequestMessageCallback;

            // Register Mlos.Core assembly.
            //
            RegisterAssembly(typeof(MlosContext).Assembly, dispatchTableBaseIndex: 0);

            // Register assemblies from the shared config.
            // Assembly Mlos.NetCore does not have a config, as it is always registered first.
            //
            for (uint index = 1; index < globalMemoryRegion.RegisteredSettingsAssemblyCount.Load(); index++)
            {
                RegisterSettingsAssembly(assemblyIndex: index);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="InterProcessMlosContext"/> class.
        /// </summary>
        /// <param name="globalMemoryRegionView"></param>
        /// <param name="controlChannelMemoryMapView"></param>
        /// <param name="feedbackChannelMemoryMapView"></param>
        /// <param name="controlChannelNamedEvent"></param>
        /// <param name="feedbackChannelNamedEvent"></param>
        /// <param name="targetProcessNamedEvent"></param>
        private InterProcessMlosContext(
            SharedMemoryRegionView <MlosProxyInternal.GlobalMemoryRegion> globalMemoryRegionView,
            SharedMemoryMapView controlChannelMemoryMapView,
            SharedMemoryMapView feedbackChannelMemoryMapView,
            NamedEvent controlChannelNamedEvent,
            NamedEvent feedbackChannelNamedEvent,
            NamedEvent targetProcessNamedEvent)
        {
            this.globalMemoryRegionView       = globalMemoryRegionView ?? throw new ArgumentNullException(nameof(globalMemoryRegionView));
            this.controlChannelMemoryMapView  = controlChannelMemoryMapView ?? throw new ArgumentNullException(nameof(controlChannelMemoryMapView));
            this.feedbackChannelMemoryMapView = feedbackChannelMemoryMapView ?? throw new ArgumentNullException(nameof(feedbackChannelMemoryMapView));

            this.controlChannelNamedEvent  = controlChannelNamedEvent ?? throw new ArgumentNullException(nameof(controlChannelNamedEvent));
            this.feedbackChannelNamedEvent = feedbackChannelNamedEvent ?? throw new ArgumentNullException(nameof(feedbackChannelNamedEvent));
            this.targetProcessNamedEvent   = targetProcessNamedEvent ?? throw new ArgumentNullException(nameof(targetProcessNamedEvent));

            // Create the shared config manager.
            //
            SharedConfigManager = new SharedConfigManager();

            MlosProxyInternal.GlobalMemoryRegion globalMemoryRegion = globalMemoryRegionView.MemoryRegion();

            // Increase the usage counter. When closing global shared memory, we will decrease the counter.
            // If there is no process using the shared memory, we will clean the OS resources. On Windows OS,
            // this is no-op; on Linux, we unlink created files.
            //
            globalMemoryRegion.AttachedProcessesCount.FetchAdd(1);

            // Create the control channel instance.
            //
            ControlChannel = new SharedChannel <InterProcessSharedChannelPolicy, SharedChannelSpinPolicy>(
                buffer: controlChannelMemoryMapView.Buffer,
                size: (uint)controlChannelMemoryMapView.MemSize,
                sync: globalMemoryRegion.ControlChannelSynchronization)
            {
                ChannelPolicy = { NotificationEvent = controlChannelNamedEvent },
            };

            // Create the feedback channel instance.
            //
            FeedbackChannel = new SharedChannel <InterProcessSharedChannelPolicy, SharedChannelSpinPolicy>(
                buffer: feedbackChannelMemoryMapView.Buffer,
                size: (uint)feedbackChannelMemoryMapView.MemSize,
                sync: globalMemoryRegion.FeedbackChannelSynchronization)
            {
                ChannelPolicy = { NotificationEvent = feedbackChannelNamedEvent },
            };

            // Register config region if available.
            //
            if (globalMemoryRegion.TryOpenExisting(
                    new MlosInternal.MemoryRegionId {
                Type = MlosInternal.MemoryRegionType.SharedConfig, Index = 0,
            },
                    out SharedMemoryMapView sharedConfigMemoryMap))
            {
                var sharedMemoryRegionView = new SharedMemoryRegionView <MlosProxyInternal.SharedConfigMemoryRegion>(sharedConfigMemoryMap);

                SharedConfigManager.RegisterSharedConfigMemoryRegion(sharedMemoryRegionView);
            }
            else
            {
                sharedConfigMemoryMap?.Dispose();
            }
        }
        /// <summary>
        /// Creates AnonymousMemoryMlosContext.
        /// </summary>
        /// <returns></returns>
        public static AnonymousMemoryMlosContext Create()
        {
            var fdExchangeServer = new FileDescriptorExchangeServer(DefaultSocketFolderPath);

            fdExchangeServer.HandleRequests();

            SharedMemoryRegionView <MlosProxyInternal.GlobalMemoryRegion> globalMemoryRegionView =
                SharedMemoryRegionView.OpenFromFileDescriptor <MlosProxyInternal.GlobalMemoryRegion>(
                    fdExchangeServer.GetSharedMemoryFd(GlobalMemoryMapName));

            // Create channel synchronization primitives.
            //
            MlosProxyInternal.GlobalMemoryRegion globalMemoryRegion = globalMemoryRegionView.MemoryRegion();

            // Control channel.
            //
            globalMemoryRegion.TryGetSharedMemoryName(
                new MlosInternal.MemoryRegionId {
                Type = MemoryRegionType.ControlChannel, Index = 0
            },
                out string sharedMemoryMapName);

            SharedMemoryMapView controlChannelMemoryMapView = SharedMemoryMapView.OpenFromFileDescriptor(
                fdExchangeServer.GetSharedMemoryFd(sharedMemoryMapName));

            // Feedback channel.
            //
            globalMemoryRegion.TryGetSharedMemoryName(
                new MlosInternal.MemoryRegionId {
                Type = MemoryRegionType.FeedbackChannel, Index = 0
            },
                out sharedMemoryMapName);

            SharedMemoryMapView feedbackChannelMemoryMapView = SharedMemoryMapView.OpenFromFileDescriptor(
                fdExchangeServer.GetSharedMemoryFd(sharedMemoryMapName));

            // Shared config.
            //
            globalMemoryRegion.TryGetSharedMemoryName(
                new MlosInternal.MemoryRegionId {
                Type = MemoryRegionType.SharedConfig, Index = 0
            },
                out sharedMemoryMapName);

            SharedMemoryMapView sharedConfigMemoryMapView = SharedMemoryMapView.OpenFromFileDescriptor(
                fdExchangeServer.GetSharedMemoryFd(sharedMemoryMapName));

            var sharedConfigMemoryRegionView = new SharedMemoryRegionView <MlosProxyInternal.SharedConfigMemoryRegion>(sharedConfigMemoryMapView);

            globalMemoryRegion.TryOpenExisting(
                new MlosInternal.MemoryRegionId {
                Type = MemoryRegionType.ControlChannel, Index = 0,
            },
                out NamedEvent controlChannelNamedEvent);

            globalMemoryRegion.TryOpenExisting(
                new MlosInternal.MemoryRegionId {
                Type = MemoryRegionType.FeedbackChannel, Index = 0,
            },
                out NamedEvent feedbackChannelNamedEvent);

            return(new AnonymousMemoryMlosContext(
                       globalMemoryRegionView,
                       controlChannelMemoryMapView,
                       feedbackChannelMemoryMapView,
                       sharedConfigMemoryRegionView,
                       controlChannelNamedEvent,
                       feedbackChannelNamedEvent,
                       fdExchangeServer));
        }