Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="InterProcessMlosContext"/> class.
        /// </summary>
        /// <returns>InterProcessMlosContext instance.</returns>
        public static InterProcessMlosContext CreateOrOpen()
        {
            NamedEvent targetProcessNamedEvent = NamedEvent.CreateOrOpen(TargetProcessEventName);
            {
                bool shouldWaitForTargetProcess = false;

                try
                {
                    // Try open a global shared memory and check if the tager process is fully initialized.
                    //
                    using SharedMemoryRegionView <MlosProxyInternal.GlobalMemoryRegion> targetProcessMemoryRegionView = SharedMemoryRegionView.OpenExisting <MlosProxyInternal.GlobalMemoryRegion>(GlobalMemoryMapName, SharedMemorySize);

                    if (targetProcessMemoryRegionView.MemoryRegion().GlobalMemoryRegionIndex == 1)
                    {
                        shouldWaitForTargetProcess = true;
                    }
                }
                catch (FileNotFoundException)
                {
                    // If the target process is not running.
                    //
                    shouldWaitForTargetProcess = true;
                }

                if (shouldWaitForTargetProcess)
                {
                    // If the target process is not fully initialized, wait for the signal.
                    //
                    targetProcessNamedEvent.Wait();
                    targetProcessNamedEvent.Signal();
                }
            }

            SharedMemoryRegionView <MlosProxyInternal.GlobalMemoryRegion> globalMemoryRegionView = SharedMemoryRegionView.OpenExisting <MlosProxyInternal.GlobalMemoryRegion>(GlobalMemoryMapName, SharedMemorySize);
            SharedMemoryMapView controlChannelMemoryMapView  = SharedMemoryMapView.OpenExisting(ControlChannelMemoryMapName, SharedMemorySize);
            SharedMemoryMapView feedbackChannelMemoryMapView = SharedMemoryMapView.OpenExisting(FeedbackChannelMemoryMapName, SharedMemorySize);

            // Create channel synchronization primitives.
            //
            NamedEvent controlChannelNamedEvent  = NamedEvent.CreateOrOpen(ControlChannelEventName);
            NamedEvent feedbackChannelNamedEvent = NamedEvent.CreateOrOpen(FeedbackChannelEventName);

            return(new InterProcessMlosContext(
                       globalMemoryRegionView,
                       controlChannelMemoryMapView,
                       feedbackChannelMemoryMapView,
                       controlChannelNamedEvent,
                       feedbackChannelNamedEvent,
                       targetProcessNamedEvent));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="InterProcessMlosContext"/> class.
        /// </summary>
        /// <returns>InterProcessMlosContext instance.</returns>
        public static InterProcessMlosContext CreateOrOpen()
        {
            // Create or open the memory mapped files.
            //
            SharedMemoryRegionView <MlosProxyInternal.GlobalMemoryRegion> globalMemoryRegionView = SharedMemoryRegionView.CreateOrOpen <MlosProxyInternal.GlobalMemoryRegion>(GlobalMemoryMapName, SharedMemorySize);
            SharedMemoryMapView controlChannelMemoryMapView  = SharedMemoryMapView.CreateOrOpen(ControlChannelMemoryMapName, SharedMemorySize);
            SharedMemoryMapView feedbackChannelMemoryMapView = SharedMemoryMapView.CreateOrOpen(FeedbackChannelMemoryMapName, SharedMemorySize);

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

            return(new InterProcessMlosContext(
                       globalMemoryRegionView,
                       controlChannelMemoryMapView,
                       feedbackChannelMemoryMapView,
                       controlChannelNamedEvent,
                       feedbackChannelNamedEvent));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="InterProcessMlosContext"/> class.
        /// </summary>
        /// <returns>InterProcessMlosContext instance.</returns>
        public static InterProcessMlosContext Create()
        {
            var targetProcessNamedEvent = NamedEvent.CreateOrOpen(TargetProcessEventName);
            {
                bool shouldWaitForTargetProcess = false;

                try
                {
                    // Try open a global shared memory and check if the target process is fully initialized.
                    //
                    using var targetProcessMemoryRegionView = SharedMemoryRegionView.OpenExisting <MlosProxyInternal.GlobalMemoryRegion>(GlobalMemoryMapName, MlosInternal.GlobalMemoryRegion.GlobalSharedMemorySize);

                    // If RegisteredSettingsAssemblyCount is 0, the context is not created.
                    //
                    if (targetProcessMemoryRegionView.MemoryRegion().RegisteredSettingsAssemblyCount.Load() == 0)
                    {
                        shouldWaitForTargetProcess = true;
                    }
                }
                catch (FileNotFoundException)
                {
                    // If the target process is not running.
                    //
                    shouldWaitForTargetProcess = true;
                }

                if (shouldWaitForTargetProcess)
                {
                    // If the target process is not fully initialized, wait for the signal.
                    //
                    targetProcessNamedEvent.Wait();
                    targetProcessNamedEvent.Signal();
                }
            }

            // Open global memory region.
            //
            var globalMemoryRegionView            = SharedMemoryRegionView.OpenExisting <MlosProxyInternal.GlobalMemoryRegion>(GlobalMemoryMapName, MlosInternal.GlobalMemoryRegion.GlobalSharedMemorySize);
            GlobalMemoryRegion globalMemoryRegion = globalMemoryRegionView.MemoryRegion();

            // Open existing shared channels memory maps.
            //
            globalMemoryRegion.TryOpenExisting(
                new MlosInternal.MemoryRegionId {
                Type = MlosInternal.MemoryRegionType.ControlChannel, Index = 0,
            },
                out SharedMemoryMapView controlChannelMemoryMapView);

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

            // Open existing channel synchronization primitives.
            //
            globalMemoryRegion.TryOpenExisting(
                new MlosInternal.MemoryRegionId {
                Type = MlosInternal.MemoryRegionType.ControlChannel, Index = 0,
            },
                out NamedEvent controlChannelNamedEvent);

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

            return(new InterProcessMlosContext(
                       globalMemoryRegionView,
                       controlChannelMemoryMapView,
                       feedbackChannelMemoryMapView,
                       controlChannelNamedEvent,
                       feedbackChannelNamedEvent,
                       targetProcessNamedEvent));
        }