Ejemplo n.º 1
0
        public static RemoteMessage ToRemoteMessage(this WireMessage message)
        {
            if (message != null)
            {
                switch (message.MessageType)
                {
                case MessageType.Default:
                    return(new RemoteMessage(
                               new Message(message.Data, message.From, message.Header, message.TimeoutMSec),
                               message?.To ?? Aid.Unknown,
                               message.Id ?? WireMessageId.Empty));

                case MessageType.FutureMessage:
                    return(new RemoteMessage(
                               new FutureMessage(message.Data, message.From, message.Header, message.TimeoutMSec),
                               message?.To ?? Aid.Unknown,
                               message.Id ?? WireMessageId.Empty));

                case MessageType.FutureResponse:
                    return(new RemoteMessage(
                               new FutureResponse(message.Data, message.From, message.Header),
                               message?.To ?? Aid.Unknown,
                               message.Id ?? WireMessageId.Empty));

                case MessageType.FutureError:
                    return(new RemoteMessage(
                               new FutureError(message.Exception, message.From, message.Header),
                               message?.To ?? Aid.Unknown,
                               message.Id ?? WireMessageId.Empty));
                }
            }

            return(RemoteMessage.Empty);
        }
Ejemplo n.º 2
0
        public static WireMessage ToWireMessage(this IMessage message, Aid to, WireMessageId id = null, Exception exception = null)
        {
            var result = new WireMessage {
                To = to, Id = id ?? WireMessageId.Next()
            };

            if (message != null)
            {
                result.MessageType = message.MessageType;
                result.From        = message.From;
                result.Data        = message.Data;
                result.TimeoutMSec = message.TimeoutMSec;

                var msgHeader   = message.Header;
                var headerCount = msgHeader?.Count ?? 0;

                if (headerCount > 0)
                {
                    var header = new Dictionary <string, string>(headerCount);
                    foreach (var kv in msgHeader)
                    {
                        header.Add(kv.Key, kv.Value);
                    }
                    result.Header = header;
                }

                var state = WireMessageState.Default;
                if (message.IsEmpty)
                {
                    state |= WireMessageState.Empty;
                }

                if (message is IFutureMessage future)
                {
                    if (future.IsCanceled)
                    {
                        state |= WireMessageState.Canceled;
                    }

                    if (future.IsCompleted)
                    {
                        state |= WireMessageState.Completed;
                    }

                    if (future.IsFaulted)
                    {
                        state |= WireMessageState.Faulted;
                    }
                }

                if (exception != null)
                {
                    result.Exception = exception;
                    state           |= WireMessageState.Faulted;
                }
                else if (message is IFutureError error)
                {
                    result.Exception = error.Exception;
                    state           |= WireMessageState.Faulted;
                }

                result.State = state;
            }
            return(result);
        }