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 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");
        }
示例#3
0
        private void ReadProc()
        {
            var lengthBuffer = new byte[4];
            var dataBuffer   = new byte[m_mmf.Length - 4];

            while (true)
            {
                // use a timeout so if the app ends, this thread can exit
                if (!m_dataReady.WaitOne(1000, false))
                {
                    continue;
                }

                // avoid receiving our own data
                if (Sending)
                {
                    // wait long enough for the sender to reset the m_dataReady flag
                    Thread2.Sleep(5);
                    continue;
                }

                // grab the mutex to prevent concurrency issues
                m_mutex.WaitOne(1000, false);

                // read from the start
                m_mmf.Seek(0, System.IO.SeekOrigin.Begin);

                // get the length
                m_mmf.Read(lengthBuffer, 0, 4);
                var length = BitConverter.ToInt32(lengthBuffer, 0);

                // get the data
                m_mmf.Read(dataBuffer, 0, length);

                // release the mutex so any other clients can receive
                m_mutex.ReleaseMutex();

                // convert to a string
                var received = Encoding.ASCII.GetString(dataBuffer, 0, length);

                Console.WriteLine("Received: " + received);
                Debug.WriteLine("Received: " + received);
            }
        }
        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 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();
        }
示例#6
0
        public static string readMemFile()
        {
            m_mmf = MemoryMappedFile.CreateInMemoryMap(SharedMapName, MaxMapSize);
            // grab the mutex to prevent concurrency issues
            if (!m_mutex.WaitOne(1000, false))
            {
                Debug.WriteLine("Unable to acquire mutex.  read Abandoned");
                return "@ERROR";
            }

            // read from the start
            m_mmf.Seek(0, System.IO.SeekOrigin.Begin);

            // get the length
            m_mmf.Read(dataBuffer, 0, MaxMapSize);

            int i;
            for (i = 0; i < MaxMapSize; ++i)
            {
                if (dataBuffer[i] == 0)
                {
                    if ((dataBuffer[i + 1] | dataBuffer[i + 2] | dataBuffer[i + 3] |
                        dataBuffer[i + 4] | dataBuffer[i + 5] | dataBuffer[i + 6] | dataBuffer[i + 7]) == 0)
                        break;
                }
            }

            string received = Encoding.UTF8.GetString(dataBuffer, 0, i);

            // release the mutex so any other clients can receive
            m_mutex.ReleaseMutex();

            Debug.WriteLine("Received: " + received);

            return received;
        }
示例#7
0
    byte[] ReadFileBytes(long Position, long Size)
    {
        if (FileBytes != null)
        {
            return(FileBytes.SubArray(Position, Size));
        }

#if USE_MEMORY_MAPPED_FILE
        var Data = new byte[Size];
        //	gr: [on OSX at least] you can read past the file size, (but within capacity)
        //		this doesn't error, but does fill the bytes with zeros.
        var BytesRead = FileView.ReadArray(Position, Data, 0, (int)Size);
        if (BytesRead != Size)
        {
            throw new System.Exception("Memory mapped file only read " + BytesRead + "/" + Size + " bytes");
        }
        return(Data);
#elif USE_FILE_HANDLE
        var Data   = new byte[Size];
        var NewPos = File.Seek(Position, System.IO.SeekOrigin.Begin);
        if (NewPos != Position)
        {
            throw new System.Exception("Seeked to " + Position + " but stream is at " + NewPos);
        }
        var BytesRead = File.Read(Data, 0, (int)Size);
        if (BytesRead != Size)
        {
            throw new System.Exception("FileStream only read " + BytesRead + "/" + Size + " bytes");
        }
        return(Data);
#elif USE_JAVA_FILEHANDLE
        return(File.ReadBytes(Position, Size));
#else
        return(FileBytes.SubArray(Position, Size));
#endif
    }
示例#8
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();
            }
        }