Пример #1
0
        public void AsyncTimer_PollInterval()
        {
            DateTime     start = SysTime.Now;
            object       state;
            IAsyncResult ar;

            ar    = AsyncTimer.BeginTimer(TimeSpan.FromSeconds(1), null, "4");
            state = ar.AsyncState;

            AsyncTimer.EndTimer(ar);

            Assert.IsTrue(SysTime.Now - start >= TimeSpan.FromSeconds(1) - SysTime.Resolution);
            Assert.AreEqual("4", state);

            AsyncTimer.PollInterval = TimeSpan.FromSeconds(1);

            start = SysTime.Now;
            ar    = AsyncTimer.BeginTimer(TimeSpan.FromSeconds(1), null, "5");
            state = ar.AsyncState;

            AsyncTimer.EndTimer(ar);

            Assert.IsTrue(SysTime.Now - start >= TimeSpan.FromSeconds(1) - SysTime.Resolution);
            Assert.AreEqual("5", state);
        }
Пример #2
0
        private void Initialize()
        {
            // Basic initialization

            ChannelHost.Start();
            hostStarted = true;

            channels     = new Dictionary <string, TInternal>();
            channelQueue = new LimitedQueue <TInternal>(maxAcceptedChannels);
            acceptQueue  = new Queue <AsyncResult <TInternal, object> >();
            waitQueue    = new Queue <AsyncResult <bool, object> >();

            // Register the endpoint with the router.

            SessionHandlerInfo sessionInfo;

            sessionInfo = this.GetSessionHandlerInfo();
            sessionMode = sessionInfo != null && sessionInfo.SessionType == typeof(DuplexSession);

            if (sessionMode)
            {
                ChannelHost.Router.Dispatcher.AddLogical(new MsgHandlerDelegate(OnReceive), ep, typeof(DuplexSessionMsg), false, sessionInfo);
            }
            else
            {
                ChannelHost.Router.Dispatcher.AddLogical(new MsgHandlerDelegate(OnReceive), ep, typeof(WcfEnvelopeMsg), false, sessionInfo);
            }

            // Start the background task timer

            arBkTimer = AsyncTimer.BeginTimer(bkTaskInterval, onBkTask, null);
        }
Пример #3
0
        public void AsyncTimer_CancelAll()
        {
            DateTime     start = SysTime.Now;
            IAsyncResult ar1, ar2;

            ar1 = AsyncTimer.BeginTimer(TimeSpan.FromSeconds(20), null, null);
            ar2 = AsyncTimer.BeginTimer(TimeSpan.FromSeconds(30), null, null);
            Thread.Sleep(5000);
            AsyncTimer.CancelTimer(ar1);
            AsyncTimer.CancelTimer(ar2);

            try
            {
                AsyncTimer.EndTimer(ar1);
                Assert.Fail("Expected a CancelException");
                AsyncTimer.EndTimer(ar2);
                Assert.Fail("Expected a CancelException");
            }
            catch (CancelException)
            {
            }

            Assert.IsTrue(SysTime.Now >= start + TimeSpan.FromSeconds(5) - SysTime.Resolution);
            Assert.IsTrue(SysTime.Now <= start + TimeSpan.FromSeconds(6) - SysTime.Resolution);
        }
Пример #4
0
        /// <summary>
        /// Internal background task method.
        /// </summary>
        /// <param name="state">Not used.</param>
        private void OnBkTask(object state)
        {
            DateTime now = SysTime.Now;

            AsyncTimer.EndTimer(arBkTimer);
            using (TimedLock.Lock(this))
            {
                try
                {
                    // Give derived classes a chance to perform any necessary
                    // background tasks.

                    OnBkTask();
                }
                catch (Exception e)
                {
                    SysLog.LogException(e);
                }
                finally
                {
                    // Schedule the next timer if this functionality is
                    // still enabled.

                    if (onBkTask != null)
                    {
                        arBkTimer = AsyncTimer.BeginTimer(bkTaskInterval, onBkTask, null);
                    }
                }
            }
        }
Пример #5
0
        public void AsyncTimer_Callback()
        {
            DateTime start = SysTime.Now;

            fireTime = start;
            AsyncTimer.BeginTimer(TimeSpan.FromSeconds(1), new AsyncCallback(OnTimer), "1");
            Thread.Sleep(2000);

            Assert.IsTrue(SysTime.Now - start >= TimeSpan.FromSeconds(1) - SysTime.Resolution);
            Assert.AreEqual("1", state);
        }
Пример #6
0
        /// <summary>
        /// Internal channel initialization.
        /// </summary>
        private void Initialize()
        {
            using (TimedLock.Lock(this))
            {
                // Start the background task timer if required.

                bkTaskInterval = GetBackgroundTaskInterval();
                if (bkTaskInterval > TimeSpan.Zero)
                {
                    arBkTimer = AsyncTimer.BeginTimer(bkTaskInterval, onBkTask, null);
                }
            }
        }
Пример #7
0
        public void AsyncTimer_Implicit_Wait()
        {
            DateTime     start = SysTime.Now;
            object       state;
            IAsyncResult ar;

            ar    = AsyncTimer.BeginTimer(TimeSpan.FromSeconds(1), null, "3");
            state = ar.AsyncState;

            AsyncTimer.EndTimer(ar);

            Assert.IsTrue(SysTime.Now - start >= TimeSpan.FromSeconds(1) - SysTime.Resolution);
            Assert.AreEqual("3", state);
        }
Пример #8
0
        /// <summary>
        /// Executed on a worker thread to look for timed-out accept and wait requests.
        /// </summary>
        /// <param name="state">Not used.</param>
        private void OnBkTask(object state)
        {
            DateTime now = SysTime.Now;

            AsyncTimer.EndTimer(arBkTimer);
            using (TimedLock.Lock(this))
            {
                try
                {
                    // Give derived classes a chance to perform any necessary
                    // background tasks.

                    OnBkTask();

                    // Scan the queued accept requests for any that have timed-out.

                    if (acceptQueue != null && acceptQueue.Count > 0)
                    {
                        do
                        {
                            AsyncResult <TInternal, object> arAccept;

                            arAccept = acceptQueue.Peek();
                            if (arAccept.TTD <= now)
                            {
                                acceptQueue.Dequeue();
                                arAccept.Notify(new TimeoutException());
                            }
                            else
                            {
                                break;
                            }
                        } while (acceptQueue.Count > 0);
                    }

                    // Scan the queued wait requests for any that have timed-out.

                    if (waitQueue != null && waitQueue.Count > 0)
                    {
                        do
                        {
                            AsyncResult <bool, object> arWait;

                            arWait = waitQueue.Peek();
                            if (arWait.TTD <= now)
                            {
                                waitQueue.Dequeue();
                                arWait.Notify(new TimeoutException());
                            }
                            else
                            {
                                break;
                            }
                        } while (waitQueue.Count > 0);
                    }
                }
                catch (Exception e)
                {
                    SysLog.LogException(e);
                }
                finally
                {
                    // Schedule the next timer if this functionality is
                    // still enabled.

                    if (onBkTask != null)
                    {
                        arBkTimer = AsyncTimer.BeginTimer(bkTaskInterval, onBkTask, null);
                    }
                }
            }
        }
Пример #9
0
        public void SipBasicCore_Request_ReplyAsync()
        {
            // Verify that we can submit a non-dialog request from
            // one core to another, respond to it asynchronously and
            // then verify that we actually received the response.

            StartCores();

            try
            {
                SipResponse core1Response = null;

                core1.ResponseReceived += delegate(object sender, SipResponseEventArgs args)
                {
                    core1Response = args.Response;
                };

                SipRequest core2Request = null;

                core2.RequestReceived += delegate(object sender, SipRequestEventArgs args)
                {
                    SipResponse reply;

                    core2Request   = args.Request;
                    reply          = core2Request.CreateResponse(SipStatus.OK, "Hello World!");
                    reply.Contents = new byte[] { 5, 6, 7, 8 };

                    args.WillRespondAsynchronously = true;
                    AsyncCallback callback = delegate(IAsyncResult ar)
                    {
                        AsyncTimer.EndTimer(ar);
                        args.Transaction.SendResponse(reply);
                    };

                    AsyncTimer.BeginTimer(TimeSpan.FromMilliseconds(100), callback, null);
                };

                // Submit a request and verify the response.

                SipRequest  request;
                SipResult   result;
                SipResponse response;

                request          = new SipRequest(SipMethod.Info, (string)core2Uri, null);
                request.Contents = new byte[] { 1, 2, 3, 4 };

                result   = core1.Request(request);
                response = result.Response;
                Assert.AreEqual(SipStatus.OK, result.Status);
                Assert.AreEqual(SipStatus.OK, response.Status);
                Assert.AreEqual("Hello World!", response.ReasonPhrase);
                CollectionAssert.AreEqual(new byte[] { 5, 6, 7, 8 }, response.Contents);

                // Verify that the core1 ResponseReceived event handler was called

                Assert.IsNotNull(core1Response);
                Assert.AreEqual(SipStatus.OK, core1Response.Status);
                Assert.AreEqual("Hello World!", core1Response.ReasonPhrase);
                CollectionAssert.AreEqual(new byte[] { 5, 6, 7, 8 }, core1Response.Contents);

                // Verify that the core2 RequestReceived event handler was called

                Assert.IsNotNull(core2Request);
                Assert.AreEqual(SipMethod.Info, core2Request.Method);
                CollectionAssert.AreEqual(new byte[] { 1, 2, 3, 4 }, core2Request.Contents);
            }
            finally
            {
                StopCores();
            }
        }
Пример #10
0
 public void OnAsyncMsg_Infinite(AsyncMsg msg)
 {
     AsyncTimer.BeginTimer(TimeSpan.FromSeconds(20), new AsyncCallback(OnAsyncDone), new AsyncState(msg._Session.Router, msg._Session, msg));
 }