Ejemplo n.º 1
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));
        }