Example #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));
        }
        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);
            }
        }
        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();
            }
        }
Example #4
0
 public override void Feed(Event e)
 {
     if (queue != null)
     {
         queue.Enqueue(e);
     }
 }
Example #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);
     }
 }
 public override void Close(Event finalItem)
 {
     lock (queue)
     {
         queue.Enqueue(finalItem);
         closing = true;
         Monitor.PulseAll(queue);
     }
 }
Example #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);
 }
        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);
            }
        }
 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);
     }
 }
Example #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);
 }
Example #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);
 }
Example #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;
        }
 public override void Enqueue(Event item)
 {
     lock (queue)
     {
         if (!closing)
         {
             queue.Enqueue(item);
             if (queue.Count == 1)
             {
                 Monitor.Pulse(queue);
             }
         }
     }
 }
Example #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);
     }
 }
Example #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);
            }
        }
Example #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();
        }
        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;
            }
        }
Example #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));
        }
Example #19
0
 void OnEvent(Event e)
 {
 }
Example #20
0
 void Send(Event e)
 {
     LinkSession.Send(e);
 }
Example #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);
        }
Example #22
0
 public WaitForSingleEvent(Coroutine coroutine, Event e, double seconds)
     : this(coroutine, null, e, seconds)
 {
 }
Example #23
0
 public WaitForSingleEvent(Coroutine coroutine, Event e)
     : this(coroutine, null, e, Config.Coroutine.DefaultTimeout)
 {
 }
Example #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();
 }
Example #25
0
 public override void Enqueue(Event item)
 {
     queue.Enqueue(item);
 }
Example #26
0
 public WaitForSingleResponse(Coroutine coroutine, Event request,
         Event response)
     : this(coroutine, request, response, Config.Coroutine.DefaultTimeout)
 {
 }
 public override bool TryDequeue(out Event value)
 {
     lock (queue)
     {
         if (queue.Count == 0)
         {
             value = null;
             return false;
         }
         value = queue.Dequeue();
         return true;
     }
 }
Example #28
0
 public override bool TryDequeue(out Event value)
 {
     return queue.TryDequeue(out value);
 }
Example #29
0
 protected override bool Process(Event e)
 {
     switch (e.GetTypeId())
     {
         case BuiltinEventType.HeartbeatEvent:
             // Do nothing
             break;
         default:
             return base.Process(e);
     }
     return true;
 }
 void Send(Event e)
 {
     if (LinkSession == null)
     {
         return;
     }
     LinkSession.Send(e);
 }