-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathClientProxy.cs
More file actions
151 lines (141 loc) · 5.33 KB
/
ClientProxy.cs
File metadata and controls
151 lines (141 loc) · 5.33 KB
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
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using static HandyIpc.Generator.TemplateExtensions;
namespace HandyIpc.Generator
{
public static class ClientProxy
{
public static string Generate(INamedTypeSymbol @interface, IReadOnlyCollection<IMethodSymbol> methods, IReadOnlyCollection<IEventSymbol> events)
{
var (@namespace, className, typeParameters) = @interface.GenerateNameFromInterface();
string interfaceType = @interface.ToFullDeclaration();
return $@"
namespace {@namespace}
{{
using System;
using HandyIpc.Core;
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Reflection.Obfuscation(Exclude = true)]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public class {nameof(ClientProxy)}{className}{typeParameters} : {interfaceType}
{@interface.TypeParameters.For(typeParameter => $@"
{typeParameter.ToGenericConstraint()}
")}
{{
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 =>
{
IParameterSymbol eSymbol = ((INamedTypeSymbol)item.Type).DelegateInvokeMethod!.Parameters[1];
string eType = eSymbol.Type.ToTypeDeclaration();
return $@"
public event {item.Type.ToTypeDeclaration()} {item.Name}
{{
add
{{
if (_{item.Name} == null)
{{
_awaiterManager.Subscribe(""{item.Name}"", args =>
{{
var e = ({eType})_serializer.Deserialize(args, typeof({eType}));
_{item.Name}?.Invoke(this, e);
}});
}}
_{item.Name} += value;
}}
remove
{{
_{item.Name} -= value;
if (_{item.Name} == null)
{{
_awaiterManager.Unsubscribe(""{item.Name}"");
}}
}}
}}
";
})}
public {nameof(ClientProxy)}{className}(Sender sender, ISerializer serializer, string key)
{{
_sender = sender;
_serializer = serializer;
_key = key;
{Text(events.Any() ? @"
_awaiterManager = new AwaiterManager(key, sender, serializer);
" : RemoveLineIfEmpty)}
}}
{methods.For(method =>
{
string methodName = $"{method.Name}{method.TypeParameters.Join(", ").If(text => $"<{text}>")}";
var parameterTypes = method.Parameters
.Select(item => item.Type)
.Select(item => item.ToFullDeclaration())
.ToList()
.AsReadOnly();
string methodId = method.GenerateMethodId();
// Add underscores to the end of parameters to avoid user fields with the same name as the generated local variables.
var parameterNames = method.Parameters.Select(parameter => $"{parameter.Name}_").ToList();
string parameters = method.Parameters
.Select(item => item.Type.ToTypeDeclaration())
.Zip(parameterNames, (type, parameter) => $"{type} {parameter}")
.Join(", ");
bool isAwaitable = method.ReturnType.IsAwaitable();
bool isVoid = method.ReturnsVoid || method.ReturnType.ReturnsVoidTask();
return $@"
/// <inheritdoc />
{(isAwaitable ? "async " : null)}{method.ReturnType.ToTypeDeclaration()} {interfaceType}.{methodName}({parameters})
{{
{Text($@"
var request = new Request(_serializer, ""{methodId}"")
{{
Name = _key,
{@interface.TypeParameters.Select(type => $"typeof({type.ToFullDeclaration()})").Join(", ").If(text => $@"
TypeArguments = new[] {{ {text} }},
", RemoveLineIfEmpty)}
{method.TypeParameters.Select(type => $"typeof({type.ToFullDeclaration()})").Join(", ").If(text => $@"
MethodTypeArguments = new[] {{ {text} }},
", RemoveLineIfEmpty)}
{parameterNames.Join(", ").If(text => $@"
Arguments = new object[] {{ {text} }},
", RemoveLineIfEmpty)}
}};
")}
{parameterTypes.Select(type => $"typeof({type})").Join(", ").If(text => $@"
request.SetArgumentTypes(new[] {{ {text} }});
", RemoveLineIfEmpty)}
{Text(isAwaitable ? @"
var responseBytes = await _sender.InvokeAsync(request.ToBytes());
" : @"
var responseBytes = _sender.Invoke(request.ToBytes());
"
)}
{Text(isAwaitable ? $@"
var response = GeneratorHelper.UnpackResponse<{(isVoid ? "byte[]" : method.ReturnType.ExtractTypeFromTask())}>(responseBytes, _serializer);
" : $@"
var response = GeneratorHelper.UnpackResponse<{(isVoid ? "byte[]" : method.ReturnType.ToTypeDeclaration())}>(responseBytes, _serializer);
")}
{Text(isVoid ? @"
if (!response.IsUnit())
{
throw new InvalidOperationException(""A method that returns void type must responses the Unit object."");
}
" : @"
return response;
")}
}}
";
})}
}}
}}
".FormatCode();
}
}
}