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 }); }
public static extern void interception_set_precedence(InterceptionContext context, InterceptionDevice device, InterceptionPrecedence precedence);
public static extern void interception_destroy_context(InterceptionContext context);
private void InsteadOfBodyInterceptor(InterceptionContext obj) { obj.SetResult((string)obj.ExecuteBody() + "Result"); }
public void OnAfterExecute(InterceptionContext context) { }
/// <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)); }
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; }
public void OnBeforeExecute(InterceptionContext context) { Console.WriteLine("InterceptorForContainer > OnBeforeExecute"); }
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"]); }
public void OnBeforeExecute(InterceptionContext context) { Console.WriteLine("OnBeforeExecute > MyInterceptor ! "); }
private static extern void interception_set_filter(InterceptionContext context, IntPtr predicate, InterceptionFilter filter);
public static void interception_set_filter_keyboard(InterceptionContext context, InterceptionFilterKeyState filter) => interception_set_filter(context, Marshal.GetFunctionPointerForDelegate(new InterceptionPredicate(interception_is_keyboard)), (InterceptionFilter)filter);
public InvocationSteps(InterceptionContext context, MoqContext moq) { _context = context; _moq = moq; }
public object OnOverride(InterceptionContext context) { Console.WriteLine("OnOverride > MyInterceptor !"); return null; }
public void OnBeforeExecute(InterceptionContext context) { _step = _profiler.Step(context.InterceptedMethod.Name); }
protected abstract object OnInterceptMethod(InterceptionContext context);
protected override void OnBeforeInsert(DbEntityEntry entry, Entity <Guid> item, InterceptionContext context) { if (item.IsNew) { item.Id = SequentialGuidGenerator.Generate(); } base.OnBeforeInsert(entry, item, context); }
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); }
public void OnError(InterceptionContext context) { }
private static IntPtr GetAtomicLongPtr(InterceptionContext c) { var atomicLong = (AtomicLong) c.Sender; var ptr = (IntPtr) (**(long**) &atomicLong); return ptr; }
public object OnOverride(InterceptionContext context) { Console.WriteLine("OnOverride > MyInterceptor !"); return(null); }
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; }
public static extern InterceptionPrecedence interception_get_precedence(InterceptionContext context, InterceptionDevice device);
public InterceptPipeline(InterceptionContext interceptionContext, Func <Task> inner) { _interceptionContext = interceptionContext; _inner = inner; }
public static extern InterceptionFilter interception_get_filter(InterceptionContext context, InterceptionDevice device);
public void OnBeforeExecute(InterceptionContext context) { }
public static void interception_set_filter_mouse(InterceptionContext context, InterceptionFilterMouseState filter) => interception_set_filter(context, Marshal.GetFunctionPointerForDelegate(new InterceptionPredicate(interception_is_mouse)), (InterceptionFilter)filter);
public bool CanHandle(InterceptionContext context) { return(true); }
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; } }
protected void AfterInterceptor(InterceptionContext context) { OnInterceptorCalled(InterceptionMode.AfterBody, context); }
public void OnBeforeExecute(InterceptionContext context) { Console.WriteLine("ProgramTypedInterceptor > OnBeforeExecute"); }
protected void BeforeInterceptor(InterceptionContext context) { OnInterceptorCalled(InterceptionMode.BeforeBody, context); }
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(); }