/// <summary> /// Write rpc result struct data to remote side. /// </summary> /// <param name="context">The context of the http.</param> /// <param name="result">The result data to write</param> /// <param name="cancellationToken">The cancellation which can cancel this method</param> /// <returns>Void</returns> private async Task WriteRpcResultAsync(IJsonRpcHttpContext context, byte[] result, CancellationToken cancellationToken = default) { context.SetResponseHeader("Server", "JsonRpcLite"); context.SetResponseHeader("Access-Control-Allow-Origin", "*"); context.SetResponseStatusCode((int)HttpStatusCode.OK); context.SetResponseContentType("application/json"); if (result != null) { var outputData = await GetOutputDataAsync(context, result, cancellationToken).ConfigureAwait(false); context.SetResponseContentLength(outputData.Length); var outputStream = context.GetOutputStream(); await outputStream.WriteAsync(outputData, cancellationToken).ConfigureAwait(false); await outputStream.FlushAsync(cancellationToken); } if (Logger.DebugMode) { if (result != null) { var resultString = Encoding.UTF8.GetString(result); Logger.WriteDebug($"Response data sent:{resultString}"); } } }
/// <summary> /// Write http message to remote side. /// </summary> /// <param name="context">The http context.</param> /// <param name="statusCode">The status code to return</param> /// <param name="message">The message to write back.</param> /// <param name="cancellationToken">The cancellation token which will cancel this method.</param> /// <returns>Void</returns> private async Task WriteHttpResultAsync(IJsonRpcHttpContext context, int statusCode, string message, CancellationToken cancellationToken = default) { context.SetResponseHeader("Server", "JsonRpcLite"); context.SetResponseHeader("Access-Control-Allow-Origin", "*"); context.SetResponseStatusCode(statusCode); context.SetResponseContentType("text/html"); var outputData = await GetOutputDataAsync(context, Encoding.UTF8.GetBytes(message), cancellationToken).ConfigureAwait(false); context.SetResponseContentLength(outputData.Length); var outputStream = context.GetOutputStream(); await outputStream.WriteAsync(outputData, cancellationToken).ConfigureAwait(false); await outputStream.FlushAsync(cancellationToken); if (Logger.DebugMode) { Logger.WriteDebug($"Response data sent:{message}"); } }