unsafe static void Run()
        {
            using (var mapping = new SharedMemory.SharedMemory(_Name))
            {
                Console.WriteLine($"P2 subscribed to shared memory.");

                void *        ptr       = mapping.FilePtr.ToPointer();
                SampleStruct *structPtr = (SampleStruct *)ptr;   // Amazing how naturally the memory mapping maps to unsafe structs

                Console.WriteLine();
                Console.WriteLine($"P2 reading values from memory");
                Console.WriteLine($"P2 Int: {structPtr->IntField}");
                Console.WriteLine($"P2 Char: {structPtr->CharField}");
                Console.WriteLine($"P2 Array[0]: {structPtr->FloatArrayField[0]}");
                Console.WriteLine($"P2 Array[1]: {structPtr->FloatArrayField[1]}");
                Console.WriteLine($"P2 Array[2]: {structPtr->FloatArrayField[3]}");

                Console.WriteLine();
                Console.WriteLine($"P2 changing values...");

                structPtr->IntField           = structPtr->IntField + 1;
                structPtr->CharField          = 'A';
                structPtr->FloatArrayField[0] = 200;
                structPtr->FloatArrayField[1] = 306;
                structPtr->FloatArrayField[2] = 202;

                Console.WriteLine($"P2 Values changed. P1 can read values now");
            }
        }
Example #2
0
        unsafe static void Run()
        {
            using (var mapping = new SharedMemory.SharedMemory(_Name, _Size))
            {
                Console.WriteLine($"P1 created a map named {_Name} of {_Size} bytes.");
                Console.WriteLine($"P1 writing to memory...");

                void *        ptr       = mapping.FilePtr.ToPointer();
                SampleStruct *structPtr = (SampleStruct *)ptr;   // This is amazing! mapped memory can be directly cast in to an unsafe struct!
                structPtr->IntField           = 656;
                structPtr->CharField          = 'M';
                structPtr->FloatArrayField[0] = 100;
                structPtr->FloatArrayField[2] = 101;

                Console.WriteLine($"P1 written to memory.");
                Console.WriteLine($"P2 can read and edit now!");
                Console.WriteLine("P1 Press 'c' when done...");

                char c;
                do
                {
                    c = Console.ReadKey().KeyChar;
                } while (c != 'c');

                Console.WriteLine();
                Console.WriteLine($"P1 reading values from memory");
                Console.WriteLine($"P1 Int: {structPtr->IntField}");
                Console.WriteLine($"P1 Char: {structPtr->CharField}");
                Console.WriteLine($"P1 Array[0]: {structPtr->FloatArrayField[0]}");
                Console.WriteLine($"P1 Array[1]: {structPtr->FloatArrayField[1]}");
                Console.WriteLine($"P1 Array[2]: {structPtr->FloatArrayField[2]}");
            }
        }