public void ReceiveStream(Stream streamFromSender) { Console.WriteLine("Receiving stream..."); try { Directory.CreateDirectory("receiver"); var teeStream = new TeeInputStream(streamFromSender, File.OpenWrite(@"receiver\received-compressed-file.rar"), true); DecompressStream(teeStream); teeStream.Close(); //closes primary and secondary stream (autoClose = true) } catch (Exception ex) { Console.WriteLine(ex); } Console.WriteLine("Copied .RAR locally + extracted .RAR!"); }
public void ReceiveStream(Stream streamFromSender) { Console.WriteLine("Forwarding the stream..."); try { //PipeStream: http://www.codeproject.com/Articles/16011/PipeStream-a-Memory-Efficient-and-Thread-Safe-Stre //redirecting the output of one process to the input of another in the command line without using any intermediate data storage. var pipeToReceiver = new PipeStream(); //setup a pipe between forwarder (this) and receiver - for now the pipe is still empty //ASYNC var forwardingTask = Task.Factory.StartNew(() => ForwardStream(pipeToReceiver)); Directory.CreateDirectory("forwarder"); var outputFileStream = File.OpenWrite(@"forwarder\received-compressed-file.rar"); var teeCache = new TeeOutputStream(outputFileStream, pipeToReceiver); var tee = new TeeInputStream(streamFromSender, teeCache, true); DecompressStream(tee); Console.WriteLine("Copied .RAR locally + extracted .RAR!"); //pipeToReceiver cannot be closed because it can still contain data to be processed at the receiver's end teeCache.Flush(); //flushes both streams Console.WriteLine("Pipe to receiver flushed..."); tee.Close(); //closes primary and secondary stream (autoClose = true) ; secondary stream => will only close outputFileStream (autoClose = false) //wait untill all bytes from input are transferred to the receiver forwardingTask.Wait(); } catch (Exception ex) { Console.WriteLine(ex); } Console.WriteLine("Stream successfully forwarded!"); }