Пример #1
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);
            }
        }
Пример #2
0
        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");
        }
Пример #3
0
        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);
        }
Пример #4
0
        public void TestReadPastEnd()
        {
            MemoryMappedFile mmf = MemoryMappedFile.CreateInMemoryMap();

            Assert.IsNotNull(mmf);

            EndOfStreamException expected = null;

            mmf.Position = MemoryMappedFile.DefaultInMemoryMapSize - 5;

            byte[] buffer = new byte[10];
            try
            {
                mmf.Read(buffer, 0, buffer.Length);
            }
            catch (EndOfStreamException ex)
            {
                expected = ex;
            }
            Assert.IsNotNull(expected);
        }
Пример #5
0
        public void TestReadBeforeStart()
        {
            MemoryMappedFile mmf = MemoryMappedFile.CreateInMemoryMap();

            Assert.IsNotNull(mmf);

            ArgumentOutOfRangeException expected = null;

            mmf.Position = 0;

            byte[] buffer = new byte[10];
            try
            {
                mmf.Read(buffer, -1, buffer.Length);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                expected = ex;
            }
            Assert.IsNotNull(expected);
        }
Пример #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
    }