-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypedClientBuilder.cs
292 lines (233 loc) · 11.3 KB
/
TypedClientBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Reflection;
using System.Reflection.Emit;
namespace Retrofit;
internal static class TypedClientBuilder<T>
{
private const string ClientModuleName = "Microsoft.AspNetCore.SignalR.TypedClientBuilder";
// There is one static instance of _builder per T
private static readonly Lazy<Func<IClientProxy, T>> _builder = new Lazy<Func<IClientProxy, T>>(GenerateClientBuilder);
private static readonly PropertyInfo CancellationTokenNoneProperty = typeof(CancellationToken).GetProperty("None", BindingFlags.Public | BindingFlags.Static)!;
private static readonly ConstructorInfo ObjectConstructor = typeof(object).GetConstructors().Single();
private static readonly Type[] ParameterTypes = new Type[] { typeof(IClientProxy) };
public static T Build(IClientProxy proxy)
{
return _builder.Value(proxy);
}
public static void Validate()
{
// The following will throw if T is not a valid type
_ = _builder.Value;
}
private static Func<IClientProxy, T> GenerateClientBuilder()
{
VerifyInterface(typeof(T));
var assemblyName = new AssemblyName(ClientModuleName);
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
var moduleBuilder = assemblyBuilder.DefineDynamicModule(ClientModuleName);
var clientType = GenerateInterfaceImplementation(moduleBuilder);
var factoryMethod = clientType.GetMethod(nameof(Build), BindingFlags.Public | BindingFlags.Static);
return (Func<IClientProxy, T>)factoryMethod!.CreateDelegate(typeof(Func<IClientProxy, T>));
}
private static Type GenerateInterfaceImplementation(ModuleBuilder moduleBuilder)
{
var name = ClientModuleName + "." + typeof(T).Name + "Impl";
var type = moduleBuilder.DefineType(name, TypeAttributes.Public, typeof(object), new[] { typeof(T) });
var proxyField = type.DefineField("_proxy", typeof(IClientProxy), FieldAttributes.Private | FieldAttributes.InitOnly);
var ctor = BuildConstructor(type, proxyField);
// Because a constructor doesn't return anything, it can't be wrapped in a
// delegate directly, so we emit a factory method that just takes the IClientProxy,
// invokes the constructor (using newobj) and returns the new instance of type T.
BuildFactoryMethod(type, ctor);
foreach (var method in GetAllInterfaceMethods(typeof(T)))
{
BuildMethod(type, method, proxyField);
}
return type.CreateTypeInfo()!;
}
private static IEnumerable<MethodInfo> GetAllInterfaceMethods(Type interfaceType)
{
foreach (var parent in interfaceType.GetInterfaces())
{
foreach (var parentMethod in GetAllInterfaceMethods(parent))
{
yield return parentMethod;
}
}
foreach (var method in interfaceType.GetMethods())
{
yield return method;
}
}
private static ConstructorInfo BuildConstructor(TypeBuilder type, FieldInfo proxyField)
{
var ctor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, ParameterTypes);
var generator = ctor.GetILGenerator();
// Call object constructor
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Call, ObjectConstructor);
// Assign constructor argument to the proxyField
generator.Emit(OpCodes.Ldarg_0); // type
generator.Emit(OpCodes.Ldarg_1); // type proxyfield
generator.Emit(OpCodes.Stfld, proxyField); // type.proxyField = proxyField
generator.Emit(OpCodes.Ret);
return ctor;
}
private static void BuildMethod(TypeBuilder type, MethodInfo interfaceMethodInfo, FieldInfo proxyField)
{
var methodAttributes =
MethodAttributes.Public
| MethodAttributes.Virtual
| MethodAttributes.Final
| MethodAttributes.HideBySig
| MethodAttributes.NewSlot;
var parameters = interfaceMethodInfo.GetParameters();
var paramTypes = parameters.Select(param => param.ParameterType).ToArray();
var returnType = interfaceMethodInfo.ReturnType;
bool isInvoke = returnType != typeof(Task);
var methodBuilder = type.DefineMethod(interfaceMethodInfo.Name, methodAttributes);
MethodInfo invokeMethod;
if (isInvoke)
{
invokeMethod = typeof(ISingleClientProxy).GetMethod(
nameof(ISingleClientProxy.InvokeCoreAsync), BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null,
new[] { typeof(string), typeof(object[]), typeof(CancellationToken) }, null)!
.MakeGenericMethod(returnType.GenericTypeArguments);
}
else
{
invokeMethod = typeof(IClientProxy).GetMethod(
nameof(IClientProxy.SendCoreAsync), BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null,
new[] { typeof(string), typeof(object[]), typeof(CancellationToken) }, null)!;
}
methodBuilder.SetReturnType(interfaceMethodInfo.ReturnType);
methodBuilder.SetParameters(paramTypes);
// Sets the number of generic type parameters
var genericTypeNames =
paramTypes.Where(p => p.IsGenericParameter).Select(p => p.Name).Distinct().ToArray();
if (genericTypeNames.Length > 0)
{
methodBuilder.DefineGenericParameters(genericTypeNames);
}
// Check to see if the last parameter of the method is a CancellationToken
bool hasCancellationToken = paramTypes.LastOrDefault() == typeof(CancellationToken);
if (hasCancellationToken)
{
// remove CancellationToken from input paramTypes
paramTypes = paramTypes.Take(paramTypes.Length - 1).ToArray();
}
var methodName =
interfaceMethodInfo.GetCustomAttribute<HubMethodNameAttribute>()?.Name ??
interfaceMethodInfo.Name;
var generator = methodBuilder.GetILGenerator();
// Declare local variable to store the arguments to IClientProxy.SendCoreAsync
generator.DeclareLocal(typeof(object[]));
// Get IClientProxy
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldfld, proxyField);
var isTypeLabel = generator.DefineLabel();
if (isInvoke)
{
var singleClientProxyType = typeof(ISingleClientProxy);
/*
if (_proxy is ISingleClientProxy singleClientProxy)
{
return ((ISingleClientProxy)_proxy).InvokeAsync<T>(methodName, args, cancellationToken);
}
throw new InvalidOperationException("InvokeAsync only works with Single clients.");
*/
generator.Emit(OpCodes.Isinst, singleClientProxyType);
generator.Emit(OpCodes.Brtrue_S, isTypeLabel);
generator.Emit(OpCodes.Ldstr, "InvokeAsync only works with Single clients.");
generator.Emit(OpCodes.Newobj, typeof(InvalidOperationException).GetConstructor(new Type[] { typeof(string) })!);
generator.Emit(OpCodes.Throw);
generator.MarkLabel(isTypeLabel);
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldfld, proxyField);
generator.Emit(OpCodes.Castclass, singleClientProxyType);
}
// The first argument to IClientProxy.SendCoreAsync is this method's name
generator.Emit(OpCodes.Ldstr, methodName);
// Create an new object array to hold all the parameters to this method
generator.Emit(OpCodes.Ldc_I4, paramTypes.Length); // Stack:
generator.Emit(OpCodes.Newarr, typeof(object)); // allocate object array
generator.Emit(OpCodes.Stloc_0);
// Store each parameter in the object array
for (var i = 0; i < paramTypes.Length; i++)
{
generator.Emit(OpCodes.Ldloc_0); // Object array loaded
generator.Emit(OpCodes.Ldc_I4, i);
generator.Emit(OpCodes.Ldarg, i + 1); // i + 1
generator.Emit(OpCodes.Box, paramTypes[i]);
generator.Emit(OpCodes.Stelem_Ref);
}
// Load parameter array on to the stack.
generator.Emit(OpCodes.Ldloc_0);
if (hasCancellationToken)
{
// Get CancellationToken from input argument and put it on the stack
generator.Emit(OpCodes.Ldarg, paramTypes.Length + 1);
}
else
{
// Get 'CancellationToken.None' and put it on the stack, for when method does not have CancellationToken
generator.Emit(OpCodes.Call, CancellationTokenNoneProperty.GetMethod!);
}
// Send!
generator.Emit(OpCodes.Callvirt, invokeMethod);
generator.Emit(OpCodes.Ret); // Return the Task returned by 'invokeMethod'
}
private static void BuildFactoryMethod(TypeBuilder type, ConstructorInfo ctor)
{
var method = type.DefineMethod(nameof(Build), MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(T), ParameterTypes);
var generator = method.GetILGenerator();
generator.Emit(OpCodes.Ldarg_0); // Load the IClientProxy argument onto the stack
generator.Emit(OpCodes.Newobj, ctor); // Call the generated constructor with the proxy
generator.Emit(OpCodes.Ret); // Return the typed client
}
private static void VerifyInterface(Type interfaceType)
{
if (!interfaceType.IsInterface)
{
throw new InvalidOperationException("Type must be an interface.");
}
if (interfaceType.GetProperties().Length != 0)
{
throw new InvalidOperationException("Type must not contain properties.");
}
if (interfaceType.GetEvents().Length != 0)
{
throw new InvalidOperationException("Type must not contain events.");
}
foreach (var method in interfaceType.GetMethods())
{
VerifyMethod(method);
}
foreach (var parent in interfaceType.GetInterfaces())
{
VerifyInterface(parent);
}
}
private static void VerifyMethod(MethodInfo interfaceMethod)
{
if (!typeof(Task).IsAssignableFrom(interfaceMethod.ReturnType))
{
throw new InvalidOperationException(
$"Cannot generate proxy implementation for '{typeof(T).FullName}.{interfaceMethod.Name}'. All client proxy methods must return '{typeof(Task).FullName}' or '{typeof(Task).FullName}<T>'.");
}
foreach (var parameter in interfaceMethod.GetParameters())
{
if (parameter.IsOut)
{
throw new InvalidOperationException(
$"Cannot generate proxy implementation for '{typeof(T).FullName}.{interfaceMethod.Name}'. Client proxy methods must not have 'out' parameters.");
}
if (parameter.ParameterType.IsByRef)
{
throw new InvalidOperationException(
$"Cannot generate proxy implementation for '{typeof(T).FullName}.{interfaceMethod.Name}'. Client proxy methods must not have 'ref' parameters.");
}
}
}
}