Exemple #1
0
        private async Task SendReplyAsync(
            ulong conversationId, IMessage response, ILayerStack layerStack, RequestMetrics requestMetrics)
        {
            var sendContext = new EpoxySendContext(this, ConnectionMetrics, requestMetrics);

            IBonded layerData;
            Error   layerError = LayerStackUtils.ProcessOnSend(
                layerStack, MessageType.RESPONSE, sendContext, out layerData, logger);

            // If there was a layer error, replace the response with the layer error
            if (layerError != null)
            {
                logger.Site().Error("{0} Sending reply for conversation ID {1} failed due to layer error (Code: {2}, Message: {3}).",
                                    this, conversationId, layerError.error_code, layerError.message);

                // Set layer error as result of this Bond method call, replacing original response.
                // Since this error will be returned to client, cleanse out internal server error details, if any.
                response = Message.FromError(Errors.CleanseInternalServerError(layerError));
            }

            var frame = MessageToFrame(conversationId, null, null, EpoxyMessageType.RESPONSE, response, layerData, logger);

            logger.Site().Debug("{0} Sending reply for conversation ID {1}.", this, conversationId);

            bool wasSent = await SendFrameAsync(frame);

            logger.Site().Debug("{0} Sending reply for conversation ID {1} {2}.",
                                this, conversationId, wasSent ? "succeedeed" : "failed");
        }
Exemple #2
0
        internal async Task SendEventAsync(string methodName, IMessage message)
        {
            var conversationId = AllocateNextConversationId();

            var         sendContext = new EpoxySendContext(this);
            IBonded     layerData   = null;
            ILayerStack layerStack;
            Error       layerError = parentTransport.GetLayerStack(out layerStack);

            if (layerError == null)
            {
                layerError = LayerStackUtils.ProcessOnSend(
                    layerStack, MessageType.Event, sendContext, out layerData, logger);
            }

            if (layerError != null)
            {
                logger.Site().Error("{0} Sending event {1}/{2} failed due to layer error (Code: {3}, Message: {4}).",
                                    this, conversationId, methodName, layerError.error_code, layerError.message);
                return;
            }

            var frame = MessageToFrame(conversationId, methodName, PayloadType.Event, message, layerData, logger);

            logger.Site().Debug("{0} Sending event {1}/{2}.", this, conversationId, methodName);

            bool wasSent = await SendFrameAsync(frame);

            logger.Site().Debug("{0} Sending event {1}/{2} {3}.",
                                this, conversationId, methodName, wasSent ? "succeeded" : "failed");
        }
Exemple #3
0
        internal async Task SendEventAsync(string methodName, IMessage message)
        {
            var conversationId = AllocateNextConversationId();

            var     sendContext = new EpoxySendContext(this);
            IBonded layerData;
            Error   layerError = LayerStackUtils.ProcessOnSend(parentTransport.LayerStack, MessageType.Event, sendContext, out layerData);

            if (layerError != null)
            {
                Log.Error("{0}.{1}: Sending event {2}/{3} failed due to layer error (Code: {4}, Message: {5}).",
                          this, nameof(SendEventAsync), conversationId, methodName, layerError.error_code, layerError.message);
                return;
            }

            var frame = MessageToFrame(conversationId, methodName, PayloadType.Event, message, layerData);

            Log.Debug("{0}.{1}: Sending event {2}/{3}.", this, nameof(SendEventAsync), conversationId, methodName);

            bool wasSent = await SendFrameAsync(frame);

            Log.Debug(
                "{0}.{1}: Sending event {2}/{3} {4}.",
                this, nameof(SendEventAsync), conversationId, methodName, wasSent ? "succeded" : "failed");
        }
Exemple #4
0
        private async Task <IMessage> SendRequestAsync <TPayload>(string serviceName, string methodName, IMessage <TPayload> request)
        {
            var conversationId = AllocateNextConversationId();
            var totalTime      = Stopwatch.StartNew();
            var requestMetrics = Metrics.StartRequestMetrics(ConnectionMetrics);
            var sendContext    = new EpoxySendContext(this, ConnectionMetrics, requestMetrics);

            IBonded     layerData = null;
            ILayerStack layerStack;
            Error       layerError = parentTransport.GetLayerStack(requestMetrics.request_id, out layerStack);

            if (layerError == null)
            {
                layerError = LayerStackUtils.ProcessOnSend(
                    layerStack, MessageType.REQUEST, sendContext, out layerData, logger);
            }

            if (layerError != null)
            {
                logger.Site().Error("{0} Sending request {1}/{2}.{3} failed due to layer error (Code: {4}, Message: {5}).",
                                    this, conversationId, serviceName, methodName, layerError.error_code, layerError.message);
                return(Message.FromError(layerError));
            }

            var frame = MessageToFrame(conversationId, serviceName, methodName, EpoxyMessageType.REQUEST, request, layerData, logger);

            logger.Site().Debug("{0} Sending request {1}/{2}.", this, conversationId, methodName);
            var responseTask = responseMap.Add(conversationId, layerStack);

            bool wasSent = await SendFrameAsync(frame);

            logger.Site().Debug("{0} Sending request {1}/{2}.{3} {4}.",
                                this, conversationId, serviceName, methodName, wasSent ? "succeeded" : "failed");

            if (!wasSent)
            {
                bool wasCompleted = responseMap.Complete(
                    conversationId,
                    Message.FromError(new Error
                {
                    error_code = (int)ErrorCode.TRANSPORT_ERROR,
                    message    = "Request could not be sent"
                }));

                if (!wasCompleted)
                {
                    logger.Site().Information("{0} Unsuccessfully sent request {1}/{2}.{3} still received response.",
                                              this, conversationId, serviceName, methodName);
                }
            }
            var message = await responseTask;

            Metrics.FinishRequestMetrics(requestMetrics, totalTime);
            metrics.Emit(requestMetrics);
            return(message);
        }
Exemple #5
0
        private async Task <IMessage> SendRequestAsync <TPayload>(string methodName, IMessage <TPayload> request)
        {
            var conversationId = AllocateNextConversationId();

            var sendContext = new EpoxySendContext(this);

            IBonded     layerData = null;
            ILayerStack layerStack;
            Error       layerError = parentTransport.GetLayerStack(out layerStack);

            if (layerError == null)
            {
                layerError = LayerStackUtils.ProcessOnSend(
                    layerStack, MessageType.Request, sendContext, out layerData, logger);
            }

            if (layerError != null)
            {
                logger.Site().Error("{0} Sending request {1}/{2} failed due to layer error (Code: {3}, Message: {4}).",
                                    this, conversationId, methodName, layerError.error_code, layerError.message);
                return(Message.FromError(layerError));
            }

            var frame = MessageToFrame(conversationId, methodName, PayloadType.Request, request, layerData, logger);

            logger.Site().Debug("{0} Sending request {1}/{2}.", this, conversationId, methodName);
            var responseTask = responseMap.Add(conversationId, layerStack);

            bool wasSent = await SendFrameAsync(frame);

            logger.Site().Debug("{0} Sending request {1}/{2} {3}.",
                                this, conversationId, methodName, wasSent ? "succeeded" : "failed");

            if (!wasSent)
            {
                bool wasCompleted = responseMap.Complete(
                    conversationId,
                    Message.FromError(new Error
                {
                    error_code = (int)ErrorCode.TransportError,
                    message    = "Request could not be sent"
                }));

                if (!wasCompleted)
                {
                    logger.Site().Information("{0} Unsuccessfully sent request {1}/{2} still received response.",
                                              this, conversationId, methodName);
                }
            }

            return(await responseTask);
        }
Exemple #6
0
        internal async Task SendEventAsync(string serviceName, string methodName, IMessage message)
        {
            var conversationId = AllocateNextConversationId();
            var totalTime      = Stopwatch.StartNew();
            var requestMetrics = Metrics.StartRequestMetrics(ConnectionMetrics);
            var sendContext    = new EpoxySendContext(this, ConnectionMetrics, requestMetrics);

            IBonded     layerData = null;
            ILayerStack layerStack;
            Error       layerError = parentTransport.GetLayerStack(requestMetrics.request_id, out layerStack);

            if (layerError == null)
            {
                layerError = LayerStackUtils.ProcessOnSend(
                    layerStack, MessageType.EVENT, sendContext, out layerData, logger);
            }

            if (layerError != null)
            {
                logger.Site().Error("{0} Sending event {1}/{2}.{3} failed due to layer error (Code: {4}, Message: {5}).",
                                    this, conversationId, serviceName, methodName, layerError.error_code, layerError.message);
                return;
            }

            var frame = MessageToFrame(conversationId, serviceName, methodName, EpoxyMessageType.EVENT, message, layerData, logger);

            logger.Site().Debug("{0} Sending event {1}/{2}.{3}.", this, conversationId, serviceName, methodName);

            bool wasSent = await SendFrameAsync(frame);

            logger.Site().Debug("{0} Sending event {1}/{2}.{3} {4}.",
                                this, conversationId, serviceName, methodName, wasSent ? "succeeded" : "failed");

            Metrics.FinishRequestMetrics(requestMetrics, totalTime);
            metrics.Emit(requestMetrics);
        }
Exemple #7
0
        private async Task SendReplyAsync(ulong conversationId, IMessage response)
        {
            var     sendContext = new EpoxySendContext(this);
            IBonded layerData;
            Error   layerError = LayerStackUtils.ProcessOnSend(parentTransport.LayerStack, MessageType.Response, sendContext, out layerData);

            // If there was a layer error, replace the response with the layer error
            if (layerError != null)
            {
                Log.Error("{0}.{1}: Sending reply for conversation ID {2} failed due to layer error (Code: {3}, Message: {4}).",
                          this, nameof(SendReplyAsync), conversationId, layerError.error_code, layerError.message);
                response = Message.FromError(layerError);
            }

            var frame = MessageToFrame(conversationId, null, PayloadType.Response, response, layerData);

            Log.Debug("{0}.{1}: Sending reply for conversation ID {2}.", this, nameof(SendReplyAsync), conversationId);

            bool wasSent = await SendFrameAsync(frame);

            Log.Debug(
                "{0}.{1}: Sending reply for conversation ID {2} {3}.",
                this, nameof(SendReplyAsync), conversationId, wasSent ? "succeded" : "failed");
        }