soundvision/UnityProject/Assets/Scripts/PdConnection/PdArray.cs

35 lines
918 B
C#
Raw Normal View History

2019-07-06 14:22:52 +00:00
using System;
using System.IO.MemoryMappedFiles;
namespace PdConnection
{
public class PdArray : IDisposable
2019-07-06 14:22:52 +00:00
{
2019-07-06 14:26:14 +00:00
public float[] Data { get; }
2019-07-06 14:22:52 +00:00
private readonly MemoryMappedFile memoryMappedFile_;
private readonly MemoryMappedViewAccessor viewAccessor_;
2019-07-06 14:26:14 +00:00
public PdArray(string name, int size)
2019-07-06 14:22:52 +00:00
{
2019-07-06 14:26:14 +00:00
memoryMappedFile_ = MemoryMappedFile.OpenExisting(name);
if (memoryMappedFile_ == null)
throw new ArgumentException("no such shared memory");
2019-07-06 14:22:52 +00:00
viewAccessor_ = memoryMappedFile_.CreateViewAccessor();
2019-07-06 14:26:14 +00:00
Data = new float[size];
}
public void Update()
{
viewAccessor_.ReadArray(0, Data, 0, Data.Length);
2019-07-06 14:22:52 +00:00
}
public void Dispose()
{
memoryMappedFile_.Dispose();
viewAccessor_.Dispose();
2019-07-06 14:22:52 +00:00
}
}
}