protected int InitAio() { // Make a copy to ensure an automatically created delegate doesn't get GC'd while native code // is still using it: // https://stackoverflow.com/questions/6193711/call-has-been-made-on-garbage-collected-delegate-in-c aioCallback = new AioCallback(callback); var res = nng_aio_alloc(out aioHandle, aioCallback, IntPtr.Zero); return(res); }
public static NngResult <INngAio> Create(AioCallback callback) { // Make a copy to ensure an automatically created delegate doesn't get GC'd while native code // is still using it: // https://stackoverflow.com/questions/6193711/call-has-been-made-on-garbage-collected-delegate-in-c // https://docs.microsoft.com/en-us/cpp/dotnet/how-to-marshal-callbacks-and-delegates-by-using-cpp-interop var res = nng_aio_alloc(out var aioHandle, callback, IntPtr.Zero); return(NngResult <INngAio> .OkThen(res, () => new NngAio { aioHandle = aioHandle, gcHandle = GCHandle.Alloc(callback) })); }
internal int Init(IMessageFactory <T> factory, ISocket socket, Action <IntPtr> callback) { Factory = factory; Socket = socket; // Make a copy to ensure an auto-matically created delegate doesn't get GC'd while native code // is still using it: // https://stackoverflow.com/questions/6193711/call-has-been-made-on-garbage-collected-delegate-in-c aioCallback = new AioCallback(callback); // FIXME: TODO: callbacks still getting GC'd. Maybe I need to move nng_aio_free() out of Dispose()? callbacks.Add(aioCallback); return(nng_aio_alloc(out aioHandle, aioCallback, IntPtr.Zero)); }
protected int InitAio() { // Make a copy to ensure an auto-matically created delegate doesn't get GC'd while native code // is still using it: // https://stackoverflow.com/questions/6193711/call-has-been-made-on-garbage-collected-delegate-in-c aioCallback = new AioCallback(callback); var res = nng_aio_alloc(out aioHandle, aioCallback, IntPtr.Zero); if (res == 0) { // FIXME: TODO: callbacks still getting GC'd. Maybe I need to move nng_aio_free() out of Dispose()? var added = callbacks.TryAdd(aioCallback, true); System.Diagnostics.Debug.Assert(added); } return(res); }
public void Basic() { Util.RepeatTest(() => { var callbacks = new AioCallback[] { null, staticAioCallback, }; foreach (var callback in callbacks) { using (var aio = factory.CreateAio(callback).Unwrap()) { aio.SetTimeout(10); aio.Wait(); aio.GetResult().Unwrap(); Assert.Equal(IntPtr.Zero, aio.GetOutput(0)); aio.Cancel(); } } }); }
public static extern int nng_aio_alloc(out nng_aio aio, AioCallback callback, IntPtr arg);
public NngResult <INngAio> CreateAio(AioCallback callback) => NngAio.Create(callback);