/// <summary> /// Send a p2p message from the specified inbox using the throttle. /// The lifecycle of the message sent is controlled by the user of the API. The /// callback indicates when the API is no longer using the message and can be /// destroyed/reused by the application. /// </summary> /// <param name="inbox">The MamaInbox which will process any response to the sent message.</param> /// <param name="message">The MamaMsg to send.</param> /// <param name="callback">The callback which will be invoked when the message /// is sent from the throttle queue.</param> /// <param name="closure">User supplied data returned when the callback is invoked.</param> public void sendFromInboxWithThrottle( MamaInbox inbox, MamaMsg message, MamaSendCompleteCallback callback, object closure) { #if MAMA_WRAPPERS_CHECK_ARGUMENTS if (inbox == null) { throw new ArgumentNullException("inbox"); } if (message == null) { throw new ArgumentNullException("message"); } if (callback == null) { throw new ArgumentNullException("callback"); } EnsurePeerCreated(); #endif // MAMA_WRAPPERS_CHECK_ARGUMENTS // Create a new callback wrapper MamaCallbackWrapper <MamaSendCompleteCallback, MamaThrottledSendCompleteDelegate> wrapper = new MamaCallbackWrapper <MamaSendCompleteCallback, MamaThrottledSendCompleteDelegate>( callback, closure, new MamaThrottledSendCompleteDelegate(onSendComplete)); // Add this to the store IntPtr nativeClosure = mCallbackStore.StoreWrapper(wrapper); // Call the native function int code = NativeMethods.mamaPublisher_sendFromInboxWithThrottle( nativeHandle, inbox.NativeHandle, message.NativeHandle, (Wombat.MamaPublisher.MamaThrottledSendCompleteDelegate)wrapper.NativeDelegate, nativeClosure); try { CheckResultCode(code); } // If something goes wrong then remove the wrapper from the store catch { // Remove the wrapper mCallbackStore.RemoveWrapper(nativeClosure); // Dispose it ((IDisposable)wrapper).Dispose(); // Rethrow the exception throw; } // Ensure that the message passed will not delete its native peer message.SelfManageLifeTime(false); }
/// <summary> /// This handler is called whenever an asynchronous publisher send has completed. /// </summary> /// <param name="publisher"> /// Native reference to the publisher/ /// </param> /// <param name="msg"> /// Native message. /// </param> /// <param name="status"> /// Status returned. /// </param> /// <param name="closure"> /// The closure is a reference to the callback wrapper object used to keep the C# /// objects alive. /// </param> private void onSendComplete(IntPtr publisher, IntPtr msg, int status, IntPtr closure) { // Extract the wrapper object from the store using (MamaCallbackWrapper <MamaSendCompleteCallback, MamaThrottledSendCompleteDelegate> wrapper = mCallbackStore.RemoveWrapper(closure)) { if (mReusableMsg == null) { mReusableMsg = new MamaMsg(msg); } else { mReusableMsg.setNativeHandle(msg); } MamaSendCompleteCallback callback = (MamaSendCompleteCallback)wrapper.Callback; callback.onSendComplete(this, mReusableMsg, (MamaStatus.mamaStatus)status, wrapper.Closure); } }
/// <summary> /// Send a p2p message from the specified inbox using the throttle. /// The lifecycle of the message sent is controlled by the user of the API. The /// callback indicates when the API is no longer using the message and can be /// destroyed/reused by the application. /// </summary> /// <param name="inbox">The MamaInbox which will process any response to the sent message.</param> /// <param name="message">The MamaMsg to send.</param> /// <param name="callback">The callback which will be invoked when the message /// is sent from the throttle queue.</param> /// <param name="closure">User supplied data returned when the callback is invoked.</param> public void sendFromInboxWithThrottle( MamaInbox inbox, MamaMsg message, MamaSendCompleteCallback callback, object closure) { #if MAMA_WRAPPERS_CHECK_ARGUMENTS if (inbox == null) { throw new ArgumentNullException("inbox"); } if (message == null) { throw new ArgumentNullException("message"); } if (callback == null) { throw new ArgumentNullException("callback"); } EnsurePeerCreated(); #endif // MAMA_WRAPPERS_CHECK_ARGUMENTS // Create a new callback wrapper MamaCallbackWrapper<MamaSendCompleteCallback, MamaThrottledSendCompleteDelegate> wrapper = new MamaCallbackWrapper<MamaSendCompleteCallback, MamaThrottledSendCompleteDelegate>( callback, closure, new MamaThrottledSendCompleteDelegate(onSendComplete)); // Add this to the store IntPtr nativeClosure = mCallbackStore.StoreWrapper(wrapper); // Call the native function int code = NativeMethods.mamaPublisher_sendFromInboxWithThrottle( nativeHandle, inbox.NativeHandle, message.NativeHandle, (Wombat.MamaPublisher.MamaThrottledSendCompleteDelegate)wrapper.NativeDelegate, nativeClosure); try { CheckResultCode(code); } // If something goes wrong then remove the wrapper from the store catch { // Remove the wrapper mCallbackStore.RemoveWrapper(nativeClosure); // Dispose it ((IDisposable)wrapper).Dispose(); // Rethrow the exception throw; } // Ensure that the message passed will not delete its native peer message.SelfManageLifeTime(false); }