chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
 
namespace OpenTap.Package.Ipc
{
    /// <summary>
    /// Used for sharing state information between processes. The data generated here is cleaned up automatically when all the processes uses it stops.
    /// </summary>
    internal class SharedState : IDisposable
    {
        protected MemoryMappedFile share;
        private Mutex appstateMutex;
 
        private string filename;
 
        public SharedState(string name, string dir)
        {
            if (dir == null)
                throw new ArgumentNullException(nameof(dir));
 
            filename = Path.GetFullPath(Path.Combine(dir, name)).Replace('\\', '/');
 
            var hasher = SHA256.Create();
            var hash = hasher.ComputeHash(Encoding.UTF8.GetBytes(filename));
 
            string mutexName = "OpenTap.Package " + BitConverter.ToString(hash).Replace("-", "");
            appstateMutex = new Mutex(true, mutexName, out bool mutexCreated);
            // if mutexCreated is true, it means that this was the first application to open it. 
            // Mutexes are automatically deleted when no application uses them anymore.
            // when this happens, the state file is cleared by writing 0's to it.
 
            // For AppState, this means that PID is 0 for the first application using it.
            // the PID is used to block other applications from using it, for example by opening two package managers.
 
            if (!mutexCreated)
                appstateMutex.WaitOne();
 
            var stream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
            share = MemoryMappedFile.CreateFromFile(stream, null, 1024, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false);
 
            if (mutexCreated)
                using (var view = share.CreateViewAccessor())
                    view.WriteArray(0, new byte[1024], 0, 1024);
 
            appstateMutex.ReleaseMutex();
        }
 
        public void Dispose()
        {
            if (share != null)
                share.Dispose();
            share = null;
 
            if (appstateMutex != null)
                appstateMutex.Dispose();
            appstateMutex = null;
        }
        protected T Read<T>(long position) where T : struct
        {
            T result = default(T);
            using (var access = share.CreateViewAccessor())
                access.Read(position, out result);
            return result;
        }
 
        protected void Write<T>(long position, T value) where T : struct
        {
            using (var access = share.CreateViewAccessor())
                access.Write<T>(position, ref value);
        }
    }
    
}