Instantly share code, notes, and snippets.
Created
June 10, 2019 15:26
The C# portions of the shared memory provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ImageSharedMemoryConsumer | |
: ISharedMemoryConsumer | |
{ | |
[DllImport("ScryUnlimitedCommunicator.so")] | |
private static extern IntPtr GetImageConsumer(string name); | |
[DllImport("ScryUnlimitedCommunicator.so")] | |
private static extern void DeleteImageNetCoreSharedMemoryConsumer(IntPtr instance); | |
[DllImport("ScryUnlimitedCommunicator.so")] | |
private static extern void ImageConsumerSetCallback(IntPtr instance, IntPtr callback); | |
[DllImport("ScryUnlimitedCommunicator.so")] | |
private static extern void StartImageConsumer(IntPtr instance); | |
[DllImport("ScryUnlimitedCommunicator.so")] | |
private static extern void SeralizedWait(IntPtr instance); | |
private readonly IntPtr _nativePointer; | |
private IntPtr _callbackPtr; | |
private GCHandle _callbackHandle; | |
public ImageSharedMemoryConsumer(string name) | |
{ | |
_nativePointer = GetImageConsumer(name); | |
} | |
~ImageSharedMemoryConsumer() | |
{ | |
DeleteImageNetCoreSharedMemoryConsumer(_nativePointer); | |
} | |
public long Size() | |
{ | |
return 3000000; | |
} | |
public void SetCallback(HandleMessage handler) | |
{ | |
void Callback(IntPtr x, int y) | |
{ | |
byte[] managedArray = new byte[y]; | |
Marshal.Copy(x, managedArray, 0, y); | |
handler(managedArray); | |
} | |
_callbackPtr = Marshal.GetFunctionPointerForDelegate(new InternalHandleMessage(Callback)); | |
_callbackHandle = GCHandle.Alloc((Action<IntPtr, int>) Callback); | |
ImageConsumerSetCallback(_nativePointer,_callbackPtr); | |
} | |
public void Start() | |
{ | |
StartImageConsumer(_nativePointer); | |
} | |
public void Wait() | |
{ | |
SeralizedWait(_nativePointer); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ImageSharedMemoryProducer | |
: ISharedMemoryProducer | |
{ | |
[DllImport("ScryUnlimitedCommunicator.so")] | |
private static extern IntPtr GetImageProducer(string name); | |
[DllImport("ScryUnlimitedCommunicator.so")] | |
private static extern void DeleteImageProducer(IntPtr handle); | |
[DllImport("ScryUnlimitedCommunicator.so")] | |
private static extern void SendImageData(IntPtr handle, byte[] data,long length); | |
private IntPtr _nativeHandle; | |
public ImageSharedMemoryProducer(string name) | |
{ | |
_nativeHandle = GetImageProducer(name); | |
} | |
~ImageSharedMemoryProducer() | |
{ | |
DeleteImageProducer(_nativeHandle); | |
} | |
public long Size() | |
{ | |
return 3000000; | |
} | |
public void Write(byte[] message) | |
{ | |
SendImageData(_nativeHandle,message,message.LongLength); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public delegate void HandleMessage(byte[] message); | |
internal delegate void InternalHandleMessage(IntPtr array, int length); | |
public interface ISharedMemoryConsumer | |
{ | |
long Size(); | |
void SetCallback(HandleMessage handler); | |
void Start(); | |
void Wait(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ISharedMemoryProducer | |
{ | |
long Size(); | |
void Write(byte[] message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment