private ConcurrentDictionary <string, string> _actorKeys = new ConcurrentDictionary <string, string>();                        // ActorKey -> SessionKey

        public ActorListenerChannel(
            ActorDescription localActor, ActorTransportListener localListener,
            IActorMessageEncoder encoder, IActorMessageDecoder decoder)
        {
            if (localActor == null)
            {
                throw new ArgumentNullException("localActor");
            }
            if (localListener == null)
            {
                throw new ArgumentNullException("localListener");
            }
            if (encoder == null)
            {
                throw new ArgumentNullException("encoder");
            }
            if (decoder == null)
            {
                throw new ArgumentNullException("decoder");
            }

            _localActor = localActor;
            _listener   = localListener;
            _encoder    = encoder;
            _decoder    = decoder;
        }
        public void Close()
        {
            try
            {
                _connector.Connected    -= OnConnected;
                _connector.Disconnected -= OnDisconnected;
                _connector.DataReceived -= OnDataReceived;

                if (_connector.IsConnected)
                {
                    _connector.Disconnect();
                }

                if (Disconnected != null)
                {
                    Disconnected(this, new ActorDisconnectedEventArgs(this.ConnectToEndPoint.ToString(), _remoteActor));
                }

                _log.InfoFormat("Disconnected with remote [{0}], SessionKey[{1}].", _remoteActor, this.ConnectToEndPoint);
            }
            finally
            {
                _remoteActor  = null;
                _isHandshaked = false;
                OnClose();
            }
        }
        public ActorDirectory(
            ActorDescription centerActor, IActorChannel centerChannel,
            IActorMessageEncoder encoder, IActorMessageDecoder decoder)
        {
            if (centerActor == null)
            {
                throw new ArgumentNullException("centerActor");
            }
            if (centerChannel == null)
            {
                throw new ArgumentNullException("centerChannel");
            }
            if (encoder == null)
            {
                throw new ArgumentNullException("encoder");
            }
            if (decoder == null)
            {
                throw new ArgumentNullException("decoder");
            }

            _centerActor   = centerActor;
            _centerChannel = centerChannel;
            _encoder       = encoder;
            _decoder       = decoder;
        }
        public ActorConnectorChannel(
            ActorDescription localActor, ActorTransportConnector remoteConnector,
            IActorMessageEncoder encoder, IActorMessageDecoder decoder)
        {
            if (localActor == null)
            {
                throw new ArgumentNullException("localActor");
            }
            if (remoteConnector == null)
            {
                throw new ArgumentNullException("remoteConnector");
            }
            if (encoder == null)
            {
                throw new ArgumentNullException("encoder");
            }
            if (decoder == null)
            {
                throw new ArgumentNullException("decoder");
            }

            _localActor = localActor;
            _connector  = remoteConnector;
            _encoder    = encoder;
            _decoder    = decoder;
        }
 public ActorConnectorReconnectableChannel(
     ActorDescription localActor, ActorTransportConnector remoteConnector,
     IActorMessageEncoder encoder, IActorMessageDecoder decoder)
     : base(localActor, remoteConnector, encoder, decoder)
 {
     this.RetryPeriod = TimeSpan.FromSeconds(15);
 }
Beispiel #6
0
 public IActorChannel GetActorChannel(ActorDescription remoteActor)
 {
     if (remoteActor == null)
     {
         throw new ArgumentNullException("remoteActor");
     }
     return(GetActorChannel(remoteActor.Type, remoteActor.Name));
 }
        public ActorDataReceivedEventArgs(string sessionKey, ActorDescription remoteActor, byte[] data, int dataOffset, int dataLength)
        {
            SessionKey  = sessionKey;
            RemoteActor = remoteActor;

            Data       = data;
            DataOffset = dataOffset;
            DataLength = dataLength;
        }
        private void Handshake(TimeSpan timeout)
        {
            var actorHandshakeRequest = new ActorHandshakeRequest()
            {
                ActorDescription = _localActor,
            };
            var actorHandshakeRequestBuffer = _encoder.Encode(actorHandshakeRequest);

            ManualResetEventSlim waitingHandshaked = new ManualResetEventSlim(false);
            ActorTransportDataReceivedEventArgs handshakeResponseEvent      = null;
            EventHandler <ActorTransportDataReceivedEventArgs> onHandshaked =
                (s, e) =>
            {
                handshakeResponseEvent = e;
                waitingHandshaked.Set();
            };

            _connector.DataReceived += onHandshaked;
            _log.InfoFormat("Handshake request from local actor [{0}].", _localActor);
            _connector.SendAsync(actorHandshakeRequestBuffer);

            bool handshaked = waitingHandshaked.Wait(timeout);

            _connector.DataReceived -= onHandshaked;
            waitingHandshaked.Dispose();

            if (handshaked && handshakeResponseEvent != null)
            {
                var actorHandshakeResponse = _decoder.Decode <ActorHandshakeResponse>(
                    handshakeResponseEvent.Data, handshakeResponseEvent.DataOffset, handshakeResponseEvent.DataLength);
                _remoteActor = actorHandshakeResponse.ActorDescription;
                _log.InfoFormat("Handshake response from remote actor [{0}].", _remoteActor);
                if (_remoteActor == null)
                {
                    _log.ErrorFormat("Handshake with remote [{0}] failed, invalid actor description.", this.ConnectToEndPoint);
                    Close();
                }
                else
                {
                    _log.InfoFormat("Handshake with remote [{0}] successfully, RemoteActor[{1}].", this.ConnectToEndPoint, _remoteActor);

                    _isHandshaked = true;
                    if (Connected != null)
                    {
                        Connected(this, new ActorConnectedEventArgs(this.ConnectToEndPoint.ToString(), _remoteActor));
                    }

                    _connector.DataReceived += OnDataReceived;
                }
            }
            else
            {
                _log.ErrorFormat("Handshake with remote [{0}] timeout [{1}].", this.ConnectToEndPoint, timeout);
                Close();
            }
        }
        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);
            }
        }
        private IActorChannel BuildActorCenterChannel(ActorDescription centerActor, ActorDescription localActor)
        {
            IPAddress actorCenterAddress  = ResolveIPAddress(centerActor.Address);
            int       actorCenterPort     = int.Parse(centerActor.Port);
            var       actorCenterEndPoint = new IPEndPoint(actorCenterAddress, actorCenterPort);

            var centerConnector = new ActorTransportConnector(actorCenterEndPoint);
            var centerChannel   = new ActorConnectorReconnectableChannel(localActor, centerConnector, this.Encoder, this.Decoder);

            return(centerChannel);
        }
Beispiel #12
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));
                }
            }
        }
Beispiel #13
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);
        }
        private ActorDescription BuildLocalActor()
        {
            var actorType    = _appSetting.GetItem <string>(ActorTypeKey);
            var actorName    = _appSetting.GetItem <string>(ActorNameKey);
            var actorAddress = _appSetting.GetItem <string>(ActorAddressKey);
            var actorPort    = _appSetting.GetItem <string>(ActorPortKey);

            var actor = new ActorDescription(actorType, actorName);

            actor.Address = actorAddress;
            actor.Port    = actorPort;

            return(actor);
        }
        public ActorConnectedEventArgs(string sessionKey, ActorDescription remoteActor)
        {
            if (string.IsNullOrEmpty(sessionKey))
            {
                throw new ArgumentNullException("sessionKey");
            }
            if (remoteActor == null)
            {
                throw new ArgumentNullException("remoteActor");
            }

            this.SessionKey  = sessionKey;
            this.RemoteActor = remoteActor;
        }
Beispiel #16
0
        public IActorChannel BuildActorChannel(ActorDescription localActor, string remoteActorType)
        {
            var actorEndPoint = _directory.LookupRemoteActorEndPoint(remoteActorType);

            if (actorEndPoint == null)
            {
                throw new ActorNotFoundException(string.Format(
                                                     "Cannot find remote actor endpoint, Type[{0}].", remoteActorType));
            }

            var channel = new ActorConnectorChannel(localActor, new ActorTransportConnector(actorEndPoint), _encoder, _decoder);

            return(channel);
        }
        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));
                }
            }
        }
        public void SendAsync(string actorType, string actorName, byte[] data, int offset, int count)
        {
            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.SendAsync(data, offset, count);
        }
Beispiel #19
0
        public IActorChannel BuildLocalActor(ActorDescription localActor)
        {
            if (string.IsNullOrWhiteSpace(localActor.Address))
            {
                throw new InvalidOperationException(string.Format(
                                                        "Invalid local actor address, [{0}].", localActor));
            }

            int localPort = -1;

            if (!int.TryParse(localActor.Port, out localPort))
            {
                throw new InvalidOperationException(string.Format(
                                                        "Invalid local actor port, [{0}].", localActor));
            }

            var localEndPoint = new IPEndPoint(IPAddress.Any, localPort);
            var channel       = new ActorListenerChannel(localActor, new ActorTransportListener(localEndPoint), _encoder, _decoder);

            return(channel);
        }
 public ActorDataReceivedEventArgs(string sessionKey, ActorDescription remoteActor, byte[] data)
     : this(sessionKey, remoteActor, data, 0, data.Length)
 {
 }
 public void Build()
 {
     _centerActor = BuildCenterActor();
     _localActor  = BuildLocalActor();
 }
        public void SendAsync(ActorDescription remoteActor, byte[] data, int offset, int count)
        {
            var channel = _manager.GetActorChannel(remoteActor);

            channel.SendAsync(remoteActor.Type, remoteActor.Name, data, offset, count);
        }
        public void Send(ActorDescription remoteActor, byte[] data)
        {
            var channel = _manager.GetActorChannel(remoteActor);

            channel.Send(remoteActor.Type, remoteActor.Name, data);
        }