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

            Assert.IsNotNull(mmf);

            ArgumentOutOfRangeException expected = null;

            try
            {
                mmf.Seek(1, SeekOrigin.End);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                expected = ex;
            }
            Assert.IsNotNull(expected);

            expected = null;
            try
            {
                mmf.Seek(MemoryMappedFile.DefaultInMemoryMapSize + 1, SeekOrigin.Begin);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                expected = ex;
            }
            Assert.IsNotNull(expected);
        }
        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 TestInMemoryNoParams()
        {
            MemoryMappedFile mmf = MemoryMappedFile.CreateInMemoryMap();

            Assert.IsNotNull(mmf);
            Assert.AreEqual(mmf.Length, MemoryMappedFile.DefaultInMemoryMapSize, "incorrect length");
            Assert.IsTrue(mmf.CanRead, "CanRead failure");
            Assert.IsTrue(mmf.CanWrite, "CanWrite failure");
            Assert.IsTrue(mmf.CanSeek, "CanSeek failure");
            Assert.AreEqual(mmf.Position, 0, "start Position failure");
        }
        public void TestPositionPositive()
        {
            MemoryMappedFile mmf = MemoryMappedFile.CreateInMemoryMap();

            Assert.IsNotNull(mmf);

            mmf.Position = 100;
            Assert.AreEqual(mmf.Position, 100, "Failed to set Position");

            mmf.Seek(100, SeekOrigin.Current);
            Assert.AreEqual(mmf.Position, 200, "Failed Seek from current");
        }
        public void TestInMemoryReadPositive()
        {
            MemoryMappedFile mmf = MemoryMappedFile.CreateInMemoryMap();

            Assert.IsNotNull(mmf);

            byte[] data   = new byte[10];
            long   oldpos = mmf.Position;

            mmf.Read(data, 0, data.Length);

            Assert.AreEqual(oldpos + data.Length, mmf.Position, "Read caused incorrect movement in position");
        }
        public void TestInMemoryNameOnlyParam()
        {
            string           mapName = "testmap";
            MemoryMappedFile mmf     = MemoryMappedFile.CreateInMemoryMap(mapName);

            Assert.IsNotNull(mmf);
            Assert.AreEqual(mmf.Length, MemoryMappedFile.DefaultInMemoryMapSize, "incorrect length");
            Assert.AreEqual(mmf.Name, mapName, "Name failure");
            Assert.IsTrue(mmf.CanRead, "CanRead failure");
            Assert.IsTrue(mmf.CanWrite, "CanWrite failure");
            Assert.IsTrue(mmf.CanSeek, "CanSeek failure");
            Assert.AreEqual(mmf.Position, 0, "start Position failure");
        }
        public void TestInMemoryAllParams()
        {
            string           mapName = "testmap";
            long             maxSize = 0x20000; // 128k
            MemoryMappedFile mmf     = MemoryMappedFile.CreateInMemoryMap(mapName, maxSize);

            Assert.IsNotNull(mmf);
            Assert.AreEqual(mmf.Length, maxSize, "Length failure");
            Assert.AreEqual(mmf.Name, mapName, "Name failure");
            Assert.IsTrue(mmf.CanRead, "CanRead failure");
            Assert.IsTrue(mmf.CanWrite, "CanWrite failure");
            Assert.IsTrue(mmf.CanSeek, "CanSeek failure");
            Assert.AreEqual(mmf.Position, 0, "start Position failure");
        }
        public void TestPositionAfterEnd()
        {
            MemoryMappedFile mmf = MemoryMappedFile.CreateInMemoryMap();

            Assert.IsNotNull(mmf);

            ArgumentOutOfRangeException expected = null;

            try
            {
                mmf.Position = MemoryMappedFile.DefaultInMemoryMapSize + 1;
            }
            catch (ArgumentOutOfRangeException ex)
            {
                expected = ex;
            }
            Assert.IsNotNull(expected);
        }
        public void TestPositionBeforeStart()
        {
            MemoryMappedFile mmf = MemoryMappedFile.CreateInMemoryMap();

            Assert.IsNotNull(mmf);

            ArgumentOutOfRangeException expected = null;

            try
            {
                mmf.Position = -1;
            }
            catch (ArgumentOutOfRangeException ex)
            {
                expected = ex;
            }
            Assert.IsNotNull(expected);
        }
        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 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);
        }
        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 TestSeekPositive()
        {
            MemoryMappedFile mmf = MemoryMappedFile.CreateInMemoryMap();

            Assert.IsNotNull(mmf);

            // begin - from begin
            mmf.Seek(0, SeekOrigin.Begin);
            Assert.AreEqual(mmf.Position, 0, "Failed Seek begin from begin");

            // the middle - from begin
            long offset = mmf.Length / 2;

            mmf.Seek(offset, SeekOrigin.Begin);
            Assert.AreEqual(mmf.Position, offset, "Failed Seek middle from begin");

            // end - from begin
            mmf.Seek(MemoryMappedFile.DefaultInMemoryMapSize, SeekOrigin.Begin);
            Assert.AreEqual(mmf.Position, MemoryMappedFile.DefaultInMemoryMapSize, "Failed Seek end from begin");

            // begin - from end
            mmf.Seek(-MemoryMappedFile.DefaultInMemoryMapSize, SeekOrigin.End);
            Assert.AreEqual(mmf.Position, 0, "Failed Seek begin from end");

            // middle - from end
            mmf.Seek(-offset, SeekOrigin.End);
            Assert.AreEqual(mmf.Position, offset, "Failed Seek middle from end");

            // end - from end
            mmf.Seek(0, SeekOrigin.End);
            Assert.AreEqual(mmf.Position, MemoryMappedFile.DefaultInMemoryMapSize, "Failed Seek end from end");

            mmf.Seek(offset, SeekOrigin.Begin);
            mmf.Seek(-offset, SeekOrigin.Current);
            Assert.AreEqual(mmf.Position, 0, "Failed Seek begin from current");

            mmf.Close();
        }
Example #14
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();
            }
        }