-
Notifications
You must be signed in to change notification settings - Fork 2
/
RpcBase.cs
209 lines (189 loc) · 7.44 KB
/
RpcBase.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
using System;
using System.Collections.Generic;
using System.Reflection;
using JieRuntime.Rpc.Attributes;
using JieRuntime.Rpc.Exceptions;
namespace JieRuntime.Rpc
{
/// <summary>
/// 提供远程调用服务基本功能的基础类, 该类是抽象的
/// </summary>
public abstract class RpcBase : IProxyHandler
{
#region --属性--
/// <summary>
/// 获取远程调用服务实例的集合
/// </summary>
protected Dictionary<string, RpcInstance> Services { get; }
#endregion
#region --构造函数--
/// <summary>
/// 初始化 <see cref="RpcBase"/> 类的新实例
/// </summary>
public RpcBase ()
{
this.Services = new Dictionary<string, RpcInstance> ();
}
#endregion
#region --公开方法--
/// <summary>
/// 注册远程调用服务实例
/// </summary>
/// <typeparam name="T">远程调用服务实例接口的类</typeparam>
/// <param name="obj">远程服务调用实例</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="RpcTypeFoundException"></exception>
public virtual void Register<T> (T obj)
where T : class
{
this.Register (typeof (T), obj);
}
/// <summary>
/// 注册远程调用服务实例
/// </summary>
/// <param name="type">远程调用服务实例接口的类型</param>
/// <param name="obj">远程服务调用实例</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="RpcTypeFoundException"></exception>
public virtual void Register (Type type, object obj)
{
if (type is null)
{
throw new ArgumentNullException (nameof (type));
}
if (obj is null)
{
throw new ArgumentNullException (nameof (obj));
}
if (!type.IsInterface)
{
throw new ArgumentException ($"类型 {type.FullName} 不是接口类型", nameof (obj));
}
// 检查是否存在远程类型特性, 如果存在则注册为指定名称的服务
RemoteTypeAttribute remoteType = type.GetCustomAttribute<RemoteTypeAttribute> ();
if (remoteType is not null)
{
if (this.Services.ContainsKey (remoteType.Name))
{
throw new RpcTypeFoundException (remoteType.Name);
}
this.Services.Add (remoteType.Name, new RpcInstance (type, obj));
}
else
{
if (this.Services.ContainsKey (type.Name))
{
throw new RpcTypeFoundException (type.Name);
}
this.Services.Add (type.Name, new RpcInstance (type, obj));
}
}
/// <summary>
/// 取消注册远程调用服务实例
/// </summary>
/// <typeparam name="T">远程调用服务实例接口的类型</typeparam>
public virtual void Unregister<T> ()
where T : class
{
this.Unregister (typeof (T));
}
/// <summary>
/// 取消注册远程调用服务实例
/// </summary>
/// <param name="type">远程调用服务实例接口的类型</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public virtual void Unregister (Type type)
{
if (type is null)
{
throw new ArgumentNullException (nameof (type));
}
if (!type.IsInterface)
{
throw new ArgumentException ($"类型 {type.FullName} 不是接口类型");
}
// 首先检查类型是否存在特性
RemoteTypeAttribute remoteType = type.GetCustomAttribute<RemoteTypeAttribute> ();
if (remoteType is not null)
{
if (this.Services.ContainsKey (remoteType.Name))
{
this.Services.Remove (remoteType.Name);
}
}
else
{
if (this.Services.ContainsKey (type.Name))
{
this.Services.Remove (type.Name);
}
}
}
/// <summary>
/// 获取指定的远程调用服务实例接口的类型是否在服务列表中注册
/// </summary>
/// <typeparam name="T">远程调用服务实例接口的类型</typeparam>
/// <returns>如果在服务实例列表中已注册则为 <see langword="true"/>; 否则为 <see langword="false"/></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public virtual bool IsRegister<T> ()
where T : class
{
return this.IsRegister (typeof (T));
}
/// <summary>
/// 获取指定的远程调用服务实例接口的类型是否在服务列表中注册
/// </summary>
/// <param name="type">远程调用服务实例接口的类型</param>
/// <returns>如果在服务实例列表中已注册则为 <see langword="true"/>; 否则为 <see langword="false"/></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public virtual bool IsRegister (Type type)
{
if (type is null)
{
throw new ArgumentNullException (nameof (type));
}
if (!type.IsInterface)
{
throw new ArgumentException ($"类型 {type.FullName} 不是接口类型");
}
// 检查是否存在远程类型特性, 如果存在则注册为指定名称的服务
RemoteTypeAttribute remoteType = type.GetCustomAttribute<RemoteTypeAttribute> ();
if (remoteType is not null)
{
return this.Services.ContainsKey (remoteType.Name);
}
else
{
return this.Services.ContainsKey (type.Name);
}
}
/// <summary>
/// 解析远程服务指定接口的代理
/// </summary>
/// <typeparam name="T">远程服务调用的接口</typeparam>
/// <returns>实现了远程调用服务接口的代理</returns>
public virtual T Resolver<T> ()
where T : class
{
T proxyObj = DispatchProxy.Create<T, RpcDispatchProxy> ();
if (proxyObj is RpcDispatchProxy proxy)
{
proxy.ProxyHandler = this;
}
return proxyObj;
}
/// <summary>
/// 每当调用代理类型上的任何方法时,都会调用此方法
/// </summary>
/// <param name="targetMethod">调用者调用的方法</param>
/// <param name="args">调用者传递给方法的参数</param>
/// <returns>返回给调用者的对象,void 方法将返回 <see langword="null"/></returns>
public abstract object InvokeMethod (MethodInfo targetMethod, object[] args);
#endregion
}
}