/// <summary> /// Invoked whenever a message is sent to the subscription. /// </summary> /// <param name="nativeHandle"> /// The native handle for the subscription. /// </param> /// <param name="nativeMsg"> /// Native handle for the message. /// </param> /// <param name="closure"> /// Native reference to the closure. /// </param> /// <param name="itemClosure"> /// Item closure for this subject. /// </param> private static void OnMessage(IntPtr nativeHandle, IntPtr nativeMsg, IntPtr closure, IntPtr itemClosure) { // Obtain the handle from the closure GCHandle handle = (GCHandle)closure; // Extract the impl from the handle MamaBasicSubscriptionImpl impl = (MamaBasicSubscriptionImpl)handle.Target; // Use the impl to invoke the callback impl.InvokeMessage(nativeMsg); }
/// <summary> /// This event handler is called by the native layer whenever an error occurs with the subscription. /// </summary> /// <param name="nativeHandle"> /// The native handle for the subscription. /// </param> /// <param name="nativeStatus"> /// The native status code for this error. /// </param> /// <param name="platformError"> /// Platform specific error information. /// </param> /// <param name="subject"> /// Subject for entitlement errors. /// </param> /// <param name="closure"> /// Native reference to the closure. /// </param> private static void OnError(IntPtr nativeHandle, int nativeStatus, IntPtr platformError, string subject, IntPtr closure) { // Obtain the handle from the closure GCHandle handle = (GCHandle)closure; // Extract the impl from the handle MamaBasicSubscriptionImpl impl = (MamaBasicSubscriptionImpl)handle.Target; // Use the impl to invoke the error callback impl.InvokeError(nativeStatus, subject); }
/* ************************************************************** */ #region Private Static Functions /// <summary> /// This event handler is called by the native layer whenever the subscription has been /// created. /// </summary> /// <param name="nativeHandle"> /// The native handle for the subscription. /// </param> /// <param name="closure"> /// Native reference to the closure. /// </param> private static void onCreate(IntPtr nativeHandle, IntPtr closure) { // Obtain the handle from the closure GCHandle handle = (GCHandle)closure; // Extract the impl from the handle MamaBasicSubscriptionImpl impl = (MamaBasicSubscriptionImpl)handle.Target; // Use the impl to invoke the error callback impl.InvokeCreate(); }
/* ************************************************************** */ #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 MamaBasicSubscription.createBasic function. /// </param> /// <param name="subscription"> /// The actual C# subscription object. /// </param> /// <returns> /// The IntPtr that can then be used for the closure. /// </returns> internal static IntPtr Create(MamaBasicSubscriptionCallback callback, object closure, MamaBasicSubscription subscription) { // Allocate a new impl MamaBasicSubscriptionImpl impl = new MamaBasicSubscriptionImpl(callback, closure, subscription); // 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 subscription has been fully destroyed. /// Note that clean up will only be performed if the subscription is about to be destroyed or deallocated, /// this can be determined by using the getState function. /// </summary> /// <param name="subscription"> /// The native subscription. /// </param> /// <param name="closure"> /// The closure passed down to the native layer. /// </param> private static void onDestroy(IntPtr subscription, IntPtr closure) { // Obtain the handle from the closure GCHandle handle = (GCHandle)closure; /* If the target is a basic impl then createBasic has been invoked on this class instead of the base class, * however as the MamaSubscriptionImpl is a derived class from MamaBasicSubscriptionImpl we must check for * the derived class first. */ if (handle.Target is MamaSubscriptionImpl) { // Extract the impl from the handle MamaSubscriptionImpl impl = (MamaSubscriptionImpl)handle.Target; /* Get the state before the destroy is called, (in case the user recreates the subscription on * the callback). */ mamaSubscriptionState state = impl.Subscription.State; // Use the impl to invoke the destroy callback, (if this has been supplied) impl.InvokeDestroy(); // If we are destroying rather than deactivating then delete the impl if ((mamaSubscriptionState.MAMA_SUBSCRIPTION_DESTROYED == state) || (mamaSubscriptionState.MAMA_SUBSCRIPTION_DEALLOCATING == state)) { /* The subscription has now been destroyed or deleted and the impl is no longer required, free the handle to * allow the garbage collector to clean it up. */ handle.Free(); } } else if (handle.Target is MamaBasicSubscriptionImpl) { // Extract the impl from the handle MamaBasicSubscriptionImpl impl = (MamaBasicSubscriptionImpl)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(); } else { // Otherwise something has gone wrong throw new InvalidOperationException(); } }
/// <summary> /// This event handler is called by the native layer whenever the subscription 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="subscription"> /// The native subscription. /// </param> /// <param name="closure"> /// The closure passed down to the native layer. /// </param> private static void onDestroy(IntPtr subscription, IntPtr closure) { // Obtain the handle from the closure GCHandle handle = (GCHandle)closure; // Extract the impl from the handle MamaBasicSubscriptionImpl impl = (MamaBasicSubscriptionImpl)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(); }
/// <summary> /// This function will create the basic subscription without marketdata semantics. /// </summary> /// <param name="transport"> /// The MamaTransport. /// </param> /// <param name="queue"> /// The MamaQueue. /// </param> /// <param name="callback"> /// Provide callback function implementations to be informed of events on the subscription. /// </param> /// <param name="symbol"> /// The symbol to subscribe to. /// </param> /// <param name="closure"> /// The closure that will be passed back with the callback functions. /// </param> public void createBasic(MamaTransport transport, MamaQueue queue, MamaBasicSubscriptionCallback callback, string symbol, object closure) { // Ensure that the native subscription has been allocated EnsurePeerCreated(); // Save arguments in member variables mClosure = closure; mQueue = queue; mTransport = transport; // Create the impl IntPtr impl = MamaBasicSubscriptionImpl.Create(callback, closure, this); /* Create the subscription, register for the destroy callback regardless if the client wants it or not, * this is to allow clean-up to be done whenever the timer has been fully destroyed. */ CheckResultCode(NativeMethods.mamaSubscription_createBasic( NativeHandle, transport.NativeHandle, queue.NativeHandle, ref mCallbackDelegates, symbol, impl)); }
/* ************************************************************** */ #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 MamaBasicSubscription.createBasic function. /// </param> /// <param name="subscription"> /// The actual C# subscription object. /// </param> /// <returns> /// The IntPtr that can then be used for the closure. /// </returns> internal static IntPtr Create(MamaBasicSubscriptionCallback callback, object closure, MamaBasicSubscription subscription) { // Allocate a new impl MamaBasicSubscriptionImpl impl = new MamaBasicSubscriptionImpl(callback, closure, subscription); // Create a GC handle GCHandle handle = GCHandle.Alloc(impl); // Return the native pointer return (IntPtr)handle; }