/// <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 }, }; }
/// <summary> /// Creates or opens a shared memory view. /// </summary> /// <param name="sharedMemoryMapName"></param> /// <param name="sharedMemorySize"></param> /// <returns></returns> public static new SharedMemoryMapView CreateOrOpen(string sharedMemoryMapName, ulong sharedMemorySize) { // Create or open shared memory view. // var sharedMemoryMapView = new SharedMemoryMapView( sharedMemoryMapName, sharedMemorySize, Native.OpenFlags.O_CREAT | Native.OpenFlags.O_RDWR); return(sharedMemoryMapView); }
/// <summary> /// Creates InterProcessMlosContext. /// </summary> /// <returns></returns> public static AnonymousMemoryMlosContext Create() { var fdExchangeServer = new FileDescriptorExchangeServer(FdUnitDomainSocketName, FdExchangeSemaphoreName); fdExchangeServer.HandleRequests(); MemoryRegionId memoryRegionId = new MemoryRegionId { Type = MemoryRegionType.Global }; SharedMemoryRegionView <MlosProxyInternal.GlobalMemoryRegion> globalMemoryRegionView = SharedMemoryRegionView.OpenAnonymousFromFileDescriptor <MlosProxyInternal.GlobalMemoryRegion>( fdExchangeServer.GetSharedMemoryFd(memoryRegionId), fdExchangeServer.GetSharedMemorySize(memoryRegionId)); memoryRegionId = new MemoryRegionId { Type = MemoryRegionType.ControlChannel }; SharedMemoryMapView controlChannelMemoryMapView = SharedMemoryMapView.OpenAnonymousFromFileDescriptor( fdExchangeServer.GetSharedMemoryFd(memoryRegionId), fdExchangeServer.GetSharedMemorySize(memoryRegionId)); memoryRegionId = new MemoryRegionId { Type = MemoryRegionType.FeedbackChannel }; SharedMemoryMapView feedbackChannelMemoryMapView = SharedMemoryMapView.OpenAnonymousFromFileDescriptor( fdExchangeServer.GetSharedMemoryFd(memoryRegionId), fdExchangeServer.GetSharedMemorySize(memoryRegionId)); memoryRegionId = new MemoryRegionId { Type = MemoryRegionType.SharedConfig }; SharedMemoryRegionView <MlosProxyInternal.SharedConfigMemoryRegion> sharedConfigMemoryRegionView = SharedMemoryRegionView.OpenAnonymousFromFileDescriptor <MlosProxyInternal.SharedConfigMemoryRegion>( fdExchangeServer.GetSharedMemoryFd(memoryRegionId), fdExchangeServer.GetSharedMemorySize(memoryRegionId)); // Create channel synchronization primitives. // NamedEvent controlChannelNamedEvent = NamedEvent.CreateOrOpen(ControlChannelSemaphoreName); NamedEvent feedbackChannelNamedEvent = NamedEvent.CreateOrOpen(FeedbackChannelSemaphoreName); return(new AnonymousMemoryMlosContext( globalMemoryRegionView, controlChannelMemoryMapView, feedbackChannelMemoryMapView, sharedConfigMemoryRegionView, controlChannelNamedEvent, feedbackChannelNamedEvent, fdExchangeServer)); }
/// <summary> /// Create a new shared memory view. /// </summary> /// <param name="sharedMemoryMapName"></param> /// <param name="sharedMemorySize"></param> /// <returns></returns> public static new SharedMemoryMapView CreateNew(string sharedMemoryMapName, ulong sharedMemorySize) { // Try to unlink existing shared memory. // _ = Native.SharedMemoryUnlink(sharedMemoryMapName); // Create shared memory view. // var sharedMemoryMapView = new SharedMemoryMapView( sharedMemoryMapName, sharedMemorySize, Native.OpenFlags.O_CREAT | Native.OpenFlags.O_RDWR | Native.OpenFlags.O_EXCL); return(sharedMemoryMapView); }
/// <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)); }