Exemplo n.º 1
0
        public void ActivateLocalActor(ActorIdentity localActor)
        {
            if (localActor == null)
            {
                throw new ArgumentNullException("localActor");
            }
            if (_localActor != null)
            {
                throw new InvalidOperationException("The local actor has already been activated.");
            }

            var channel = _factory.BuildLocalActor(localActor);

            channel.Connected    += OnActorConnected;
            channel.Disconnected += OnActorDisconnected;
            channel.DataReceived += OnActorDataReceived;

            try
            {
                channel.Open();

                _localActor = localActor;
                _channels.Add(_localActor.GetKey(), channel);
                _actorKeys.Add(_localActor.GetKey(), _localActor);

                _log.DebugFormat("Local actor [{0}] is activated.", _localActor);
            }
            catch
            {
                CloseChannel(channel);
                throw;
            }
        }
Exemplo n.º 2
0
        public void ActivateLocalActor(ActorIdentity 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);
        }
Exemplo n.º 3
0
        public IActorChannel GetActorChannel(string actorType, string actorName)
        {
            if (string.IsNullOrEmpty(actorName))
            {
                return(GetActorChannel(actorType));
            }

            IActorChannel channel  = null;
            var           actorKey = ActorIdentity.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);
                bool activated = ActivateChannel(channel);
                if (activated)
                {
                    return(channel);
                }
                else
                {
                    throw new ActorNotFoundException(string.Format(
                                                         "Activate channel failed, cannot connect remote actor, Type[{0}], Name[{1}].", actorType, actorName));
                }
            }
        }
Exemplo n.º 4
0
        public void Send(string actorType, string actorName, byte[] data, int offset, int count)
        {
            var actorKey = ActorIdentity.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, offset, count);
            _keepAliveTracker.OnDataSent();
        }
Exemplo n.º 5
0
        public void EndSend(string actorType, string actorName, IAsyncResult asyncResult)
        {
            var actorKey   = ActorIdentity.GetKey(actorType, actorName);
            var sessionKey = _actorKeys.Get(actorKey);

            if (!string.IsNullOrEmpty(sessionKey))
            {
                _listener.EndSendTo(sessionKey, asyncResult);
            }
        }
Exemplo n.º 6
0
        public IActorChannel GetActorChannel(string actorType, string actorName)
        {
            IActorChannel channel  = null;
            var           actorKey = ActorIdentity.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));
                }
            }
        }
Exemplo n.º 7
0
        public void BeginSend(string actorType, string actorName, byte[] data, int offset, int count)
        {
            var actorKey   = ActorIdentity.GetKey(actorType, actorName);
            var sessionKey = _actorKeys.Get(actorKey);

            if (!string.IsNullOrEmpty(sessionKey))
            {
                ActorChannelSession session = null;
                if (_sessions.TryGetValue(sessionKey, out session))
                {
                    session.BeginSend(actorType, actorName, data, offset, count);
                }
            }
        }
Exemplo n.º 8
0
        public IAsyncResult BeginSend(string actorType, string actorName, byte[] data, int offset, int count, AsyncCallback callback, object state)
        {
            var actorKey   = ActorIdentity.GetKey(actorType, actorName);
            var sessionKey = _actorKeys.Get(actorKey);

            if (!string.IsNullOrEmpty(sessionKey))
            {
                ActorChannelSession session = null;
                if (_sessions.TryGetValue(sessionKey, out session))
                {
                    return(session.BeginSend(actorType, actorName, data, offset, count, callback, state));
                }
            }

            return(null);
        }
Exemplo n.º 9
0
        public IAsyncResult BeginSend(string actorType, string actorName, byte[] data, int offset, int count, AsyncCallback callback, object state)
        {
            var actorKey = ActorIdentity.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));
            }

            var ar = _connector.BeginSend(data, offset, count, callback, state);

            _keepAliveTracker.OnDataSent();

            return(ar);
        }
Exemplo n.º 10
0
        public void ActivateLocalActor(ActorIdentity localActor)
        {
            if (localActor == null)
            {
                throw new ArgumentNullException("localActor");
            }
            if (_localActor != null)
            {
                throw new InvalidOperationException("The local actor has already been activated.");
            }

            var channel = _factory.BuildLocalActor(localActor);

            channel.ChannelConnected    += OnActorChannelConnected;
            channel.ChannelDisconnected += OnActorChannelDisconnected;
            channel.ChannelDataReceived += OnActorChannelDataReceived;

            try
            {
                channel.Open();

                _localActor = localActor;

                var item = new ChannelItem(channel.Identifier, channel);
                item.RemoteActorKey = _localActor.GetKey();
                item.RemoteActor    = _localActor;
                _channels.Add(channel.Identifier, item);

                _log.DebugFormat("Local actor [{0}] is activated on channel [{1}].", _localActor, channel);
            }
            catch
            {
                CloseChannel(channel);
                throw;
            }
        }
Exemplo n.º 11
0
        public IActorChannel GetActorChannel(string actorType, string actorName)
        {
            if (string.IsNullOrEmpty(actorName))
            {
                return(GetActorChannel(actorType));
            }

            var         actorKey = ActorIdentity.GetKey(actorType, actorName);
            ChannelItem item     = null;

            item = _channels.Values.FirstOrDefault(i => i.RemoteActorKey == actorKey);
            if (item != null)
            {
                return(item.Channel);
            }

            lock (_syncLock)
            {
                item = _channels.Values.FirstOrDefault(i => i.RemoteActorKey == actorKey);
                if (item != null)
                {
                    return(item.Channel);
                }

                BuildActorChannel(actorType, actorName);

                item = _channels.Values.FirstOrDefault(i => i.RemoteActorKey == actorKey);
                if (item != null)
                {
                    return(item.Channel);
                }

                throw new ActorNotFoundException(string.Format(
                                                     "Build actor channel failed, cannot connect remote actor, Type[{0}], Name[{1}].", actorType, actorName));
            }
        }
Exemplo n.º 12
0
        private void OnDisconnected(object sender, ActorTransportSessionDisconnectedEventArgs e)
        {
            ActorChannelSession session = null;

            if (_sessions.TryRemove(e.SessionKey, out session))
            {
                session.Handshaked   -= OnSessionHandshaked;
                session.DataReceived -= OnSessionDataReceived;
                session.Close();
            }

            ActorIdentity 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));
                }
            }
        }