前言:此文章是建立在对Remoting通讯由一定认知的基础上,并为对Remoting技术做过多讲解。
服务端
服务端启动入口,位于 OThinker.H3.Console Program.cs
private static EngineChannel Service = new EngineChannel();
[STAThread]
static void Main(string[] args){
try{
Service.OnStart();
}
.
.
.
}
EngineChannel ,引擎信道类。派生于VesselChannel。
H3Console启动时,注册一个Remoting服务,对外提供RPC服务。这是一个分布式的可横向扩展的服务。
启动服务代码如下:
protected void OnStart(ObjectPoolCreator Creator, ConfigFactory ConfigFactory, LogicUnitServiceFactory LogicUnitServiceFactory, Type LogicUnitType)
{
this.ConfigFactory = ConfigFactory;
IDictionary dictionary = new Hashtable();
BinaryServerFormatterSinkProvider binaryServerFormatterSinkProvider = new BinaryServerFormatterSinkProvider();
binaryServerFormatterSinkProvider.TypeFilterLevel = TypeFilterLevel.Full;
IServerChannelSinkProvider next = new ClientIPServerSinkProvider();
binaryServerFormatterSinkProvider.Next = next;
this.Channel = new TcpServerChannel(Guid.NewGuid().ToString(), ConfigFactory.CurrentVesselConfig.Port, binaryServerFormatterSinkProvider);
ChannelServices.RegisterChannel(this.Channel, false);
Vessel vessel = VesselFactory.Create(Creator, this.ConfigFactory, LogicUnitServiceFactory, LogicUnitType);
RemotingServices.Marshal(vessel, ConfigFactory.CurrentVesselConfig.ObjectUri);
vessel.TryStartingCluster(ConfigFactory.Policy.InternalTokenId);
}
注意代码里面注册的远程对象是Vessel对象。
Vessel vessel = VesselFactory.Create(Creator, this.ConfigFactory, LogicUnitServiceFactory, LogicUnitType);
RemotingServices.Marshal(vessel, ConfigFactory.CurrentVesselConfig.ObjectUri);
Marshal()方法是将MarshalByRefObject类对象转化为ObjRef类对象,这个对象是存储生成代理以与远程对象通讯所需的所有相关信息。这样就可以将该实例序列化以便在应用程序域之间以及通过网络进行传输,客户端就可以调用了。
在服务器端,实际上就注册了一个Vessel对象,对外提供服务,客户端连接的也是Vessel对象。提供InvokeMethod_O() 方法给客户端进行调用,这个方法也是客户端的唯一调用入口。
客户端
客户端调用
EngineClient类图关系
客户端表面上看是使用EngineClient调用服务器端的Remoting服务。但深层次来看,其实它最终调用的是位于LogincUnitConnection中的Vessel对象的InvokeMethod_O()。
调用关系图如下:
客户端调用栈.png
- 从代码角度看,EngineClient 和 OrganizationClient 其实是同级对象,只是EngineClient封装了其他的业务类(OrganizationClient 等)。因此从业务层面上看 EngineClient 类要优越于其他客户端类。
代码角度,他们都是ModuleClient 的派生类 ,隶属于一个父类。
- 在AppUtility.cs 中,声明的是静态的Engine。
internal static OThinker.H3.IEngine MonoEngine
意味着整个web站点只会存在一个IEngine的引用,是一个常驻于内存的对象。
- 在初次获取EngineClient时,会与服务端建立连接,并获取Vessel对象进行缓存。
public class EngineClient : ModuleClient, IEngine
{
public EngineClient(LogicUnitConnectionPool ConnectionPool) : base(ConnectionPool)
{
base.Pool.GetConnection();
}
}
通过LogicUnitConnectionPool来建立连接。
LogicUnitConnectionPool:集群的连接池,缓存与服务端建立的Vessel对象,并且对连接进行管理。
- 客户端每次对服务端请求时,会通过LogicUnitConnectionPool请求一个LogicUnitConnection对象。远程对象是通过调用Connect()来进行激活并缓存的。
public class LogicUnitConnectionPool
{
public LogicUnitConnection Connect(int Index)
{
vessel_O = (IVessel_O)Activator.GetObject(typeof(IVessel_I), string.Concat(new object[]
{
"tcp://",
connectionSetting.Address,
":",
connectionSetting.Port,
"/",
connectionSetting.ObjectUri
}));
}
}
建立连接,并将激活的远程对象缓存。
- 最终的调用是由 LogicUnitConnection中的Vessel对象执行。
public class LogicUnitConnection
{
public object Invoke(string ModuleName, string MethodName, object[] Parameters, out object[] Parameters2){
result = this.Vessel.InvokeMethod_O(this.Pool.LogicUnitCode, this.Pool.AuthenticationInfos[this.Index], out this.Pool.AuthenticationInfos[this.Index].TokenId, Thread.CurrentThread.CurrentUICulture.Name, ModuleName, MethodName, Parameters, out Parameters2);
return result ;
}
}
这里需要注意,调用的时候是传递了,调用的 ModuleName,调用的
MethodName,调用的Parameters。将这些信息传递到服务端,再由服务端通过反射来执行实际的方法。
至此,Remoting在服务端的建立,客户端是如何去激活远程对象,并调用远程对象方法的全过程就完成了。
项目代码地址:https://github.com/DoneOff/H3BPM
如果你觉得这篇文章对你有帮助或启发,请点下关注以及赞,谢谢
网友评论