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)); } } }
private void CloseChannel(IActorChannel channel) { channel.Close(); channel.ChannelConnected -= OnActorChannelConnected; channel.ChannelDisconnected -= OnActorChannelDisconnected; channel.ChannelDataReceived -= OnActorChannelDataReceived; }
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 void Close() { if (_centerChannel != null) { _centerChannel.Close(); _centerChannel = null; } }
public void Close() { if (_centerChannel != null) { _centerChannel.Close(); _centerChannel.ChannelDataReceived -= OnCenterChannelDataReceived; _centerChannel = null; } }
public IActorChannel GetActorChannel(string actorType) { IActorChannel channel = null; var actor = _actorKeys.Values.Where(a => a.Type == actorType).OrderBy(t => Guid.NewGuid()).FirstOrDefault(); if (actor != null && _channels.TryGetValue(actor.GetKey(), out channel)) { return(channel); } lock (_syncLock) { actor = _actorKeys.Values.Where(a => a.Type == actorType).OrderBy(t => Guid.NewGuid()).FirstOrDefault(); if (actor != null && _channels.TryGetValue(actor.GetKey(), out channel)) { return(channel); } channel = _factory.BuildActorChannel(_localActor, actorType); 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(30)); 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}].", actorType)); } } }
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)); } } }
public void Register(ActorIdentity localActor) { if (localActor == null) { throw new ArgumentNullException("localActor"); } lock (_openLock) { if (this.Active) { throw new InvalidOperationException( string.Format("Center actor [{0}] has already been registered.", this.CenterActor)); } _log.DebugFormat("Connecting to center actor [{0}].", this.CenterActor); var centerChannel = BuildCenterActorChannel(localActor); centerChannel.Open(); int retryTimes = 1; TimeSpan retryPeriod = TimeSpan.FromMilliseconds(100); while (true) { if (centerChannel.Active) { break; } Thread.Sleep(retryPeriod); if (centerChannel.Active) { break; } retryTimes++; if (retryTimes > 300) { centerChannel.Close(); throw new InvalidOperationException( string.Format("Cannot connect to center actor [{0}] after wait [{1}] milliseconds.", this.CenterActor, retryTimes * (int)retryPeriod.TotalMilliseconds)); } } _log.DebugFormat("Connected to center actor [{0}].", this.CenterActor); _centerChannel = centerChannel; _centerChannel.ChannelDataReceived += OnCenterChannelDataReceived; } }
private bool ActivateChannel(IActorChannel channel) { channel.ChannelConnected += OnActorChannelConnected; channel.ChannelDisconnected += OnActorChannelDisconnected; channel.ChannelDataReceived += OnActorChannelDataReceived; ManualResetEventSlim waitingConnected = new ManualResetEventSlim(false); object connectedSender = null; ActorChannelConnectedEventArgs connectedEvent = null; EventHandler <ActorChannelConnectedEventArgs> onConnected = (s, e) => { connectedSender = s; connectedEvent = e; waitingConnected.Set(); }; channel.ChannelConnected += onConnected; channel.Open(); bool connected = waitingConnected.Wait(TimeSpan.FromSeconds(5)); channel.ChannelConnected -= onConnected; waitingConnected.Dispose(); if (connected && channel.Active) { var item = new ChannelItem(((IActorChannel)connectedSender).Identifier, (IActorChannel)connectedSender); item.RemoteActorKey = connectedEvent.RemoteActor.GetKey(); item.RemoteActor = connectedEvent.RemoteActor; _channels.TryAdd(channel.Identifier, item); return(true); } else { CloseChannel(channel); return(false); } }
public ActorDirectory( ActorIdentity centerActor, IActorChannel centerChannel, ActorChannelConfiguration channelConfiguration) { if (centerActor == null) { throw new ArgumentNullException("centerActor"); } if (centerChannel == null) { throw new ArgumentNullException("centerChannel"); } if (channelConfiguration == null) { throw new ArgumentNullException("channelConfiguration"); } _centerActor = centerActor; _centerChannel = centerChannel; _channelConfiguration = channelConfiguration; }
private bool ActivateChannel(IActorChannel channel) { 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 && channel.Active) { _channels.Add(connectedEvent.RemoteActor.GetKey(), (IActorChannel)connectedSender); _actorKeys.Add(connectedEvent.RemoteActor.GetKey(), connectedEvent.RemoteActor); return(true); } else { CloseChannel(channel); return(false); } }
public IActorChannel GetActorChannel(string actorType) { if (string.IsNullOrEmpty(actorType)) { throw new ArgumentNullException("actorType"); } IActorChannel channel = null; var actor = _actorKeys.Values.Where(a => a.Type == actorType).OrderBy(t => Guid.NewGuid()).FirstOrDefault(); if (actor != null && _channels.TryGetValue(actor.GetKey(), out channel)) { return(channel); } lock (_syncLock) { actor = _actorKeys.Values.Where(a => a.Type == actorType).OrderBy(t => Guid.NewGuid()).FirstOrDefault(); if (actor != null && _channels.TryGetValue(actor.GetKey(), out channel)) { return(channel); } channel = _factory.BuildActorChannel(_localActor, actorType); bool activated = ActivateChannel(channel); if (activated) { return(channel); } else { throw new ActorNotFoundException(string.Format( "Activate channel failed, cannot connect remote actor, Type[{0}].", actorType)); } } }
public ChannelItem(string channelIdentifier, IActorChannel channel) { this.ChannelIdentifier = channelIdentifier; this.Channel = channel; }