Exemplo n.º 1
0
        public void Capture()
        {
            SecurityContext sc = SecurityContext.Capture();

            Assert.IsNotNull(sc, "Capture");

            AsyncFlowControl afc = SecurityContext.SuppressFlow();

            Assert.IsTrue(SecurityContext.IsFlowSuppressed(), "IsFlowSuppressed-1");
            try
            {
                sc = SecurityContext.Capture();
                Assert.IsNull(sc, "Capture with SuppressFlow");
            }
            finally
            {
                afc.Undo();
            }

            afc = SecurityContext.SuppressFlowWindowsIdentity();
            Assert.IsTrue(SecurityContext.IsWindowsIdentityFlowSuppressed(), "IsWindowsIdentityFlowSuppressed-1");
            try
            {
                sc = SecurityContext.Capture();
                Assert.IsNotNull(sc, "Capture with SuppressFlowWindowsIdentity");
            }
            finally
            {
                afc.Undo();
            }
        }
Exemplo n.º 2
0
        public void IsWindowsIdentityFlowSuppressed()
        {
            Assert.IsFalse(SecurityContext.IsWindowsIdentityFlowSuppressed(), "IsWindowsIdentityFlowSuppressed-1");

            AsyncFlowControl afc = SecurityContext.SuppressFlow();

            Assert.IsTrue(SecurityContext.IsWindowsIdentityFlowSuppressed(), "IsWindowsIdentityFlowSuppressed-2");
            afc.Undo();

            afc = SecurityContext.SuppressFlowWindowsIdentity();
            Assert.IsTrue(SecurityContext.IsWindowsIdentityFlowSuppressed(), "IsWindowsIdentityFlowSuppressed-3");
            afc.Undo();

            Assert.IsFalse(SecurityContext.IsWindowsIdentityFlowSuppressed(), "IsWindowsIdentityFlowSuppressed-4");
        }
Exemplo n.º 3
0
        public ReusableTask <int> SendAsync(SocketMemory buffer)
        {
            SocketAsyncEventArgs args = GetSocketAsyncEventArgs(buffer);

            args.UserToken = SendTcs;

            AsyncFlowControl?control = null;

            if (!ExecutionContext.IsFlowSuppressed())
            {
                control = ExecutionContext.SuppressFlow();
            }

            try {
                if (!Socket.SendAsync(args))
                {
                    SendTcs.SetResult(buffer.Length);
                }
            } catch (ObjectDisposedException) {
                SendTcs.SetResult(0);
            } finally {
                control?.Undo();
            }

            return(SendTcs.Task);
        }
Exemplo n.º 4
0
        static void Main()
        {
            // Помещаем данные в контекст логического вызова потока метода Main
            CallContext.LogicalSetData("Name", "Denny");

            // Заставляем поток из пула работать
            // Поток из пула имеет доступ к данным контекта логического вызова
            ThreadPool.QueueUserWorkItem(
                state => Console.WriteLine("Name={0}", CallContext.LogicalGetData("Name")));

            // Запрещаем копирование контекста исполнения потока метода Main
            AsyncFlowControl asyncFlowControl = ExecutionContext.SuppressFlow();

            // Заставляем поток из пула работать
            // Поток из пула теперь НЕ имеет доступа к данным контекста логического вызова
            ThreadPool.QueueUserWorkItem(
                state => Console.WriteLine("Name={0}", CallContext.LogicalGetData("Name")));

            // Восстанавливаем копирование контекста исполнения потока метода Main
            // на случай будущей работы с другими потоками из пула
            asyncFlowControl.Undo(); // Или так: ExecutionContext.RestoreFlow();

            ThreadPool.QueueUserWorkItem(
                state => Console.WriteLine("Name={0}", CallContext.LogicalGetData("Name")));

            Console.ReadKey();
        }
Exemplo n.º 5
0
        public ReusableTask <int> ReceiveAsync(ByteBuffer buffer, int offset, int count)
        {
            // If this has been disposed, then bail out
            if (Socket == null)
            {
                ReceiveTcs.SetResult(0);
                return(ReceiveTcs.Task);
            }

            SocketAsyncEventArgs args = GetSocketAsyncEventArgs(buffer);

            args.SetBuffer(offset, count);
            args.UserToken = ReceiveTcs;

#if ALLOW_EXECUTION_CONTEXT_SUPPRESSION
            AsyncFlowControl?control = null;
            if (!ExecutionContext.IsFlowSuppressed())
            {
                control = ExecutionContext.SuppressFlow();
            }
#endif

            try {
                if (!Socket.ReceiveAsync(args))
                {
                    ReceiveTcs.SetResult(args.BytesTransferred);
                }
            } finally {
#if ALLOW_EXECUTION_CONTEXT_SUPPRESSION
                control?.Undo();
#endif
            }

            return(ReceiveTcs.Task);
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            foreach (string arg in args)
            {
                string lowerArg = arg.ToLower();
                if (lowerArg == "/test" || lowerArg == "/t" || lowerArg == "-test" || lowerArg == "-t")
                {
                    Settings.TestMode = true;
                }
            }

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            AsyncFlowControl flowControl = ExecutionContext.SuppressFlow();

            OperationalTrace.WriteInformation("starting up operational");

            Console.SetWindowSize(Math.Min(130, Console.LargestWindowWidth), Math.Min(50, Console.LargestWindowHeight));
            Console.SetBufferSize(Math.Min(130, Console.LargestWindowWidth), 999);

            OperationalBuilder.Build();

            Console.Write("operational> ");

            flowControl.Undo();

            while (Console.ReadLine() != "quit")
            {
            }
        }
Exemplo n.º 7
0
        public ReusableTask <int> SendAsync(ByteBuffer buffer, int offset, int count)
        {
            SocketAsyncEventArgs args = GetSocketAsyncEventArgs(buffer);

            args.SetBuffer(offset, count);
            args.UserToken = SendTcs;

#if ALLOW_EXECUTION_CONTEXT_SUPPRESSION
            AsyncFlowControl?control = null;
            if (!ExecutionContext.IsFlowSuppressed())
            {
                control = ExecutionContext.SuppressFlow();
            }
#endif

            try {
                if (!Socket.SendAsync(args))
                {
                    SendTcs.SetResult(count);
                }
            } catch (ObjectDisposedException) {
                SendTcs.SetResult(0);
            } finally {
#if ALLOW_EXECUTION_CONTEXT_SUPPRESSION
                control?.Undo();
#endif
            }

            return(SendTcs.Task);
        }
Exemplo n.º 8
0
        public void SuppressFlow()
        {
            Assert.IsFalse(ExecutionContext.IsFlowSuppressed(), "IsFlowSuppressed-1");

            AsyncFlowControl afc = ExecutionContext.SuppressFlow();

            Assert.IsTrue(ExecutionContext.IsFlowSuppressed(), "IsFlowSuppressed-3");
            afc.Undo();

            Assert.IsFalse(ExecutionContext.IsFlowSuppressed(), "IsFlowSuppressed-4");
        }
Exemplo n.º 9
0
        public void Run_SuppressFlow()
        {
            Assert.IsFalse(SecurityContext.IsFlowSuppressed());
            AsyncFlowControl afc = SecurityContext.SuppressFlow();

            Assert.IsTrue(SecurityContext.IsFlowSuppressed());
            try {
                SecurityContext.Run(SecurityContext.Capture(), new ContextCallback(Callback), "Hello world.");
            }
            finally {
                afc.Undo();
            }
        }
Exemplo n.º 10
0
        public void ExecutionContext_SuppressFlow()
        {
            Assert.IsFalse(ExecutionContext.IsFlowSuppressed(), "IsFlowSuppressed-1");
            AsyncFlowControl afc = ExecutionContext.SuppressFlow();

            try {
                Assert.IsTrue(ExecutionContext.IsFlowSuppressed(), "IsFlowSuppressed-2");
                Assert.IsFalse(SecurityContext.IsFlowSuppressed(), "IsFlowSuppressed-3");
                Assert.IsNotNull(SecurityContext.Capture(), "Capture");
            }
            finally {
                afc.Undo();
            }
        }
Exemplo n.º 11
0
        public void Capture()
        {
            ExecutionContext ec = ExecutionContext.Capture();

            Assert.IsNotNull(ec, "Capture");

            AsyncFlowControl afc = ExecutionContext.SuppressFlow();

            Assert.IsTrue(ExecutionContext.IsFlowSuppressed(), "IsFlowSuppressed-1");
            try {
                ec = ExecutionContext.Capture();
                Assert.IsNull(ec, "Capture with SuppressFlow");
            }
            finally {
                afc.Undo();
            }
        }
Exemplo n.º 12
0
        public void SuppressFlow_Two_Undo()
        {
            Assert.IsFalse(ExecutionContext.IsFlowSuppressed(), "IsFlowSuppressed-1");

            AsyncFlowControl afc = ExecutionContext.SuppressFlow();

            Assert.IsTrue(ExecutionContext.IsFlowSuppressed(), "IsFlowSuppressed-2");

            AsyncFlowControl afc2 = ExecutionContext.SuppressFlow();

            Assert.IsTrue(ExecutionContext.IsFlowSuppressed(), "IsFlowSuppressed-3");
            afc2.Undo();

            // note: afc2 Undo return to the original (not the previous) state
            Assert.IsFalse(ExecutionContext.IsFlowSuppressed(), "IsFlowSuppressed-4");

            // we can't use the first AsyncFlowControl
            afc.Undo();
        }
Exemplo n.º 13
0
        public void SuppressFlow_Both()
        {
            Assert.IsFalse(SecurityContext.IsFlowSuppressed(), "IsFlowSuppressed-1");
            Assert.IsFalse(SecurityContext.IsWindowsIdentityFlowSuppressed(), "IsWindowsIdentityFlowSuppressed-1");

            AsyncFlowControl afc = SecurityContext.SuppressFlowWindowsIdentity();

            Assert.IsFalse(SecurityContext.IsFlowSuppressed(), "IsFlowSuppressed-2");
            Assert.IsTrue(SecurityContext.IsWindowsIdentityFlowSuppressed(), "IsWindowsIdentityFlowSuppressed-2");

            AsyncFlowControl afc2 = SecurityContext.SuppressFlow();

            Assert.IsTrue(SecurityContext.IsFlowSuppressed(), "IsFlowSuppressed-2");
            Assert.IsTrue(SecurityContext.IsWindowsIdentityFlowSuppressed(), "IsWindowsIdentityFlowSuppressed-2");
            afc2.Undo();

            // note: afc2 Undo return to the original (not the previous) state
            Assert.IsFalse(SecurityContext.IsFlowSuppressed(), "IsFlowSuppressed-2");
            Assert.IsFalse(SecurityContext.IsWindowsIdentityFlowSuppressed(), "IsWindowsIdentityFlowSuppressed-2");
        }
Exemplo n.º 14
0
        private void Thread_Run_UnmanagedCode_SuppressFlow_AfterCapture()
        {
            bool result = false;

            Assert.IsFalse(success, "pre-check");
            SecurityContext  sc  = GetSecurityContextUnmanaged();
            AsyncFlowControl afc = SecurityContext.SuppressFlow();

            try {
                SecurityContext.Run(sc, new ContextCallback(Callback), true);
            }
            catch (SecurityException) {
                result = true;
            }
            finally {
                Assert.IsFalse(success, "post-check");
                afc.Undo();
                Assert.IsTrue(result, "Result");
            }
        }
Exemplo n.º 15
0
        private void Thread_Run_UnmanagedCode_SuppressFlow_BeforeCapture()
        {
            bool result = false;

            Assert.IsFalse(success, "pre-check");
            AsyncFlowControl afc = ExecutionContext.SuppressFlow();

            try {
                ExecutionContext ec = GetExecutionContextUnmanaged();
                ExecutionContext.Run(ec, new ContextCallback(Callback), true);
            }
            catch (InvalidOperationException) {
                result = true;
            }
            finally {
                Assert.IsFalse(success, "post-check");
                afc.Undo();
                Assert.IsTrue(result, "Result");
            }
        }
        private static T EscapeExecutionContext <T>(Func <T> callback)
        {
            AsyncFlowControl suppressExecutionContextFlow = ExecutionContext.SuppressFlow();
            T retval2;

            try
            {
                T         retval          = default(T);
                Exception threadException = null;
                Thread    thread          = new Thread(new ThreadStart(delegate
                {
                    try
                    {
                        try
                        {
                            SCryptAlgorithm.SafeSetPrincipal(null);
                            retval = callback();
                        }
                        catch (Exception e)
                        {
                            threadException = e;
                        }
                    }
                    catch
                    {
                    }
                }));
                thread.Start();
                thread.Join();
                if (threadException != null)
                {
                    throw new TargetInvocationException(threadException);
                }
                retval2 = retval;
            }
            finally
            {
                suppressExecutionContextFlow.Undo();
            }
            return(retval2);
        }
Exemplo n.º 17
0
        static void Main(string[] args)
        {
            new Thread(() =>
            {
                Console.WriteLine("i-0:" + _localThread.Value);
            }).Start();


            new Thread(() =>
            {
                Console.WriteLine("i-1:" + _localThread.Value);
            }).Start();
            AsyncFlowControl aFC = ExecutionContext.SuppressFlow();

            Console.WriteLine("Is the flow suppressed? " +
                              ExecutionContext.IsFlowSuppressed());
            Console.WriteLine("Restore the flow.");
            aFC.Undo();
            Console.WriteLine("Is the flow suppressed? " +
                              ExecutionContext.IsFlowSuppressed());

            Console.ReadKey();
        }
        public ReusableTask <int> SendAsync(Memory <byte> buffer)
        {
            if (Socket is null)
            {
                throw new InvalidOperationException("The underlying socket is not connected");
            }

            if (SendArgs == null)
            {
                SendArgs            = new SocketAsyncEventArgs();
                SendArgs.Completed += Handler;
                SendArgs.UserToken  = SendTcs;
            }
            SetBuffer(SendArgs, buffer);

            SocketAsyncEventArgs args = SendArgs;

            AsyncFlowControl?control = null;

            if (!ExecutionContext.IsFlowSuppressed())
            {
                control = ExecutionContext.SuppressFlow();
            }

            try {
                if (!Socket.SendAsync(args))
                {
                    SendTcs.SetResult(buffer.Length);
                }
            } catch (ObjectDisposedException) {
                SendTcs.SetResult(0);
            } finally {
                control?.Undo();
            }

            return(SendTcs.Task);
        }
Exemplo n.º 19
0
 public void Dispose()
 {
     _undoer.Undo();
 }