Removing formatters
This commit is contained in:
parent
a854b13754
commit
fd10deba6c
|
|
@ -63,7 +63,7 @@ namespace SocketsSample
|
|||
foreach (var connection in _endPoint.Connections)
|
||||
{
|
||||
|
||||
var invocationAdapter = _endPoint._serviceProvider.GetRequiredService<SocketFormatters>()
|
||||
var invocationAdapter = _endPoint._serviceProvider.GetRequiredService<InvocationAdapterRegistry>()
|
||||
.GetInvocationAdapter((string)connection.Metadata["formatType"]);
|
||||
|
||||
tasks.Add(invocationAdapter.InvokeClientMethod(connection.Channel.GetStream(), message));
|
||||
|
|
@ -88,7 +88,7 @@ namespace SocketsSample
|
|||
{
|
||||
var connection = _endPoint.Connections[_connectionId];
|
||||
|
||||
var invocationAdapter = _endPoint._serviceProvider.GetRequiredService<SocketFormatters>()
|
||||
var invocationAdapter = _endPoint._serviceProvider.GetRequiredService<InvocationAdapterRegistry>()
|
||||
.GetInvocationAdapter((string)connection.Metadata["formatType"]);
|
||||
|
||||
if (_endPoint._logger.IsEnabled(LogLevel.Debug))
|
||||
|
|
|
|||
|
|
@ -44,13 +44,8 @@ namespace SocketsSample
|
|||
// TODO: Dispatch from the caller
|
||||
await Task.Yield();
|
||||
|
||||
/*
|
||||
var formatter = _serviceProvider.GetRequiredService<SocketFormatters>()
|
||||
.GetFormatter<InvocationDescriptor>((string)connection.Metadata["formatType"]);
|
||||
*/
|
||||
|
||||
var stream = connection.Channel.GetStream();
|
||||
var invocationAdapter = _serviceProvider.GetRequiredService<SocketFormatters>()
|
||||
var invocationAdapter = _serviceProvider.GetRequiredService<InvocationAdapterRegistry>()
|
||||
.GetInvocationAdapter((string)connection.Metadata["formatType"]);
|
||||
|
||||
while (true)
|
||||
|
|
|
|||
|
|
@ -1,41 +1,31 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Sockets;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace SocketsSample
|
||||
{
|
||||
public static class FormatterExtensions
|
||||
public static class RpcExtensions
|
||||
{
|
||||
public static IApplicationBuilder UseFormatters(this IApplicationBuilder app, Action<FormatterBuilder> registerFormatters)
|
||||
public static IApplicationBuilder UseRpc(this IApplicationBuilder app, Action<RpcBuilder> registerAdapters)
|
||||
{
|
||||
var formatters = app.ApplicationServices.GetRequiredService<SocketFormatters>();
|
||||
registerFormatters(new FormatterBuilder(formatters));
|
||||
var adapters = app.ApplicationServices.GetRequiredService<InvocationAdapterRegistry>();
|
||||
registerAdapters(new RpcBuilder(adapters));
|
||||
return app;
|
||||
}
|
||||
}
|
||||
|
||||
public class FormatterBuilder
|
||||
public class RpcBuilder
|
||||
{
|
||||
private SocketFormatters _socketFormatters;
|
||||
private InvocationAdapterRegistry _invocationAdapters;
|
||||
|
||||
public FormatterBuilder(SocketFormatters socketFormatters)
|
||||
public RpcBuilder(InvocationAdapterRegistry invocationAdapters)
|
||||
{
|
||||
_socketFormatters = socketFormatters;
|
||||
}
|
||||
|
||||
public void MapFormatter<T, TFormatterType>(string format)
|
||||
where TFormatterType : IFormatter<T>
|
||||
{
|
||||
_socketFormatters.RegisterFormatter<T, TFormatterType>(format);
|
||||
_invocationAdapters = invocationAdapters;
|
||||
}
|
||||
|
||||
public void AddInvocationAdapter(string format, IInvocationAdapter adapter)
|
||||
{
|
||||
_socketFormatters.RegisterInvocationAdapter(format, adapter);
|
||||
_invocationAdapters.RegisterInvocationAdapter(format, adapter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +0,0 @@
|
|||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SocketsSample
|
||||
{
|
||||
// TODO: Is this name too generic?
|
||||
public interface IFormatter<T>
|
||||
{
|
||||
Task<T> ReadAsync(Stream stream);
|
||||
Task WriteAsync(T value, Stream stream);
|
||||
}
|
||||
}
|
||||
|
|
@ -5,43 +5,10 @@ using Microsoft.Extensions.DependencyInjection;
|
|||
|
||||
namespace SocketsSample
|
||||
{
|
||||
public class SocketFormatters
|
||||
public class InvocationAdapterRegistry
|
||||
{
|
||||
private IServiceProvider _serviceProvider;
|
||||
private Dictionary<string, Dictionary<Type, Type>> _formatters = new Dictionary<string, Dictionary<Type, Type>>();
|
||||
private Dictionary<string, IInvocationAdapter> _invocationAdapters = new Dictionary<string, IInvocationAdapter>();
|
||||
|
||||
public SocketFormatters(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public void RegisterFormatter<T, TFormatterType>(string format)
|
||||
where TFormatterType : IFormatter<T>
|
||||
{
|
||||
Dictionary<Type, Type> formatFormatters;
|
||||
if (!_formatters.TryGetValue(format, out formatFormatters))
|
||||
{
|
||||
formatFormatters = _formatters[format] = new Dictionary<Type, Type>();
|
||||
}
|
||||
|
||||
formatFormatters[typeof(T)] = typeof(TFormatterType);
|
||||
}
|
||||
|
||||
public IFormatter<T> GetFormatter<T>(string format)
|
||||
{
|
||||
Dictionary<Type, Type> formatters;
|
||||
Type targetFormatterType;
|
||||
|
||||
if (_formatters.TryGetValue(format, out formatters) && formatters.TryGetValue(typeof(T), out targetFormatterType))
|
||||
{
|
||||
return (IFormatter<T>)_serviceProvider.GetRequiredService(targetFormatterType);
|
||||
}
|
||||
|
||||
return null;
|
||||
// throw new InvalidOperationException($"No formatter register for format '{format}' and type '{typeof(T).GetType().FullName}'");
|
||||
}
|
||||
|
||||
public void RegisterInvocationAdapter(string format, IInvocationAdapter adapter)
|
||||
{
|
||||
_invocationAdapters[format] = adapter;
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ namespace SocketsSample
|
|||
services.AddSingleton<ChatEndPoint>();
|
||||
|
||||
services.AddSingleton<ProtobufSerializer>();
|
||||
services.AddSingleton<SocketFormatters>();
|
||||
services.AddSingleton<InvocationAdapterRegistry>();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
|
@ -44,11 +44,11 @@ namespace SocketsSample
|
|||
routes.MapSocketEndpoint<RpcEndpoint>("/jsonrpc");
|
||||
});
|
||||
|
||||
app.UseFormatters(formatters=>
|
||||
app.UseRpc(invocationAdapters =>
|
||||
{
|
||||
formatters.AddInvocationAdapter("protobuf", new Protobuf.ProtobufInvocationAdapter(app.ApplicationServices));
|
||||
formatters.AddInvocationAdapter("json", new JSonInvocationAdapter());
|
||||
formatters.AddInvocationAdapter("line", new LineInvocationAdapter());
|
||||
invocationAdapters.AddInvocationAdapter("protobuf", new Protobuf.ProtobufInvocationAdapter(app.ApplicationServices));
|
||||
invocationAdapters.AddInvocationAdapter("json", new JSonInvocationAdapter());
|
||||
invocationAdapters.AddInvocationAdapter("line", new LineInvocationAdapter());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +0,0 @@
|
|||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.AspNetCore.Sockets
|
||||
{
|
||||
// TODO: Is this name too generic?
|
||||
public interface IFormatter<T>
|
||||
{
|
||||
Task<T> ReadAsync(Stream stream);
|
||||
Task WriteAsync(T value, Stream stream);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue