Beispiel #1
0
 /// Block on the stream forever, reading data as soon as we can.
 private void ReadStreamData()
 {
     using (var streamReader = new StreamReader(_stream))
     {
         try
         {
             while (!streamReader.EndOfStream)
             {
                 var line = streamReader.ReadLine();
                 lock (this)
                 {
                     EventHandler.Trigger(new DataReceivedEvent(line));
                 }
             }
         }
         catch (IOException)
         {
         }
     }
     lock (this)
     {
         _state  = TcpTransportState.Idle;
         _writer = null;
         _stream = null;
         EventHandler.Trigger(new ConnectionClosedEvent());
     }
 }
Beispiel #2
0
 public static void ActorLifecycle(Actor actor, ActorLifecycle status)
 {
     if (status == Events.ActorLifecycle.Created)
     {
         Instance().Active.Add(actor);
         EventHandler.Trigger(new ActorLifecycleEvent()
         {
             Actor  = actor,
             Status = status
         });
     }
     else if (status == Events.ActorLifecycle.Destroyed)
     {
         var instance = Instance(true);
         if (instance != null)
         {
             instance.Active.RemoveAll(i => i == actor);
             EventHandler.Trigger(new ActorLifecycleEvent()
             {
                 Actor  = actor,
                 Status = status
             });
         }
     }
 }
Beispiel #3
0
        /// Open a connection to the remote host or dispatch an error event.
        private void Connect()
        {
            lock (this)
            {
                _client = new TcpClient();
                try
                {
                    _client.Connect(_host, _port);
                }
                catch (Exception error)
                {
                    EventHandler.Trigger(new ErrorEvent {
                        Exception = error
                    });
                    _state = TcpTransportState.Idle;
                    return;
                }

                _state = TcpTransportState.Connected;
                EventHandler.Trigger(new ConnectionOpenedEvent());
            }

            _stream = _client.GetStream();
            _writer = new StreamWriter(_stream);
            ReadStreamData();
        }
Beispiel #4
0
 public void Execute()
 {
     _events.Trigger(new DummyCompletionEvent {
         Complete = true
     });
     _events.ClearEventHandlers <DummyCompletionEvent>();
 }
Beispiel #5
0
 /// Update internal state, and dispatch any pending events.
 public void Update()
 {
     foreach (var queuedEvent in _eventCache)
     {
         _eventHandler.Trigger(queuedEvent);
     }
     _eventCache.Clear();
 }
Beispiel #6
0
        private void Process(object body, GenericMotionConfig config)
        {
            var sendEvent = false;
            var state     = _motion.GetState();
            var direction = state.GetDirection();
            var isJumping = state.GetJumping();
            var isFalling = state.GetFalling();

            // Check if we need to send an event?
            if ((_isFalling && !isFalling) || (!_isFalling && isFalling))
            {
                sendEvent  = true;
                _isFalling = isFalling;
            }
            else if ((_isJumping && !isJumping) || (!_isJumping && isJumping))
            {
                sendEvent  = true;
                _isJumping = isJumping;
            }
            else
            {
                var v  = SignOf(direction.Vertical);
                var vo = SignOf(_direction.Vertical);
                var h  = SignOf(direction.Horizontal);
                var ho = SignOf(_direction.Horizontal);
                if ((v != vo) || (h != ho))
                {
                    sendEvent  = true;
                    _direction = direction.Clone();
                }
            }

            // Send event
            if (sendEvent)
            {
                _eventHandler.Trigger(new GenericMotionEvent()
                {
                    IsFalling = isFalling,
                    IsJumping = isJumping,
                    Direction = direction.AsVector(body, config)
                });
            }
        }
Beispiel #7
0
        public void Write <T>(T target)
        {
            lock (this)
            {
                if (_state != TcpTransportState.Connected)
                {
                    throw new TcpTransportException(TcpTransportErrors.NotConencted, "No active connection");
                }

                var output = JsonUtility.ToJson(target);
                try
                {
                    _writer.WriteLine(output);
                    _writer.Flush();
                }
                catch (Exception error)
                {
                    EventHandler.Trigger(new ErrorEvent()
                    {
                        Exception = error
                    });
                }
            }
        }