UnmanagedCircularBuffer(IntPtr bufferPointer, int bufferLength) { // we need space for the header and at least 2 bytes of data (because the buffer can never be completely filled) if (bufferLength < sizeof(UnmanagedCircularBufferHeader) + 2) throw new ArgumentOutOfRangeException("bufferLength", "bufferLength is too small to hold the header"); this.header = (UnmanagedCircularBufferHeader*)bufferPointer.ToPointer(); this.data = (byte*)(header + 1); this.dataLength = bufferLength - sizeof(UnmanagedCircularBufferHeader); }
private UnmanagedCircularBuffer(IntPtr bufferPointer, int bufferLength) { // we need space for the header and at least 2 bytes of data (because the buffer can never be completely filled) if (bufferLength < sizeof(UnmanagedCircularBufferHeader) + 2) throw new ArgumentOutOfRangeException("bufferLength", "bufferLength is too small to hold the header"); this.header = (UnmanagedCircularBufferHeader*)bufferPointer.ToPointer(); this.data = (byte*)(header + 1); this.dataLength = bufferLength - sizeof(UnmanagedCircularBufferHeader); }
/// <summary> /// Opens on existing circular buffer. /// </summary> /// <param name="buffer">Position of the circular buffer</param> /// <returns>NativeCircularBuffer object representing the circular buffer</returns> public static UnmanagedCircularBuffer Open(IntPtr buffer) { UnmanagedCircularBufferHeader* header = (UnmanagedCircularBufferHeader*)buffer.ToPointer(); if (header->Magic != 0x7e434231) { throw new InvalidDataException("Can not open cicular buffer. Invalid header."); } int totalLength = header->TotalLength; UnmanagedCircularBuffer b = new UnmanagedCircularBuffer(buffer, totalLength); b.nonEmptyEvent = EventWaitHandle.OpenExisting(GetEventName(b.header->NonEmptyEventName)); b.nonFullEvent = EventWaitHandle.OpenExisting(GetEventName(b.header->NonFullEventName)); return b; }
public BaseStream(UnmanagedCircularBuffer circularBuffer) { this.circularBuffer = circularBuffer; this.header = circularBuffer.header; }