From f3dfb62af96daeac238eb2f16998cae29a51426c Mon Sep 17 00:00:00 2001 From: moozzyk Date: Mon, 7 Nov 2016 13:06:16 -0800 Subject: [PATCH] Adding protobuf formatter --- .../ProtobufWeatherStreamFormatter.cs | 40 +++ .../SocialWeather/Protobuf/WeatherReport.cs | 263 ++++++++++++++++++ .../Protobuf/WeatherReport.proto | 19 ++ .../ProtobufWeatherStreamFormatter.cs | 19 -- samples/SocialWeather/SocialWeather.xproj | 6 +- samples/SocialWeather/Startup.cs | 1 + samples/SocialWeather/project.json | 3 +- 7 files changed, 327 insertions(+), 24 deletions(-) create mode 100644 samples/SocialWeather/Protobuf/ProtobufWeatherStreamFormatter.cs create mode 100644 samples/SocialWeather/Protobuf/WeatherReport.cs create mode 100644 samples/SocialWeather/Protobuf/WeatherReport.proto delete mode 100644 samples/SocialWeather/ProtobufWeatherStreamFormatter.cs diff --git a/samples/SocialWeather/Protobuf/ProtobufWeatherStreamFormatter.cs b/samples/SocialWeather/Protobuf/ProtobufWeatherStreamFormatter.cs new file mode 100644 index 0000000000..bbc224e5f1 --- /dev/null +++ b/samples/SocialWeather/Protobuf/ProtobufWeatherStreamFormatter.cs @@ -0,0 +1,40 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using Google.Protobuf; + +namespace SocialWeather.Protobuf +{ + public class ProtobufWeatherStreamFormatter : IStreamFormatter + { + public Task ReadAsync(Stream stream) + { + var inputStream = new CodedInputStream(stream, leaveOpen: true); + var protoWeatherReport = new Protobuf.WeatherReport(); + inputStream.ReadMessage(protoWeatherReport); + return Task.FromResult(new SocialWeather.WeatherReport + { + Temperature = protoWeatherReport.Temperature, + ReportTime = protoWeatherReport.ReportTime, + Weather = (Weather)(int)protoWeatherReport.Weather, + ZipCode = protoWeatherReport.ZipCode + }); + } + + public async Task WriteAsync(SocialWeather.WeatherReport weatherReport, Stream stream) + { + var outputStream = new CodedOutputStream(stream, leaveOpen: true); + var protoWeatherReport = new Protobuf.WeatherReport + { + Temperature = weatherReport.Temperature, + ReportTime = weatherReport.ReportTime, + Weather = (Protobuf.WeatherReport.Types.WeatherKind)(int)weatherReport.Weather, + ZipCode = weatherReport.ZipCode + }; + + outputStream.WriteMessage(protoWeatherReport); + outputStream.Flush(); + await stream.FlushAsync(); + } + } +} diff --git a/samples/SocialWeather/Protobuf/WeatherReport.cs b/samples/SocialWeather/Protobuf/WeatherReport.cs new file mode 100644 index 0000000000..12f608310b --- /dev/null +++ b/samples/SocialWeather/Protobuf/WeatherReport.cs @@ -0,0 +1,263 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: WeatherReport.proto +#pragma warning disable 1591, 0612, 3021 +#region Designer generated code + +using pb = global::Google.Protobuf; +using pbc = global::Google.Protobuf.Collections; +using pbr = global::Google.Protobuf.Reflection; +using scg = global::System.Collections.Generic; +namespace SocialWeather.Protobuf { + + /// Holder for reflection information generated from WeatherReport.proto + public static partial class WeatherReportReflection { + + #region Descriptor + /// File descriptor for WeatherReport.proto + public static pbr::FileDescriptor Descriptor { + get { return descriptor; } + } + private static pbr::FileDescriptor descriptor; + + static WeatherReportReflection() { + byte[] descriptorData = global::System.Convert.FromBase64String( + string.Concat( + "ChNXZWF0aGVyUmVwb3J0LnByb3RvIuIBCg1XZWF0aGVyUmVwb3J0EhMKC1Rl", + "bXBlcmF0dXJlGAEgASgFEhIKClJlcG9ydFRpbWUYAiABKAMSKwoHV2VhdGhl", + "chgDIAEoDjIaLldlYXRoZXJSZXBvcnQuV2VhdGhlcktpbmQSDwoHWmlwQ29k", + "ZRgEIAEoCSJqCgtXZWF0aGVyS2luZBIJCgVTdW5ueRAAEg8KC01vc3RseVN1", + "bm55EAESDwoLUGFydGx5U3VubnkQAhIQCgxQYXJ0bHlDbG91ZHkQAxIQCgxN", + "b3N0bHlDbG91ZHkQBBIKCgZDbG91ZHkQBUIZqgIWU29jaWFsV2VhdGhlci5Q", + "cm90b2J1ZmIGcHJvdG8z")); + descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, + new pbr::FileDescriptor[] { }, + new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] { + new pbr::GeneratedClrTypeInfo(typeof(global::SocialWeather.Protobuf.WeatherReport), global::SocialWeather.Protobuf.WeatherReport.Parser, new[]{ "Temperature", "ReportTime", "Weather", "ZipCode" }, null, new[]{ typeof(global::SocialWeather.Protobuf.WeatherReport.Types.WeatherKind) }, null) + })); + } + #endregion + + } + #region Messages + public sealed partial class WeatherReport : pb::IMessage { + private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new WeatherReport()); + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public static pb::MessageParser Parser { get { return _parser; } } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public static pbr::MessageDescriptor Descriptor { + get { return global::SocialWeather.Protobuf.WeatherReportReflection.Descriptor.MessageTypes[0]; } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + pbr::MessageDescriptor pb::IMessage.Descriptor { + get { return Descriptor; } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public WeatherReport() { + OnConstruction(); + } + + partial void OnConstruction(); + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public WeatherReport(WeatherReport other) : this() { + temperature_ = other.temperature_; + reportTime_ = other.reportTime_; + weather_ = other.weather_; + zipCode_ = other.zipCode_; + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public WeatherReport Clone() { + return new WeatherReport(this); + } + + /// Field number for the "Temperature" field. + public const int TemperatureFieldNumber = 1; + private int temperature_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int Temperature { + get { return temperature_; } + set { + temperature_ = value; + } + } + + /// Field number for the "ReportTime" field. + public const int ReportTimeFieldNumber = 2; + private long reportTime_; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public long ReportTime { + get { return reportTime_; } + set { + reportTime_ = value; + } + } + + /// Field number for the "Weather" field. + public const int WeatherFieldNumber = 3; + private global::SocialWeather.Protobuf.WeatherReport.Types.WeatherKind weather_ = 0; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public global::SocialWeather.Protobuf.WeatherReport.Types.WeatherKind Weather { + get { return weather_; } + set { + weather_ = value; + } + } + + /// Field number for the "ZipCode" field. + public const int ZipCodeFieldNumber = 4; + private string zipCode_ = ""; + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public string ZipCode { + get { return zipCode_; } + set { + zipCode_ = pb::ProtoPreconditions.CheckNotNull(value, "value"); + } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override bool Equals(object other) { + return Equals(other as WeatherReport); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public bool Equals(WeatherReport other) { + if (ReferenceEquals(other, null)) { + return false; + } + if (ReferenceEquals(other, this)) { + return true; + } + if (Temperature != other.Temperature) return false; + if (ReportTime != other.ReportTime) return false; + if (Weather != other.Weather) return false; + if (ZipCode != other.ZipCode) return false; + return true; + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override int GetHashCode() { + int hash = 1; + if (Temperature != 0) hash ^= Temperature.GetHashCode(); + if (ReportTime != 0L) hash ^= ReportTime.GetHashCode(); + if (Weather != 0) hash ^= Weather.GetHashCode(); + if (ZipCode.Length != 0) hash ^= ZipCode.GetHashCode(); + return hash; + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public override string ToString() { + return pb::JsonFormatter.ToDiagnosticString(this); + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void WriteTo(pb::CodedOutputStream output) { + if (Temperature != 0) { + output.WriteRawTag(8); + output.WriteInt32(Temperature); + } + if (ReportTime != 0L) { + output.WriteRawTag(16); + output.WriteInt64(ReportTime); + } + if (Weather != 0) { + output.WriteRawTag(24); + output.WriteEnum((int) Weather); + } + if (ZipCode.Length != 0) { + output.WriteRawTag(34); + output.WriteString(ZipCode); + } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public int CalculateSize() { + int size = 0; + if (Temperature != 0) { + size += 1 + pb::CodedOutputStream.ComputeInt32Size(Temperature); + } + if (ReportTime != 0L) { + size += 1 + pb::CodedOutputStream.ComputeInt64Size(ReportTime); + } + if (Weather != 0) { + size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Weather); + } + if (ZipCode.Length != 0) { + size += 1 + pb::CodedOutputStream.ComputeStringSize(ZipCode); + } + return size; + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void MergeFrom(WeatherReport other) { + if (other == null) { + return; + } + if (other.Temperature != 0) { + Temperature = other.Temperature; + } + if (other.ReportTime != 0L) { + ReportTime = other.ReportTime; + } + if (other.Weather != 0) { + Weather = other.Weather; + } + if (other.ZipCode.Length != 0) { + ZipCode = other.ZipCode; + } + } + + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public void MergeFrom(pb::CodedInputStream input) { + uint tag; + while ((tag = input.ReadTag()) != 0) { + switch(tag) { + default: + input.SkipLastField(); + break; + case 8: { + Temperature = input.ReadInt32(); + break; + } + case 16: { + ReportTime = input.ReadInt64(); + break; + } + case 24: { + weather_ = (global::SocialWeather.Protobuf.WeatherReport.Types.WeatherKind) input.ReadEnum(); + break; + } + case 34: { + ZipCode = input.ReadString(); + break; + } + } + } + } + + #region Nested types + /// Container for nested types declared in the WeatherReport message type. + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + public static partial class Types { + public enum WeatherKind { + [pbr::OriginalName("Sunny")] Sunny = 0, + [pbr::OriginalName("MostlySunny")] MostlySunny = 1, + [pbr::OriginalName("PartlySunny")] PartlySunny = 2, + [pbr::OriginalName("PartlyCloudy")] PartlyCloudy = 3, + [pbr::OriginalName("MostlyCloudy")] MostlyCloudy = 4, + [pbr::OriginalName("Cloudy")] Cloudy = 5, + } + + } + #endregion + + } + + #endregion + +} + +#endregion Designer generated code diff --git a/samples/SocialWeather/Protobuf/WeatherReport.proto b/samples/SocialWeather/Protobuf/WeatherReport.proto new file mode 100644 index 0000000000..313be7874f --- /dev/null +++ b/samples/SocialWeather/Protobuf/WeatherReport.proto @@ -0,0 +1,19 @@ +syntax = "proto3"; +option csharp_namespace = "SocialWeather.Protobuf"; + +message WeatherReport { + + enum WeatherKind { + Sunny = 0; + MostlySunny = 1; + PartlySunny = 2; + PartlyCloudy = 3; + MostlyCloudy = 4; + Cloudy = 5; + } + + int32 Temperature = 1; + int64 ReportTime = 2; + WeatherKind Weather = 3; + string ZipCode = 4; +} \ No newline at end of file diff --git a/samples/SocialWeather/ProtobufWeatherStreamFormatter.cs b/samples/SocialWeather/ProtobufWeatherStreamFormatter.cs deleted file mode 100644 index b93d6a5021..0000000000 --- a/samples/SocialWeather/ProtobufWeatherStreamFormatter.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.IO; -using System.Threading.Tasks; - -namespace SocialWeather -{ - public class ProtobufWeatherStreamFormatter : IStreamFormatter - { - public Task ReadAsync(Stream stream) - { - throw new NotImplementedException(); - } - - public Task WriteAsync(WeatherReport value, Stream stream) - { - throw new NotImplementedException(); - } - } -} diff --git a/samples/SocialWeather/SocialWeather.xproj b/samples/SocialWeather/SocialWeather.xproj index acc78cdcb5..0f982c8d9a 100644 --- a/samples/SocialWeather/SocialWeather.xproj +++ b/samples/SocialWeather/SocialWeather.xproj @@ -4,16 +4,14 @@ 14.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - 8d789f94-cb74-45fd-ace7-92af6e55042e - PersisitentConnection + SocialWeather .\obj .\bin\ v4.6 - 2.0 @@ -22,4 +20,4 @@ - + \ No newline at end of file diff --git a/samples/SocialWeather/Startup.cs b/samples/SocialWeather/Startup.cs index 6209e4580f..4b539789f1 100644 --- a/samples/SocialWeather/Startup.cs +++ b/samples/SocialWeather/Startup.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using SocialWeather.Protobuf; namespace SocialWeather { diff --git a/samples/SocialWeather/project.json b/samples/SocialWeather/project.json index 69eeb08e1a..0e9ec1ac8d 100644 --- a/samples/SocialWeather/project.json +++ b/samples/SocialWeather/project.json @@ -12,7 +12,8 @@ "Microsoft.AspNetCore.Sockets": { "target": "project" }, - "Microsoft.AspNetCore.StaticFiles": "1.1.0-*" + "Microsoft.AspNetCore.StaticFiles": "1.1.0-*", + "Google.Protobuf": "3.1.0" }, "tools": {