コード例 #1
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));
                }
            }
        }