Пример #1
0
        public void TestInstanceMethods()
        {
            var handler1 = new MethodHandler<Event>(intBox1.Increment);
            var handler2 = new MethodHandler<Event>(intBox1.Increment);
            var handler3 = new MethodHandler<Event>(intBox1.Decrement);
            var handler4 = new MethodHandler<Event>(intBox2.Decrement);

            // Properties
            Assert.True(handler1.Action.Equals(handler2.Action));
            Assert.False(handler2.Action.Equals(handler3.Action));
            Assert.False(handler3.Action.Equals(handler4.Action));

            // Invocation
            Event e = new Event();
            handler1.Invoke(e);
            Assert.AreEqual(1, intBox1.Value);
            handler2.Invoke(e);
            Assert.AreEqual(2, intBox1.Value);
            handler3.Invoke(e);
            Assert.AreEqual(1, intBox1.Value);
            Assert.AreEqual(0, intBox2.Value);
            handler4.Invoke(e);
            Assert.AreEqual(1, intBox1.Value);
            Assert.AreEqual(-1, intBox2.Value);

            // Equality
            Assert.True(handler1.Equals(handler2));
            Assert.False(handler2.Equals(handler3));
            Assert.False(handler3.Equals(handler4));
        }
Пример #2
0
        protected WaitForMultipleEvents(Coroutine coroutine, Event[] requests,
            double seconds, params Event[] e)
        {
            this.coroutine = coroutine;

            if (!Object.ReferenceEquals(requests, null))
            {
                waitHandle = WaitHandlePool.Acquire();
                for (int i = 0, count = requests.Length; i < count; ++i)
                {
                    requests[i]._WaitHandle = waitHandle;
                }
                for (int i = 0, count = e.Length; i < count; ++i)
                {
                    e[i]._WaitHandle = waitHandle;
                }
            }

            expected = e;
            actual = new Event[expected.Length];

            handlerTokens = new Binder.Token[expected.Length];
            for (int i = 0; i < expected.Length; ++i)
            {
                handlerTokens[i] = Flow.Bind(expected[i], OnEvent);
            }

            if (seconds > 0)
            {
                TimeoutEvent timeoutEvent = new TimeoutEvent { Key = this };
                timeoutToken = Flow.Bind(timeoutEvent, OnTimeout);
                timerToken = TimeFlow.Default.Reserve(timeoutEvent, seconds);
            }
        }
Пример #3
0
        void OnEvent(Event e)
        {
            for (int i = 0; i < expected.Length; ++i)
            {
                if (actual[i] == null && expected[i].Equivalent(e))
                {
                    Flow.Unbind(handlerTokens[i]);
                    handlerTokens[i] = new Binder.Token();
                    actual[i] = e;
                    ++count;
                    break;
                }
            }

            if (count >= expected.Length)
            {
                if (timerToken.HasValue)
                {
                    TimeFlow.Default.Cancel(timerToken.Value);
                    Flow.Unbind(timeoutToken);
                }

                if (waitHandle != 0)
                {
                    WaitHandlePool.Release(waitHandle);
                }

                coroutine.Result = actual;
                coroutine.Continue();
            }
        }
Пример #4
0
 public override void Feed(Event e)
 {
     if (queue != null)
     {
         queue.Enqueue(e);
     }
 }
Пример #5
0
 /// <summary>
 /// Broadcasts the specified event to all the connected clients.
 /// </summary>
 public void Broadcast(Event e)
 {
     List<LinkSession> snapshot = TakeSessionsSnapshot();
     for (int i = 0, count = snapshot.Count; i < count; ++i)
     {
         snapshot[i].Send(e);
     }
 }
Пример #6
0
 public override void Close(Event finalItem)
 {
     lock (queue)
     {
         queue.Enqueue(finalItem);
         closing = true;
         Monitor.PulseAll(queue);
     }
 }
Пример #7
0
 /// <summary>
 /// Sends out the specified event through this link channel.
 /// </summary>
 public override void Send(Event e)
 {
     LinkSession currentSession = Session;
     if (Object.ReferenceEquals(currentSession, null))
     {
         Log.Warn("{0} dropped event {1}", Name, e);
         return;
     }
     currentSession.Send(e);
 }
Пример #8
0
        public WaitForMultipleEvents(Coroutine coroutine, params Event[] e)
        {
            this.coroutine = coroutine;
            expected = e;
            actual = new Event[expected.Length];

            for (int i = 0; i < expected.Length; ++i)
            {
                Flow.Bind(expected[i], OnEvent);
            }
        }
Пример #9
0
 public WaitForSingleEvent(Coroutine coroutine, Event e, double seconds)
 {
     this.coroutine = coroutine;
     handlerToken = Flow.Bind(e, OnEvent);
     if (seconds >= 0)
     {
         TimeoutEvent timeoutEvent = new TimeoutEvent { Key = this };
         timeoutToken = Flow.Bind(timeoutEvent, OnTimeout);
         timerToken = TimeFlow.Default.Reserve(timeoutEvent, seconds);
     }
 }
Пример #10
0
 /// <summary>
 /// Sends out the specified event through this link channel.
 /// </summary>
 public override void Send(Event e)
 {
     using (new ReadLock(rwlock))
     {
         if (session == null)
         {
             Log.Warn("{0} dropped event {1}", Name, e);
             return;
         }
     }
     session.Send(e);
 }
Пример #11
0
 /// <summary>
 /// Sends out the specified event through this link channel.
 /// </summary>
 public override void Send(Event e)
 {
     LinkSession session;
     using (new ReadLock(rwlock))
     {
         if (!sessions.TryGetValue(e._Handle, out session))
         {
             return;
         }
     }
     session.Send(e);
 }
Пример #12
0
        void OnEvent(Event e)
        {
            if (timerToken.HasValue)
            {
                TimeFlow.Default.Cancel(timerToken.Value);
                Flow.Unbind(timeoutToken);
            }
            Flow.Unbind(handlerToken);

            coroutine.Context = e;
            coroutine.Continue();
            coroutine.Context = null;
        }
Пример #13
0
 public override void Enqueue(Event item)
 {
     lock (queue)
     {
         if (!closing)
         {
             queue.Enqueue(item);
             if (queue.Count == 1)
             {
                 Monitor.Pulse(queue);
             }
         }
     }
 }
Пример #14
0
 /// <summary>
 /// Broadcasts the specified event to all the connected clients.
 /// </summary>
 public void Broadcast(Event e)
 {
     List<LinkSession> snapshot;
     using (new ReadLock(rwlock))
     {
         snapshot = new List<LinkSession>(sessions.Count);
         var list = sessions.Values;
         for (int i = 0, count = list.Count; i < count; ++i)
         {
             snapshot.Add(list[i]);
         }
     }
     for (int i = 0, count = snapshot.Count; i < count; ++i)
     {
         snapshot[i].Send(e);
     }
 }
Пример #15
0
        protected WaitForSingleEvent(Coroutine coroutine, Event request, Event e, double seconds)
        {
            this.coroutine = coroutine;

            if (!Object.ReferenceEquals(request, null))
            {
                int waitHandle = WaitHandlePool.Acquire();
                request._WaitHandle = waitHandle;
                e._WaitHandle = waitHandle;
            }

            handlerToken = Flow.Bind(e, OnEvent);
            if (seconds > 0)
            {
                TimeoutEvent timeoutEvent = new TimeoutEvent { Key = this };
                timeoutToken = Flow.Bind(timeoutEvent, OnTimeout);
                timerToken = TimeFlow.Default.Reserve(timeoutEvent, seconds);
            }
        }
Пример #16
0
        void OnEvent(Event e)
        {
            Flow.Unbind(handlerToken);

            if (timerToken.HasValue)
            {
                TimeFlow.Default.Cancel(timerToken.Value);
                Flow.Unbind(timeoutToken);
            }

            int waitHandle = handlerToken.Key._WaitHandle;
            if (waitHandle != 0)
            {
                WaitHandlePool.Release(waitHandle);
            }

            coroutine.Result = e;
            coroutine.Continue();
        }
Пример #17
0
        void OnEvent(Event e)
        {
            for (int i = 0; i < expected.Length; ++i)
            {
                if (actual[i] == null && expected[i].IsEquivalent(e))
                {
                    Flow.Unbind(expected[i], OnEvent);
                    actual[i] = e;
                    ++count;
                    break;
                }
            }

            if (count >= expected.Length)
            {
                coroutine.Context = actual;
                coroutine.Continue();
                coroutine.Context = null;
            }
        }
Пример #18
0
        public void TestStaticMethods()
        {
            var handler1 = new MethodHandler<Event>(IntBox.StaticIncrement);
            var handler2 = new MethodHandler<Event>(IntBox.StaticIncrement);
            var handler3 = new MethodHandler<Event>(IntBox.StaticDecrement);

            // Properties
            Assert.True(handler1.Action.Equals(handler2.Action));
            Assert.False(handler2.Action.Equals(handler3.Action));

            // Invocation
            Event e = new Event();
            handler1.Invoke(e);
            Assert.AreEqual(1, IntBox.StaticValue);
            handler2.Invoke(e);
            Assert.AreEqual(2, IntBox.StaticValue);
            handler3.Invoke(e);
            Assert.AreEqual(1, IntBox.StaticValue);

            // Equality
            Assert.True(handler1.Equals(handler2));
            Assert.False(handler2.Equals(handler3));
        }
Пример #19
0
 void OnEvent(Event e)
 {
 }
Пример #20
0
 void Send(Event e)
 {
     LinkSession.Send(e);
 }
Пример #21
0
        protected override void OnEventSent(Event e)
        {
            hasSent = true;

            if (e.GetTypeId() != BuiltinEventType.HeartbeatEvent)
            {
                Log.Debug("{0} {1} sent event {2}",
                    link.Name, InternalHandle, e);
            }
            else
            {
                Log.Trace("{0} {1} sent event {2}",
                    link.Name, InternalHandle, e);
            }

            base.OnEventSent(e);
        }
Пример #22
0
 public WaitForSingleEvent(Coroutine coroutine, Event e, double seconds)
     : this(coroutine, null, e, seconds)
 {
 }
Пример #23
0
 public WaitForSingleEvent(Coroutine coroutine, Event e)
     : this(coroutine, null, e, Config.Coroutine.DefaultTimeout)
 {
 }
Пример #24
0
 public WaitForSingleResponse(Coroutine coroutine, Event request,
         Event response, double seconds)
     : base(coroutine, request, response, seconds)
 {
     if (Object.ReferenceEquals(request, null))
     {
         throw new ArgumentNullException();
     }
     request.Post();
 }
Пример #25
0
 public override void Enqueue(Event item)
 {
     queue.Enqueue(item);
 }
Пример #26
0
 public WaitForSingleResponse(Coroutine coroutine, Event request,
         Event response)
     : this(coroutine, request, response, Config.Coroutine.DefaultTimeout)
 {
 }
Пример #27
0
 public override bool TryDequeue(out Event value)
 {
     lock (queue)
     {
         if (queue.Count == 0)
         {
             value = null;
             return false;
         }
         value = queue.Dequeue();
         return true;
     }
 }
Пример #28
0
 public override bool TryDequeue(out Event value)
 {
     return queue.TryDequeue(out value);
 }
Пример #29
0
 protected override bool Process(Event e)
 {
     switch (e.GetTypeId())
     {
         case BuiltinEventType.HeartbeatEvent:
             // Do nothing
             break;
         default:
             return base.Process(e);
     }
     return true;
 }
Пример #30
0
 void Send(Event e)
 {
     if (LinkSession == null)
     {
         return;
     }
     LinkSession.Send(e);
 }