/// <summary>Calculates the CRC32 check-sum on specified portion of a buffer.</summary> /// <param name="data">Data buffer to perform check-sum on.</param> /// <param name="startIndex">Starts index in data buffer to begin check-sum.</param> /// <param name="length">Total number of bytes from <paramref name="startIndex">startIndex</paramref> to /// perform check-sum over.</param> /// <returns>Computed CRC32 checksum over the specified portion of the buffer.</returns> public static uint Crc32Checksum(this byte[] data, int startIndex, int length) { Crc32 checksum = new Crc32(); checksum.Update(data, startIndex, length); return checksum.Value; }
/// <summary> /// Calculates a CRC-32 based check-sum on this <see cref="MetadataRecord"/> instance. /// </summary> /// <returns>CRC-32 based check-sum on this <see cref="MetadataRecord"/> instance.</returns> public long CalculateChecksum() { Crc32 checksum = new Crc32(); checksum.Update(BitConverter.GetBytes(ID)); checksum.Update(Encoding.Default.GetBytes(UniqueID ?? "")); checksum.Update(Encoding.Default.GetBytes(PointTag ?? "")); checksum.Update(Encoding.Default.GetBytes(Source ?? "")); checksum.Update(Encoding.Default.GetBytes(Device ?? "")); checksum.Update(BitConverter.GetBytes(Longitude)); checksum.Update(BitConverter.GetBytes(Latitude)); checksum.Update(Encoding.Default.GetBytes(Protocol ?? "")); checksum.Update(Encoding.Default.GetBytes(SignalType ?? "")); checksum.Update(Encoding.Default.GetBytes(EngineeringUnits ?? "")); checksum.Update(Encoding.Default.GetBytes(PhasorType ?? "")); checksum.Update(Encoding.Default.GetBytes(Phase ?? "")); checksum.Update(Encoding.Default.GetBytes(Description ?? "")); checksum.Update(Encoding.Default.GetBytes(LastUpdate ?? "")); return checksum.Value; }
/// <summary> /// Renders an HTTP response for a given request. /// </summary> /// <param name="request">HTTP request message.</param> /// <param name="pageName">Name of page to render.</param> /// <param name="isPost"><c>true</c>if <paramref name="request"/> is HTTP post; otherwise, <c>false</c>.</param> /// <param name="cancellationToken">Propagates notification from client that operations should be canceled.</param> /// <param name="model">Reference to model to use when rendering Razor templates, if any.</param> /// <param name="modelType">Type of <paramref name="model"/>, if any.</param> /// <param name="database"><see cref="AdoDataConnection"/> to use, if any.</param> /// <returns>HTTP response for provided request.</returns> public async Task<HttpResponseMessage> RenderResponse(HttpRequestMessage request, string pageName, bool isPost, CancellationToken cancellationToken, object model = null, Type modelType = null, AdoDataConnection database = null) { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); string content, fileExtension = FilePath.GetExtension(pageName).ToLowerInvariant(); bool embeddedResource = pageName.StartsWith("@"); Tuple<Type, Type> pagedViewModelTypes; if (embeddedResource) pageName = pageName.Substring(1).Replace('/', '.'); response.RequestMessage = request; switch (fileExtension) { case ".cshtml": m_pagedViewModelTypes.TryGetValue(pageName, out pagedViewModelTypes); content = await new RazorView(embeddedResource ? RazorEngine<CSharpEmbeddedResource>.Default : m_razorEngineCS, pageName, model, modelType, pagedViewModelTypes?.Item1, pagedViewModelTypes?.Item2, database, OnExecutionException).ExecuteAsync(request, isPost, cancellationToken); response.Content = new StringContent(content, Encoding.UTF8, "text/html"); break; case ".vbhtml": m_pagedViewModelTypes.TryGetValue(pageName, out pagedViewModelTypes); content = await new RazorView(embeddedResource ? RazorEngine<VisualBasicEmbeddedResource>.Default : m_razorEngineVB, pageName, model, modelType, pagedViewModelTypes?.Item1, pagedViewModelTypes?.Item2, database, OnExecutionException).ExecuteAsync(request, isPost, cancellationToken); response.Content = new StringContent(content, Encoding.UTF8, "text/html"); break; case ".ashx": await ProcessHTTPHandlerAsync(pageName, embeddedResource, request, response, cancellationToken); break; default: string fileName = GetResourceFileName(pageName, embeddedResource); if (ClientCacheEnabled) { long responseHash; if (!m_etagCache.TryGetValue(fileName, out responseHash)) { if (!ResourceExists(fileName, embeddedResource)) { response.StatusCode = HttpStatusCode.NotFound; break; } await Task.Run(async () => { using (Stream source = await OpenResourceAsync(fileName, embeddedResource, cancellationToken)) { // Calculate check-sum for file const int BufferSize = 32768; byte[] buffer = new byte[BufferSize]; Crc32 calculatedHash = new Crc32(); int bytesRead = await source.ReadAsync(buffer, 0, BufferSize, cancellationToken); while (bytesRead > 0) { calculatedHash.Update(buffer, 0, bytesRead); bytesRead = await source.ReadAsync(buffer, 0, BufferSize, cancellationToken); } responseHash = calculatedHash.Value; m_etagCache.TryAdd(fileName, responseHash); OnStatusMessage($"Cache [{responseHash}] added for file \"{fileName}\""); } }, cancellationToken); } if (PublishResponseContent(request, response, responseHash)) { response.Content = new StreamContent(await OpenResourceAsync(fileName, embeddedResource, cancellationToken)); response.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(pageName)); } } else { if (!ResourceExists(fileName, embeddedResource)) { response.StatusCode = HttpStatusCode.NotFound; break; } response.Content = new StreamContent(await OpenResourceAsync(fileName, embeddedResource, cancellationToken)); response.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(pageName)); } break; } return response; }