Exemple #1
0
        public async Task <BlazeMessageData> ProcessMessage(BlazeMessageData requestMessageData)
        {
            var requestHeader  = requestMessageData.Header;
            var requestPayload = requestMessageData.Payload;

            if (_blazeTypeLookup.TryGetRequestType(requestHeader.Component, requestHeader.Command, out var requestType))
            {
                try
                {
                    var parsedRequest = _blazeDeserializer.Deserialize(requestPayload, requestType);

                    var response = (BlazeResponse)await _mediator.Send(parsedRequest);

                    if (response == null)
                    {
                        Logger.Warn($"Response was null for request {requestType}");
                        return(null);
                    }
                    var messageType    = response.BlazeErrorCode > 0 ? BlazeMessageType.ErrorReply : BlazeMessageType.Reply;
                    var responseHeader = new BlazeHeader
                    {
                        Component   = requestHeader.Component,
                        Command     = requestHeader.Command,
                        ErrorCode   = response.BlazeErrorCode, //TODO: have a better way to handle error code set by the handlers
                        MessageType = messageType,
                        MessageId   = requestHeader.MessageId
                    };

                    //TODO: remove stream
                    var output = new MemoryStream();
                    _blazeSerializer.Serialize(output, response);

                    var responseMessage = new BlazeMessageData
                    {
                        Header  = responseHeader,
                        Payload = new ReadOnlySequence <byte>(output.ToArray())
                    };

                    return(responseMessage);
                }
                catch (Exception e)
                {
                    Logger.Error(e);
                    return(null);
                }
            }

            Logger.Error($"Unknown Component: {requestHeader.Component} and Command: 0x{requestHeader.Command:X2}");
            _debugParser.TryParseBody(requestPayload);
            return(null);
        }
        public async Task <IList <BlazeMessageData> > ProcessMessage(BlazeMessageData requestMessageData, ClientContext clientContext)
        {
            var requestHeader  = requestMessageData.Header;
            var requestPayload = requestMessageData.Payload;

            if (_blazeTypeLookup.TryGetRequestType(requestHeader.Component, requestHeader.Command, out var requestType))
            {
                try
                {
                    var parsedRequest = _blazeDeserializer.Deserialize(requestPayload, requestType);

                    var response = (IBlazeResponse)await _mediator.Send(parsedRequest);

                    var responseHeader = new BlazeHeader
                    {
                        Component   = requestHeader.Component,
                        Command     = requestHeader.Command,
                        ErrorCode   = 0,
                        MessageType = BlazeMessageType.Reply,
                        MessageId   = requestHeader.MessageId
                    };

                    //TODO: remove stream
                    var output = new MemoryStream();
                    _blazeSerializer.Serialize(output, response);

                    var responseMessage = new BlazeMessageData
                    {
                        Header  = responseHeader,
                        Payload = new ReadOnlySequence <byte>(output.ToArray())
                    };

                    var responseMessages = new List <BlazeMessageData>
                    {
                        responseMessage
                    };

                    //Send new notifications
                    while (clientContext.Notifications.TryDequeue(out var notification))
                    {
                        //TODO: remove stream
                        var notificationOutput = new MemoryStream();
                        _blazeSerializer.Serialize(notificationOutput, notification.Item2);
                        var messageData = new BlazeMessageData
                        {
                            Header  = notification.Item1,
                            Payload = new ReadOnlySequence <byte>(notificationOutput.ToArray())
                        };
                        responseMessages.Add(messageData);
                    }
                    return(responseMessages);
                }
                catch (Exception e)
                {
                    Logger.Error(e);
                    return(null);
                }
            }

            Logger.Error($"Unknown Component: {requestHeader.Component} and Command: 0x{requestHeader.Command:X2}");
            _debugParser.TryParseBody(requestPayload);
            return(null);
        }