Exemple #1
0
        public void ActivateLocalActor(ActorDescription localActor)
        {
            var channel = _factory.BuildLocalActor(localActor);

            channel.Connected    += OnActorConnected;
            channel.Disconnected += OnActorDisconnected;
            channel.DataReceived += OnActorDataReceived;
            channel.Open();

            _localActor = localActor;
            _channels.Add(_localActor.GetKey(), channel);
            _actorKeys.Add(_localActor.GetKey(), _localActor);
        }
        public void Send(string actorType, string actorName, byte[] data)
        {
            var actorKey = ActorDescription.GetKey(actorType, actorName);

            if (_remoteActor == null)
            {
                throw new InvalidOperationException(
                          string.Format("The remote actor has not been connected, Type[{0}], Name[{1}].", actorType, actorName));
            }
            if (_remoteActor.GetKey() != actorKey)
            {
                throw new InvalidOperationException(
                          string.Format("Remote actor key not matched, [{0}]:[{1}].", _remoteActor.GetKey(), actorKey));
            }

            _connector.Send(data);
        }
        public void SendAsync(string actorType, string actorName, byte[] data, int offset, int count)
        {
            var actorKey   = ActorDescription.GetKey(actorType, actorName);
            var sessionKey = _actorKeys.Get(actorKey);

            if (!string.IsNullOrEmpty(sessionKey))
            {
                _listener.SendToAsync(sessionKey, data, offset, count);
            }
        }
        public void Send(string actorType, string actorName, byte[] data)
        {
            var actorKey   = ActorDescription.GetKey(actorType, actorName);
            var sessionKey = _actorKeys.Get(actorKey);

            if (!string.IsNullOrEmpty(sessionKey))
            {
                _listener.SendTo(sessionKey, data);
            }
        }
Exemple #5
0
        public IActorChannel GetActorChannel(string actorType, string actorName)
        {
            IActorChannel channel  = null;
            var           actorKey = ActorDescription.GetKey(actorType, actorName);

            if (_channels.TryGetValue(actorKey, out channel))
            {
                return(channel);
            }

            lock (_syncLock)
            {
                if (_channels.TryGetValue(actorKey, out channel))
                {
                    return(channel);
                }

                channel               = _factory.BuildActorChannel(_localActor, actorType, actorName);
                channel.Connected    += OnActorConnected;
                channel.Disconnected += OnActorDisconnected;
                channel.DataReceived += OnActorDataReceived;

                ManualResetEventSlim waitingConnected = new ManualResetEventSlim(false);
                object connectedSender = null;
                ActorConnectedEventArgs connectedEvent             = null;
                EventHandler <ActorConnectedEventArgs> onConnected =
                    (s, e) =>
                {
                    connectedSender = s;
                    connectedEvent  = e;
                    waitingConnected.Set();
                };

                channel.Connected += onConnected;
                channel.Open();

                bool connected = waitingConnected.Wait(TimeSpan.FromSeconds(5));
                channel.Connected -= onConnected;
                waitingConnected.Dispose();

                if (connected)
                {
                    _channels.Add(connectedEvent.RemoteActor.GetKey(), (IActorChannel)connectedSender);
                    _actorKeys.Add(connectedEvent.RemoteActor.GetKey(), connectedEvent.RemoteActor);
                    return(channel);
                }
                else
                {
                    CloseChannel(channel);
                    throw new ActorNotFoundException(string.Format(
                                                         "Cannot connect remote actor, Type[{0}], Name[{1}].", actorType, actorName));
                }
            }
        }
        private void OnDisconnected(object sender, ActorTransportDisconnectedEventArgs e)
        {
            ActorDescription remoteActor = null;

            if (_remoteActors.TryRemove(e.SessionKey, out remoteActor))
            {
                _actorKeys.Remove(remoteActor.GetKey());
                _log.InfoFormat("Disconnected with remote [{0}], SessionKey[{1}].", remoteActor, e.SessionKey);

                if (Disconnected != null)
                {
                    Disconnected(this, new ActorDisconnectedEventArgs(e.SessionKey, remoteActor));
                }
            }
        }