InterfacedActorRef IActorBoundChannelSync.BindActor(InterfacedActorRef actor, ActorBindingFlags bindingFlags) { if (actor == null) { throw new ArgumentNullException(nameof(actor)); } var targetActor = actor.CastToIActorRef(); if (targetActor == null) { throw new ArgumentException("InterfacedActorRef should have valid IActorRef target."); } var actorId = BindActor(targetActor, new[] { new BoundType(actor.InterfaceType) }, bindingFlags); if (actorId == 0) { return(null); } var actorRef = (InterfacedActorRef)Activator.CreateInstance(actor.GetType()); InterfacedActorRefModifier.SetTarget(actorRef, new BoundActorTarget(actorId)); return(actorRef); }
public TRef CreateRef <TRef>(int actorId = 1) where TRef : InterfacedActorRef, new() { var actorRef = new TRef(); InterfacedActorRefModifier.SetTarget(actorRef, new BoundActorTarget(actorId)); InterfacedActorRefModifier.SetRequestWaiter(actorRef, this); return(actorRef); }
protected void OnPacket(Packet packet) { switch (packet.Type) { case PacketType.Notification: if (ObserverRegistry.OnNotificationPacket(packet) == false) { _logger?.WarnFormat("Notification didn't find observer. (ObserverId={0}, Message={1})", packet.ActorId, packet.Message.GetType().Name); } break; case PacketType.Response: ResponseWaitingItem waitingItem; if (_responseWaitingItems.TryRemove(packet.RequestId, out waitingItem)) { var actorRefUpdatable = packet.Message as IPayloadActorRefUpdatable; if (actorRefUpdatable != null) { actorRefUpdatable.Update(a => { var actorRef = (InterfacedActorRef)a; var target = (BoundActorTarget)actorRef.Target; if (string.IsNullOrEmpty(target.Address)) { // use this channel InterfacedActorRefModifier.SetRequestWaiter(actorRef, this); } else { // routed and use channel with specified address if (ChannelRouter != null) { var channel = ChannelRouter(this, target.Address); if (channel != null) { InterfacedActorRefModifier.SetTarget(actorRef, new BoundActorTarget(target.Id)); InterfacedActorRefModifier.SetRequestWaiter(actorRef, channel); } } } }); } waitingItem.ResponseHandler(waitingItem.TaskCompletionSource, new ResponseMessage { RequestId = packet.RequestId, ReturnPayload = (IValueGetable)packet.Message, Exception = packet.Exception }); } break; } }
InterfacedActorRef IActorBoundGatewaySync.OpenChannel(InterfacedActorRef actor, object tag, ActorBindingFlags bindingFlags) { var targetActor = actor.CastToIActorRef(); if (targetActor == null) { throw new ArgumentNullException(nameof(actor)); } var target = ((IActorBoundGatewaySync)this).OpenChannel(targetActor, new TaggedType[] { actor.InterfaceType }, bindingFlags); var actorRef = (InterfacedActorRef)Activator.CreateInstance(actor.GetType()); InterfacedActorRefModifier.SetTarget(actorRef, target); return(actorRef); }