private async Task DispatchMessage(IMessage messageIn) { var messageHandler = actorHandlerMap.Get(messageIn); if (messageHandler != null) { try { var response = (await messageHandler(messageIn)).Messages; foreach (var messageOut in response) { messageRouter.Route(SetOutMessageProperties(messageOut)); } } catch (Exception err) { var payload = err.BuildExceptionMessage(); var messageOut = Message.Create(payload, MessageIdentifiers.Exception); messageRouter.Route(SetOutMessageProperties(messageOut)); } } else { throw new MessageHandlerNotFoundException(messageIn); } IMessage SetOutMessageProperties(IMessage messageOut) { messageOut.Domain = securityProvider.GetDomain(messageOut.Identity); return(messageOut.CopyMessageProperties(messageIn)); } }
private void ProcessRequests(CancellationToken token) { try { var waitHandles = new[] { receivingSocket.CanReceive(), token.WaitHandle }; while (!token.IsCancellationRequested) { try { if (WaitHandle.WaitAny(waitHandles) == 0) { var message = (Message)receivingSocket.TryReceive(); if (message != null) { try { var actorIdentifier = new MessageIdentifier(message); var handler = actorHandlerMap.Get(actorIdentifier); if (handler != null) { var task = handler(message); HandleTaskResult(token, task, message); } else { HandlerNotFound(message); } } catch (Exception err) { //TODO: Add more context to exception about which Actor failed CallbackException(err, message); logger.Error(err); } } } } catch (OperationCanceledException) { } catch (Exception err) { logger.Error(err); } } } catch (Exception err) { logger.Error(err); } logger.Warn($"{GetType().Name} requests processing stopped."); }