Пример #1
0
        private void Init()
        {
            lock (String.Intern(_filePath))
            {
                using (FileStream fs = new FileStream(_filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    if (fs.Length < _length)
                    {
                        fs.SetLength(_length);
                    }
                    fs.Close();
                }
                _memoryMappedFile = MemoryMappedFile.CreateFromFile(_filePath);

                if (!_headerDict.ContainsKey(_filePath))
                {
                    _headerDict.Add(_filePath, new Header());
                }

                Header header = GetHeader();
                if (header.CurrentOffset == 0L)
                {
                    header.CurrentOffset = Header.OFFSET_SIZE;

                    header.VersionToken = Int32.Parse(DateTime.Now.ToString("yyyyMMdd"));
                    using (MemoryMappedViewStream stream = _memoryMappedFile.CreateViewStream(0, Header.OFFSET_SIZE, MemoryMappedFileAccess.ReadWrite))
                    {
                        header.Write(stream);
                        stream.Flush();
                    }
                }
            }
        }
Пример #2
0
        private void ReceiverThreadFunc()
        {
            byte[] zeroSizeValue = new byte[] { 0, 0, 0, 0 }, sizeBuff = new byte[4];
            MemoryMappedViewStream viewStream = this.ipcMemory.CreateViewStream();

            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();

            while (true)
            {
                watch.Start();
                this.ipcSentSemaphore.WaitOne(10000);
                watch.Stop();
                if (this.isExit)
                {
                    break;
                }
                this.ipcMutex.WaitOne();

                viewStream.Position = 0;
                viewStream.Read(sizeBuff, 0, 4);
                int size = BitConverter.ToInt32(sizeBuff, 0);

                if (size == 0)
                {
                    this.ipcMutex.ReleaseMutex();
                    if (watch.ElapsedMilliseconds < 10000)
                    {
                        this.ipcReceivedSemaphore.Release();
                        continue;
                    }
                    this.isExit = true;
                    this.queueSem.Release();
                    this.queueThread.Join();
                    break;
                }

                byte[] message = new byte[size];
                viewStream.Read(message, 0, size);
                viewStream.Position = 0;
                viewStream.Write(zeroSizeValue, 0, 4);
                viewStream.Flush();
                this.ipcMutex.ReleaseMutex();
                this.ipcReceivedSemaphore.Release();

                this.queueMutex.WaitOne();
                this.messagesQueue.Enqueue(message);
                this.queueMutex.ReleaseMutex();
                try
                {
                    this.queueSem.Release();
                }
                catch (SemaphoreFullException) { }
            }
        }
Пример #3
0
        public void FlushSupportedOnBothReadAndWriteAccessors(MemoryMappedFileAccess access)
        {
            const int Capacity = 256;

            foreach (MemoryMappedFile mmf in CreateSampleMaps(Capacity))
            {
                using (mmf)
                    using (MemoryMappedViewStream s = mmf.CreateViewStream(0, Capacity, access))
                    {
                        s.Flush();
                    }
            }
        }
Пример #4
0
        public override void Put(byte[] key, byte[] value)
        {
            try {
                Console.WriteLine("{0}:", name);
                Console.Write("{0}:", key.ToHexadecimal());
                try {
                    Console.WriteLine("{0}:", System.Text.Encoding.Unicode.GetString(value));
                } catch {
                    Console.WriteLine("{0}", value.ToHexadecimal());
                }
            } catch {
                Console.WriteLine("XXX");
            }

            long OO;

            lock (index)
                if (index.TryGetValue(key, out OO))
                {
                    //TODO:resize object if we can
                    Delete(key);
                    Put(key, value);
                }
                else
                {
                    ObjectHeader ObjectHeader = new ObjectHeader();
                    ObjectHeader.Deleted = false;
                    ObjectHeader.Length  = value.Length;
                    ObjectHeader.Key     = key;
                    int       ObjectSize       = value.Length;
                    const int ObjectHeaderSize = 40;
                    OO              = mh.WriteOffset;
                    mh.WriteOffset += ObjectSize + ObjectHeaderSize;
                    EnsureCapacity(mh.WriteOffset);
                    MemoryMappedViewStream accessor = mmfBlock.CreateViewStream(reservedBytes + OO, ObjectSize + ObjectHeaderSize, MemoryMappedFileAccess.Write);
                    byte[] ObjectHeaderBytes        = new byte[ObjectHeaderSize];
                    System.IO.BinaryWriter bw       = new BinaryWriter(new MemoryStream(ObjectHeaderBytes, true));
                    bw.Write(ObjectHeader.Deleted);
                    bw.BaseStream.Seek(3, SeekOrigin.Current);
                    bw.Write(ObjectHeader.Length);
                    bw.Write(ObjectHeader.Key);
                    accessor.Write(ObjectHeaderBytes, 0, ObjectHeaderSize);
                    accessor.Write(value, 0, value.Length);
                    accessor.Flush();
                    accessor.Dispose();
                    maMeta.Write(0, ref mh);
                    maMeta.Flush();
                    index.Add(key, OO);
                    index.Commit();
                }
        }
Пример #5
0
        private Int64 SetOffset(Int64 offset, DateTime time, out Int64 prevOffset)
        {
            lock (String.Intern(_filePath))
            {
                Header header = GetHeader();
                prevOffset = header.PrevOffset;
                Int64 @newOffset = header.CurrentOffset + offset;

                if (@newOffset > _length)
                {
                    @newOffset            = Header.OFFSET_SIZE + offset;
                    header.FinalOffset    = header.PrevOffset;
                    header.FinalFileTime  = header.ActiveTime;
                    header.OverwriteCount = header.OverwriteCount + 1;
                    if (header.OverwriteCount > _length)
                    {
                        header.OverwriteCount = 0;
                    }
                    header.OverwriteTime       = DateTime.Now;
                    header.CycleTotalFileCount = header.FileCount;
                    header.FileCount           = 0;
                }

                if (@newOffset < Header.OFFSET_SIZE)
                {
                    @newOffset                 = Header.OFFSET_SIZE + offset;
                    header.FinalOffset         = 0;
                    header.FinalFileTime       = DateTime.MinValue;
                    header.OverwriteCount      = 0;
                    header.OverwriteTime       = DateTime.MinValue;
                    header.CycleTotalFileCount = 0;
                    header.FileCount           = 0;
                }

                header.FileCount     = header.FileCount + 1;
                header.PrevOffset    = header.CurrentOffset;
                header.CurrentOffset = @newOffset;
                header.ActiveTime    = time;
                header.FreeStorage   = _length - header.CurrentOffset;

                using (MemoryMappedViewStream stream = _memoryMappedFile.CreateViewStream(0, Header.OFFSET_SIZE, MemoryMappedFileAccess.ReadWrite))
                {
                    header.Write(stream);
                    stream.Flush();
                }

                return(@newOffset);
            }
        }
Пример #6
0
            public override void Flush()
            {
                lock (_writingQueue)
                {
                    _output.Flush();

                    while (_writingQueue.Count > 0)
                    {
                        long position = _writingQueue.Dequeue();
                        long count    = _writingQueue.Dequeue();

                        _flute.SetReadableSize(position, count);
                    }
                }
            }
Пример #7
0
        public Int64 Write(Byte[] buffer)
        {
            Int64 result = 0L;
            Int64 index  = 0L;

            Byte[] lengthArray    = BitConverter.GetBytes(buffer.Length);
            Byte[] timeArray      = BitConverter.GetBytes(Header.ToInt64(DateTime.Now));
            Byte   lengthChecksum = Checksum(lengthArray);
            Byte   dataChecksum   = Checksum(buffer);
            Byte   timeChecksum   = Checksum(timeArray);
            Int64  dataLength     = buffer.Length + Header.BLOCK_HEADER_SIZE;
            Int64  prevOffset     = 0;
            Int64  nextOffset     = SetOffset(dataLength, DateTime.Now, out prevOffset);

            Byte[] prevOffsetArray    = BitConverter.GetBytes(prevOffset);
            Byte   prevOffsetChecksum = Checksum(prevOffsetArray);

            Byte[] nextOffsetArray    = BitConverter.GetBytes(nextOffset);
            Byte   nextOffsetChecksum = Checksum(nextOffsetArray);

            result = nextOffset - dataLength;
            Byte[] dataPositionArray    = BitConverter.GetBytes(result + Header.BLOCK_HEADER_SIZE);
            Byte   dataPositionChecksum = Checksum(dataPositionArray);

            using (MemoryMappedViewStream stream = _memoryMappedFile.CreateViewStream(result, buffer.Length + Header.BLOCK_HEADER_SIZE, MemoryMappedFileAccess.ReadWrite))
            {
                Header.Write(stream, Header.MagicCode, ref index);
                Header.Write(stream, lengthArray, ref index);
                Header.Write(stream, lengthChecksum, ref index);
                Header.Write(stream, dataChecksum, ref index);

                Header.Write(stream, timeArray, ref index);
                Header.Write(stream, timeChecksum, ref index);

                Header.Write(stream, dataPositionArray, ref index);
                Header.Write(stream, dataPositionChecksum, ref index);
                Header.Write(stream, prevOffsetArray, ref index);
                Header.Write(stream, prevOffsetChecksum, ref index);
                Header.Write(stream, nextOffsetArray, ref index);
                Header.Write(stream, nextOffsetChecksum, ref index);

                index = Header.BLOCK_HEADER_SIZE;
                Header.Write(stream, buffer, ref index);
                stream.Flush();
            }

            return(result);
        }
Пример #8
0
        public void Dispose()
        {
            if (accesor != null)
            {
                accesor.Dispose();
                accesor = null;
            }

            if (mappedFile != null)
            {
                mappedViewStream.Flush();
                mappedViewStream.Dispose();
                mappedFile.Dispose();
                mappedFile = null;
            }
        }
Пример #9
0
        public void Dispose()
        {
            if (_stream != null)
            {
                _stream.Flush();
                _stream.Dispose();
            }

            if (_handle != null)
            {
                _handle.Dispose();
            }
            if (_map != null)
            {
                SegmentMap.Save(_map);
            }
        }
        public void ViewsShareData()
        {
            const int MapLength = 256;

            foreach (MemoryMappedFile mmf in CreateSampleMaps(MapLength))
            {
                using (mmf)
                {
                    // Create two views over the same map, and verify that data
                    // written to one is readable by the other.
                    using (MemoryMappedViewStream s1 = mmf.CreateViewStream())
                        using (MemoryMappedViewStream s2 = mmf.CreateViewStream())
                        {
                            for (int i = 0; i < MapLength; i++)
                            {
                                s1.WriteByte((byte)i);
                            }
                            s1.Flush();
                            for (int i = 0; i < MapLength; i++)
                            {
                                Assert.Equal(i, s2.ReadByte());
                            }
                        }

                    // Then verify that after those views have been disposed of,
                    // we can create another view and still read the same data.
                    using (MemoryMappedViewStream s3 = mmf.CreateViewStream())
                    {
                        for (int i = 0; i < MapLength; i++)
                        {
                            Assert.Equal(i, s3.ReadByte());
                        }
                    }

                    // Finally, make sure such data is also visible to a stream view
                    // created subsequently from the same map.
                    using (MemoryMappedViewAccessor acc4 = mmf.CreateViewAccessor())
                    {
                        for (int i = 0; i < MapLength; i++)
                        {
                            Assert.Equal(i, acc4.ReadByte(i));
                        }
                    }
                }
            }
        }
Пример #11
0
 /// START HELPER FUNCTIONS
 public void VerifyFlushException <EXCTYPE>(String strLoc, MemoryMappedViewStream view) where EXCTYPE : Exception
 {
     _iCountTestcases++;
     try
     {
         view.Flush();
         _iCountErrors++;
         Console.WriteLine("ERROR, {0}: No exception thrown, expected {1}", strLoc, typeof(EXCTYPE));
     }
     catch (EXCTYPE)
     {
         // Expected
     }
     catch (Exception ex)
     {
         _iCountErrors++;
         Console.WriteLine("ERROR, {0}: Unexpected exception, {1}", strLoc, ex);
     }
 }
Пример #12
0
        public void InvalidAfterDisposal()
        {
            foreach (MemoryMappedFile mmf in CreateSampleMaps())
            {
                using (mmf)
                {
                    MemoryMappedViewStream     s      = mmf.CreateViewStream();
                    SafeMemoryMappedViewHandle handle = s.SafeMemoryMappedViewHandle;

                    Assert.False(handle.IsClosed);
                    s.Dispose();
                    Assert.True(handle.IsClosed);

                    Assert.Throws <ObjectDisposedException>(() => s.ReadByte());
                    Assert.Throws <ObjectDisposedException>(() => s.Write(new byte[1], 0, 1));
                    Assert.Throws <ObjectDisposedException>(() => s.Flush());
                }
            }
        }
Пример #13
0
        /// <summary>
        /// Writes the message to a memory mapped file, where the memory mapped file name is WebsocketPipe.Address + id.
        /// mmf format: [wasread? 1 byte][datasize(int)][length(int)][msg][length(int)][msg]...
        /// If wasread=0, then writes the new message to the end of the message list and advances the number of messages +1.
        /// If wasread=1, clears the mmf and then writes the new message.
        /// </summary>
        /// <param name="wp">The calling WebsocketPipe</param>
        /// <param name="msg">The message to write.</param>
        /// <param name="to">The stream to write the mmf filename to.</param>
        /// <param name="id">The id of the targer we are writing to, since there may be many we open a mmf for each</param>
        public virtual void WriteMessage(WebsocketPipeMessageInfo msg, Stream to)
        {
            if (msg.Data.Length < this.UseInternalPacketDataSendingIfMsgByteSizeIsLessThen)
            {
                // write that this is an internal message.
                to.WriteByte(1);
                WebsocketAsInternalDataSocket.WriteMessage(msg, to);
                return;
            }

            // write that this is a mmf msg.
            to.WriteByte(0);

            // make the id and write it to the stream.
            string id = MakeValidMmfID(msg.DataSocketId);

            byte[] mmfnamebuffer = ASCIIEncoding.ASCII.GetBytes(id);
            to.Write(mmfnamebuffer, 0, mmfnamebuffer.Length);

            Mutex mu = new Mutex(false, id + "_mutex");

            if (!mu.WaitOne(MemoryMappedFileAccessTimeout))
            {
                throw new Exception("Memory mapped file access timedout.");
            }

            // The data size for a single message.
            int totalDataSize = msg.Data.Length + sizeof(int) + 1;

            // Creating/Opening the stream.
            MemoryMappedViewStream strm = GetDataWritingMemoryMappedViewStream(id, ref totalDataSize);

            // we are at the position of the write, and are ready for the message write.
            msg.WriteToStream(strm);

            strm.Flush();
            strm.Close();
            strm.Dispose();
            strm = null;

            // release the mutex.
            mu.ReleaseMutex();
        }
Пример #14
0
    public bool runTest()
    {
        try
        {
            using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, Int16.MaxValue))
            {
                using (MemoryMappedViewStream view = mmf.CreateViewStream())
                {
                    _iCountTestcases++;
                    view.Flush();
                }

                // Stream does not support reading
                using (MemoryMappedViewStream view = mmf.CreateViewStream(0, 0, MemoryMappedFileAccess.Read))
                {
                    _iCountTestcases++;
                    view.Flush();
                }

                // Call after view has been disposed
                MemoryMappedViewStream view1 = mmf.CreateViewStream();
                view1.Dispose();
                VerifyFlushException <ObjectDisposedException>("Loc101", view1);
            }

            /// END TEST CASES

            if (_iCountErrors == 0)
            {
                return(true);
            }
            else
            {
                Console.WriteLine("Fail! iCountErrors==" + _iCountErrors);
                return(false);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("ERR999: Unexpected exception in runTest, {0}", ex);
            return(false);
        }
    }
Пример #15
0
        public void Update()
        {
            if (!IsOpen)
            {
                return;
            }

            MappedStream.Flush();
            MappedStream.Seek(0, SeekOrigin.Begin);
            // Read the first 4 bytes
            MappedStream.Read(MessageBuffer, 0, 4);
            int nextID = BitConverter.ToInt32(MessageBuffer, 0);

            if (nextID != this.UpdateID)
            {
                // There was a change. Fill the rest of the buffer
                MappedStream.Read(MessageBuffer, 4, MessageBuffer.Length - 4);
                UpdateFromBuffer();
            }
        }
Пример #16
0
        private XmlDocument GetDataFromAIDA64()
        {
            String      data    = "";
            XmlDocument DataXml = new XmlDocument();

            try
            {
                //從AIDA64獲取電腦感應器資料
                MemoryMappedFile       Aida64_Data = MemoryMappedFile.OpenExisting("AIDA64_SensorValues");
                MemoryMappedViewStream stream      = Aida64_Data.CreateViewStream();
                StreamReader           reader      = new StreamReader(stream);
                //將資料轉換成XML格式
                data = "<Root>" + reader.ReadToEnd() + "</Root>";
                label_AIDA64_IsConnect.Text      = "已連線";
                label_AIDA64_IsConnect.BackColor = Color.Green;
                data = data.Replace("\0", " ");

                if (data != "")
                {
                    try
                    {
                        //資料讀取(XML格式)
                        DataXml.LoadXml(data);
                        label7.Text = "狀態正常";
                    }
                    catch (Exception e) {
                        label7.Text = "error\n (等待數秒直到錯誤解除,等太久就重開)" + "\n" + e.Message;
                        reader.DiscardBufferedData();
                        stream.Flush();
                        return(new XmlDocument());
                    }
                    DataDictionaryFromAida64.Clear();
                    foreach (XmlNode xmlNode in DataXml.FirstChild.ChildNodes)  //將資料分類存入dictionary中<KEY:資料列別,VALUE:資料內容(以LIST形式儲存)>
                    {
                        if (!DataDictionaryFromAida64.ContainsKey(xmlNode.Name))
                        {
                            DataDictionaryFromAida64.Add(xmlNode.Name, new List <XmlNode>()
                            {
                                xmlNode
                            });
                        }
                        else
                        {
                            DataDictionaryFromAida64[xmlNode.Name].Add(xmlNode);
                        }
                    }

                    if (!Initialize)
                    {
                        InitializeForm();//初始化傳輸給ARDUINO資料的選項區
                    }
                    if (!LightWeightMode)
                    {
                        //將資料顯示在感應器數據顯示框中
                        label_LowCpu.Visible = false;
                        dataSet1.Clear();
                        dataSet1.ReadXml(new XmlNodeReader(DataXml));
                        for (int x = 0; x < (dataSet1.Tables.Count - 1); x++)
                        {
                            dataSet1.Tables[0].Merge(dataSet1.Tables[x + 1]);
                        }
                        dataGridView1.DataSource = dataSet1.Tables[0];

                        if (DataSetRowCount != dataGridView1.RowCount)
                        {
                            DataSetRowCount = dataGridView1.RowCount;
                            InitializeForm();
                        }
                    }
                    else
                    {
                        dataGridView1.DataSource = null;
                        label_LowCpu.Visible     = true;
                    }
                }
            }
            catch (FileNotFoundException) {
                label_AIDA64_IsConnect.Text      = "未連線";
                label_AIDA64_IsConnect.BackColor = Color.Red;
            }
            return(DataXml);
        }
Пример #17
0
 public override void Flush()
 {
     _input.Flush();
 }
Пример #18
0
        public void TransmitMessage(MultiPartMessage msg)
        {
            try
            {
                int result = WaitHandle.WaitAny(new WaitHandle[] { _mutex, _abortEvent }, _config.PendingOperationsTimeout);

                if (result == WaitHandle.WaitTimeout)
                {
                    throw new TimeoutException("Timed out waiting to acquire shared memory.");
                }
                else if (result == 0)
                {
                    try
                    {
                        _clientProcessedEvent.Reset();

                        var buffer = new byte[MemoryMappedFileSize];

                        byte[] eof = Encoding.Unicode.GetBytes("\u0000");

                        msg.Data.Seek(0, SeekOrigin.End);
                        msg.Data.Write(eof, 0, 2);
                        msg.Data.Seek(0, SeekOrigin.Begin);

                        while (msg.Data.Position < msg.Data.Length)
                        {
                            int numberOfBytes = msg.Data.Read(buffer, 0, buffer.Length);

                            _stream.Seek(0, SeekOrigin.Begin);
                            _stream.Write(buffer, 0, numberOfBytes);
                            _stream.Flush();

                            _clientNotifyEvent.Set();

                            result = WaitHandle.WaitAny(new WaitHandle[] { _clientReceivedNotifyEvent, _abortEvent }, _config.PendingOperationsTimeout);

                            if (result == WaitHandle.WaitTimeout)
                            {
                                throw new TimeoutException("Timed out waiting for application to acknowledge the message.");
                            }
                            else if (result == 1)
                            {
                                return;
                            }
                        }
                    }
                    finally
                    {
                        _mutex.ReleaseMutex();
                    }
                }

                result = WaitHandle.WaitAny(new WaitHandle[] { _clientProcessedEvent, _abortEvent }, _config.PendingOperationsTimeout);

                if (result == WaitHandle.WaitTimeout)
                {
                    if ((MessageEngine.Instance.Tracing.Switch.Level & SourceLevels.Warning) == SourceLevels.Warning)
                    {
                        MessageEngine.Instance.Tracing.TraceEvent(TraceEventType.Warning, 0, "The application \"{0}\" did not process the message within the given timeframe. This could be due to a long running operation or the application being unresponsive.", ToString());
                    }
                }
            }
            catch (ObjectDisposedException)
            {
            }
        }
Пример #19
0
    public bool runTest()
    {
        uint pageSize = SystemInfoHelpers.GetPageSize();

        Byte[] byteArray1 = new Byte[] { 194, 58, 217, 150, 114, 62, 81, 203, 251, 39, 83, 124, 0, 0, 14, 128 };
        Byte[] byteArray2 = new Byte[] { 0x35, 0x4C, 0x4A, 0x79, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0,
                                         1,
                                         0x4A, 0xCE, 0x96, 0x0C,
                                         0, 0, 0 };
        Byte[] byteArray3  = new Byte[] { 0x35, 0x4C, 0x4A, 0x79, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0 };
        Byte[] byteArray3b = new Byte[] { 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        Byte[] byteArray4  = new Byte[] { 1 };
        Byte[] byteArray5  = new Byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 1, 0xBB, 0x68, 0x01, 0x00, 0, 0, 0 };

        Decimal  dec  = 20349.12309m;
        DateTime date = DateTime.Now;

        try
        {
            if (File.Exists("file1.dat"))
            {
                File.Delete("file1.dat");
            }
            using (FileStream fileStream = new FileStream("file1.dat", FileMode.Create, FileAccess.ReadWrite))
            {
                using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fileStream, null, pageSize * 10, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false))
                {
                    // Default ViewStream size - whole MMF
                    using (MemoryMappedViewStream view = mmf.CreateViewStream())
                    {
                        BinaryReader reader = new BinaryReader(view);
                        BinaryWriter writer = new BinaryWriter(view);

                        // nothing written - read zeros
                        VerifyRead("Loc001", view, new Byte[24], 0, 24, new Byte[24], 24);
                        VerifyReadBoolean("Loc002", reader, false);
                        VerifyReadUInt16("Loc003", reader, 0);
                        VerifyReadInt32("Loc004", reader, 0);
                        VerifyReadDouble("Loc005", reader, 0d);
                        VerifyReadDecimal("Loc006", reader, 0m);

                        // Write to ViewStream
                        view.Seek(0, SeekOrigin.Begin);
                        view.Write(byteArray1, 0, 16);
                        view.Seek(0, SeekOrigin.Begin);
                        VerifyRead("Loc011", view, new Byte[16], 0, 16, byteArray1, 16);
                        reader.BaseStream.Seek(0, SeekOrigin.Begin);
                        VerifyReadBoolean("Loc012", reader, true);
                        VerifyReadUInt16("Loc013", reader, 55610);
                        VerifyReadInt32("Loc014", reader, 1363047062);
                        VerifyReadDouble("Loc015", reader, BitConverter.ToDouble(byteArray1, 7));
                        reader.BaseStream.Seek(0, SeekOrigin.Begin);
                        VerifyReadDecimal("Loc016", reader, new Decimal(new Int32[] { BitConverter.ToInt32(byteArray1, 0), BitConverter.ToInt32(byteArray1, 4), BitConverter.ToInt32(byteArray1, 8), BitConverter.ToInt32(byteArray1, 12) }));

                        // Write to BinaryWriter
                        writer.BaseStream.Seek(2161, SeekOrigin.Begin);
                        writer.Write(dec);
                        writer.Write(true);
                        writer.Write(211209802);
                        view.Seek(2161, SeekOrigin.Begin);
                        VerifyRead("Loc021", view, new Byte[24], 0, 24, byteArray2, 24);
                        reader.BaseStream.Seek(2161, SeekOrigin.Begin);
                        VerifyReadDecimal("Loc022", reader, dec);
                        VerifyReadBoolean("Loc023", reader, true);
                        VerifyReadInt32("Loc024", reader, 211209802);

                        // Write to end of stream
                        view.Seek(-16, SeekOrigin.End);
                        view.Write(byteArray2, 0, 16); // now at end of stream
                        VerifyRead("Loc031", view, new Byte[16], 0, 16, new Byte[16], 0);
                        view.Seek(-16, SeekOrigin.End);
                        VerifyRead("Loc032", view, new Byte[16], 0, 16, byteArray3, 16);
                        view.Seek(-8, SeekOrigin.End);
                        VerifyRead("Loc033", view, new Byte[16], 0, 16, byteArray3b, 8);  // read partial array
                        // BinaryReader
                        reader.BaseStream.Seek(-16, SeekOrigin.End);
                        VerifyReadDecimal("Loc034", reader, dec); // now at end of stream
                        VerifyReadDecimalException <IOException>("Loc035", reader);
                        reader.BaseStream.Seek(-8, SeekOrigin.End);
                        VerifyRead("Loc036", reader, new Byte[16], 0, 16, byteArray3b, 8);  // read partial array

                        // Write to end of stream as calculated from viewstream capacity
                        view.Seek(view.Capacity - 1, SeekOrigin.Begin);
                        view.Write(byteArray2, 16, 1); // now at end of stream
                        VerifyRead("Loc041", view, new Byte[16], 0, 16, new Byte[16], 0);
                        view.Seek(view.Capacity - 1, SeekOrigin.Begin);
                        VerifyRead("Loc042", view, new Byte[1], 0, 1, byteArray4, 1);
                        reader.BaseStream.Seek(view.Capacity - 1, SeekOrigin.Begin);
                        VerifyReadBoolean("Loc043", reader, true); // now at end of stream
                        VerifyReadBooleanException <EndOfStreamException>("Loc044", reader);

                        // Write past end of stream
                        view.Seek(0, SeekOrigin.End);
                        VerifyWriteException <NotSupportedException>("Loc051", view, new Byte[16], 0, 16);
                        writer.Seek(0, SeekOrigin.End);
                        VerifyWriteException <NotSupportedException>("Loc052", writer, Byte.MaxValue);

                        // Seek past end
                        view.Seek(1, SeekOrigin.End);
                        VerifyRead("Loc061", view, new Byte[1], 0, 1, new Byte[1], 0);
                        view.Seek((int)(view.Capacity + 1), SeekOrigin.Begin);
                        VerifyRead("Loc062", view, new Byte[1], 0, 1, new Byte[1], 0);

                        // Seek before beginning
                        VerifySeekException <IOException>("Loc065", view, -1, SeekOrigin.Begin);
                        VerifySeekException <IOException>("Loc066", view, (int)(-view.Capacity - 1), SeekOrigin.End);
                        VerifySeekException <IOException>("Loc067", reader.BaseStream, -1, SeekOrigin.Begin);
                        VerifySeekException <IOException>("Loc068", reader.BaseStream, (int)(-view.Capacity - 1), SeekOrigin.End);

                        view.Flush();

                        // Verify file state
                        BinaryReader reader2 = new BinaryReader(fileStream);
                        reader2.BaseStream.Seek(0, SeekOrigin.Begin);
                        VerifyReadBoolean("Loc071", reader2, true);
                        VerifyReadUInt16("Loc072", reader2, 55610);
                        VerifyReadInt32("Loc073", reader2, 1363047062);
                        VerifyReadDouble("Loc074", reader2, BitConverter.ToDouble(byteArray1, 7));
                        reader2.BaseStream.Seek(0, SeekOrigin.Begin);
                        VerifyReadDecimal("Loc075", reader2, new Decimal(new Int32[] { BitConverter.ToInt32(byteArray1, 0), BitConverter.ToInt32(byteArray1, 4), BitConverter.ToInt32(byteArray1, 8), BitConverter.ToInt32(byteArray1, 12) }));
                        reader2.BaseStream.Seek(2161, SeekOrigin.Begin);
                        VerifyReadDecimal("Loc076", reader2, dec);
                        VerifyReadBoolean("Loc077", reader2, true);
                        VerifyReadInt32("Loc078", reader2, 211209802);
                        reader2.BaseStream.Seek(-1, SeekOrigin.End);
                        VerifyReadBoolean("Loc079", reader2, true);
                    }

                    // ViewStream starts at nonzero offset, spans remainder of MMF
                    using (MemoryMappedViewStream view = mmf.CreateViewStream(1000, 0))
                    {
                        BinaryReader reader = new BinaryReader(view);
                        BinaryWriter writer = new BinaryWriter(view);

                        // nothing written - read zeros
                        VerifyRead("Loc101", view, new Byte[24], 0, 24, new Byte[24], 24);
                        VerifyReadBoolean("Loc102", reader, false);
                        VerifyReadUInt16("Loc103", reader, 0);
                        VerifyReadInt32("Loc104", reader, 0);
                        VerifyReadDouble("Loc105", reader, 0d);
                        VerifyReadDecimal("Loc106", reader, 0m);

                        // Write to ViewStream
                        view.Seek(0, SeekOrigin.Begin);
                        view.Write(byteArray1, 0, 16);
                        view.Seek(0, SeekOrigin.Begin);
                        VerifyRead("Loc111", view, new Byte[16], 0, 16, byteArray1, 16);
                        reader.BaseStream.Seek(0, SeekOrigin.Begin);
                        VerifyReadBoolean("Loc112", reader, true);
                        VerifyReadUInt16("Loc113", reader, 55610);
                        VerifyReadInt32("Loc114", reader, 1363047062);
                        VerifyReadDouble("Loc115", reader, BitConverter.ToDouble(byteArray1, 7));
                        reader.BaseStream.Seek(0, SeekOrigin.Begin);
                        VerifyReadDecimal("Loc116", reader, new Decimal(new Int32[] { BitConverter.ToInt32(byteArray1, 0), BitConverter.ToInt32(byteArray1, 4), BitConverter.ToInt32(byteArray1, 8), BitConverter.ToInt32(byteArray1, 12) }));

                        // Read existing values
                        view.Seek(1161, SeekOrigin.Begin);
                        VerifyRead("Loc117", view, new Byte[24], 0, 24, byteArray2, 24);
                        reader.BaseStream.Seek(1161, SeekOrigin.Begin);
                        VerifyReadDecimal("Loc118", reader, dec);
                        VerifyReadBoolean("Loc119", reader, true);
                        VerifyReadInt32("Loc120", reader, 211209802);

                        // Write to BinaryWriter
                        writer.BaseStream.Seek(3000, SeekOrigin.Begin);
                        writer.Write(Decimal.MaxValue);
                        writer.Write(true);
                        writer.Write(92347);
                        view.Seek(3000, SeekOrigin.Begin);
                        VerifyRead("Loc121", view, new Byte[24], 0, 24, byteArray5, 24);
                        reader.BaseStream.Seek(3000, SeekOrigin.Begin);
                        VerifyReadDecimal("Loc122", reader, Decimal.MaxValue);
                        VerifyReadBoolean("Loc123", reader, true);
                        VerifyReadInt32("Loc124", reader, 92347);

                        // Write to end of stream
                        view.Seek(-16, SeekOrigin.End);
                        view.Write(byteArray2, 0, 16); // now at end of stream
                        VerifyRead("Loc131", view, new Byte[16], 0, 16, new Byte[16], 0);
                        view.Seek(-16, SeekOrigin.End);
                        VerifyRead("Loc132", view, new Byte[16], 0, 16, byteArray3, 16);
                        view.Seek(-8, SeekOrigin.End);
                        VerifyRead("Loc133", view, new Byte[16], 0, 16, byteArray3b, 8);  // read partial array
                        // BinaryReader
                        reader.BaseStream.Seek(-16, SeekOrigin.End);
                        VerifyReadDecimal("Loc134", reader, dec); // now at end of stream
                        VerifyReadDecimalException <IOException>("Loc135", reader);
                        reader.BaseStream.Seek(-8, SeekOrigin.End);
                        VerifyRead("Loc136", reader, new Byte[16], 0, 16, byteArray3b, 8);  // read partial array

                        // Write to end of stream as calculated from viewstream capacity
                        view.Seek(view.Capacity - 1, SeekOrigin.Begin);
                        view.Write(byteArray2, 16, 1); // now at end of stream
                        VerifyRead("Loc141", view, new Byte[16], 0, 16, new Byte[16], 0);
                        view.Seek(view.Capacity - 1, SeekOrigin.Begin);
                        VerifyRead("Loc142", view, new Byte[1], 0, 1, byteArray4, 1);
                        reader.BaseStream.Seek(view.Capacity - 1, SeekOrigin.Begin);
                        VerifyReadBoolean("Loc143", reader, true); // now at end of stream
                        VerifyReadBooleanException <IOException>("Loc144", reader);

                        // Write past end of stream
                        view.Seek(-8, SeekOrigin.End);
                        VerifyWriteException <NotSupportedException>("Loc151", view, new Byte[16], 0, 16);
                        writer.Seek(-8, SeekOrigin.End);
                        VerifyWriteException <NotSupportedException>("Loc152", writer, Decimal.MaxValue);

                        // Seek past end
                        view.Seek(1, SeekOrigin.End);
                        VerifyRead("Loc161", view, new Byte[1], 0, 1, new Byte[1], 0);
                        view.Seek((int)(view.Capacity + 1), SeekOrigin.Begin);
                        VerifyRead("Loc162", view, new Byte[1], 0, 1, new Byte[1], 0);

                        // Seek before beginning
                        VerifySeekException <IOException>("Loc165", view, -1, SeekOrigin.Begin);
                        VerifySeekException <IOException>("Loc166", view, (int)(-view.Capacity - 1), SeekOrigin.End);
                        VerifySeekException <IOException>("Loc167", reader.BaseStream, -1, SeekOrigin.Begin);
                        VerifySeekException <IOException>("Loc168", reader.BaseStream, (int)(-view.Capacity - 1), SeekOrigin.End);
                    }

                    // ViewStream starts at nonzero offset, with size shorter than MMF
                    using (MemoryMappedViewStream view = mmf.CreateViewStream(2000, 10000))
                    {
                        BinaryReader reader = new BinaryReader(view);
                        BinaryWriter writer = new BinaryWriter(view);

                        // nothing written - read zeros
                        VerifyRead("Loc201", view, new Byte[24], 0, 24, new Byte[24], 24);
                        VerifyReadBoolean("Loc202", reader, false);
                        VerifyReadUInt16("Loc203", reader, 0);
                        VerifyReadInt32("Loc204", reader, 0);
                        VerifyReadDouble("Loc205", reader, 0d);
                        VerifyReadDecimal("Loc206", reader, 0m);

                        // Write to ViewStream
                        view.Seek(0, SeekOrigin.Begin);
                        view.Write(byteArray1, 0, 16);
                        view.Seek(0, SeekOrigin.Begin);
                        VerifyRead("Loc211", view, new Byte[16], 0, 16, byteArray1, 16);
                        reader.BaseStream.Seek(0, SeekOrigin.Begin);
                        VerifyReadBoolean("Loc212", reader, true);
                        VerifyReadUInt16("Loc213", reader, 55610);
                        VerifyReadInt32("Loc214", reader, 1363047062);
                        VerifyReadDouble("Loc215", reader, BitConverter.ToDouble(byteArray1, 7));
                        reader.BaseStream.Seek(0, SeekOrigin.Begin);
                        VerifyReadDecimal("Loc216", reader, new Decimal(new Int32[] { BitConverter.ToInt32(byteArray1, 0), BitConverter.ToInt32(byteArray1, 4), BitConverter.ToInt32(byteArray1, 8), BitConverter.ToInt32(byteArray1, 12) }));

                        // Read existing values
                        view.Seek(161, SeekOrigin.Begin);
                        VerifyRead("Loc217", view, new Byte[24], 0, 24, byteArray2, 24);
                        reader.BaseStream.Seek(161, SeekOrigin.Begin);
                        VerifyReadDecimal("Loc218", reader, dec);
                        VerifyReadBoolean("Loc219", reader, true);
                        VerifyReadInt32("Loc220", reader, 211209802);

                        // Write to BinaryWriter
                        writer.BaseStream.Seek(3000, SeekOrigin.Begin);
                        writer.Write(Decimal.MaxValue);
                        writer.Write(true);
                        writer.Write(92347);
                        view.Seek(3000, SeekOrigin.Begin);
                        VerifyRead("Loc221", view, new Byte[24], 0, 24, byteArray5, 24);
                        reader.BaseStream.Seek(3000, SeekOrigin.Begin);
                        VerifyReadDecimal("Loc222", reader, Decimal.MaxValue);
                        VerifyReadBoolean("Loc223", reader, true);
                        VerifyReadInt32("Loc224", reader, 92347);

                        // Write to end of stream
                        view.Seek(-16, SeekOrigin.End);
                        view.Write(byteArray2, 0, 16); // now at end of stream
                        VerifyRead("Loc231", view, new Byte[16], 0, 16, new Byte[16], 0);
                        view.Seek(-16, SeekOrigin.End);
                        VerifyRead("Loc232", view, new Byte[16], 0, 16, byteArray3, 16);
                        view.Seek(-8, SeekOrigin.End);
                        VerifyRead("Loc233", view, new Byte[16], 0, 16, byteArray3b, 8);  // read partial array
                        // BinaryReader
                        reader.BaseStream.Seek(-16, SeekOrigin.End);
                        VerifyReadDecimal("Loc234", reader, dec); // now at end of stream
                        VerifyReadDecimalException <IOException>("Loc235", reader);
                        reader.BaseStream.Seek(-8, SeekOrigin.End);
                        VerifyRead("Loc236", reader, new Byte[16], 0, 16, byteArray3b, 8);  // read partial array

                        // Write to end of stream as calculated from viewstream capacity
                        view.Seek(view.Capacity - 1, SeekOrigin.Begin);
                        view.Write(byteArray2, 16, 1); // now at end of stream
                        VerifyRead("Loc241", view, new Byte[16], 0, 16, new Byte[16], 0);
                        view.Seek(view.Capacity - 1, SeekOrigin.Begin);
                        VerifyRead("Loc242", view, new Byte[1], 0, 1, byteArray4, 1);
                        reader.BaseStream.Seek(view.Capacity - 1, SeekOrigin.Begin);
                        VerifyReadBoolean("Loc243", reader, true); // now at end of stream
                        VerifyReadBooleanException <IOException>("Loc244", reader);

                        // Write past end of stream
                        view.Seek(-1, SeekOrigin.End);
                        VerifyWriteException <NotSupportedException>("Loc251", view, new Byte[16], 0, 2);
                        writer.Seek(-1, SeekOrigin.End);
                        VerifyWriteException <NotSupportedException>("Loc252", writer, Char.MaxValue);

                        // Seek past end
                        view.Seek(1, SeekOrigin.End);
                        VerifyRead("Loc261", view, new Byte[1], 0, 1, new Byte[1], 0);
                        view.Seek((int)(view.Capacity + 1), SeekOrigin.Begin);
                        VerifyRead("Loc262", view, new Byte[1], 0, 1, new Byte[1], 0);

                        // Seek before beginning
                        VerifySeekException <IOException>("Loc265", view, -1, SeekOrigin.Begin);
                        VerifySeekException <IOException>("Loc266", view, (int)(-view.Capacity - 1), SeekOrigin.End);
                        VerifySeekException <IOException>("Loc267", reader.BaseStream, -1, SeekOrigin.Begin);
                        VerifySeekException <IOException>("Loc268", reader.BaseStream, (int)(-view.Capacity - 1), SeekOrigin.End);
                    }
                }
            }

            if (Interop.PlatformDetection.OperatingSystem == Interop.OperatingSystem.Windows) // currently don't support sharing views of pagefile backed MMFs on Unix
            {
                // Use a pagefile backed MMF instead of file backed
                using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, pageSize * 10))
                {
                    using (MemoryMappedViewStream view = mmf.CreateViewStream())
                    {
                        BinaryReader reader = new BinaryReader(view);
                        BinaryWriter writer = new BinaryWriter(view);

                        // nothing written - read zeros
                        VerifyRead("Loc401", view, new Byte[24], 0, 24, new Byte[24], 24);
                        VerifyReadBoolean("Loc402", reader, false);
                        VerifyReadUInt16("Loc403", reader, 0);
                        VerifyReadInt32("Loc404", reader, 0);
                        VerifyReadDouble("Loc405", reader, 0d);
                        VerifyReadDecimal("Loc406", reader, 0m);

                        // Write to ViewStream
                        view.Seek(0, SeekOrigin.Begin);
                        view.Write(byteArray1, 0, 16);
                        view.Seek(0, SeekOrigin.Begin);
                        VerifyRead("Loc411", view, new Byte[16], 0, 16, byteArray1, 16);
                        reader.BaseStream.Seek(0, SeekOrigin.Begin);
                        VerifyReadBoolean("Loc412", reader, true);
                        VerifyReadUInt16("Loc413", reader, 55610);
                        VerifyReadInt32("Loc414", reader, 1363047062);
                        VerifyReadDouble("Loc415", reader, BitConverter.ToDouble(byteArray1, 7));
                        reader.BaseStream.Seek(0, SeekOrigin.Begin);
                        VerifyReadDecimal("Loc416", reader, new Decimal(new Int32[] { BitConverter.ToInt32(byteArray1, 0), BitConverter.ToInt32(byteArray1, 4), BitConverter.ToInt32(byteArray1, 8), BitConverter.ToInt32(byteArray1, 12) }));

                        // Write to BinaryWriter
                        writer.BaseStream.Seek(2161, SeekOrigin.Begin);
                        writer.Write(dec);
                        writer.Write(true);
                        writer.Write(211209802);
                        view.Seek(2161, SeekOrigin.Begin);
                        VerifyRead("Loc421", view, new Byte[24], 0, 24, byteArray2, 24);
                        reader.BaseStream.Seek(2161, SeekOrigin.Begin);
                        VerifyReadDecimal("Loc422", reader, dec);
                        VerifyReadBoolean("Loc423", reader, true);
                        VerifyReadInt32("Loc424", reader, 211209802);

                        // Write to end of stream
                        view.Seek(-16, SeekOrigin.End);
                        view.Write(byteArray2, 0, 16); // now at end of stream
                        VerifyRead("Loc431", view, new Byte[16], 0, 16, new Byte[16], 0);
                        view.Seek(-16, SeekOrigin.End);
                        VerifyRead("Loc432", view, new Byte[16], 0, 16, byteArray3, 16);
                        view.Seek(-8, SeekOrigin.End);
                        VerifyRead("Loc433", view, new Byte[16], 0, 16, byteArray3b, 8); // read partial array
                                                                                         // BinaryReader
                        reader.BaseStream.Seek(-16, SeekOrigin.End);
                        VerifyReadDecimal("Loc434", reader, dec);                        // now at end of stream
                        VerifyReadDecimalException <IOException>("Loc435", reader);
                        reader.BaseStream.Seek(-8, SeekOrigin.End);
                        VerifyRead("Loc436", reader, new Byte[16], 0, 16, byteArray3b, 8);  // read partial array

                        // Write to end of stream as calculated from viewstream capacity
                        view.Seek(view.Capacity - 1, SeekOrigin.Begin);
                        view.Write(byteArray2, 16, 1); // now at end of stream
                        VerifyRead("Loc441", view, new Byte[16], 0, 16, new Byte[16], 0);
                        view.Seek(view.Capacity - 1, SeekOrigin.Begin);
                        VerifyRead("Loc442", view, new Byte[1], 0, 1, byteArray4, 1);
                        reader.BaseStream.Seek(view.Capacity - 1, SeekOrigin.Begin);
                        VerifyReadBoolean("Loc443", reader, true); // now at end of stream
                        VerifyReadBooleanException <IOException>("Loc444", reader);

                        // Write past end of stream
                        view.Seek(0, SeekOrigin.End);
                        VerifyWriteException <NotSupportedException>("Loc451", view, new Byte[16], 0, 16);
                        writer.Seek(0, SeekOrigin.End);
                        VerifyWriteException <NotSupportedException>("Loc452", writer, Byte.MaxValue);

                        // Seek past end
                        view.Seek(1, SeekOrigin.End);
                        VerifyRead("Loc461", view, new Byte[1], 0, 1, new Byte[1], 0);
                        view.Seek((int)(view.Capacity + 1), SeekOrigin.Begin);
                        VerifyRead("Loc462", view, new Byte[1], 0, 1, new Byte[1], 0);

                        // Seek before beginning
                        VerifySeekException <IOException>("Loc465", view, -1, SeekOrigin.Begin);
                        VerifySeekException <IOException>("Loc466", view, (int)(-view.Capacity - 1), SeekOrigin.End);
                        VerifySeekException <IOException>("Loc467", reader.BaseStream, -1, SeekOrigin.Begin);
                        VerifySeekException <IOException>("Loc468", reader.BaseStream, (int)(-view.Capacity - 1), SeekOrigin.End);
                    }

                    using (MemoryMappedViewStream view = mmf.CreateViewStream(0, 10000, MemoryMappedFileAccess.CopyOnWrite))
                    {
                        BinaryReader reader = new BinaryReader(view);
                        BinaryWriter writer = new BinaryWriter(view);

                        // Read existing values
                        view.Seek(2161, SeekOrigin.Begin);
                        VerifyRead("Loc501", view, new Byte[24], 0, 24, byteArray2, 24);
                        reader.BaseStream.Seek(2161, SeekOrigin.Begin);
                        VerifyReadDecimal("Loc502", reader, dec);
                        VerifyReadBoolean("Loc503", reader, true);
                        VerifyReadInt32("Loc504", reader, 211209802);

                        // Write to ViewStream
                        view.Seek(4000, SeekOrigin.Begin);
                        view.Write(byteArray1, 0, 16);
                        view.Seek(4000, SeekOrigin.Begin);
                        VerifyRead("Loc511", view, new Byte[16], 0, 16, byteArray1, 16);
                        reader.BaseStream.Seek(4000, SeekOrigin.Begin);
                        VerifyReadBoolean("Loc512", reader, true);
                        VerifyReadUInt16("Loc513", reader, 55610);
                        VerifyReadInt32("Loc514", reader, 1363047062);
                        VerifyReadDouble("Loc515", reader, BitConverter.ToDouble(byteArray1, 7));
                        reader.BaseStream.Seek(4000, SeekOrigin.Begin);
                        VerifyReadDecimal("Loc516", reader, new Decimal(new Int32[] { BitConverter.ToInt32(byteArray1, 0), BitConverter.ToInt32(byteArray1, 4), BitConverter.ToInt32(byteArray1, 8), BitConverter.ToInt32(byteArray1, 12) }));
                    }

                    using (MemoryMappedViewStream view = mmf.CreateViewStream(0, 10000, MemoryMappedFileAccess.Read))
                    {
                        BinaryReader reader = new BinaryReader(view);

                        // Read existing values
                        view.Seek(2161, SeekOrigin.Begin);
                        VerifyRead("Loc601", view, new Byte[24], 0, 24, byteArray2, 24);
                        reader.BaseStream.Seek(2161, SeekOrigin.Begin);
                        VerifyReadDecimal("Loc602", reader, dec);
                        VerifyReadBoolean("Loc603", reader, true);
                        VerifyReadInt32("Loc604", reader, 211209802);

                        // Values from CopyOnWrite ViewStream were not preserved
                        view.Seek(4000, SeekOrigin.Begin);
                        VerifyRead("Loc611", view, new Byte[16], 0, 16, new Byte[16], 16);
                        reader.BaseStream.Seek(4000, SeekOrigin.Begin);
                        VerifyReadBoolean("Loc612", reader, false);
                        VerifyReadUInt16("Loc613", reader, 0);
                        VerifyReadInt32("Loc614", reader, 0);
                        VerifyReadDouble("Loc615", reader, 0d);
                        reader.BaseStream.Seek(4000, SeekOrigin.Begin);
                        VerifyReadDecimal("Loc616", reader, 0m);
                    }
                }
            }

            /// END TEST CASES

            if (iCountErrors == 0)
            {
                return(true);
            }
            else
            {
                Console.WriteLine("Fail! iCountErrors==" + iCountErrors);
                return(false);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("ERR999: Unexpected exception in runTest, {0}", ex);
            return(false);
        }
    }
Пример #20
0
        private void MainScan()
        {
            Array.Clear(this.data, 0, this.data.Length);
            players.Clear();
            MemoryMappedViewStream memoryMappedViewStream = MemoryMappedFile.OpenExisting("DataSend").CreateViewStream();

            byte[] array = new byte[560];
            memoryMappedViewStream.Read(array, 0, 560);
            Buffer.BlockCopy(array, 0, this.data, 0, array.Length);
            memoryMappedViewStream.Flush();
            memoryMappedViewStream.Dispose();
            #region Get Local Player


            for (uint i = 0; i < 70; i++)
            {
                GPlayer player = new GPlayer();

                ulong num = this.data[i];
                if (bRPM.IsValid(num))
                {
                    string text  = bRPM.ReadString2(num + Offsets.PZ_ClientPlayer.szName, 0xF);
                    string value = Pro.NamePlayer.Substring(0, 4);
                    if (text.Substring(0, 4).Equals(value, StringComparison.InvariantCultureIgnoreCase))
                    {
                        pLocalPlayerDecrypted = num;

                        ulong pLocalSoldier = PegaClienteSoldadodEntidade(num, localPlayer);
                        localPlayer.pSoldier = pLocalSoldier;
                        localPlayer.Team     = bRPM.Read <uint>(num + Offsets.PZ_ClientPlayer.m_teamId);

                        if (!bRPM.IsValid(pLocalSoldier))
                        {
                            return;
                        }

                        ulong pHealthComponent = bRPM.Read <ulong>(pLocalSoldier + Offsets.PZ_ClientSoldierEntity.m_pHealthComponent);

                        // Health
                        localPlayer.Health    = bRPM.Read <float>(pHealthComponent + Offsets.PZ_HealthComponent.m_Health);
                        localPlayer.MaxHealth = bRPM.Read <float>(pHealthComponent + Offsets.PZ_HealthComponent.m_MaxHealth);

                        // Origin
                        localPlayer.Origin     = bRPM.Read <Vector3>(pLocalSoldier + 0X0990);
                        localPlayer.IsOccluded = bRPM.Read <byte>(pLocalSoldier + Offsets.PZ_ClientSoldierEntity.m_occluded);
                        localPlayer.IsFriendly = (player.Team == localPlayer.Team);

                        #endregion


                        #region Get Other Players by Id
                    }
                    ulong num9 = PegaClienteSoldadodEntidade(num, player);
                    if (bRPM.IsValid(num9) && num != pLocalPlayerDecrypted)
                    {
                        ulong pGameRenderer = bRPM.Read <ulong>(Offsets.OFFSET_GAMERENDERER);
                        ulong pRenderView   = bRPM.Read <ulong>(pGameRenderer + Offsets.PZ_GameRenderer.m_pRenderView);
                        // Read Screen Matrix4x4
                        localPlayer.ViewProj = bRPM.Read <Matrix4x4>(pRenderView + Offsets.PZ_RenderView.m_viewProj);

                        if (player.IsSpectator)
                        {
                            spectatorCount++;
                        }

                        player.Name     = bRPM.ReadString(num + Offsets.PZ_ClientPlayer.szName, 10);
                        player.pSoldier = num9;

                        ulong pEnemyHealthComponent = bRPM.Read <ulong>(num9 + Offsets.PZ_ClientSoldierEntity.m_pHealthComponent);
                        // Health
                        player.Health    = bRPM.Read <float>(pEnemyHealthComponent + Offsets.PZ_HealthComponent.m_Health);
                        player.MaxHealth = bRPM.Read <float>(pEnemyHealthComponent + Offsets.PZ_HealthComponent.m_MaxHealth);


                        // Origin (Position in Game X, Y, Z)
                        player.Origin     = bRPM.Read <Vector3>(num9 + 0X0990);
                        player.Team       = bRPM.Read <uint>(num + Offsets.PZ_ClientPlayer.m_teamId);
                        player.Pose       = bRPM.Read <uint>(num9 + Offsets.PZ_ClientSoldierEntity.m_poseType);
                        player.Yaw        = bRPM.Read <float>(num9 + Offsets.PZ_ClientSoldierEntity.m_authorativeYaw);
                        player.IsOccluded = bRPM.Read <byte>(num9 + Offsets.PZ_ClientSoldierEntity.m_occluded);
                        player.IsFriendly = (player.Team == localPlayer.Team);
                        // Distance to You
                        player.Distance = Vector3.Distance(localPlayer.Origin, player.Origin);
                        bRANDER.SetViewProjection(localPlayer.ViewProj);
                        players.Add(player);

                        if (player.IsValid())
                        {
                            // Player Bone
                            bBoneOk = (GetBoneById(num9, (int)Offsets.PZ_UpdatePoseResultData.BONES.BONE_HEAD, out player.Bone.BONE_HEAD) &&
                                       GetBoneById(num9, (int)Offsets.PZ_UpdatePoseResultData.BONES.BONE_LEFTELBOWROLL, out player.Bone.BONE_LEFTELBOWROLL) &&
                                       GetBoneById(num9, (int)Offsets.PZ_UpdatePoseResultData.BONES.BONE_LEFTFOOT, out player.Bone.BONE_LEFTFOOT) &&
                                       GetBoneById(num9, (int)Offsets.PZ_UpdatePoseResultData.BONES.BONE_LEFTHAND, out player.Bone.BONE_LEFTHAND) &&
                                       GetBoneById(num9, (int)Offsets.PZ_UpdatePoseResultData.BONES.BONE_LEFTKNEEROLL, out player.Bone.BONE_LEFTKNEEROLL) &&
                                       GetBoneById(num9, (int)Offsets.PZ_UpdatePoseResultData.BONES.BONE_LEFTSHOULDER, out player.Bone.BONE_LEFTSHOULDER) &&
                                       GetBoneById(num9, (int)Offsets.PZ_UpdatePoseResultData.BONES.BONE_NECK, out player.Bone.BONE_NECK) &&
                                       GetBoneById(num9, (int)Offsets.PZ_UpdatePoseResultData.BONES.BONE_RIGHTELBOWROLL, out player.Bone.BONE_RIGHTELBOWROLL) &&
                                       GetBoneById(num9, (int)Offsets.PZ_UpdatePoseResultData.BONES.BONE_RIGHTFOOT, out player.Bone.BONE_RIGHTFOOT) &&
                                       GetBoneById(num9, (int)Offsets.PZ_UpdatePoseResultData.BONES.BONE_RIGHTHAND, out player.Bone.BONE_RIGHTHAND) &&
                                       GetBoneById(num9, (int)Offsets.PZ_UpdatePoseResultData.BONES.BONE_RIGHTKNEEROLL, out player.Bone.BONE_RIGHTKNEEROLL) &&
                                       GetBoneById(num9, (int)Offsets.PZ_UpdatePoseResultData.BONES.BONE_RIGHTSHOULDER, out player.Bone.BONE_RIGHTSHOULDER) &&
                                       GetBoneById(num9, (int)Offsets.PZ_UpdatePoseResultData.BONES.BONE_SPINE, out player.Bone.BONE_SPINE) &&
                                       GetBoneById(num9, (int)Offsets.PZ_UpdatePoseResultData.BONES.BONE_SPINE1, out player.Bone.BONE_SPINE1) &&
                                       GetBoneById(num9, (int)Offsets.PZ_UpdatePoseResultData.BONES.BONE_SPINE2, out player.Bone.BONE_SPINE2));



                            #region Drawing ESP on Overlay

                            // Desconsidera Aliados
                            if (!bEspAllies && (player.Team == localPlayer.Team))
                            {
                                continue;
                            }

                            #region ESP Bone
                            if (bBoneOk && ESP_Bone)
                            {
                                DrawBone(player);
                            }
                            #endregion

                            Vector3 textPos = new Vector3(player.Origin.X, player.Origin.Y, player.Origin.Z);
                            if (bRANDER.WorldToScreen(textPos, out Vector3 w2sFoot) && bRANDER.WorldToScreen(textPos, player.Pose, out Vector3 w2sHead))
                            {
                                float H = w2sFoot.Y - w2sHead.Y;
                                float W = H / 2;
                                float X = w2sHead.X - W / 2;

                                float heightoffset = Distance3D(w2sFoot, w2sHead);
                                #region ESP Color
                                var color      = (player.Team == localPlayer.Team) ? Color.FromArgb(0, 0, 255) : player.IsVisible() ? Color.FromArgb(0, 255, 0) : Color.FromArgb(255, 0, 0);
                                var colordist  = (player.Team == localPlayer.Team) ? Color.FromArgb(0, 0, 255) : player.IsVisible() ? Color.FromArgb(255, 0, 137) : Color.FromArgb(255, 156, 210);
                                var applycolor = (ESP_Spot ? player.Distance <= 50 ? colordist : color : color);

                                #endregion

                                #region ESP Box
                                // ESP Box
                                if (ESP_Box)
                                {
                                    if (bEsp3D)
                                    {
                                        DrawAABB(player.GetAABB(), player.Origin, player.Yaw, applycolor); // 3D Box
                                    }
                                    else
                                    {
                                        float factor = (heightoffset / 5);

                                        Vector3 m2 = new Vector3(w2sHead.X - factor, w2sHead.Y, 0);
                                        Vector3 m1 = new Vector3(w2sHead.X + factor, w2sHead.Y, 0);
                                        Vector3 m3 = new Vector3(w2sFoot.X - factor, w2sFoot.Y, 0);
                                        Vector3 m4 = new Vector3(w2sFoot.X + factor, w2sFoot.Y, 0);

                                        bRANDER.DrawLine(m1.X, m1.Y, m2.X, m2.Y, 3, applycolor);
                                        bRANDER.DrawLine(m2.X, m2.Y, m3.X, m3.Y, 3, applycolor);
                                        bRANDER.DrawLine(m3.X, m3.Y, m4.X, m4.Y, 3, applycolor);
                                        bRANDER.DrawLine(m4.X, m4.Y, m1.X, m1.Y, 3, applycolor);
                                    }
                                }
                                #endregion

                                #region ESP Vehicle
                                if (ESP_Vehicle)
                                {
                                    DrawAABB(player.VehicleAABB, player.VehicleTranfsorm, applycolor);
                                }
                                #endregion

                                #region ESP Name
                                if (ESP_Name)
                                {
                                    int   fontsize = 20;
                                    float offset   = (player.Name.Length * fontsize) / 5;
                                    bRANDER.DrawString(w2sHead.X - offset, w2sHead.Y - (heightoffset / 4) - fontsize, player.Name, Color.FromArgb(255, 255, 255));
                                }
                                #endregion

                                #region ESP Distance
                                if (ESP_Distance)
                                {
                                    bRANDER.DrawString(w2sFoot.X, w2sFoot.Y, (int)player.Distance + "m", Color.FromArgb(240, 240, 240, 255));
                                }
                                #endregion

                                #region ESP Health
                                if (ESP_Health)
                                {
                                    if (player.Health <= 0)
                                    {
                                        player.Health = 1;
                                    }

                                    if (player.MaxHealth < player.Health)
                                    {
                                        player.MaxHealth = 100;
                                    }

                                    float   factor = (-heightoffset / 4 - 8);
                                    Vector3 m1     = new Vector3(w2sHead.X + factor, w2sHead.Y, 0);
                                    Vector3 m2     = new Vector3(w2sFoot.X + factor, w2sFoot.Y, 0);

                                    int   progress = (int)((float)player.Health / ((float)player.MaxHealth / 100));
                                    float perc     = (player.Health / player.MaxHealth);
                                    int   xnxx     = (int)perc;
                                    float thicc    = heightoffset / 18;
                                    if (thicc < 4)
                                    {
                                        thicc = 4;
                                    }
                                    var HealthColor = Color.FromArgb(1 - xnxx, xnxx, 0);

                                    bRANDER.DrawLine(m1.X + thicc / 2, m1.Y - 2, m2.X + thicc / 2, m2.Y + 2, thicc + 4, Color.FromArgb(1, 1, 1));
                                    bRANDER.DrawLine(m1.X + thicc / 2, m1.Y + ((m2.Y - m1.Y) * (1 - perc)), m2.X + thicc / 2, m2.Y, thicc, Color.Green);
                                    bRANDER.DrawString(m1.X + thicc - 9, m1.Y - 10, "%" + progress, Color.FromArgb(255, 255, 255, 255));
                                }
                                #endregion

                                #region ESP DMG Vehicle
                                if (ESP_dmg_Vehicle)
                                {
                                    if (player.InVehicle && player.IsDriver)
                                    {
                                        DrawHealth((int)X, (int)w2sHead.Y - 30, (int)W + 65, 6, (int)player.VehicleHealth, (int)player.VehicleMaxHealth);
                                    }
                                }
                                #endregion

                                if (ESP_Line)
                                {
                                    bRANDER.DrawLine(w2sFoot.X, w2sFoot.Y, (rect.Right - rect.Left) / 2, rect.Bottom - rect.Top, 3, applycolor);
                                }

                                if (ESP_Spot && player.Distance <= 50)
                                {
                                    DrawProximityAlert(rect.Width / 2 + 300, rect.Height - 80, 155, 50);
                                }

                                if (ESP_SpectatorWarn && spectatorCount > 0)
                                {
                                    DrawSpectatorWarn(rect.Bottom - 125, 25, 350, 55);
                                }
                            }
                            #endregion
                        }

                        #endregion
                    }
                }
            }
        }