Reputation: 127543
First off I want to be clear that I am not referring to simulcasting a TCP/IP stream, I want to take the output of a c# stream and have it go to multiple destations.
For example if I had a FileStream(fs) and a MemoryStream(ms) and a FtpStream(ftps) and did something like
...
SuperStreamWriter ss = new SuperStreamWriter(fs, ms, ftps);
ss.Write(helloWorld, 0, helloWorld.length);
}
class SuperStreamWriter : Stream
{
Stream[] _s;
public SuperStreamWriter(params Stream[] s)
{
_s = s;
}
public override void Write(byte[] buffer, int offset, int count)
{
foreach (Stream s in _s)
{
s.Write(buffer, offset, count);
}
}
//Other functions cut for example
}
Hello world would be pushed out to all three of my streams. Does anyone know of anything that will give me similar functionality to what I am describing other using that foreach loop on the buffer? Also if the foreach loop is my only/best option would it be safe to thread each iteration of the foreach loop as is?
Upvotes: 4
Views: 231
Reputation: 13419
I don't know of anything that will simultaneously write to multiple streams, and it would boil down to a loop at some point in any case.
Note that if you go with the threaded implementation (which would certainly be beneficial from a performance perspective), be aware that the default implementation of Stream.BeginWrite
is synchronous - derived classes must implement their own asynchronous logic. FileStream
implements its own asynchronous behavior, for example, but MemoryStream
does not.
Upvotes: 1