From 5d41b218f010291bae01c9403cc306ad016adc5a Mon Sep 17 00:00:00 2001 From: moozzyk Date: Tue, 11 Oct 2016 15:51:48 -0700 Subject: [PATCH] Fixing line protocol --- .../InvocationDescriptorLineFormatter.cs | 54 ------------------- ...InvocationResultDescriptorLineFormatter.cs | 32 ----------- .../SocketsSample/JSonInvocationAdapter.cs | 20 +++---- .../SocketsSample/LineInvocationAdapter.cs | 52 ++++++++++++++++++ samples/SocketsSample/RpcJSonFormatter.cs | 27 ---------- samples/SocketsSample/Startup.cs | 12 +---- 6 files changed, 64 insertions(+), 133 deletions(-) delete mode 100644 samples/SocketsSample/InvocationDescriptorLineFormatter.cs delete mode 100644 samples/SocketsSample/InvocationResultDescriptorLineFormatter.cs create mode 100644 samples/SocketsSample/LineInvocationAdapter.cs delete mode 100644 samples/SocketsSample/RpcJSonFormatter.cs diff --git a/samples/SocketsSample/InvocationDescriptorLineFormatter.cs b/samples/SocketsSample/InvocationDescriptorLineFormatter.cs deleted file mode 100644 index 01708d1227..0000000000 --- a/samples/SocketsSample/InvocationDescriptorLineFormatter.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Sockets; - -namespace SocketsSample -{ - public class InvocationDescriptorLineFormatter: IFormatter - { - public async Task ReadAsync(Stream stream) - { - var streamReader = new StreamReader(stream); - var line = await streamReader.ReadLineAsync(); - var values = line.Split(','); - - return new InvocationDescriptor - { - Id = values[0].Substring(2), - Method = values[1].Substring(1), - Arguments = values.Skip(2).ToArray() - }; - } - - public async Task WriteAsync(InvocationDescriptor value, Stream stream) - { - var msg = $"CI{value.Id},M{value.Method},{string.Join(",", value.Arguments.Select(a => a.ToString()))}\n"; - await WriteAsync(stream, msg); - return; - } - - private async Task WriteAsync(Stream stream, string msg) - { - var writer = new StreamWriter(stream); - await writer.WriteAsync(msg); - await writer.FlushAsync(); - } - } - - /* - var result = value as InvocationResultDescriptor; - if (result != null) - { - var msg = $"RI{result.Id}," + string.IsNullOrEmpty(result.Error) != null - ? $"E{result.Error}\n" - : $"R{result.Result.ToString()}\n"; - - await WriteAsync(stream, msg); - return; - } - - var invocation = value as InvocationDescriptor; - if (invocation != null) -*/ -} diff --git a/samples/SocketsSample/InvocationResultDescriptorLineFormatter.cs b/samples/SocketsSample/InvocationResultDescriptorLineFormatter.cs deleted file mode 100644 index 967802dab8..0000000000 --- a/samples/SocketsSample/InvocationResultDescriptorLineFormatter.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.IO; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Sockets; - -namespace SocketsSample -{ - public class InvocationResultDescriptorLineFormatter : IFormatter - { - public Task ReadAsync(Stream stream) - { - throw new NotImplementedException(); - } - - public async Task WriteAsync(InvocationResultDescriptor value, Stream stream) - { - var msg = $"RI{value.Id}," + - (!string.IsNullOrEmpty(value.Error) - ? $"E{value.Error}\n" - : $"R{value?.Result?.ToString() ?? string.Empty}\n"); - - await WriteAsync(stream, msg); - } - - private async Task WriteAsync(Stream stream, string msg) - { - var writer = new StreamWriter(stream); - await writer.WriteAsync(msg); - await writer.FlushAsync(); - } - } -} diff --git a/samples/SocketsSample/JSonInvocationAdapter.cs b/samples/SocketsSample/JSonInvocationAdapter.cs index 49262b075b..4b0b706e4e 100644 --- a/samples/SocketsSample/JSonInvocationAdapter.cs +++ b/samples/SocketsSample/JSonInvocationAdapter.cs @@ -7,35 +7,35 @@ namespace SocketsSample { public class JSonInvocationAdapter : IInvocationAdapter { - IServiceProvider _serviceProvider; private JsonSerializer _serializer = new JsonSerializer(); - public JSonInvocationAdapter(IServiceProvider serviceProvider) + public JSonInvocationAdapter() { - _serviceProvider = serviceProvider; } public async Task CreateInvocationDescriptor(Stream stream, Func getParams) { - // TODO: use a formatter (?) var reader = new JsonTextReader(new StreamReader(stream)); return await Task.Run(() => _serializer.Deserialize(reader)); } public Task WriteInvocationResult(Stream stream, InvocationResultDescriptor resultDescriptor) { - var writer = new JsonTextWriter(new StreamWriter(stream)); - _serializer.Serialize(writer, resultDescriptor); - writer.Flush(); + Write(stream, resultDescriptor); return Task.FromResult(0); } public Task InvokeClientMethod(Stream stream, InvocationDescriptor invocationDescriptor) { - var writer = new JsonTextWriter(new StreamWriter(stream)); - _serializer.Serialize(writer, invocationDescriptor); - writer.Flush(); + Write(stream, invocationDescriptor); return Task.FromResult(0); } + + private void Write(Stream stream, object value) + { + var writer = new JsonTextWriter(new StreamWriter(stream)); + _serializer.Serialize(writer, value); + writer.Flush(); + } } } diff --git a/samples/SocketsSample/LineInvocationAdapter.cs b/samples/SocketsSample/LineInvocationAdapter.cs new file mode 100644 index 0000000000..af1dce60b6 --- /dev/null +++ b/samples/SocketsSample/LineInvocationAdapter.cs @@ -0,0 +1,52 @@ + +using System; +using System.IO; +using System.Linq; +using System.Threading.Tasks; + +namespace SocketsSample +{ + public class LineInvocationAdapter : IInvocationAdapter + { + public async Task CreateInvocationDescriptor(Stream stream, Func getParams) + { + var streamReader = new StreamReader(stream); + var line = await streamReader.ReadLineAsync(); + var values = line.Split(','); + + var method = values[1].Substring(1); + + return new InvocationDescriptor + { + Id = values[0].Substring(2), + Method = method, + Arguments = values.Skip(2).Zip(getParams(method), (v, t) => Convert.ChangeType(v, t)).ToArray() + }; + } + + public async Task InvokeClientMethod(Stream stream, InvocationDescriptor invocationDescriptor) + { + var msg = $"CI{invocationDescriptor.Id},M{invocationDescriptor.Method},{string.Join(",", invocationDescriptor.Arguments.Select(a => a.ToString()))}\n"; + await WriteAsync(stream, msg); + } + + public async Task WriteInvocationResult(Stream stream, InvocationResultDescriptor resultDescriptor) + { + if (string.IsNullOrEmpty(resultDescriptor.Error)) + { + await WriteAsync(stream, $"RI{resultDescriptor.Id},E{resultDescriptor.Error}\n"); + } + else + { + await WriteAsync(stream, $"RI{resultDescriptor.Id},R{(resultDescriptor.Result != null ? resultDescriptor.Result.ToString() : string.Empty)}\n"); + } + } + + private async Task WriteAsync(Stream stream, string msg) + { + var writer = new StreamWriter(stream); + await writer.WriteAsync(msg); + await writer.FlushAsync(); + } + } +} diff --git a/samples/SocketsSample/RpcJSonFormatter.cs b/samples/SocketsSample/RpcJSonFormatter.cs deleted file mode 100644 index e58d9875e5..0000000000 --- a/samples/SocketsSample/RpcJSonFormatter.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.IO; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Sockets; -using Newtonsoft.Json; - -namespace SocketsSample -{ - public class RpcJSonFormatter: IFormatter - { - private JsonSerializer _serializer = new JsonSerializer(); - - public async Task ReadAsync(Stream stream) - { - var reader = new JsonTextReader(new StreamReader(stream)); - return await Task.Run(() => _serializer.Deserialize(reader)); - } - - public Task WriteAsync(T value, Stream stream) - { - var writer = new JsonTextWriter(new StreamWriter(stream)); - _serializer.Serialize(writer, value); - writer.Flush(); - return Task.FromResult(0); - } - } -} diff --git a/samples/SocketsSample/Startup.cs b/samples/SocketsSample/Startup.cs index dc88274411..be9dc3a8ad 100644 --- a/samples/SocketsSample/Startup.cs +++ b/samples/SocketsSample/Startup.cs @@ -21,10 +21,6 @@ namespace SocketsSample services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton>(); - services.AddSingleton>(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -49,13 +45,9 @@ namespace SocketsSample app.UseFormatters(formatters=> { - formatters.MapFormatter("line"); - formatters.MapFormatter("line"); - formatters.MapFormatter>("json"); - formatters.MapFormatter>("json"); - formatters.AddInvocationAdapter("protobuf", new Protobuf.ProtobufInvocationAdapter()); - formatters.AddInvocationAdapter("json", new JSonInvocationAdapter(app.ApplicationServices)); + formatters.AddInvocationAdapter("json", new JSonInvocationAdapter()); + formatters.AddInvocationAdapter("line", new LineInvocationAdapter()); }); } }