Skip to content

Commit

Permalink
reactor: refine code for supporting event. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
DingpingZhang committed Nov 16, 2021
1 parent bb5436d commit a3e1b8c
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 43 deletions.
9 changes: 6 additions & 3 deletions HandyIpc.Generator/ClientProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ public class {nameof(ClientProxy)}{className}{typeParameters} : {interfaceType}
private readonly Sender _sender;
private readonly ISerializer _serializer;
private readonly string _key;
{Text(events.Any() ? @"
private readonly AwaiterManager _awaiterManager;
" : RemoveLineIfEmpty)}
{events.For(item => $@"
private event {item.Type.ToTypeDeclaration()} _{item.Name};
")}
{events.For(item =>
{
var eSymbol = ((INamedTypeSymbol)item.Type).DelegateInvokeMethod!.Parameters[1];
IParameterSymbol eSymbol = ((INamedTypeSymbol)item.Type).DelegateInvokeMethod!.Parameters[1];
string eType = eSymbol.Type.ToTypeDeclaration();
return $@"
public event {item.Type.ToTypeDeclaration()} {item.Name}
{{
add
Expand All @@ -65,7 +67,6 @@ public class {nameof(ClientProxy)}{className}{typeParameters} : {interfaceType}
}}
}}
}}
";
})}
Expand All @@ -74,7 +75,9 @@ public class {nameof(ClientProxy)}{className}{typeParameters} : {interfaceType}
_sender = sender;
_serializer = serializer;
_key = key;
{Text(events.Any() ? @"
_awaiterManager = new AwaiterManager(key, sender, serializer);
" : RemoveLineIfEmpty)}
}}
{methods.For(method =>
{
Expand Down
9 changes: 5 additions & 4 deletions HandyIpc.Generator/Dispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace {@namespace}
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Reflection.Obfuscation(Exclude = true)]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public class {nameof(Dispatcher)}{className}{typeParameters} : IMethodDispatcher
public class {nameof(Dispatcher)}{className}{typeParameters} : IMethodDispatcher{(events.Any() ? ", INotifiable" : null)}
{@interface.TypeParameters.For(typeParameter => $@"
{typeParameter.ToGenericConstraint()}
")}
Expand All @@ -36,19 +36,20 @@ public class {nameof(Dispatcher)}{className}{typeParameters} : IMethodDispatcher
private readonly Lazy<IReadOnlyDictionary<string, MethodInfo>> _genericMethodMapping;
" : RemoveLineIfEmpty)}
public NotifierManager NotifierManager {{ get; set; }}
{Text(events.Any() ? @"
public NotifierManager NotifierManager { get; set; }
" : RemoveLineIfEmpty)}
public {nameof(Dispatcher)}{className}({interfaceType} instance)
{{
_instance = instance;
{Text(methods.Any(item => item.TypeParameters.Any()) ? $@"
_genericMethodMapping = new Lazy<IReadOnlyDictionary<string, MethodInfo>>(
() => GeneratorHelper.GetGenericMethodMapping(typeof({interfaceType}), _instance));
" : RemoveLineIfEmpty)}
{events.For(item => $@"
instance.{item.Name} += (_, e) => NotifierManager.Publish(""{item.Name}"", e);
", RemoveLineIfEmpty)}
")}
}}
public async Task Dispatch(Context ctx, Func<Task> next)
Expand Down
1 change: 0 additions & 1 deletion HandyIpc.Tests/EventTypeTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
using HandyIpc;
using HandyIpcTests.Fixtures;
using HandyIpcTests.Interfaces;
Expand Down
28 changes: 17 additions & 11 deletions HandyIpc/ContainerServerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ public IContainerServer Build()
ConcurrentDictionary<string, NotifierManager> notifiers = new();
foreach (var (key, type, factory) in _interfaceMap)
{
IMethodDispatcher dispatcher = CreateDispatcher(type, factory);
dispatcher.NotifierManager = notifiers.GetOrAdd(key, _ => new NotifierManager(_serializerFactory()));
Middleware methodDispatcher = dispatcher.Dispatch;
object dispatcher = CreateDispatcher(type, factory);
if (dispatcher is INotifiable notifiable)
{
notifiable.NotifierManager = notifiers.GetOrAdd(key, _ => new NotifierManager(_serializerFactory()));
}

Middleware methodDispatcher = ((IMethodDispatcher)dispatcher).Dispatch;
map.Add(key, methodDispatcher);
}

Expand All @@ -65,11 +69,15 @@ public IContainerServer Build()
Middleware methodDispatcher = Middlewares.GetMethodDispatcher(
genericTypes =>
{
IMethodDispatcher dispatcher = CreateDispatcher(
object dispatcher = CreateDispatcher(
type.MakeGenericType(genericTypes),
() => factory(genericTypes));
dispatcher.NotifierManager = notifiers.GetOrAdd(key, _ => new NotifierManager(_serializerFactory()));
return dispatcher;
if (dispatcher is INotifiable notifiable)
{
notifiable.NotifierManager = notifiers.GetOrAdd(key, _ => new NotifierManager(_serializerFactory()));
}

return (IMethodDispatcher)dispatcher;
});
map.Add(key, methodDispatcher);
}
Expand All @@ -78,13 +86,13 @@ public IContainerServer Build()
Middlewares.Heartbeat,
Middlewares.ExceptionHandler,
Middlewares.GetHandleRequest(map),
Middlewares.GetHandleEvent(notifiers),
Middlewares.GetHandleSubscription(notifiers),
Middlewares.NotFound);

return new ContainerServer(_serverFactory(), middleware, _serializerFactory(), _loggerFactory());
}

private static IMethodDispatcher CreateDispatcher(Type interfaceType, Func<object> factory)
private static object CreateDispatcher(Type interfaceType, Func<object> factory)
{
object instance = factory();

Expand All @@ -105,9 +113,7 @@ private static IMethodDispatcher CreateDispatcher(Type interfaceType, Func<objec
// this does not lead to naming conflicts even if the user also declares a Dispatch method
// with the same signature in the IContract interface.
object proxy = Activator.CreateInstance(interfaceType.GetServerProxyType(), instance);
var dispatcher = (IMethodDispatcher)Activator.CreateInstance(interfaceType.GetDispatcherType(), proxy);

return dispatcher;
return Activator.CreateInstance(interfaceType.GetDispatcherType(), proxy);
}
}
}
4 changes: 2 additions & 2 deletions HandyIpc/Core/AsyncPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public sealed class AsyncPool<TValue> : PoolBase<TValue> where TValue : IDisposa
private readonly Func<Task<TValue>> _factory;
private readonly Func<TValue, Task<bool>> _checkValue;

public AsyncPool(Func<Task<TValue>> factory, Func<TValue, Task<bool>>? checkValue = null)
public AsyncPool(Func<Task<TValue>> factory, Func<TValue, Task<bool>> checkValue)
{
_factory = factory;
_checkValue = checkValue ?? (_ => Task.FromResult(true));
_checkValue = checkValue;
}

public async Task<RentedValue<TValue>> RentAsync()
Expand Down
2 changes: 0 additions & 2 deletions HandyIpc/Core/IMethodDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ namespace HandyIpc.Core
{
public interface IMethodDispatcher
{
NotifierManager NotifierManager { get; set; }

Task Dispatch(Context context, Func<Task> next);
}
}
7 changes: 7 additions & 0 deletions HandyIpc/Core/INotifiable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace HandyIpc.Core
{
public interface INotifiable
{
NotifierManager NotifierManager { get; set; }
}
}
19 changes: 9 additions & 10 deletions HandyIpc/Core/Middlewares.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -54,7 +53,7 @@ public static Middleware GetHandleRequest(IReadOnlyDictionary<string, Middleware
};
}

public static Middleware GetHandleEvent(ConcurrentDictionary<string, NotifierManager> notifiers)
public static Middleware GetHandleSubscription(IReadOnlyDictionary<string, NotifierManager> notifiers)
{
return async (ctx, next) =>
{
Expand All @@ -64,12 +63,14 @@ public static Middleware GetHandleEvent(ConcurrentDictionary<string, NotifierMan
{
case SubscriptionType.Add:
{
var manager = notifiers.GetOrAdd(subscription.Name, _ => new NotifierManager(ctx.Serializer));
manager.Subscribe(subscription.CallbackName, subscription.ProcessId, ctx.Connection);
ctx.Output = Signals.Unit;
ctx.KeepAlive = false;
if (notifiers.TryGetValue(subscription.Name, out NotifierManager manager))
{
manager.Subscribe(subscription.CallbackName, subscription.ProcessId, ctx.Connection);
ctx.Output = Signals.Unit;
ctx.KeepAlive = false;
}
}
break;
return;
case SubscriptionType.Remove:
{
if (notifiers.TryGetValue(subscription.Name, out NotifierManager manager))
Expand All @@ -79,10 +80,8 @@ public static Middleware GetHandleEvent(ConcurrentDictionary<string, NotifierMan

ctx.Output = Signals.Unit;
}
break;
return;
}

return;
}

await next();
Expand Down
4 changes: 2 additions & 2 deletions HandyIpc/Core/Pool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ public sealed class Pool<TValue> : PoolBase<TValue> where TValue : IDisposable
private readonly Func<TValue> _factory;
private readonly Func<TValue, bool> _checkValue;

public Pool(Func<TValue> factory, Func<TValue, bool>? checkValue = null)
public Pool(Func<TValue> factory, Func<TValue, bool> checkValue)
{
_factory = factory;
_checkValue = checkValue ?? (_ => true);
_checkValue = checkValue;
}

public RentedValue<TValue> Rent()
Expand Down
10 changes: 2 additions & 8 deletions HandyIpc/Core/Subscription.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Diagnostics;
using System.Text;

namespace HandyIpc.Core
{
public enum SubscriptionType { Add, Promise, Remove }
public enum SubscriptionType { Add, Remove }

public class Subscription
{
Expand All @@ -19,7 +18,7 @@ public class Subscription

public string CallbackName { get; set; } = string.Empty;

public int ProcessId { get; } = InnerProcessId;
public int ProcessId => InnerProcessId;

internal static bool TryParse(byte[] bytes, ISerializer serializer, out Subscription subscription)
{
Expand All @@ -45,11 +44,6 @@ internal static byte[] Add(string key, string name, ISerializer serializer)
return GetBytes(SubscriptionType.Add, key, name, serializer);
}

internal static byte[] Promise(string key, string name, ISerializer serializer)
{
return GetBytes(SubscriptionType.Promise, key, name, serializer);
}

internal static byte[] Remove(string key, string name, ISerializer serializer)
{
return GetBytes(SubscriptionType.Remove, key, name, serializer);
Expand Down

0 comments on commit a3e1b8c

Please sign in to comment.