/// <summary> /// Invoked whenever a message is sent to the inbox. /// </summary> /// <param name="nativeMsg"> /// Native handle for the message. /// </param> /// <param name="closure"> /// Native reference to the closure. /// </param> private static void onMessage(IntPtr nativeMsg, IntPtr closure) { // Obtain the handle from the closure GCHandle handle = (GCHandle)closure; // Extract the impl from the handle MamaInboxImpl impl = (MamaInboxImpl)handle.Target; // Use the impl to invoke the error callback impl.InvokeMessage(nativeMsg); }
/// <summary> /// Allows the native layer to invoke the error callback. /// </summary> /// <param name="nativeStatus"> /// The native error status. /// </param> /// <param name="closure"> /// Native reference to the closure. /// </param> private static void onError(int nativeStatus, IntPtr closure) { // Obtain the handle from the closure GCHandle handle = (GCHandle)closure; // Extract the impl from the handle MamaInboxImpl impl = (MamaInboxImpl)handle.Target; // Use the impl to invoke the error callback impl.InvokeError(nativeStatus); }
/* ************************************************************** */ #region Internal Operations /// <summary> /// This function creates a new impl and returns an IntPtr that can then be passed to /// the native layer. /// </summary> /// <param name="callback"> /// The user callback implementation. /// </param> /// <param name="closure"> /// The closure supplied to the MamaInbox.create function. /// </param> /// <param name="inbox"> /// The actual C# inbox object. /// </param> /// <returns> /// The IntPtr that can then be used for the closure. /// </returns> internal static IntPtr Create(MamaInboxCallback callback, object closure, MamaInbox inbox) { // Allocate a new impl MamaInboxImpl impl = new MamaInboxImpl(callback, closure, inbox); // Create a GC handle GCHandle handle = GCHandle.Alloc(impl); // Return the native pointer return((IntPtr)handle); }
/* ************************************************************** */ #region Private Static Functions /// <summary> /// This event handler is called by the native layer whenever the inbox has been fully destroyed. /// It will perform all clean-up and then invoke the onDestroy callback function if this has /// been supplied. /// </summary> /// <param name="inbox"> /// The native inbox. /// </param> /// <param name="closure"> /// The closure passed down to the native layer. /// </param> private static void onDestroy(IntPtr inbox, IntPtr closure) { // Obtain the handle from the closure GCHandle handle = (GCHandle)closure; // Extract the impl from the handle MamaInboxImpl impl = (MamaInboxImpl)handle.Target; // Use the impl to invoke the destroy callback, (if this has been supplied) impl.InvokeDestroy(); /* The timer has now been destroyed and the impl is no longer required, free the handle to * allow the garbage collector to clean it up. */ handle.Free(); }
/* ************************************************************** */ #region Public Functions /// <summary> /// Creates an inbox and stores at the address specified by the calling client. /// </summary> /// <param name="transport"> /// The mamaTransport being used. /// </param> /// <param name="queue"> /// Optional mamaQueue. Will use default queue if null. /// </param> /// <param name="callback"> /// A callback interface invoked in response to a p2p message being received or when an error is /// encountered during p2p messaging. /// </param> /// <param name="closure"> /// User supplied data /// </param> public void create(MamaTransport transport, MamaQueue queue, MamaInboxCallback callback, object closure) { // Check the arguments if (transport == null) { throw new ArgumentNullException("transport"); } if (queue == null) { throw new ArgumentNullException("queue"); } if (callback == null) { throw new ArgumentNullException("callback"); } // Save the clousre in the member variable mClosure = closure; // Create the impl IntPtr impl = MamaInboxImpl.Create(callback, closure, this); /* Create the inbox, register for the destroy callback regardless if the client wants it or not, * this is to allow clean-up to be done whenever the inbox has been fully destroyed. */ IntPtr nativeInbox = IntPtr.Zero; CheckResultCode(NativeMethods.mamaInbox_create2( ref nativeInbox, transport.NativeHandle, queue.NativeHandle, mMsgDelegate, mErrorDelegate, mDestroyDelegate, impl)); // Save the native timer in the member variable NativeHandle = nativeInbox; }
/* ************************************************************** */ #region Internal Operations /// <summary> /// This function creates a new impl and returns an IntPtr that can then be passed to /// the native layer. /// </summary> /// <param name="callback"> /// The user callback implementation. /// </param> /// <param name="closure"> /// The closure supplied to the MamaInbox.create function. /// </param> /// <param name="inbox"> /// The actual C# inbox object. /// </param> /// <returns> /// The IntPtr that can then be used for the closure. /// </returns> internal static IntPtr Create(MamaInboxCallback callback, object closure, MamaInbox inbox) { // Allocate a new impl MamaInboxImpl impl = new MamaInboxImpl(callback, closure, inbox); // Create a GC handle GCHandle handle = GCHandle.Alloc(impl); // Return the native pointer return (IntPtr)handle; }