C#流管道(流间谍) [英] C# Stream Pipe (Stream Spy)
问题描述
我有一个输入流和一个流读取器组件.这工作正常,但现在我想记录所有流量(将副本保存在文件中). 所以我需要监视流.我正在考虑的解决方案是流管道(管道)或流包装器,该流包装器将流作为输入,然后让我首先了解流量.像这样:
I have an input stream and a stream reader component. This works fine but now I want to log all the traffic (save a copy in a file). So I need to spy on a stream. A solution I am thinking of is a stream pipe (pipeline) or a stream wrapper that takes a stream as input and then gives me a first look at the traffic. Something like this:
void Init(System.Net.Sockets.NetworkStream stream)
{
System.IO.Stream wrappedStream = new MyWrapper(stream);
wrappedStream.ReadSpy = MyMethod;
XmlReader reader = XmlReader.Create(wrappedStream);
}
// This will get called after some bytes have been read from a stream,
// but before they get passed to the XmlReader
byte[] MyMethod(byte[] buffer)
{
m_Writer.Write(buffer); // write to a file
return buffer; // Give to XmlReader
}
推荐答案
您想要的称为装饰器模式.这是一种动态添加/修改行为的技术:
What you want is called the Decorator Pattern. It's a technique for dynamically adding/modifying behavior:
- http://sourcemaking.com/design_patterns/decorator
- http://www.oodesign.com/decorator-pattern.html
要执行此操作,您想使用接受另一个Stream
实例的factory的构造函数来实现抽象类Stream
.您为抽象类的每个方法/重载提供一个实现,该实现在修饰的Stream
上调用相同的方法/重载,并执行您需要的任何其他工作.
To do this, you want to implement the abstract class Stream
, with a constructor of factory that accepts another Stream
instance. You provide an implementation for every method/overload of the abstract class that invokes the same method/overload on the decorated Stream
, plus doing whatever additional work your needs require.
完成此操作并用新的装饰器装饰Stream
后,它可以与接受Stream
的其他任何东西互换使用,包括其他类似的装饰器:装饰器甚至可以嵌套,例如洋葱来组成您所需的行为.
Once you've done that and decorated a Stream
with your new decorator, It can be used interchangeably by anything else that accepts a Stream
, including other similar decorators: decorators can even be nested, like layers of an onion to compose the behaviors you need.
类似这样的东西:
class StreamInterceptor : Stream
{
public Stream DecoratedInstance { get; set; }
public event Action<byte[]> BytesRead;
public event Action<byte[]> BytesWritten;
public StreamInterceptor( Stream instance )
{
if ( instance == null ) throw new ArgumentNullException("instance");
this.DecoratedInstance = instance ;
return ;
}
public override bool CanRead
{
get { return DecoratedInstance.CanRead; }
}
public override bool CanSeek
{
get { return DecoratedInstance.CanSeek; }
}
public override bool CanWrite
{
get { return DecoratedInstance.CanWrite; }
}
public override void Flush()
{
DecoratedInstance.Flush();
return;
}
public override long Length
{
get { return DecoratedInstance.Length; }
}
public override long Position
{
get { return DecoratedInstance.Position; }
set { DecoratedInstance.Position = value; }
}
public override int Read( byte[] buffer , int offset , int count )
{
int bytesRead = DecoratedInstance.Read(buffer, offset, count);
// raise the bytes read event
byte[] temp = new byte[bytesRead];
Array.Copy(buffer,offset,temp,0,bytesRead);
BytesRead(temp);
return bytesRead;
}
public override long Seek( long offset , SeekOrigin origin )
{
return DecoratedInstance.Seek(offset, origin);
}
public override void SetLength( long value )
{
DecoratedInstance.SetLength(value);
return;
}
public override void Write( byte[] buffer , int offset , int count )
{
// raise the bytes written event
byte[] temp = new byte[count];
Array.Copy(buffer,offset,temp,0,count);
BytesWritten(temp);
DecoratedInstance.Write(buffer, offset, count);
return;
}
}
一旦有了,您可以说出这样的话:
Once you have that, you can say something like this:
static void Main()
{
StreamInterceptor si = new StreamInterceptor(File.Open("foo.bar.txt",FileMode.Open,FileAccess.ReadWrite,FileShare.Read));
si.BytesRead += (bytes) => { Console.WriteLine("{0} bytes read", bytes.Length); } ;
si.BytesWritten += (bytes) => { Console.WriteLine("{0} bytes written", bytes.Length); } ;
Stream s = (Stream) si ;
DoSomethingUseful(s);
}
无论何时有人从流中读取或写入流,您的事件处理程序都将被调用.
And your event handler will be invoked whenever somebody reads or writes from the stream.
这篇关于C#流管道(流间谍)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!