public Task ConnectedAsync(IContext context) { switch (context.Message) { case RemoteTerminate msg: { if (_watched.TryGetValue(msg.Watcher.Id, out var pidSet)) { pidSet.Remove(msg.Watchee); if (pidSet.Count == 0) { _watched.Remove(msg.Watcher.Id); } } //create a terminated event for the Watched actor var t = new Terminated { Who = msg.Watchee }; //send the address Terminated event to the Watcher msg.Watcher.SendSystemMessage(t); break; } case EndpointTerminatedEvent _: { _logger.LogDebug($"Handle terminated address {_address}"); foreach (var(id, pidSet) in _watched) { var watcherPid = new PID(ProcessRegistry.Instance.Address, id); var watcherRef = ProcessRegistry.Instance.Get(watcherPid); if (watcherRef != DeadLetterProcess.Instance) { foreach (var pid in pidSet) { //create a terminated event for the Watched actor var t = new Terminated { Who = pid, AddressTerminated = true }; //send the address Terminated event to the Watcher watcherPid.SendSystemMessage(t); } } } _watched.Clear(); _behavior.Become(TerminatedAsync); context.Self.Stop(); break; } case RemoteUnwatch msg: { if (_watched.TryGetValue(msg.Watcher.Id, out var pidSet)) { pidSet.Remove(msg.Watchee); if (pidSet.Count == 0) { _watched.Remove(msg.Watcher.Id); } } var w = new Unwatch(msg.Watcher); Remote.SendMessage(msg.Watchee, w, -1); break; } case RemoteWatch msg: { if (_watched.TryGetValue(msg.Watcher.Id, out var pidSet)) { pidSet.Add(msg.Watchee); } else { _watched[msg.Watcher.Id] = new HashSet <PID> { msg.Watchee }; } var w = new Watch(msg.Watcher); Remote.SendMessage(msg.Watchee, w, -1); break; } case Stopped _: { _logger.LogDebug($"Stopped EndpointWatcher at {_address}"); break; } } return(Actor.Done); }