internal ChannelCursor(DirectoryInfo location, string stateFileName) { stateFileName = Path.Combine(location.FullName, stateFileName); stateFile = MemoryMappedFile.CreateFromFile(stateFileName, FileMode.OpenOrCreate, null, StateFileSize, MemoryMappedFileAccess.ReadWrite); stateView = stateFile.CreateViewAccessor(); position = stateView.ReadInt64(PositionOffset); offset = stateView.ReadInt64(OffsetOffset); }
public override long GetLong() { var c = _accessor.ReadInt64(Position); Position += 8; //conform to how the index was written return(Number.FlipEndian(c)); }
public override long GetLong() { var littleEndian = _accessor.ReadInt64(Ix(NextGetIndex(8))); if (bigEndian) { return(Number.FlipEndian(littleEndian)); } return(littleEndian); }
public static Node Read(MemoryMappedViewAccessor view, long offset = 0) { #if DOTNETSTANDARD_1_3 return(new Node(view.ReadInt64(offset), view.ReadInt64(offset + sizeof(long)))); #else Node result; view.Read(offset, out result); return(result); #endif }
public static BigBitmapHeader GetHeader(MemoryMappedViewAccessor Reader, out int MMPosition) { MMPosition = 0; var header = new BigBitmapHeader(); header.Type = Encoding.ASCII.GetString(new byte[] { Reader.ReadByte(MMPosition) }); MMPosition++; header.Type += Encoding.ASCII.GetString(new byte[] { Reader.ReadByte(MMPosition) }); MMPosition++; header.FileSize = Reader.ReadInt64(MMPosition); MMPosition += 8; header.Reserved = Reader.ReadInt32(MMPosition); MMPosition += 4; header.Offset = Reader.ReadInt32(MMPosition); MMPosition += 4; header.BitmapInfoHeaderSize = Reader.ReadInt32(MMPosition); MMPosition += 4; return(header); }
/// <summary> /// Check if the an AppHost is a single-file bundle /// </summary> /// <param name="appHostFilePath">The path of Apphost to check</param> /// <param name="bundleHeaderOffset">An out parameter containing the offset of the bundle header (if any)</param> /// <returns>True if the AppHost is a single-file bundle, false otherwise</returns> public static bool IsBundle(string appHostFilePath, out long bundleHeaderOffset) { byte[] bundleSignature = { // 32 bytes represent the bundle signature: SHA-256 for ".net core bundle" 0x8b, 0x12, 0x02, 0xb9, 0x6a, 0x61, 0x20, 0x38, 0x72, 0x7b, 0x93, 0x02, 0x14, 0xd7, 0xa0, 0x32, 0x13, 0xf5, 0xb9, 0xe6, 0xef, 0xae, 0x33, 0x18, 0xee, 0x3b, 0x2d, 0xce, 0x24, 0xb3, 0x6a, 0xae }; long headerOffset = 0; void FindBundleHeader() { using (var memoryMappedFile = MemoryMappedFile.CreateFromFile(appHostFilePath)) { using (MemoryMappedViewAccessor accessor = memoryMappedFile.CreateViewAccessor()) { int position = BinaryUtils.SearchInFile(accessor, bundleSignature); if (position == -1) { throw new PlaceHolderNotFoundInAppHostException(bundleSignature); } headerOffset = accessor.ReadInt64(position - sizeof(Int64)); } } } RetryUtil.RetryOnIOError(FindBundleHeader); bundleHeaderOffset = headerOffset; return(headerOffset != 0); }
private long term, commitIndex, lastIndex, lastApplied; // volatile internal NodeState(DirectoryInfo location) { mappedFile = MemoryMappedFile.CreateFromFile(Path.Combine(location.FullName, FileName), FileMode.OpenOrCreate, null, Capacity, MemoryMappedFileAccess.ReadWrite); stateView = mappedFile.CreateViewAccessor(); term = stateView.ReadInt64(TermOffset); commitIndex = stateView.ReadInt64(CommitIndexOffset); lastIndex = stateView.ReadInt64(LastIndexOffset); lastApplied = stateView.ReadInt64(LastAppliedOffset); var hasLastVote = ValueTypeExtensions.ToBoolean(stateView.ReadByte(LastVotePresenceOffset)); if (hasLastVote) { stateView.Read(LastVoteOffset, out ClusterMemberId votedFor); this.votedFor = votedFor; } }
static IntPtr ReadSharedMemory() { CreateSharedMemory(); Int64 value = m_Accessor.ReadInt64(0); return(new IntPtr(value)); }
private static void DecodeCommon(out HeapReallocEvent ev, MemoryMappedViewAccessor view, long pos) { ev.HeapId = -1; ev.OldAddress = view.ReadUInt64(pos); ev.NewAddress = view.ReadUInt64(pos + 8); ev.NewSize = view.ReadUInt64(pos + 16); ev.SourceEventPos = view.ReadInt64(pos + 8); }
private void EnsureMappedFile() { if (m_MmapFile != null) { return; } FileStream f = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.Read); m_MmapFile = MemoryMappedFile.CreateFromFile(f, String.Format("Mmap{0}", mapNo++), 0, MemoryMappedFileAccess.Read, null, HandleInheritability.None, false); m_MmapView = m_MmapFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read); // Decode all strings and call stacks m_MmapStringTablePos = m_MmapView.ReadInt64(0); m_MmapStackFrameTablePos = m_MmapView.ReadInt64(8); m_MmapPos = 16; m_MmapPrevPos = -1; }
internal long ReadInt64(long translatedRowIndex, long translatedColumnIndex) { var columnMetadata = Metadata.Columns[translatedColumnIndex]; var dataOffset = columnMetadata.DataOffset; var valueOffset = dataOffset + translatedRowIndex * sizeof(long); return(View.ReadInt64(valueOffset)); }
public override int Read(byte[] buffer, int offset, int count) { int bytesRead; using (Lock()) { var currentLength = _header.ReadInt64(0); long readCount = count; if (_mmfstr.Position + count > currentLength) { readCount = currentLength - _mmfstr.Position; } count = (int)readCount; bytesRead = _mmfstr.Read(buffer, offset, count); } return(bytesRead); }
/// <summary> /// /// </summary> /// <returns></returns> public Int64 ReadInt64(long inPosition) { try { return(_sharedMemoryAccesor.ReadInt64(inPosition)); } catch { return(0); } }
public static BigBitmapInfoHeader GetInfoHeader(MemoryMappedViewAccessor Reader, ref int MMPosition) { //MMPosition = 18; var header = new BigBitmapInfoHeader(); // HeaderSize = reader.ReadInt32(), header.BitmapWidth = Reader.ReadInt64(MMPosition); MMPosition += 8; header.BitmapHeight = Reader.ReadInt64(MMPosition); MMPosition += 8; header.ColorPlanes = Reader.ReadInt16(MMPosition); MMPosition += 2; header.BitsPerPixel = Reader.ReadInt16(MMPosition); MMPosition += 2; header.CompressinoMethod = Reader.ReadInt32(MMPosition); MMPosition += 4; header.ColorDataSize = Reader.ReadInt64(MMPosition); MMPosition += 8; header.HorizantalResolution = Reader.ReadInt32(MMPosition); MMPosition += 4; header.VerticalResolution = Reader.ReadInt32(MMPosition); MMPosition += 4; header.Colors = Reader.ReadInt32(MMPosition); MMPosition += 4; header.IColors = Reader.ReadInt32(MMPosition); MMPosition += 4; return(header); }
private long term, commitIndex, lastIndex, lastApplied; // volatile internal NodeState(DirectoryInfo location, AsyncLock writeLock) { mappedFile = MemoryMappedFile.CreateFromFile(Path.Combine(location.FullName, FileName), FileMode.OpenOrCreate, null, Capacity, MemoryMappedFileAccess.ReadWrite); syncRoot = writeLock; stateView = mappedFile.CreateViewAccessor(); term = stateView.ReadInt64(TermOffset); commitIndex = stateView.ReadInt64(CommitIndexOffset); lastIndex = stateView.ReadInt64(LastIndexOffset); lastApplied = stateView.ReadInt64(LastAppliedOffset); var port = stateView.ReadInt32(PortOffset); var length = stateView.ReadInt32(AddressLengthOffset); if (length == 0) { votedFor = null; } else { var address = new byte[length]; stateView.ReadArray(AddressOffset, address, 0, length); votedFor = new IPEndPoint(new IPAddress(address), port); } }
public long ReadInt64() { long position = _position + _origin; if (position + 8 > _length) { throw new IOException(SR.BlobReadOutOfBound); } var value = _accessor.ReadInt64(position); _position += 8; return(value); }
public long GetLong() { const long size = 8; if (_position + size > _size) { _logger.Error($"Trying to read {size} bytes at {_position:X} with just {_size - _position} bytes left"); throw new OverflowException(); } var value = _va.ReadInt64(_offset + _position); _position += size; return(value); }
public SMemIPCData SendMessage(SMemIPCData data_in) { try { sharedmem_mutex.WaitOne(); //take the mutex //save message in shared memory IPCCodec.Serialize(phile_stream, data_in); //prepare the message prefix phile_prefix.Write(0, phile_stream.Position); message_wait.Set(); //signal message ready message_handled.WaitOne(); //wait for reply ready signal //copy results from shared memory long result_size = phile_prefix.ReadInt64(0); if (result_size > phile_stream.Capacity) { //enlarge the view stream if the result is too big try { phile_stream.Close(); phile_stream = phile.CreateViewStream(sizeof(long), result_size); } catch { throw new SystemException("ijiCore: Result set too large."); } } return(IPCCodec.Deserialize(phile_stream)); } catch (Exception e) { return(null); } finally { sharedmem_mutex.ReleaseMutex(); } }
public void Initialize(String domain) { _mDomain = domain; if (_mIpAddress == null) { byte[] bytes = NetworkInterfaceManager.AddressBytes; StringBuilder sb = new StringBuilder(); foreach (byte b in bytes) { sb.Append(((b >> 4) & 0x0F).ToString("x")); sb.Append((b & 0x0F).ToString("x")); } _mIpAddress = sb.ToString(); } // !Important, force GC for MessageIdFactory of the previous process, so that memory mapped file is disposed. GC.Collect(); memoryMappedViewAccessor = CreateOrOpenMarkFile(_mDomain); if (null != memoryMappedViewAccessor && memoryMappedViewAccessor.CanRead) { var index = memoryMappedViewAccessor.ReadInt32(CatConstants.ID_MARK_FILE_INDEX_OFFSET); var lastTimestamp = memoryMappedViewAccessor.ReadInt64(CatConstants.ID_MARK_FILE_TS_OFFSET); if (lastTimestamp == _mTimestamp) { _mIndex = index + 10000; } else { _mIndex = 0; } } SaveMark(true); }
public IEnumerable <NAeronCounterMetaData> GetCounterMetaData() { int offset_Record = 0; while (offset_Record < vaCountersMetadata.Capacity) { int recordState = vaCountersMetadata.ReadInt32(offset_Record + 0); int typeID = vaCountersMetadata.ReadInt32(offset_Record + 4); long freeForUseDeadline = vaCountersMetadata.ReadInt64(offset_Record + 8); int labelLength = vaCountersMetadata.ReadInt32(offset_Record + 128); if (labelLength > 0) { var arrayLabel = new byte[offset_Record + 380]; vaCountersMetadata.ReadArray <byte>(offset_Record + 132, arrayLabel, offset: 0, labelLength); var label = Encoding.UTF8.GetString(arrayLabel, index: 0, labelLength); yield return(new NAeronCounterMetaData(recordState, typeID, freeForUseDeadline, label)); } offset_Record += 512; } }
public static Header Read(MemoryMappedViewAccessor view, long offset = 0) { #if DOTNETSTANDARD_1_3 return(new Header( view.ReadInt64(offset), view.ReadInt64(offset + sizeof(long)), view.ReadInt64(offset + 2 * sizeof(long)), view.ReadInt64(offset + 3 * sizeof(long)), view.ReadInt64(offset + 4 * sizeof(long)), view.ReadInt64(offset + 5 * sizeof(long)))); #else Header result; view.Read(offset, out result); return(result); #endif }
/// <summary> /// Reads the <see cref="long"/> at the specified index using the byte order /// specified by <see cref="ByteBuffer.Order"/>. No validation is done on /// <paramref name="index"/>. /// </summary> /// <param name="index">The index to begin reading bytes.</param> /// <returns>The <see cref="long"/> at the specified index.</returns> protected long LoadInt64(int index) { int baseOffset = offset + index; long bytes = 0; if (order == Endianness.BigEndian) { for (int i = 0; i < 8; i++) { bytes = bytes << 8; bytes = bytes | (uint)(accessor.ReadByte(baseOffset + i) & 0xFF); } } else { //for (int i = 7; i >= 0; i--) //{ // bytes = bytes << 8; // bytes = bytes | (uint)(accessor.ReadByte(baseOffset + i) & 0xFF); //} bytes = accessor.ReadInt64(baseOffset); } return(bytes); }
/// <summary> /// This should be done very frequently. /// </summary> public void Poll(OnInvoked callback = null) { SetCurrentFrame(MySide, GetCurrentFrame(MySide) + 1); MemoryMappedViewAccessor input = Input; if (input == null) { return; } //the other application finished polling all messages if (GetReadingState(OtherSide) == ReadingState.Finished) { SetReadingState(OtherSide, ReadingState.Reading); //send out the next batch of messages if (queue.Count > 0) { SendQueue(); } } int position = 0; //read the next 8 bytes as an id long id = input.ReadInt64(position); position += sizeof(long); //this message was already processed before if (pastMessages.Contains(id)) { //so were done then SetReadingState(MySide, ReadingState.Finished); return; } //next 4 bytes is how many messages there are int messages = input.ReadInt32(position); position += sizeof(int); if (messages > 0) { List <Invocation> invokes = new List <Invocation>(); for (int m = 0; m < messages; m++) { //read message length int length = input.ReadInt32(position); position += sizeof(int); //read message data itself byte[] data = new byte[length]; for (int i = 0; i < length; i++) { data[i] = input.ReadByte(position); position++; } //process data if (data.Length > 0) { Invocation newInoke = new Invocation(data, Serializer); invokes.Add(newInoke); } } for (int i = 0; i < invokes.Count; i++) { //try and invoke globally InvokeCallbacks(invokes[i], callback); } SetReadingState(MySide, ReadingState.Finished); pastMessages.Add(id); } else { SetReadingState(MySide, ReadingState.Finished); } }
/// <summary>Performs many reads and writes of various data types against the accessor.</summary> private static void AssertWritesReads(MemoryMappedViewAccessor acc) { // Successful reads and writes at the beginning for each data type AssertWriteRead <bool>(false, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadBoolean(pos)); AssertWriteRead <bool>(true, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadBoolean(pos)); AssertWriteRead <byte>(42, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadByte(pos)); AssertWriteRead <char>('c', 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadChar(pos)); AssertWriteRead <decimal>(9, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadDecimal(pos)); AssertWriteRead <double>(10, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadDouble(pos)); AssertWriteRead <short>(11, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadInt16(pos)); AssertWriteRead <int>(12, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadInt32(pos)); AssertWriteRead <long>(13, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadInt64(pos)); AssertWriteRead <sbyte>(14, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadSByte(pos)); AssertWriteRead <float>(15, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadSingle(pos)); AssertWriteRead <ushort>(16, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadUInt16(pos)); AssertWriteRead <uint>(17, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadUInt32(pos)); AssertWriteRead <ulong>(17, 0, (pos, value) => acc.Write(pos, value), pos => acc.ReadUInt64(pos)); // Successful reads and writes at the end for each data type long end = acc.Capacity; AssertWriteRead <bool>(false, end - sizeof(bool), (pos, value) => acc.Write(pos, value), pos => acc.ReadBoolean(pos)); AssertWriteRead <bool>(true, end - sizeof(bool), (pos, value) => acc.Write(pos, value), pos => acc.ReadBoolean(pos)); AssertWriteRead <byte>(42, end - sizeof(byte), (pos, value) => acc.Write(pos, value), pos => acc.ReadByte(pos)); AssertWriteRead <char>('c', end - sizeof(char), (pos, value) => acc.Write(pos, value), pos => acc.ReadChar(pos)); AssertWriteRead <decimal>(9, end - sizeof(decimal), (pos, value) => acc.Write(pos, value), pos => acc.ReadDecimal(pos)); AssertWriteRead <double>(10, end - sizeof(double), (pos, value) => acc.Write(pos, value), pos => acc.ReadDouble(pos)); AssertWriteRead <short>(11, end - sizeof(short), (pos, value) => acc.Write(pos, value), pos => acc.ReadInt16(pos)); AssertWriteRead <int>(12, end - sizeof(int), (pos, value) => acc.Write(pos, value), pos => acc.ReadInt32(pos)); AssertWriteRead <long>(13, end - sizeof(long), (pos, value) => acc.Write(pos, value), pos => acc.ReadInt64(pos)); AssertWriteRead <sbyte>(14, end - sizeof(sbyte), (pos, value) => acc.Write(pos, value), pos => acc.ReadSByte(pos)); AssertWriteRead <float>(15, end - sizeof(float), (pos, value) => acc.Write(pos, value), pos => acc.ReadSingle(pos)); AssertWriteRead <ushort>(16, end - sizeof(ushort), (pos, value) => acc.Write(pos, value), pos => acc.ReadUInt16(pos)); AssertWriteRead <uint>(17, end - sizeof(uint), (pos, value) => acc.Write(pos, value), pos => acc.ReadUInt32(pos)); AssertWriteRead <ulong>(17, end - sizeof(ulong), (pos, value) => acc.Write(pos, value), pos => acc.ReadUInt64(pos)); // Failed reads and writes just at the border of the end. This triggers different exception types // for some types than when we're completely beyond the end. long beyondEnd = acc.Capacity + 1; AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadBoolean(beyondEnd - sizeof(bool))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadByte(beyondEnd - sizeof(byte))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadSByte(beyondEnd - sizeof(sbyte))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadChar(beyondEnd - sizeof(char))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadDecimal(beyondEnd - sizeof(decimal))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadDouble(beyondEnd - sizeof(double))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadInt16(beyondEnd - sizeof(short))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadInt32(beyondEnd - sizeof(int))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadInt64(beyondEnd - sizeof(long))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadSingle(beyondEnd - sizeof(float))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadUInt16(beyondEnd - sizeof(ushort))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadUInt32(beyondEnd - sizeof(uint))); AssertExtensions.Throws <ArgumentException>("position", () => acc.ReadUInt64(beyondEnd - sizeof(ulong))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(bool), false)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(byte), (byte)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(sbyte), (sbyte)0)); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(char), 'c')); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(decimal), (decimal)0)); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(double), (double)0)); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(short), (short)0)); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(int), (int)0)); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(long), (long)0)); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(float), (float)0)); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(ushort), (ushort)0)); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(uint), (uint)0)); AssertExtensions.Throws <ArgumentException>("position", () => acc.Write(beyondEnd - sizeof(ulong), (ulong)0)); // Failed reads and writes well past the end beyondEnd = acc.Capacity + 20; AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadBoolean(beyondEnd - sizeof(bool))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadByte(beyondEnd - sizeof(byte))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadSByte(beyondEnd - sizeof(sbyte))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadChar(beyondEnd - sizeof(char))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadDecimal(beyondEnd - sizeof(decimal))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadDouble(beyondEnd - sizeof(double))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadInt16(beyondEnd - sizeof(short))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadInt32(beyondEnd - sizeof(int))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadInt64(beyondEnd - sizeof(long))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadSingle(beyondEnd - sizeof(float))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadUInt16(beyondEnd - sizeof(ushort))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadUInt32(beyondEnd - sizeof(uint))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.ReadUInt64(beyondEnd - sizeof(ulong))); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(bool), false)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(byte), (byte)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(sbyte), (sbyte)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(char), 'c')); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(decimal), (decimal)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(double), (double)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(short), (short)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(int), (int)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(long), (long)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(float), (float)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(ushort), (ushort)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(uint), (uint)0)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => acc.Write(beyondEnd - sizeof(ulong), (ulong)0)); }
public long GetPageFromFileOffset(long FileOffset, ref long[] block) { var rv = 0L; var NewMapViewBase = MapViewBase; var NewMapViewSize = MapViewSize; var CheckBase = FileOffset / MapViewSize; if (MapViewBase != CheckBase * MapViewSize) { NewMapViewBase = CheckBase * MapViewSize; } if (FileOffset > FileSize) { return(0); } if (FileOffset < NewMapViewBase) { throw new OverflowException("FileOffset must be >= than base"); } var AbsOffset = FileOffset - NewMapViewBase; var BlockOffset = AbsOffset & ~(PAGE_SIZE - 1); try { if (NewMapViewBase != MapViewBase) { cntInAccessor++; if (NewMapViewBase + MapViewSize > FileSize) { NewMapViewSize = FileSize - NewMapViewBase; } else { NewMapViewSize = MapViewSize; } mappedAccess = mappedFile.CreateViewAccessor( NewMapViewBase, NewMapViewSize, MemoryMappedFileAccess.Read); MapViewBase = NewMapViewBase; } else { cntOutAccsor++; } if (block != null) { UnsafeHelp.ReadBytes(mappedAccess, BlockOffset, ref block); } rv = mappedAccess.ReadInt64(AbsOffset); } catch (Exception ex) { block = null; throw new MemoryMapWindowFailedException("Unable to map or read into", ex); } return(rv); }
public long ReadInt64(int offset) { return(_f.ReadInt64(offset)); }
public static void SDTCompression(int year, int month, float E = 0.7f) { //Stopwatch sw = Stopwatch.StartNew(); string path = string.Concat(m_Path, "\\", year.ToString(), "-", month.ToString()); using (FileStream stream = File.Open(path + ".bin", FileMode.Open, FileAccess.Read, FileShare.Read)) { using (FileStream outstream = File.Create(path + ".sdt")) { outstream.Write(new byte[0x100], 0, 0x100); BinaryWriter w = new BinaryWriter(outstream); using (MemoryMappedFile mapp = MemoryMappedFile.CreateFromFile(stream, "map1", stream.Length, MemoryMappedFileAccess.ReadWrite, HandleInheritability.Inheritable, false)) { int days = DateTime.DaysInMonth(year, month); long[] ps = new long[days + 1]; long[] ps1 = new long[days + 1]; long[] sizes = new long[days]; MemoryMappedViewAccessor acc1 = mapp.CreateViewAccessor(0, 8 * days); long begin = 0; ps[0] = acc1.ReadInt64(begin); for (int i = 0; i < days; i++) { begin += 8; ps[i + 1] = (i == days - 1 ? stream.Length : acc1.ReadInt64(begin)); sizes[i] = ps[i + 1] - ps[i]; } acc1.Dispose(); for (int i = 0; i < days; i++) { if (ps[i] < 0x100 || sizes[i] <= 0) { continue; } using (MemoryMappedViewAccessor acc = mapp.CreateViewAccessor(ps[i], sizes[i])) { ps1[i] = outstream.Position; int len = acc.ReadInt32(8); int len1 = len * 11 + 12; HDataFormat[] list = new HDataFormat[len]; w.Write(acc.ReadInt32(0)); w.Write(acc.ReadInt32(4)); w.Write(len); outstream.Write(new byte[len1 - 12], 0, len1 - 12); long pos = 12; int off = 0; for (int j = 0; j < len; j++) { short id; byte type; int count; int offset; id = acc.ReadInt16(pos); type = acc.ReadByte(pos + 2); count = acc.ReadInt32(pos + 3); offset = acc.ReadInt32(pos + 7); list[j].ID = id; list[j].Type = (DataType)type; list[j].Offset = off;//此处可采取三次到五次抽样得到E和TLM if (count < 3) { long pos2 = len1 + offset; for (int m = 0; m < count; m++) { w.Write(acc.ReadInt32(pos2)); pos2 += 4; w.Write(acc.ReadSingle(pos2)); pos2 += 4; } continue; } else { switch (list[j].Type) { case DataType.FLOAT: { int crt = 0; int net = 0; float crv = 0; float nev = 0; int maxt = 0; int mint = maxt; int sumt = 0; float minv = 0; float maxv = minv; float sumv = 0; int old_time = 0; int time = 0; float mem = 0; float old_mem = 0; long pp = len1 + offset; long pos2 = pp + 16; for (int c = 0; c < 9; c++) { crt = acc.ReadInt32(pp); pp += 4; crv = acc.ReadSingle(pp); pp += 4; if (c > 0) { float cv = crv - nev; int ct = crt - net; if (c == 1) { time = crt; mem = crv; maxt = mint = ct; minv = maxv = cv; } else { if (cv > maxv) { maxv = cv; } if (cv < minv) { minv = cv; } if (ct > maxt) { maxt = ct; } if (ct < mint) { mint = ct; } } sumv += cv; sumt += ct; } else { old_mem = crv; old_time = crt; } nev = crv; net = crt; } int TLM = (sumt - maxt - mint) / 2; float E1 = E * (sumv - maxv - minv) / 6; int sum = 1; //old_time = now_time = new_time = 0; float timespan; w.Write(old_time); w.Write(old_mem); float k1, k2, k; timespan = time - old_time; k = (mem - old_mem) / timespan; k1 = k + (E1 / timespan); k2 = 2 * k - k1; for (int m = 2; m < count; m++) { if (timespan >= TLM || k < k2 || k > k1) { ++sum; w.Write(old_time); w.Write(old_mem); k1 = k + (E1 / timespan); k2 = 2 * k - k1; } old_time = time; old_mem = mem; time = acc.ReadInt32(pos2); pos2 += 4; mem = acc.ReadSingle(pos2); pos2 += 4; timespan = time - old_time; k = (mem - old_mem) / timespan; } list[j].Count = sum; off += sum * 8; } break; case DataType.WORD: case DataType.SHORT: { int crt = 0; int net = 0; short crv = 0; short nev = 0; int maxt = 0; int mint = maxt; int sumt = 0; int minv = 0; int maxv = minv; int sumv = 0; int old_time = 0; int time = 0; short mem = 0; short old_mem = 0; long pp = len1 + offset; long pos2 = pp + 12; for (int c = 0; c < 9; c++) { crt = acc.ReadInt32(pp); pp += 4; crv = acc.ReadInt16(pp); pp += 2; if (c > 0) { int cv = crv - nev; int ct = crt - net; if (c == 1) { time = crt; maxt = mint = ct; mem = crv; minv = maxv = cv; } else { if (cv > maxv) { maxv = cv; } if (cv < minv) { minv = cv; } if (ct > maxt) { maxt = ct; } if (ct < mint) { mint = ct; } } sumv += cv; sumt += ct; } else { old_mem = crv; old_time = crt; } nev = crv; net = crt; } int TLM = (sumt - maxt - mint) / 2; float E1 = E * (sumv - maxv - minv) / 6; int sum = 1; float timespan; w.Write(old_time); w.Write(old_mem); float k1, k2, k; timespan = time - old_time; k = (mem - old_mem) / timespan; k1 = k + (E1 / timespan); k2 = 2 * k - k1; for (int m = 2; m < count; m++) { if (timespan >= TLM || k < k2 || k > k1) { ++sum; w.Write(old_time); w.Write(old_mem); k1 = k + (E1 / timespan); k2 = 2 * k - k1; } old_time = time; old_mem = mem; time = acc.ReadInt32(pos2); pos2 += 4; mem = acc.ReadInt16(pos2); pos2 += 2; timespan = time - old_time; k = (mem - old_mem) / timespan; } list[j].Count = sum; off += sum * 8; } break; default: { byte[] buffer = new byte[count * dataLen[type]]; stream.Seek(ps[i] + len1 + offset, SeekOrigin.Begin); stream.Read(buffer, 0, buffer.Length); outstream.Write(buffer, 0, buffer.Length); list[j].Count = count; off += buffer.Length; } break; } pos += 11; } } outstream.Seek(ps1[i] + 12, SeekOrigin.Begin); for (int j = 0; j < len; j++) { w.Write(list[j].ID); w.Write((byte)list[j].Type); w.Write(list[j].Count); w.Write(list[j].Offset); } ps1[i + 1] = outstream.Seek(0, SeekOrigin.End); } } outstream.Seek(0, SeekOrigin.Begin); for (int i = 0; i < days + 1; i++) { w.Write(ps1[i]); } } } } }
/// <summary> /// Reads a 64-bit integer of a specific Endianness from the accessor and converts it to the current system Endianness. /// </summary> /// <param name="mem">The specified MemoryMappedViewAccessor.</param> /// <param name="position">The number of bytes into the accessor at which to begin reading.</param> /// <param name="convertFrom">The Endianness to convert from.</param> /// <returns>The value that was read as converted to the current system Endianness.</returns> public static long ReadInt64(this MemoryMappedViewAccessor mem, long position, Endianness convertFrom) { return(mem.ReadInt64(position).ConvertToSystemEndian(convertFrom)); }
/// <summary> /// Get a pagesized block that contains the data from the byte offset specified /// </summary> /// <param name="FileOffset">byte offset of long aligned page block</param> /// <param name="block">to be filled on return optionally</param> /// <param name="DataRead">signals success</param> /// <returns>long value from fileoffset</returns> public long GetPageFromFileOffset(long FileOffset, ref long[] block, ref bool DataRead) { var rv = 0L; DataRead = false; var NewMapViewSize = MapViewSize; var CheckBase = FileOffset / MapViewSize; var NewMapViewBase = CheckBase * MapViewSize; if (FileOffset > FileSize) { return(0); } var AbsOffset = FileOffset - NewMapViewBase; var BlockOffset = AbsOffset & ~(PAGE_SIZE - 1); try { if (NewMapViewBase != MapViewBase) { cntInAccessor++; if (NewMapViewBase + MapViewSize > FileSize) { NewMapViewSize = FileSize - NewMapViewBase; } else { NewMapViewSize = MapViewSize; } mappedAccess = mappedFile.CreateViewAccessor( NewMapViewBase, NewMapViewSize, MemoryMappedFileAccess.Read); MapViewBase = NewMapViewBase; } else { cntOutAccsor++; } if (block != null) { var copy_len = block.Length; if (BlockOffset + (block.Length * 8) > NewMapViewSize) { copy_len = (int)((NewMapViewSize - BlockOffset) / 8); } UnsafeHelp.ReadBytes(mappedAccess, BlockOffset, ref block, copy_len); rv = block[((AbsOffset >> 3) & 0x1ff)]; } // FIX: ReadInt64 uses byte address so when we use it must adjust, check for other callers // assumptions since we changed this from array<long>[] maybe expecting old behavior, however // caller from getpageforphysaddr passes valid block usually so that's the main one from V2P else { rv = mappedAccess.ReadInt64(BlockOffset | (AbsOffset & 0x1ff)); } DataRead = true; } catch (Exception ex) { throw new MemoryMapWindowFailedException("Unable to map or read memory offset", ex); } return(rv); }
private long ReadCurrent() { return(_accessor.ReadInt64(0)); }