Beispiel #1
0
        public void Open(string name, SyncObjectTypes syncObjects)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            _name = name;

            // Open file mapping.
            _hSharedMemory = WinApi.OpenFileMapping(SectionAccessFlags.SECTION_ALL_ACCESS, false, _name);
            if (_hSharedMemory == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(),
                                         "Error opening file mapping.");
            }

            // Get base address of file mapping.
            _pSharedMemory = WinApi.MapViewOfFile(_hSharedMemory, SectionAccessFlags.SECTION_ALL_ACCESS,
                                                  0, 0, IntPtr.Zero);
            if (_pSharedMemory == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(),
                                         "Error getting bsae address of file mapping.");
            }

            // Create synchronisation objects.
            if (syncObjects != SyncObjectTypes.SyncObjNone)
            {
                CreateSyncObjects(syncObjects, name);
            }

            this.IsAvailable = true;
        }
Beispiel #2
0
        private bool _isDisposed = false; // True if object has been disposed.

        public SharedMemory(string name, int size, SyncObjectTypes syncObjects, bool create)
        {
            if (create)
            {
                Create(name, size, syncObjects);
            }
            else
            {
                Open(name, syncObjects);
            }
        }
Beispiel #3
0
        private void CreateSyncObjects(SyncObjectTypes syncObjects, string name)
        {
            // Create synchronisation objects.
            if (syncObjects >= SyncObjectTypes.SyncObjRequest)
            {
                _sharedMutex    = new Mutex(false, name + "_mutex");
                _sharedReqEvent = new EventWaitHandle(false, EventResetMode.AutoReset,
                                                      name + "_req_event");
            }

            if (syncObjects >= SyncObjectTypes.SyncObjBoth)
            {
                _sharedRespEvent = new EventWaitHandle(false, EventResetMode.AutoReset,
                                                       name + "_resp_event");
            }
        }
Beispiel #4
0
        public void Create(string name, int size, SyncObjectTypes syncObjects)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            _name = name;
            _size = size;

            // Create file mapping.
            _hSharedMemory = WinApi.CreateFileMapping((IntPtr)WinApi.INVALID_HANDLE_VALUE, IntPtr.Zero,
                                                      PageProtection.ReadWrite, 0, _size * Marshal.SizeOf(typeof(T)), name);
            if (_hSharedMemory == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(),
                                         "Error creating file mapping.");
            }

            // Get base address of file mapping.
            _pSharedMemory = WinApi.MapViewOfFile(_hSharedMemory, SectionAccessFlags.SECTION_ALL_ACCESS,
                                                  0, 0, IntPtr.Zero);
            if (_pSharedMemory == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(),
                                         "Error getting bsae address of file mapping.");
            }

            // Initialize file-mapped memory to zero.
            WinApi.ZeroMemory(_pSharedMemory, _size * Marshal.SizeOf(typeof(T)));

            // Create synchronisation objects.
            if (syncObjects != SyncObjectTypes.SyncObjNone)
            {
                CreateSyncObjects(syncObjects, name);
            }

            this.IsAvailable = true;
        }