/// <summary>
        /// Stops the listener
        /// </summary>
        private void StopEventListener()
        {
            try
            {
                if (eventListenerService != null)
                {
                    RemotingServices.Unmarshal(eventListenerService);
                }

                if (eventInvocation != null)
                {
                    RemotingServices.Disconnect(eventInvocation);
                }

                if (eventListenerChannel != null)
                {
                    ChannelServices.UnregisterChannel(eventListenerChannel);
                }

                if (eventQueue != null)
                {
                    eventQueue.Clear();
                    eventQueue = null;
                }
                if (eventTrigger != null)
                {
                    eventTrigger.Close();
                    eventTrigger = null;
                }
                if (eventProcessor != null)
                {
                    try
                    {
                        if (eventProcessor.IsAlive)
                        {
                            eventProcessor.Abort();
                        }
                    }
                    catch (Exception) { }
                    eventProcessor = null;
                }
                eventListenerService = null;
                eventInvocation      = null;
                eventListenerChannel = null;
            }
            catch (Exception ex)
            {
                log.Error("Error stopping event listener: " + ex.Message, ex);
            }
        }
Ejemplo n.º 2
0
        //protected virtual void EmitEventInvocationMethodForWeaklyReferencedEvents(
        //   EventModel eventModel,
        //   CompositeTypeGenerationInfo thisGenerationInfo,
        //   CILField eventField,
        //   CILTypeBase eventType,
        //   CompositeMethodGenerationInfo invokeMethod,
        //   CILMethod eventMethodToInvoke
        //   )
        //{
        //   //   EventHandlerInfoArrayWrapper wrapper = this._myEventManualWeak;
        //   //   if ( wrapper != null )
        //   //   {
        //   //      EventHandlerInfo[] evts = wrapper.Array;
        //   //      Boolean cleanUp = false;
        //   //      try
        //   //      {
        //   //      for ( Int32 idx = 0; idx < wrapper.ElementCount; ++idx )
        //   //      {
        //   //         EventHandlerInfo info = evts[idx];
        //   //         Object target = info.Target;
        //   //         if ( EventHandlerInfoArrayWrapper.IsDead( info ) )
        //   //         {
        //   //            cleanUp = true;
        //   //         }
        //   //         else
        //   //         {
        //   //            (result) = new <event type>(target, info.Method).Invoke(params);
        //   //         }
        //   //      }
        //   //
        //   //      }
        //   //      finally
        //   //      {
        //   //      if ( cleanUp )
        //   //      {
        //   //         Interlocked.CompareExchange( ref this._myEventManualWeak, EventHandlerInfoArrayWrapper.CleanUp( wrapper ), wrapper );
        //   //      }
        //   //      }
        //   //   }

        //   this.EmitEventInvocationMethodCore(
        //      eventModel,
        //      thisGenerationInfo,
        //      eventField,
        //      WEAK_EVENT_WRAPPER_TYPE,
        //      invokeMethod,
        //      eventMethodToInvoke,
        //      ( eventLB, invocationStyle, exceptionType ) =>
        //      {
        //         var il = invokeMethod.IL;

        //         //      EventHandlerInfo[] evts = wrapper.Array;
        //         var evtsArrayB = il.DeclareLocal( WEAK_EVENT_ARRAY_WRAPPER_ARRAY_GETTER.GetReturnType() );
        //         il
        //            .EmitLoadLocal( eventLB )
        //            .EmitCall( WEAK_EVENT_ARRAY_WRAPPER_ARRAY_GETTER )
        //            .EmitStoreLocal( evtsArrayB );

        //         //      Boolean cleanUp = false;
        //         var cleanUpB = il.DeclareLocal( BOOLEAN_TYPE );
        //         il
        //            .EmitLoadBoolean( false )
        //            .EmitStoreLocal( cleanUpB );

        //         Boolean countingDead = !VOID_TYPE.Equals( invokeMethod.ReturnType );
        //         // Int32 amountOfDead = wrapper.ElementCount;
        //         LocalBuilder amountOfDeadB = null;
        //         if ( countingDead )
        //         {
        //            amountOfDeadB = invokeMethod.GetOrCreateLocal( LB_AMOUNT_OF_DEAD_EVENT_INFOS );
        //            il
        //               .EmitLoadLocal( eventLB )
        //               .EmitCall( WEAK_EVENT_ARRAY_WRAPPER_COUNT_GETTER )
        //               .EmitStoreLocal( amountOfDeadB );
        //         }

        //         il.EmitTryFinally(
        //            il2 => il2.EmitSimpleForLoop(
        //               il3 =>
        //               {
        //                  // Int32 idx = 0
        //                  var idxB = invokeMethod.GetOrCreateLocal( LB_INDEX );
        //                  il3
        //                     .EmitLoadInt32( 0 )
        //                     .EmitStoreLocal( idxB );
        //                  return idxB;
        //               },
        //               ( il3, idxB, loopBodyStartLabel ) =>
        //               {
        //                  // idx < wrapper.ElementCount
        //                  il3
        //                     .EmitLoadLocal( idxB )
        //                     .EmitLoadLocal( eventLB )
        //                     .EmitCall( WEAK_EVENT_ARRAY_WRAPPER_COUNT_GETTER )
        //                     .EmitBranch( BranchType.IF_FIRST_LESSER_THAN_SECOND, loopBodyStartLabel );
        //               },
        //               E_MethodIL.EmitLeftPlusPlus,
        //               ( il3, idxB ) =>
        //               {
        //                  var infoB = il3.DeclareLocal( evtsArrayB.LocalType.GetElementType() );

        //                  //         EventHandlerInfo info = evts[idx];
        //                  il3
        //                     .EmitLoadLocal( evtsArrayB )
        //                     .EmitLoadLocal( idxB )
        //                     .EmitLoadElement( evtsArrayB.LocalType )
        //                     .EmitStoreLocal( infoB );

        //                  //         Object target = info.Target;
        //                  var targetB = il3.DeclareLocal( EVENT_INFO_TARGET_GETTER.GetReturnType() );
        //                  il3
        //                     .EmitLoadLocal( infoB )
        //                     .EmitCall( EVENT_INFO_TARGET_GETTER )
        //                     .EmitStoreLocal( targetB );

        //                  il3.EmitIfElse(
        //                     ( il4, elseLabel, endIfLabel ) =>
        //                     {
        //                        il4
        //                           .EmitLoadLocal( infoB )
        //                           // TODO write out is dead -method
        //                           .EmitCall( IS_EVENT_INFO_DEAD_METHOD )
        //                           .EmitBranch( BranchType.IF_TRUE, elseLabel );
        //                     },
        //                     ( il4, elseLabel, endIfLabel ) =>
        //                     {
        //                        // (result) = new <event type>(target, info.Method).Invoke(params);

        //                        // TODO TODO TODO
        //                        // Consider changing info.Method into returning MethodInfo object, and then
        //                        // use info.Method.Invoke(target, params).
        //                        // Slower, but works with .NET 4 portable and PEVerifier won't give errors/warnings.
        //                        this.EmitEventInvocationWithTryCatchIfNeeded(
        //                           invocationStyle,
        //                           exceptionType,
        //                           invokeMethod,
        //                           eventMethodToInvoke,
        //                           il5 =>
        //                           {
        //                              il5
        //                                 .EmitLoadLocal( targetB )

        //                                 .EmitLoadLocal( infoB )
        //                                 .EmitCall( EVENT_INFO_METHOD_GETTER )
        //                                 .EmitNewObject( TypeGenerationUtils.GetMethodForEmitting( decType => TypeGenerationUtils.CreateTypeForEmittingCILType( decType, thisGenerationInfo.GenericArguments, null ), eventModel.NativeInfo.EventHandlerType.LoadConstructorOrThrow( 2 ).NewWrapper( this.ctx ) ) );
        //                           },
        //                           true
        //                           );
        //                     },
        //                     ( il4, endIfLabel ) =>
        //                     {
        //                        // cleanUp = true;
        //                        il4
        //                           .EmitLoadBoolean( true )
        //                           .EmitStoreLocal( cleanUpB );

        //                        if ( amountOfDeadB != null )
        //                        {
        //                           // --amountOfDead
        //                           il4.EmitLeftMinusMinus( amountOfDeadB );
        //                        }
        //                     }
        //                     );
        //               }
        //            ),
        //            il2 =>
        //               //      if ( cleanUp )
        //               //      {
        //               //         Interlocked.CompareExchange( ref this._myEventManualWeak, EventHandlerInfoArrayWrapper.CleanUp( wrapper ), wrapper );
        //               //      }
        //               il2.EmitIf(
        //               ( il3, endIfLabel ) =>
        //               {
        //                  il3
        //                     .EmitLoadLocal( cleanUpB )
        //                     .EmitBranch( BranchType.IF_FALSE, endIfLabel );
        //               },
        //               ( il3, endIfLabel ) =>
        //               {
        //                  il3
        //                     .EmitLoadThisFieldAddress( eventField )

        //                     .EmitLoadLocal( eventLB )
        //                     .EmitCall( WEAK_EVENT_ARRAY_CLEANUP_METHOD )

        //                     .EmitLoadLocal( eventLB )

        //                     .EmitCall( INTERLOCKED_COMPARE_EXCHANGE_METHOD_GDEF.MakeGenericMethod( eventLB.LocalType ) )
        //                     .EmitPop();
        //               }
        //            ) );
        //      } );
        //}

        //protected virtual void EmitEventAdditionMethodForWeaklyReferencedEvents(
        //   CILField eventField,
        //   MethodIL il
        //   )
        //{
        //   //Delegate[] invocations = <arg-1>.GetInvocationList();
        //   //EventHandlerInfo[] evtsToAdd = new EventHandlerInfo[invocations.Length];
        //   //for ( Int32 i = 0; i < invocations.Length; ++i )
        //   //{
        //   //   evtsToAdd[i] = new EventHandlerInfo( invocations[i] );
        //   //}

        //   //EventHandlerInfoArrayWrapper current = this._myEventManualWeak;
        //   //EventHandlerInfoArrayWrapper oldCurrent, combined;
        //   //do
        //   //{
        //   //   oldCurrent = EventHandlerInfoArrayWrapper.CleanUp( current );
        //   //   combined = EventHandlerInfoArrayWrapper.Combine( oldCurrent, evtsToAdd );
        //   //   current = Interlocked.CompareExchange( ref this._myEventManualWeak, combined, oldCurrent );
        //   //} while ( !Object.ReferenceEquals( current, oldCurrent ) );

        //   var invocationsB = il.DeclareLocal( GET_INVOCATION_LIST_METHOD.GetReturnType() );
        //   il
        //      .EmitLoadArg( 1 )
        //      .EmitCall( GET_INVOCATION_LIST_METHOD )
        //      .EmitStoreLocal( invocationsB );

        //   var evtsToAddB = il.DeclareLocal( STRONG_EVENT_WRAPPER_TYPE.MakeArrayType() );
        //   il
        //      .EmitLoadLocal( invocationsB )
        //      .EmitLoadArrayLength()
        //      .EmitNewArray( evtsToAddB.LocalType.GetElementType() )
        //      .EmitStoreLocal( evtsToAddB )

        //      .EmitSimpleForLoop(
        //      il2 =>
        //      {
        //         // Int32 i = 0
        //         var idxB = il.DeclareLocal( LB_INDEX.Type.NewWrapper( this.ctx ) );
        //         il2
        //            .EmitLoadInt32( 0 )
        //            .EmitStoreLocal( idxB );
        //         return idxB;
        //      },
        //      ( il2, idxB, loopBodyStartLabel ) =>
        //      {
        //         // index < evtsToAdd.Length
        //         il2
        //            .EmitLoadLocal( idxB )
        //            .EmitLoadLocal( evtsToAddB )
        //            .EmitLoadArrayLength()
        //            .EmitNumericConversion( CILTypeCode.UInt32, CILTypeCode.Int32, false )
        //            .EmitBranch( BranchType.IF_FIRST_LESSER_THAN_SECOND, loopBodyStartLabel );
        //      },
        //      E_MethodIL.EmitLeftPlusPlus,
        //      ( il2, idxB ) =>
        //      {
        //         il2.EmitLoadLocal( evtsToAddB )
        //            .EmitLoadLocal( idxB )
        //            .EmitLoadLocal( invocationsB )
        //            .EmitLoadLocal( idxB )
        //            .EmitLoadElement( invocationsB.LocalType )
        //            .EmitNewObject( EVENT_INFO_CTOR )
        //            .EmitStoreElement( evtsToAddB.LocalType );
        //      }
        //      )
        //      .EmitInterlockedCompareExchangeFieldSettingLoop(
        //      eventField,
        //      //fieldType,
        //      ( il2, currentB ) =>
        //      {
        //         il2
        //            .EmitLoadLocal( currentB )
        //            .EmitCall( WEAK_EVENT_ARRAY_CLEANUP_METHOD );
        //      },
        //      ( il2, oldCurrentB ) =>
        //      {
        //         il2
        //            .EmitLoadLocal( oldCurrentB )
        //           .EmitLoadLocal( evtsToAddB )
        //           .EmitCall( WEAK_EVENT_ARRAY_COMBINE_METHOD );
        //      }
        //      );
        //   il.EmitReturn();
        //}

        //protected virtual void EmitEventRemovingMethodForWeaklyReferencedEvents(
        //   CILField eventField,
        //   MethodIL il
        //   )
        //{
        //   //Delegate[] invocations = other.GetInvocationList();
        //   //EventHandlerInfoArrayWrapper current = this._myEventManualWeak;
        //   //EventHandlerInfoArrayWrapper oldCurrent, combined;
        //   //do
        //   //{
        //   //   oldCurrent = EventHandlerInfoArrayWrapper.CleanUp( current );
        //   //   combined = EventHandlerInfoArrayWrapper.Remove( oldCurrent, invocations );
        //   //   current = Interlocked.CompareExchange( ref this._myEventManualWeak, combined, oldCurrent );
        //   //} while ( !Object.ReferenceEquals( current, oldCurrent ) );

        //   var invocationsB = il.DeclareLocal( GET_INVOCATION_LIST_METHOD.GetReturnType() );
        //   il
        //      .EmitLoadArg( 1 )
        //      .EmitCall( GET_INVOCATION_LIST_METHOD )
        //      .EmitStoreLocal( invocationsB )

        //      .EmitInterlockedCompareExchangeFieldSettingLoop(
        //      eventField,
        //      ( il2, currentB ) =>
        //      {
        //         il2
        //            .EmitLoadLocal( currentB )
        //            .EmitCall( WEAK_EVENT_ARRAY_CLEANUP_METHOD );
        //      },
        //      ( il2, oldCurrentB ) =>
        //      {
        //         il2
        //            .EmitLoadLocal( oldCurrentB )
        //            .EmitLoadLocal( invocationsB )
        //            .EmitCall( WEAK_EVENT_ARRAY_REMOVE_METHOD );
        //      }
        //      )
        //      .EmitReturn();
        //}

        protected virtual void GetEventInvocationStyle(EventModel eventModel, out EventInvocation invocationStyle, out Type exceptionType)
        {
            EventInvocationStyleAttribute attr = eventModel.AllAttributes.OfType <EventInvocationStyleAttribute>().FirstOrDefault();

            if (attr == null)
            {
                invocationStyle = EventInvocationStyleAttribute.DEFAULT_INVOCATION_STYLE;
                exceptionType   = null;
            }
            else
            {
                invocationStyle = attr.InvocationStyle;
                exceptionType   = attr.RethrowException;
            }
        }
        /// <summary>
        /// Starts the listen.
        /// </summary>
        private void StartEventListener()
        {
            StopEventListener(); // if there is any channel still open --> close it

            try
            {
                log.Info("Starting event listener");
                int port = AppConfigSettings.GetInt(ConfigParameter.ListenerPort, ModuleName.Console);
                eventListenerChannel = new TcpChannel(port);
                ChannelServices.RegisterChannel(eventListenerChannel, false);

                eventInvocation = new EventInvocation();
                string serviceName = AppConfigSettings.GetString(ConfigParameter.ListenerServiceName, ModuleName.Console);

                eventListenerService = RemotingServices.Marshal(eventInvocation, serviceName);

                //RemotingConfiguration.RegisterWellKnownServiceType(typeof(EventInvocation),
                //        serviceName, WellKnownObjectMode.SingleCall);


                // define the event which is triggered when the Master calls the CallSlave() function
                eventInvocation.EventReceived += new EventInvocation.Received(OnEventReceived);

                // Create the event queue
                eventLock      = new object();
                eventQueue     = new PriorityQueue <EventAction, EventPriority>(10);
                eventTrigger   = new AutoResetEvent(false);
                eventProcessor = new Thread(ProcessEvents);
                eventProcessor.IsBackground = true;
                eventProcessor.Start();

                log.Info(string.Format("Successfully started event listener {0} on port {1}", serviceName, port));
            }
            catch (Exception ex)
            {
                log.Error("Error starting event listener: " + ex.Message, ex);
                StopEventListener(); // calls StopEventListener
            }
        }
Ejemplo n.º 4
0
 protected virtual void EmitEventInvocationWithTryCatchIfNeeded(
     EventInvocation invocationStyle,
     Type exceptionType,
     CompositeMethodGenerationInfo invokeMethod,
     CILMethod eventMethodToInvoke,
     Action <MethodIL> loadEventAction,
     Boolean storeResult
     )
 {
     invokeMethod.IL.EmitTryCatch(
         EXCEPTION_TYPE,
         il2 =>
     {
         loadEventAction(il2);
         for (Int32 idx = 0; idx < invokeMethod.Parameters.Count; ++idx)
         {
             il2.EmitLoadArg(invokeMethod.Parameters[idx].Position + 1);
         }
         il2.EmitCall(eventMethodToInvoke);
         if (storeResult && !VOID_TYPE.Equals(eventMethodToInvoke.GetReturnType()))
         {
             var resultB = invokeMethod.GetOrCreateLocal(LB_RESULT, invokeMethod.ReturnType);
             il2.EmitStoreLocal(resultB);
         }
     },
         EventInvocation.INVOKE_DIRECTLY.Equals(invocationStyle) ? (Action <MethodIL>)null : il2 =>
     {
         if (exceptionType != null)
         {
             this.EmitStoreExceptionListWithinCatch(invokeMethod);
         }
         else
         {
             throw new InternalException("Non-direct event invocation style, but no exception type specified.");
         }
     },
         false
         );
 }
Ejemplo n.º 5
0
 private EventInvocationStyleAttribute(EventInvocation invocationStyle, Type rethrowExceptionType)
 {
     this._invocationStyle      = invocationStyle;
     this._rethrowExceptionType = rethrowExceptionType;
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Creates a new instance of <see cref="EventInvocationStyleAttribute"/> with specified event invocation style.
 /// </summary>
 /// <param name="invocationStyle">The <see cref="EventInvocation"/> style for target event.</param>
 /// <remarks>
 /// If <paramref name="invocationStyle"/> is <see cref="EventInvocation.INVOKE_ALL"/>, then the exception type will be <see cref="AggregateException"/>.
 /// </remarks>
 /// <seealso cref="EventInvocation"/>
 public EventInvocationStyleAttribute(EventInvocation invocationStyle)
     : this(invocationStyle, EventInvocation.INVOKE_ALL.Equals(invocationStyle) ? DEFAULT_RETHROW_EXCEPTION_TYPE : null)
 {
 }