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 RelayEpoxySendContext(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); }
async Task SendReplyAsync( ulong conversationId, IMessage response, ILayerStack layerStack, RequestMetrics requestMetrics) { var sendContext = new RelayEpoxySendContext(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"); }
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 RelayEpoxySendContext(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); }