public void TestInMemoryWritePositive()
        {
            MemoryMappedFile mmf = MemoryMappedFile.CreateInMemoryMap();

            Assert.IsNotNull(mmf);

            byte[] data = Encoding.ASCII.GetBytes("This is some test data");
            mmf.Write(data, 0, data.Length);

            Assert.AreEqual(mmf.Position, data.Length, "Write did not properly change Position");
        }
        public void TestBasicReadWritePositive()
        {
            MemoryMappedFile mmf = MemoryMappedFile.CreateInMemoryMap();

            Assert.IsNotNull(mmf);

            string sourceString = "This is some test data";

            byte[] outdata = Encoding.ASCII.GetBytes(sourceString);
            mmf.Write(outdata, 0, outdata.Length);

            mmf.Seek(-outdata.Length, SeekOrigin.Current);

            byte[] indata = new byte[outdata.Length];
            mmf.Read(indata, 0, indata.Length);

            string targetString = Encoding.ASCII.GetString(indata, 0, indata.Length);

            Assert.AreEqual(sourceString, targetString);
        }
        public void TestWriteBeforeStart()
        {
            MemoryMappedFile mmf = MemoryMappedFile.CreateInMemoryMap();

            Assert.IsNotNull(mmf);

            ArgumentOutOfRangeException expected = null;

            mmf.Position = 0;

            byte[] buffer = new byte[10];
            try
            {
                mmf.Write(buffer, -1, buffer.Length);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                expected = ex;
            }
            Assert.IsNotNull(expected);
        }
        public void TestWritePastEnd()
        {
            MemoryMappedFile mmf = MemoryMappedFile.CreateInMemoryMap();

            Assert.IsNotNull(mmf);

            EndOfStreamException expected = null;

            mmf.Position = MemoryMappedFile.DefaultInMemoryMapSize - 5;

            byte[] buffer = new byte[10];
            try
            {
                mmf.Write(buffer, 0, buffer.Length);
            }
            catch (EndOfStreamException ex)
            {
                expected = ex;
            }
            Assert.IsNotNull(expected);
        }
Example #5
0
        public void Run()
        {
            var processName = Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().GetName().CodeBase);

            // create the MMF
            m_mmf = MemoryMappedFile.CreateInMemoryMap(SharedMapName, MaxMapSize);

            // create a shared mutex
            m_mutex = new NamedMutex(false, SharedMutexName);

            // create a data-ready event
            m_dataReady = new EventWaitHandle(false, EventResetMode.ManualReset, DataReadyEventName);

            // fire up a "listener"
            new System.Threading.Thread(ReadProc)
            {
                IsBackground = true,
                Name         = "MMF Peer Reader"
            }
            .Start();

            Console.WriteLine("Memory Mapped File Created.  Enter text to send to peer(s)");

            // wait for user input
            while (true)
            {
                var input = Console.ReadLine();

                if (input == null)
                {
                    Thread2.Sleep(5000);
                    input = GetMockInput();
                    Debug.WriteLine(string.Format("Platform does not have a Console installed. Sending mock data '{0}'", input));
                }

                if (input == "exit")
                {
                    break;
                }

                // prefix our process name so we can tell who sent the data
                input = processName + ":" + input;

                // grab the mutex
                if (!m_mutex.WaitOne(5000, false))
                {
                    Console.WriteLine("Unable to acquire mutex.  Send Abandoned");
                    Debug.WriteLine("Unable to acquire mutex.  Send Abandoned");
                    continue;
                }

                // mark as "sending" so the listener will ignore what we send
                Sending = true;

                // create a "packet" (length + data)
                var packet = new byte[4 + input.Length];
                Buffer.BlockCopy(BitConverter.GetBytes(input.Length), 0, packet, 0, 4);
                Buffer.BlockCopy(Encoding.ASCII.GetBytes(input), 0, packet, 4, input.Length);

                // write the packet at the start
                m_mmf.Seek(0, System.IO.SeekOrigin.Begin);
                m_mmf.Write(packet, 0, packet.Length);

                // notify all clients that data is ready (manual reset events will release all waiting clients)
                m_dataReady.Set();

                // yield to allow the receiver to unblock and check the "Sending" flag
                Thread2.Sleep(1);

                // reset the event
                m_dataReady.Reset();

                // unmark "sending"
                Sending = false;

                // release the mutex
                m_mutex.ReleaseMutex();
            }
        }