private IEnumerable <ActorIdentity> LookupRemoteActors(string actorType, Func <IEnumerable <ActorIdentity>, IEnumerable <ActorIdentity> > matchActorFunc) { var actorLookupCondition = new ActorIdentityLookup() { Type = actorType, }; var actorLookupRequestData = this.ChannelConfiguration.FrameBuilder.ControlFrameDataEncoder.EncodeFrameData(actorLookupCondition); var actorLookupRequest = new WhereFrame(actorLookupRequestData); var actorLookupRequestBuffer = this.ChannelConfiguration.FrameBuilder.EncodeFrame(actorLookupRequest); ManualResetEventSlim waitingResponse = new ManualResetEventSlim(false); ActorChannelDataReceivedEventArgs lookupResponseEvent = null; EventHandler <ActorChannelDataReceivedEventArgs> onDataReceived = (s, e) => { lookupResponseEvent = e; waitingResponse.Set(); }; _centerChannel.ChannelDataReceived += onDataReceived; _centerChannel.BeginSend(_centerChannel.Identifier, actorLookupRequestBuffer); bool lookedup = waitingResponse.Wait(TimeSpan.FromSeconds(15)); _centerChannel.ChannelDataReceived -= onDataReceived; waitingResponse.Dispose(); if (lookedup && lookupResponseEvent != null) { ActorFrameHeader actorLookupResponseFrameHeader = null; bool isHeaderDecoded = this.ChannelConfiguration.FrameBuilder.TryDecodeFrameHeader( lookupResponseEvent.Data, lookupResponseEvent.DataOffset, lookupResponseEvent.DataLength, out actorLookupResponseFrameHeader); if (isHeaderDecoded && actorLookupResponseFrameHeader.OpCode == OpCode.Here) { byte[] payload; int payloadOffset; int payloadCount; this.ChannelConfiguration.FrameBuilder.DecodePayload( lookupResponseEvent.Data, lookupResponseEvent.DataOffset, actorLookupResponseFrameHeader, out payload, out payloadOffset, out payloadCount); var actorLookupResponseData = this.ChannelConfiguration.FrameBuilder.ControlFrameDataDecoder.DecodeFrameData <ActorIdentityCollection>( payload, payloadOffset, payloadCount); var actors = actorLookupResponseData != null ? actorLookupResponseData.Items : null; if (actors != null) { _log.DebugFormat("Lookup actors, ActorType[{0}], Count[{1}].", actorType, actors.Count); var matchedActors = matchActorFunc(actors); if (matchedActors != null && matchedActors.Any()) { _log.DebugFormat("Resolve actors, ActorType[{0}], Count[{1}].", actorType, matchedActors.Count()); return(matchedActors); } } } } return(null); }
private IPEndPoint LookupRemoteActorEndPoint(string actorType, Func <IEnumerable <ActorIdentity>, ActorIdentity> matchActorFunc) { var actorLookupCondition = new ActorIdentityLookup() { Type = actorType, }; var actorLookupRequestData = _channelConfiguration.FrameBuilder.ControlFrameDataEncoder.EncodeFrameData(actorLookupCondition); var actorLookupRequest = new WhereFrame(actorLookupRequestData); var actorLookupRequestBuffer = _channelConfiguration.FrameBuilder.EncodeFrame(actorLookupRequest); ManualResetEventSlim waitingResponse = new ManualResetEventSlim(false); ActorDataReceivedEventArgs lookupResponseEvent = null; EventHandler <ActorDataReceivedEventArgs> onDataReceived = (s, e) => { lookupResponseEvent = e; waitingResponse.Set(); }; _centerChannel.DataReceived += onDataReceived; _centerChannel.BeginSend(_centerActor.Type, _centerActor.Name, actorLookupRequestBuffer); bool lookedup = waitingResponse.Wait(TimeSpan.FromSeconds(15)); _centerChannel.DataReceived -= onDataReceived; waitingResponse.Dispose(); if (lookedup && lookupResponseEvent != null) { ActorFrameHeader actorLookupResponseFrameHeader = null; bool isHeaderDecoded = _channelConfiguration.FrameBuilder.TryDecodeFrameHeader( lookupResponseEvent.Data, lookupResponseEvent.DataOffset, lookupResponseEvent.DataLength, out actorLookupResponseFrameHeader); if (isHeaderDecoded && actorLookupResponseFrameHeader.OpCode == OpCode.Here) { byte[] payload; int payloadOffset; int payloadCount; _channelConfiguration.FrameBuilder.DecodePayload( lookupResponseEvent.Data, lookupResponseEvent.DataOffset, actorLookupResponseFrameHeader, out payload, out payloadOffset, out payloadCount); var actorLookupResponseData = _channelConfiguration.FrameBuilder.ControlFrameDataDecoder.DecodeFrameData <ActorIdentityCollection>( payload, payloadOffset, payloadCount); var actors = actorLookupResponseData != null ? actorLookupResponseData.Items : null; if (actors != null) { _log.DebugFormat("Lookup actors [{0}].", actors.Count); var actor = matchActorFunc(actors); if (actor != null) { IPAddress actorAddress = ResolveIPAddress(actor.Address); int actorPort = int.Parse(actor.Port); var actorEndPoint = new IPEndPoint(actorAddress, actorPort); return(actorEndPoint); } } } } return(null); }