在Remoting Server上取得Remoting Client的IP地址
2004-11-25 08:53
465 查看
In short, the Remoting Server can make use of Sink and Sink Provider to retrieve the IP address of the incoming request. The IP address is available in the Transport Headers of the incoming message. After retrieving the IP address in the Sink, we save the IP address to the CallContext, so that it will be available later in the code execution path as well. The followings are some background information regarding the topics:
Sinks and Sink Chains
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconsinkssinkchains.asp
Using CallContext
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconusingcallcontext.asp
Code modified from the sample by Ingo Rammer, author of “Advanced .Net Remoting”:
using System;
using System.Collections;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging ;
using System.Runtime.Remoting.Channels;
using System.Threading;
using System.Net;
namespace ClassLibRemotingIPSink
{
public class ClientIPServerSinkProvider: IServerChannelSinkProvider
{
private IServerChannelSinkProvider next = null;
public ClientIPServerSinkProvider(IDictionary properties, ICollection providerData)
{
}
public void GetChannelData (IChannelDataStore channelData)
{
}
public IServerChannelSink CreateSink (IChannelReceiver channel)
{
IServerChannelSink nextSink = null;
if (next != null)
{
nextSink = next.CreateSink(channel);
}
return new ClientIPServerSink(nextSink);
}
public IServerChannelSinkProvider Next
{
get { return next; }
set { next = value; }
}
}
public class ClientIPServerSink : BaseChannelObjectWithProperties, IServerChannelSink, IChannelSinkBase
{
private IServerChannelSink _next;
public ClientIPServerSink (IServerChannelSink next)
{
_next = next;
}
public void AsyncProcessResponse ( System.Runtime.Remoting.Channels.IServerResponseChannelSinkStack sinkStack , System.Object state , System.Runtime.Remoting.Messaging.IMessage msg , System.Runtime.Remoting.Channels.ITransportHeaders headers , System.IO.Stream stream )
{
}
public Stream GetResponseStream ( System.Runtime.Remoting.Channels.IServerResponseChannelSinkStack sinkStack , System.Object state , System.Runtime.Remoting.Messaging.IMessage msg , System.Runtime.Remoting.Channels.ITransportHeaders headers )
{
return null;
}
public System.Runtime.Remoting.Channels.ServerProcessing ProcessMessage(System.Runtime.Remoting.Channels.IServerChannelSinkStack sinkStack, System.Runtime.Remoting.Messaging.IMessage requestMsg, System.Runtime.Remoting.Channels.ITransportHeaders requestHeaders, System.IO.Stream requestStream, out System.Runtime.Remoting.Messaging.IMessage responseMsg, out System.Runtime.Remoting.Channels.ITransportHeaders responseHeaders, out System.IO.Stream responseStream)
{
if (_next != null)
{
IPAddress ip = requestHeaders[CommonTransportKeys.IPAddress] as IPAddress;
Console.WriteLine(ip.ToString());
CallContext.SetData("ClientIPAddress",ip);
ServerProcessing spres = _next.ProcessMessage (sinkStack,requestMsg, requestHeaders,requestStream,out responseMsg,out responseHeaders,out responseStream);
return spres;
}
else
{
responseMsg=null;
responseHeaders=null;
responseStream=null;
return new ServerProcessing();
}
}
public IServerChannelSink NextChannelSink
{
get {return _next;}
set {_next = value;}
}
}
}
Sinks and Sink Chains
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconsinkssinkchains.asp
Using CallContext
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconusingcallcontext.asp
Code modified from the sample by Ingo Rammer, author of “Advanced .Net Remoting”:
using System;
using System.Collections;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging ;
using System.Runtime.Remoting.Channels;
using System.Threading;
using System.Net;
namespace ClassLibRemotingIPSink
{
public class ClientIPServerSinkProvider: IServerChannelSinkProvider
{
private IServerChannelSinkProvider next = null;
public ClientIPServerSinkProvider(IDictionary properties, ICollection providerData)
{
}
public void GetChannelData (IChannelDataStore channelData)
{
}
public IServerChannelSink CreateSink (IChannelReceiver channel)
{
IServerChannelSink nextSink = null;
if (next != null)
{
nextSink = next.CreateSink(channel);
}
return new ClientIPServerSink(nextSink);
}
public IServerChannelSinkProvider Next
{
get { return next; }
set { next = value; }
}
}
public class ClientIPServerSink : BaseChannelObjectWithProperties, IServerChannelSink, IChannelSinkBase
{
private IServerChannelSink _next;
public ClientIPServerSink (IServerChannelSink next)
{
_next = next;
}
public void AsyncProcessResponse ( System.Runtime.Remoting.Channels.IServerResponseChannelSinkStack sinkStack , System.Object state , System.Runtime.Remoting.Messaging.IMessage msg , System.Runtime.Remoting.Channels.ITransportHeaders headers , System.IO.Stream stream )
{
}
public Stream GetResponseStream ( System.Runtime.Remoting.Channels.IServerResponseChannelSinkStack sinkStack , System.Object state , System.Runtime.Remoting.Messaging.IMessage msg , System.Runtime.Remoting.Channels.ITransportHeaders headers )
{
return null;
}
public System.Runtime.Remoting.Channels.ServerProcessing ProcessMessage(System.Runtime.Remoting.Channels.IServerChannelSinkStack sinkStack, System.Runtime.Remoting.Messaging.IMessage requestMsg, System.Runtime.Remoting.Channels.ITransportHeaders requestHeaders, System.IO.Stream requestStream, out System.Runtime.Remoting.Messaging.IMessage responseMsg, out System.Runtime.Remoting.Channels.ITransportHeaders responseHeaders, out System.IO.Stream responseStream)
{
if (_next != null)
{
IPAddress ip = requestHeaders[CommonTransportKeys.IPAddress] as IPAddress;
Console.WriteLine(ip.ToString());
CallContext.SetData("ClientIPAddress",ip);
ServerProcessing spres = _next.ProcessMessage (sinkStack,requestMsg, requestHeaders,requestStream,out responseMsg,out responseHeaders,out responseStream);
return spres;
}
else
{
responseMsg=null;
responseHeaders=null;
responseStream=null;
return new ServerProcessing();
}
}
public IServerChannelSink NextChannelSink
{
get {return _next;}
set {_next = value;}
}
}
}
相关文章推荐
- 各地的ADSL MODEM VPI、VCI 和DNS的信息
- Windows多线程多任务设计初步
- html及javascript技巧备份
- Windows平台下的多线程编程
- 曝光模式
- 一年的辛勤换作激情燃烧的一晚
- eclipse启动参数研究
- 编程打开一些特殊的系统窗口(如控制面板等)
- 编程打开一些特殊的系统窗口(如控制面板等)
- 计算机族必喝的茶
- 白领Man 丢掉肥肉!要“骨感”
- 保护代码中的数据库连接字符串和其他敏感设置
- 整合Oracle 10g、Apache 2.0、Php 5
- 中文搜索引擎技术揭密: 中文分词
- 中文搜索引擎技术揭密:排序技术
- 中文搜索引擎技术揭密:网络蜘蛛
- 中文搜索引擎技术揭密:系统架构
- 一个女孩从十楼跳下自杀所看到的~
- 清晨6点半的街
- JSP-----循序渐进教程