protected override object OnInterceptMethod(InterceptionContext context)
        {
            var interceptedMethod = (MethodInfo)context.InterceptedMethod;
            var target = (LambdaExpression)context.Parameters[0].Value;
            var body = (MethodCallExpression)target.Body;

            if (!body.Method.IsStatic)
            {
                var method = interceptedMethod.IsGenericMethod
                    ? interceptedMethod.MakeGenericMethod(context.GenericArguments.ToArray())
                    : interceptedMethod;

                return method.Execute(context.Sender, context.Parameters.Select(p => p.Value).ToArray());
            }

            Type returnType;
            if (interceptedMethod.IsGenericMethod)
            {
                var genericMethod = interceptedMethod.MakeGenericMethod(context.GenericArguments.ToArray());
                returnType = genericMethod.ReturnType;
            }
            else
            {
                returnType = interceptedMethod.ReturnType;
            }

            return Create(body.Method, returnType);
        }
 public void OnError(InterceptionContext context)
 {
     if(Log.Logger != null && context.Exception != null)
     {
         Log.Logger.Error(context.Exception, string.Format("There was an error method {0} from {1} type", context.InterceptedMethod.Name, context.InterceptedMethod.DeclaringType.FullName));
     }
 }
        public void OnOverride_GivenContext_CallsCache()
        {
            // arrange
            var context = new InterceptionContext();
            context.Parameters = new List<Parameter>(new[] { new Parameter { Value = 2 } });

            var cacheMock = new Mock<ICache>();
            var expectedReturn = new MockDataReader();

            cacheMock
                .Setup(x => x.GetDataReader(It.IsAny<SqlCommand>(), It.IsAny<Func<DbDataReader>>()))
                .Returns(expectedReturn);

            var sut = new ReaderCachingInterceptor(cacheMock.Object);

            // act
            var actualReturn = sut.OnOverride(context);

            // assert
            Assert.That(actualReturn, Is.SameAs(expectedReturn));

            cacheMock.Verify(
                x => x.GetDataReader(It.IsAny<SqlCommand>(), It.IsAny<Func<DbDataReader>>()),
                Times.Once());
        }
 public void OnAfterExecute(InterceptionContext context)
 {
     if (HttpContext.Current != null && !HttpContext.Current.User.Identity.IsAuthenticated)
     {
         HttpContext.Current.Response.Redirect(ConfigurationSettings.AppSettings["UnauthorizedURL"]);
     }
 }
 public void OnAfterExecute(InterceptionContext context)
 {
     if (_step != null)
     {
         _step.Dispose();
         _step = null;
     }
 }
 public void OnBeforeExecute(InterceptionContext context)
 {
     foreach (var parameter in context.Parameters)
     {
         if (!parameter.IsOptional && (parameter.Value == null || (parameter.Value is string && string.IsNullOrEmpty(parameter.Value.ToString()))))
         {
             throw new ArgumentNullException(parameter.Name);
         }
     }
 }
        public void GuardNullShouldThrowArgumentNullExceptionWhenFirstParameterIsNullAndHasOptionalParameter()
        {
            // Arrange
            var gni = new GuardNullInterceptor();
            var context = new InterceptionContext
            {
                Parameters = new List<Parameter> { new Parameter() { Name = "Dummy", Value = null}, new Parameter() { Name = "Dummy2", Value = null, IsOptional = true } }
            };

            // Act & Assert
            gni.Invoking(x => gni.OnBeforeExecute(context)).ShouldThrow<ArgumentNullException>();
        }
        public void GuardNullShouldThrowArgumentNullExceptionWhenArgumentTypeIsStringAndIsEmpty()
        {
            // Arrange
            var gni = new GuardNullInterceptor();
            var context = new InterceptionContext
            {
                Parameters = new List<Parameter> {new Parameter() {Name = "Dummy", Value = ""}}
            };

            // Act & Assert
            gni.Invoking(x => gni.OnBeforeExecute(context)).ShouldThrow<ArgumentNullException>();
        }
        public void OnAfterExecute(InterceptionContext context)
        {
            var startTimeStamp = context.Parameters.FirstOrDefault(x => x.Name == "StartTimeStamp");

            if (startTimeStamp != null)
            {
                var start = (DateTime)startTimeStamp.Value;
                var end = context.TimeStamp;

                var elapsed = end - start;
                Metrics.Timer(string.Format("{0}.{1}", "total_executiontime", context.InterceptedMethod.Name), (int)elapsed.TotalMilliseconds);
            }
        }
        public object OnOverride(InterceptionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var command = context.Sender as SqlCommand;
            var parameters = context.Parameters.Select(x => x.Value).ToArray();

            // query to run if we actually want to hit the database
            Func<DbDataReader> query = () => (DbDataReader)context.InterceptedMethod.Execute(command, parameters);

            return cache.GetDataReader(command, query);
        }
        public void OnBeforeExecute(InterceptionContext context)
        {
            int counter = 1;

            if(_callCounter.ContainsKey(context.InterceptedMethod.Name))
            {

                _callCounter.TryGetValue(context.InterceptedMethod.Name, out counter);
                _callCounter.Remove(context.InterceptedMethod.Name);
                counter += 1;

            }

             _callCounter.Add(context.InterceptedMethod.Name, counter);

            Metrics.Counter(
                string.Format(
                    "{0}.{1}",
                    context.InterceptedMethod.Name,
                    "times_called"),
                counter);
            context.Parameters.Add(new Parameter { Name = "StartTimeStamp", Value = context.TimeStamp });
        }
Пример #12
0
 public static extern void interception_set_precedence(InterceptionContext context, InterceptionDevice device, InterceptionPrecedence precedence);
Пример #13
0
 public static extern void interception_destroy_context(InterceptionContext context);
Пример #14
0
 private void InsteadOfBodyInterceptor(InterceptionContext obj)
 {
     obj.SetResult((string)obj.ExecuteBody() + "Result");
 }
Пример #15
0
 public void OnAfterExecute(InterceptionContext context)
 {
 }
Пример #16
0
        /// <summary>
        /// Called before entity update.
        /// </summary>
        /// <param name="entry">The entry.</param>
        /// <param name="item">The item.</param>
        /// <param name="context">The context.</param>
        protected override void OnBeforeUpdate(DbEntityEntry entry, IAuditable item, InterceptionContext context)
        {
            item.UpdatedUtc = DateTime.UtcNow;

            base.OnBeforeUpdate(entry, item, context);
        }
 public bool CanHandle(InterceptionContext context)
 {
     // Only operate on Glass Interface Factory class proxies
     return(ProxyUtil.IsProxy(context.ExistingInstance) &&
            context.ComponentServices.All(service => service.ServiceType.GetCustomAttribute <GlassFactoryTypeAttribute>() != null));
 }
Пример #18
0
 public static extern InterceptionDevice interception_wait_with_timeout(InterceptionContext context, ulong milliseconds);
        /// <summary>
        /// Called before entity delete.
        /// </summary>
        /// <param name="entry">The entry.</param>
        /// <param name="item">The item.</param>
        /// <param name="context">The context.</param>
        /// <exception cref="System.InvalidOperationException">Item is already deleted.</exception>
        protected override void OnBeforeDelete(DbEntityEntry entry, ISoftDelitable item, InterceptionContext context)
        {
            if (item.IsDeleted)
            {
                throw new InvalidOperationException("Item is already deleted.");
            }

            base.OnBeforeDelete(entry, item, context);
            item.IsDeleted = true;
            entry.State    = EntityState.Modified;
        }
 public object OnOverride(InterceptionContext context)
 {
     Console.WriteLine("InterceptorForContainer > OnOverride");
     return null;
 }
Пример #21
0
 public void OnBeforeExecute(InterceptionContext context)
 {
     Console.WriteLine("InterceptorForContainer > OnBeforeExecute");
 }
Пример #22
0
 public object OnOverride(InterceptionContext context)
 {
     Console.WriteLine("InterceptorForContainer > OnOverride");
     return(null);
 }
        public void Returns_null_when_given_key_does_not_exist()
        {
            var testing = new InterceptionContext("test");

            Assert.IsNull(testing["key"]);
        }
Пример #24
0
 public void OnBeforeExecute(InterceptionContext context)
 {
     Console.WriteLine("OnBeforeExecute > MyInterceptor ! ");
 }
Пример #25
0
 private static extern void interception_set_filter(InterceptionContext context, IntPtr predicate, InterceptionFilter filter);
Пример #26
0
 public static void interception_set_filter_keyboard(InterceptionContext context, InterceptionFilterKeyState filter) =>
 interception_set_filter(context, Marshal.GetFunctionPointerForDelegate(new InterceptionPredicate(interception_is_keyboard)), (InterceptionFilter)filter);
Пример #27
0
 public InvocationSteps(InterceptionContext context, MoqContext moq)
 {
     _context = context;
     _moq     = moq;
 }
Пример #28
0
 public object OnOverride(InterceptionContext context)
 {
     Console.WriteLine("OnOverride > MyInterceptor !");
     return null;
 }
 public void OnBeforeExecute(InterceptionContext context)
 {
     _step = _profiler.Step(context.InterceptedMethod.Name);
 }
Пример #30
0
 protected abstract object OnInterceptMethod(InterceptionContext context);
Пример #31
0
        protected override void OnBeforeInsert(DbEntityEntry entry, Entity <Guid> item, InterceptionContext context)
        {
            if (item.IsNew)
            {
                item.Id = SequentialGuidGenerator.Generate();
            }

            base.OnBeforeInsert(entry, item, context);
        }
Пример #32
0
        public void ThreadStart()
        {
            byte[] keybdState = new byte[256];
            GetKeyboardState(keybdState);

            NumLock    = (keybdState[(int)VK.VK_NUMLOCK] & 1) != 0;
            CapsLock   = (keybdState[(int)VK.VK_CAPITAL] & 1) != 0;
            ScrollLock = (keybdState[(int)VK.VK_SCROLL] & 1) != 0;

            Shift  = (keybdState[(int)VK.VK_SHIFT] & 0x80) != 0;
            LShift = (keybdState[(int)VK.VK_LSHIFT] & 0x80) != 0;
            RShift = (keybdState[(int)VK.VK_RSHIFT] & 0x80) != 0;

            Ctrl  = (keybdState[(int)VK.VK_CONTROL] & 0x80) != 0;
            LCtrl = (keybdState[(int)VK.VK_LCONTROL] & 0x80) != 0;
            RCtrl = (keybdState[(int)VK.VK_RCONTROL] & 0x80) != 0;

            Alt  = (keybdState[(int)VK.VK_MENU] & 0x80) != 0;
            LAlt = (keybdState[(int)VK.VK_LMENU] & 0x80) != 0;
            RAlt = (keybdState[(int)VK.VK_RMENU] & 0x80) != 0;

            InterceptionContext context = IntPtr.Zero;
            InterceptionDevice  device;

            WinAPI.SetThreadPriority(WinAPI.GetCurrentThread(), WinAPI.ThreadPriority.THREAD_PRIORITY_TIME_CRITICAL);

            context = Lib.interception_create_context();
            Console.WriteLine("Ctx: " + context);
            Lib.interception_set_filter_keyboard(context, Lib.InterceptionFilterKeyState.INTERCEPTION_FILTER_KEY_ALL);

            Lib.InterceptionKeyStroke[] rawKeys = new Lib.InterceptionKeyStroke[1];

            string    keys             = "";
            VK        vk               = 0x00;
            Stopwatch holdInSW         = new Stopwatch();
            Stopwatch holdInIntervalSW = new Stopwatch();

            holdInSW.Start();
            holdInIntervalSW.Start();

            while (Lib.interception_receive_keyboard(context, device = Lib.interception_wait(context), rawKeys, 1) > 0)
            {
                var key = rawKeys.First();
                if (key.state.HasFlag(Lib.InterceptionKeyState.INTERCEPTION_KEY_UP) && key.code == 0x54)
                {
                    if (!Base.TheBox)
                    {
                        Base.InitShow();
                    }
                    else
                    {
                        Base.Exit();
                    }
                    continue;
                }

                Lib.interception_send_keyboard(context, device, rawKeys, 1);

                holdInSW.Restart();

                var isPressed = !key.state.HasFlag(Lib.InterceptionKeyState.INTERCEPTION_KEY_UP);

                if (key.state.HasFlag(Lib.InterceptionKeyState.INTERCEPTION_KEY_E0))
                {
                    E0 = isPressed;
                }
                if (key.state.HasFlag(Lib.InterceptionKeyState.INTERCEPTION_KEY_E1))
                {
                    E1 = isPressed;
                }

                var scancode = key.code;

                if (holdInSW.Elapsed > PressedInHoldTime && isPressed)
                {
                    if (holdInIntervalSW.Elapsed > PressedInInterval)
                    {
                        HandleKey(keys, vk, key.code, isPressed);
                        holdInIntervalSW.Restart();
                    }
                }

                if (isPressed)
                {
                    switch (scancode)
                    {
                    case 58: CapsLock = !CapsLock;              break;

                    case 69: NumLock = !NumLock;               break;

                    case 70: ScrollLock = !ScrollLock;    break;
                    }
                }
                switch (scancode)
                {
                case 54:        RShift = isPressed; break;

                case 42:        LShift = isPressed; break;

                case 29:        LCtrl = isPressed; break;

                case 157:       RCtrl = isPressed; break;

                case 56:        LAlt = isPressed; break;

                case 184:       RAlt = isPressed; break;
                }

                Shift = LShift || RShift;
                Ctrl  = LCtrl || RCtrl;
                Alt   = LAlt || RAlt;

                if (CapsLock)
                {
                    keyState[(int)VK.VK_CAPITAL] = 0x01;
                }
                else
                {
                    keyState[(int)VK.VK_CAPITAL] = 0x00;
                }
                if (NumLock)
                {
                    keyState[(int)VK.VK_NUMLOCK] = 0x01;
                }
                else
                {
                    keyState[(int)VK.VK_NUMLOCK] = 0x00;
                }
                if (ScrollLock)
                {
                    keyState[(int)VK.VK_SCROLL] = 0x01;
                }
                else
                {
                    keyState[(int)VK.VK_SCROLL] = 0x00;
                }
                if (Shift)
                {
                    keyState[(int)VK.VK_SHIFT] = 0x80;
                }
                else
                {
                    keyState[(int)VK.VK_SHIFT] = 0x00;
                }
                if (Ctrl)
                {
                    keyState[(int)VK.VK_CONTROL] = 0x80;
                }
                else
                {
                    keyState[(int)VK.VK_CONTROL] = 0x00;
                }
                if (Alt)
                {
                    keyState[(int)VK.VK_MENU] = 0x80;
                }
                else
                {
                    keyState[(int)VK.VK_MENU] = 0x00;
                }

                keys = ScancodeToUnicode(scancode, keyState, E0);
                vk   = ScancodeToVKCode(scancode, NumLock, Ctrl, Shift, E0, E1);

                HandleKey(keys, vk, scancode, isPressed);

                //Render.ImportantMessage.Instance.AddMessageTimeout("keys: " + keys + "VK: " + vk + " Scancode " + scancode + " pressed? " + (isPressed ? "true" : "false"), TimeSpan.FromSeconds(1));
            }

            Render.ImportantMessage.Instance.AddMessageTimeout("Interception lost device handle", TimeSpan.FromMinutes(1));
            Lib.interception_destroy_context(context);
        }
Пример #33
0
 public void OnError(InterceptionContext context)
 {
 }
Пример #34
0
 private static IntPtr GetAtomicLongPtr(InterceptionContext c)
 {
     var atomicLong = (AtomicLong) c.Sender;
     var ptr = (IntPtr) (**(long**) &atomicLong);
     return ptr;
 }
Пример #35
0
 public object OnOverride(InterceptionContext context)
 {
     Console.WriteLine("OnOverride > MyInterceptor !");
     return(null);
 }
Пример #36
0
 public LoggingInterceptorSteps(LoggingContext logging, InterceptionContext interception, ErrorContext errors)
 {
     _logging      = logging;
     _interception = interception;
     _errors       = errors;
 }
 public object OnOverride(InterceptionContext context)
 {
     Console.WriteLine("ProgramTypedInterceptor > OnOverride");
     return null;
 }
Пример #38
0
 public static extern InterceptionPrecedence interception_get_precedence(InterceptionContext context, InterceptionDevice device);
Пример #39
0
 public InterceptPipeline(InterceptionContext interceptionContext, Func <Task> inner)
 {
     _interceptionContext = interceptionContext;
     _inner = inner;
 }
Пример #40
0
 public static extern InterceptionFilter interception_get_filter(InterceptionContext context, InterceptionDevice device);
 public void OnBeforeExecute(InterceptionContext context)
 {
 }
Пример #42
0
 public static void interception_set_filter_mouse(InterceptionContext context, InterceptionFilterMouseState filter) =>
 interception_set_filter(context, Marshal.GetFunctionPointerForDelegate(new InterceptionPredicate(interception_is_mouse)), (InterceptionFilter)filter);
Пример #43
0
 public bool CanHandle(InterceptionContext context)
 {
     return(true);
 }
Пример #44
0
 public static extern InterceptionDevice interception_wait(InterceptionContext context);
        public object OnOverride(InterceptionContext context)
        {
            lock (_lock)
            {
                object result = null;
                try
                {
                    result = context.InterceptedMethod.Execute(
                        context.Sender,
                        context.Parameters.Any()
                            ? context.Parameters.Where(p => p.Name != "StartTimeStamp").Select(x => x.Value)
                                .ToArray()
                            : null);
                }
                catch (Exception)
                {
                    int counter = 1;

                    if (_exceptionCounter.ContainsKey(context.InterceptedMethod.Name))
                    {
                        _exceptionCounter.TryGetValue(context.InterceptedMethod.Name, out counter);
                        _exceptionCounter.Remove(context.InterceptedMethod.Name);
                        counter += 1;

                    }
                    _exceptionCounter.Add(context.InterceptedMethod.Name, counter);

                    Metrics.Counter(
                        string.Format(
                            "{0}.{1}",
                            context.InterceptedMethod.Name,
                            "num_errors"),
                        counter);
                }

                return result;
            }
        }
Пример #46
0
 public void OnBeforeExecute(InterceptionContext context)
 {
     Console.WriteLine("OnBeforeExecute > MyInterceptor ! ");
 }
 public void OnBeforeExecute(InterceptionContext context)
 {
     Console.WriteLine("InterceptorForContainer > OnBeforeExecute");
 }
        public void OnError(InterceptionContext context)
        {

        }
Пример #49
0
 protected void AfterInterceptor(InterceptionContext context)
 {
     OnInterceptorCalled(InterceptionMode.AfterBody, context);
 }
 public void OnBeforeExecute(InterceptionContext context)
 {
     Console.WriteLine("ProgramTypedInterceptor > OnBeforeExecute");
 }
Пример #51
0
 protected void BeforeInterceptor(InterceptionContext context)
 {
     OnInterceptorCalled(InterceptionMode.BeforeBody, context);
 }
 public void OnAfterExecute(InterceptionContext context)
 {
 }
Пример #53
0
 protected void InsteadInterceptor(InterceptionContext context)
 {
     context.SetResult(context.ExecuteBody());
     OnInterceptorCalled(InterceptionMode.InsteadOfBody, context);
 }
 public IEnumerable<Type> InterceptorsToApply(InterceptionContext ctx)
 {
     return _interceptorMetas.Where(x => x.Where(ctx)).Select(x=> x.Interceptor).Distinct();
 }
Пример #55
0
 public void OnBeforeExecute(InterceptionContext context)
 {
     Console.WriteLine("ProgramTypedInterceptor > OnBeforeExecute");
 }