public int Receive(Memory<byte> buffer) { // This can work with Span<byte> because it's synchronous but we need pinning support unsafe { ArraySegment<byte> segment; if (buffer.TryGetArray(out segment)) { return Receive(segment); } else { void* pointer; buffer.TryGetPointer(out pointer); return ReceivePinned(new IntPtr(pointer), buffer.Length); } } }
private unsafe RioBufferSegment GetSegmentFromMemory(Memory<byte> memory) { void* pointer; if(!memory.TryGetPointer(out pointer)) { throw new InvalidOperationException("Memory needs to be pinned"); } var spanPtr = (IntPtr)pointer; long startAddress; long spanAddress = spanPtr.ToInt64(); var bufferId = _rioThread.GetBufferId(spanPtr, out startAddress); checked { var offset = (uint)(spanAddress - startAddress); return new RioBufferSegment(bufferId, offset, (uint)memory.Length); } }