diff --git a/samples/SocketsSample/EndPoints/ChatEndPoint.cs b/samples/SocketsSample/EndPoints/ChatEndPoint.cs index 60afc6bee2..18218b025a 100644 --- a/samples/SocketsSample/EndPoints/ChatEndPoint.cs +++ b/samples/SocketsSample/EndPoints/ChatEndPoint.cs @@ -8,15 +8,11 @@ namespace SocketsSample { public class ChatEndPoint : EndPoint { - public ChatEndPoint() - { - } public override async Task OnConnected(Connection connection) { await Broadcast($"{connection.ConnectionId} connected ({connection.Metadata["transport"]})"); - while (true) { var result = await connection.Channel.Input.ReadAsync(); diff --git a/samples/SocketsSample/EndPoints/HubEndpoint.cs b/samples/SocketsSample/EndPoints/HubEndpoint.cs index 17d52ca9af..a8b6afeab2 100644 --- a/samples/SocketsSample/EndPoints/HubEndpoint.cs +++ b/samples/SocketsSample/EndPoints/HubEndpoint.cs @@ -63,10 +63,12 @@ namespace SocketsSample foreach (var connection in _endPoint.Connections) { - var invocationAdapter = _endPoint._serviceProvider.GetRequiredService() - .GetInvocationAdapter((string)connection.Metadata["formatType"]); + var invocationAdapter = + _endPoint._serviceProvider + .GetRequiredService() + .GetInvocationAdapter((string)connection.Metadata["formatType"]); - tasks.Add(invocationAdapter.InvokeClientMethod(connection.Channel.GetStream(), message)); + tasks.Add(invocationAdapter.WriteInvocationDescriptor(message, connection.Channel.GetStream())); } return Task.WhenAll(tasks); @@ -88,8 +90,10 @@ namespace SocketsSample { var connection = _endPoint.Connections[_connectionId]; - var invocationAdapter = _endPoint._serviceProvider.GetRequiredService() - .GetInvocationAdapter((string)connection.Metadata["formatType"]); + var invocationAdapter = + _endPoint._serviceProvider + .GetRequiredService() + .GetInvocationAdapter((string)connection.Metadata["formatType"]); if (_endPoint._logger.IsEnabled(LogLevel.Debug)) { @@ -102,7 +106,7 @@ namespace SocketsSample Arguments = args }; - return invocationAdapter.InvokeClientMethod(connection.Channel.GetStream(), message); + return invocationAdapter.WriteInvocationDescriptor(message, connection.Channel.GetStream()); } } } diff --git a/samples/SocketsSample/EndPoints/RpcEndpoint.cs b/samples/SocketsSample/EndPoints/RpcEndpoint.cs index 216b310ed3..65c1cd6fda 100644 --- a/samples/SocketsSample/EndPoints/RpcEndpoint.cs +++ b/samples/SocketsSample/EndPoints/RpcEndpoint.cs @@ -44,13 +44,15 @@ namespace SocketsSample await Task.Yield(); var stream = connection.Channel.GetStream(); - var invocationAdapter = _serviceProvider.GetRequiredService() - .GetInvocationAdapter((string)connection.Metadata["formatType"]); + var invocationAdapter = + _serviceProvider + .GetRequiredService() + .GetInvocationAdapter((string)connection.Metadata["formatType"]); while (true) { var invocationDescriptor = - await invocationAdapter.CreateInvocationDescriptor( + await invocationAdapter.ReadInvocationDescriptor( stream, methodName => { Type[] types; // TODO: null or throw? @@ -84,7 +86,7 @@ namespace SocketsSample }; } - await invocationAdapter.WriteInvocationResult(stream, result); + await invocationAdapter.WriteInvocationResult(result, stream); } } diff --git a/samples/SocketsSample/IInvocationAdapter.cs b/samples/SocketsSample/IInvocationAdapter.cs index 8900fea3e3..2f4436e245 100644 --- a/samples/SocketsSample/IInvocationAdapter.cs +++ b/samples/SocketsSample/IInvocationAdapter.cs @@ -6,10 +6,10 @@ namespace SocketsSample { public interface IInvocationAdapter { - Task CreateInvocationDescriptor(Stream stream, Func getParams); + Task ReadInvocationDescriptor(Stream stream, Func getParams); - Task WriteInvocationResult(Stream stream, InvocationResultDescriptor resultDescriptor); + Task WriteInvocationResult(InvocationResultDescriptor resultDescriptor, Stream stream); - Task InvokeClientMethod(Stream stream, InvocationDescriptor invocationDescriptor); + Task WriteInvocationDescriptor(InvocationDescriptor invocationDescriptor, Stream stream); } } diff --git a/samples/SocketsSample/JSonInvocationAdapter.cs b/samples/SocketsSample/JSonInvocationAdapter.cs index 4b0b706e4e..c14d576e53 100644 --- a/samples/SocketsSample/JSonInvocationAdapter.cs +++ b/samples/SocketsSample/JSonInvocationAdapter.cs @@ -5,33 +5,33 @@ using Newtonsoft.Json; namespace SocketsSample { - public class JSonInvocationAdapter : IInvocationAdapter + public class JsonInvocationAdapter : IInvocationAdapter { private JsonSerializer _serializer = new JsonSerializer(); - public JSonInvocationAdapter() + public JsonInvocationAdapter() { } - public async Task CreateInvocationDescriptor(Stream stream, Func getParams) + public async Task ReadInvocationDescriptor(Stream stream, Func getParams) { var reader = new JsonTextReader(new StreamReader(stream)); return await Task.Run(() => _serializer.Deserialize(reader)); } - public Task WriteInvocationResult(Stream stream, InvocationResultDescriptor resultDescriptor) + public Task WriteInvocationResult(InvocationResultDescriptor resultDescriptor, Stream stream) { - Write(stream, resultDescriptor); + Write(resultDescriptor, stream); return Task.FromResult(0); } - public Task InvokeClientMethod(Stream stream, InvocationDescriptor invocationDescriptor) + public Task WriteInvocationDescriptor(InvocationDescriptor invocationDescriptor, Stream stream) { - Write(stream, invocationDescriptor); + Write(invocationDescriptor, stream); return Task.FromResult(0); } - private void Write(Stream stream, object value) + private void Write(object value, Stream stream) { var writer = new JsonTextWriter(new StreamWriter(stream)); _serializer.Serialize(writer, value); diff --git a/samples/SocketsSample/LineInvocationAdapter.cs b/samples/SocketsSample/LineInvocationAdapter.cs index 8bed0c94c3..e079c6d79a 100644 --- a/samples/SocketsSample/LineInvocationAdapter.cs +++ b/samples/SocketsSample/LineInvocationAdapter.cs @@ -8,7 +8,7 @@ namespace SocketsSample { public class LineInvocationAdapter : IInvocationAdapter { - public async Task CreateInvocationDescriptor(Stream stream, Func getParams) + public async Task ReadInvocationDescriptor(Stream stream, Func getParams) { var streamReader = new StreamReader(stream); var line = await streamReader.ReadLineAsync(); @@ -29,25 +29,25 @@ namespace SocketsSample }; } - public async Task InvokeClientMethod(Stream stream, InvocationDescriptor invocationDescriptor) + public async Task WriteInvocationDescriptor(InvocationDescriptor invocationDescriptor, Stream stream) { var msg = $"CI{invocationDescriptor.Id},M{invocationDescriptor.Method},{string.Join(",", invocationDescriptor.Arguments.Select(a => a.ToString()))}\n"; - await WriteAsync(stream, msg); + await WriteAsync(msg, stream); } - public async Task WriteInvocationResult(Stream stream, InvocationResultDescriptor resultDescriptor) + public async Task WriteInvocationResult(InvocationResultDescriptor resultDescriptor, Stream stream) { if (string.IsNullOrEmpty(resultDescriptor.Error)) { - await WriteAsync(stream, $"RI{resultDescriptor.Id},E{resultDescriptor.Error}\n"); + await WriteAsync($"RI{resultDescriptor.Id},E{resultDescriptor.Error}\n", stream); } else { - await WriteAsync(stream, $"RI{resultDescriptor.Id},R{(resultDescriptor.Result != null ? resultDescriptor.Result.ToString() : string.Empty)}\n"); + await WriteAsync($"RI{resultDescriptor.Id},R{(resultDescriptor.Result != null ? resultDescriptor.Result.ToString() : string.Empty)}\n", stream); } } - private async Task WriteAsync(Stream stream, string msg) + private async Task WriteAsync(string msg, Stream stream) { var writer = new StreamWriter(stream); await writer.WriteAsync(msg); diff --git a/samples/SocketsSample/Protobuf/ProtobufInvocationAdapter.cs b/samples/SocketsSample/Protobuf/ProtobufInvocationAdapter.cs index c993e25f61..3e438ee1a9 100644 --- a/samples/SocketsSample/Protobuf/ProtobufInvocationAdapter.cs +++ b/samples/SocketsSample/Protobuf/ProtobufInvocationAdapter.cs @@ -8,14 +8,14 @@ namespace SocketsSample.Protobuf { public class ProtobufInvocationAdapter : IInvocationAdapter { - IServiceProvider _serviceProvider; + private IServiceProvider _serviceProvider; public ProtobufInvocationAdapter(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } - public async Task CreateInvocationDescriptor(Stream stream, Func getParams) + public async Task ReadInvocationDescriptor(Stream stream, Func getParams) { return await Task.Run(() => CreateInvocationDescriptorInt(stream, getParams)); } @@ -58,7 +58,7 @@ namespace SocketsSample.Protobuf return Task.FromResult(invocationDescriptor); } - public async Task WriteInvocationResult(Stream stream, InvocationResultDescriptor resultDescriptor) + public async Task WriteInvocationResult(InvocationResultDescriptor resultDescriptor, Stream stream) { var outputStream = new CodedOutputStream(stream, leaveOpen: true); outputStream.WriteMessage(new RpcMessageKind() { MessageKind = RpcMessageKind.Types.Kind.Result }); @@ -100,7 +100,7 @@ namespace SocketsSample.Protobuf await stream.FlushAsync(); } - public async Task InvokeClientMethod(Stream stream, InvocationDescriptor invocationDescriptor) + public async Task WriteInvocationDescriptor(InvocationDescriptor invocationDescriptor, Stream stream) { var outputStream = new CodedOutputStream(stream, leaveOpen: true); outputStream.WriteMessage(new RpcMessageKind() { MessageKind = RpcMessageKind.Types.Kind.Invocation }); @@ -124,6 +124,12 @@ namespace SocketsSample.Protobuf { outputStream.WriteMessage(new PrimitiveValue { StringValue = (string)arg }); } + else + { + var serializer = _serviceProvider.GetRequiredService(); + var message = serializer.GetMessage(arg); + outputStream.WriteMessage(message); + } } outputStream.Flush(); diff --git a/samples/SocketsSample/Startup.cs b/samples/SocketsSample/Startup.cs index 7677d6e56c..e52c53325a 100644 --- a/samples/SocketsSample/Startup.cs +++ b/samples/SocketsSample/Startup.cs @@ -47,7 +47,7 @@ namespace SocketsSample app.UseRpc(invocationAdapters => { invocationAdapters.AddInvocationAdapter("protobuf", new Protobuf.ProtobufInvocationAdapter(app.ApplicationServices)); - invocationAdapters.AddInvocationAdapter("json", new JSonInvocationAdapter()); + invocationAdapters.AddInvocationAdapter("json", new JsonInvocationAdapter()); invocationAdapters.AddInvocationAdapter("line", new LineInvocationAdapter()); }); } diff --git a/src/Microsoft.AspNetCore.Sockets/HttpDispatcherAppBuilderExtensions.cs b/src/Microsoft.AspNetCore.Sockets/HttpDispatcherAppBuilderExtensions.cs index 11855ad1e6..97a87f5cc4 100644 --- a/src/Microsoft.AspNetCore.Sockets/HttpDispatcherAppBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Sockets/HttpDispatcherAppBuilderExtensions.cs @@ -37,7 +37,6 @@ namespace Microsoft.AspNetCore.Builder public void MapSocketEndpoint(string path) where TEndPoint : EndPoint { _routes.AddPrefixRoute(path, new RouteHandler(c => _dispatcher.Execute(path, c))); - } } }