[Remoting专题系列] 五:信道

None.gif 信道(Channel)是 Remoting 体系的承载平台,负责处理客户端和服务器之间的通讯,其内容包括跨域通讯、消息传递、对象编码等等。信道必须实现 IChannel 接口,根据通讯方向又分别提供了继承版本 IChannelReceiver 和 IChannelSender。Remoting 框架为我们提供了 IPC、TCP 以及 HTTP 的实现版本,当然我们还可以在网络上找到其他协议的实现版本。
None.gif
None.gifTcpServerChannel channel 
=   new  TcpServerChannel( 801 );
None.gifChannelServices.RegisterChannel(channel, 
false );
None.gif
None.gif我们可以使用实用类 ChannelServices 来管理程序域内的信道,诸如注册、注销等等。程序域内可以同时使用多个信道,每个信道需提供唯一的名称,以便 ChannelServices 进行管理,同时信道会随程序域的退出自动销毁。
None.gif
None.gifTcpServerChannel channel 
=   new  TcpServerChannel( " tcp801 " 801 );
None.gifChannelServices.RegisterChannel(channel, 
false );
None.gif
None.gifIChannel c2 
=  ChannelServices.GetChannel( " tcp801 " );
None.gifConsole.WriteLine(Object.ReferenceEquals(channel, c2));
None.gif
None.gifchannel.StopListening(
null );
None.gifchannel.StartListening(
null );
None.gif
None.gif
foreach  (IChannel c  in  ChannelServices.RegisteredChannels)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  Console.WriteLine(c.ChannelName);
ExpandedBlockEnd.gif}

None.gif
None.gifChannelServices.UnregisterChannel(channel);
None.gif
None.gif信道内部包含由多个接收器(Sink)组成的接收链(Sink Chain)。接收器用于处理客户端和服务器之间的来往消息,诸如格式化程序接收器(FormatterSink)、传输接收器(TransportSink)或堆栈生成器接收器(StackBuilderSink)等等。每个接收器或实现 IClientChannelSink,或实现 IServerChannelSink。
None.gif
None.gifRemoting 采用信道接收提供程序(Channel Sink Provider,实现 IClientChannelSinkProvider、IClientFormatterSinkProvider 或 IServerChannelSinkProvider 接口的对象) 来创建接收器,已有的提供程序包括 BinaryClientFormatterSinkProvider 
/  BinaryServerFormatterSinkProvider、SoapClientFormatterSinkProvider  /  SoapServerFormatterSinkProvider。
None.gif
None.gif在客户端接收链中第一个接收器通常是格式化程序接收器(IClientFormatterSink),且必须实现 IMessageSink。代理通过信道接收提供程序找个该接收器,并通过接口方法将消息传递给链中所有的接收器,最后由传输接收器发送到服务器。同样,在服务器排在最后的接收器是格式化程序接收器和堆栈生成器接收器,分别执行反序列化和将将消息转换成相应的调用堆栈。
None.gif
None.gifTcpServerChannel channel 
=   new  TcpServerChannel( " tcp801 " 801 new  BinaryServerFormatterSinkProvider());
None.gifChannelServices.RegisterChannel(channel, 
false );
None.gif
None.gifdot.gif
None.gif
None.gifTcpClientChannel channel 
=   new  TcpClientChannel( " tcp801 " new  BinaryClientFormatterSinkProvider());
None.gifChannelServices.RegisterChannel(channel, 
false );
None.gif
None.gif只要看看 BinaryClientFormatterSinkProvider 和 BinaryClientFormatterSink 的代码就很容易理解如何使用提供者模型构造一个接收链了。
None.gif
None.gifBinaryClientFormatterSinkProvider
None.gif
public  IClientChannelSink CreateSink(IChannelSender channel,  string  url,  object  remoteChannelData)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  IClientChannelSink sink1 
= null;
InBlock.gif  
if (this._next != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    sink1 
= this._next.CreateSink(channel, url, remoteChannelData);
InBlock.gif    
if (sink1 == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return null;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

InBlock.gif  SinkChannelProtocol protocol1 
= CoreChannel.DetermineChannelProtocol(channel);
InBlock.gif  BinaryClientFormatterSink sink2 
= new BinaryClientFormatterSink(sink1);
InBlock.gif  sink2.IncludeVersioning 
= this._includeVersioning;
InBlock.gif  sink2.StrictBinding 
= this._strictBinding;
InBlock.gif  sink2.ChannelProtocol 
= protocol1;
InBlock.gif  
return sink2;
ExpandedBlockEnd.gif}

None.gif
None.gifBinaryClientFormatterSink
None.gif
public  BinaryClientFormatterSink(IClientChannelSink nextSink)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
this._includeVersioning = true;
InBlock.gif  
this._channelProtocol = SinkChannelProtocol.Other;
InBlock.gif  
this._nextSink = nextSink;
ExpandedBlockEnd.gif}

None.gif
None.gif
public  IClientChannelSink NextChannelSink
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
get
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
return this._nextSink;
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif

 

信道内的接收器是可 "插入的",这意味着我们可以实现自己的接收器,并将其装配到信道接收链中。比如对消息进行加密,或者对数据流进行压缩等等。
有关细节可参考:
.NET Remoting Customization Made Easy: Custom Sinks
如何定制Sink扩展.Net Remoting功能

转载于:https://www.cnblogs.com/nbwzy/archive/2007/06/05/771572.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值