9.3.4. Пример Stream

sdk\c-sharp\stream\stream\Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Linq;
using System.Reflection;

#if (DEBUG)
using RobotNET.Factory.Debug;
#else
using RobotNET.Factory.Release;
#endif

namespace Stream
{
    class Program
    {
        static void Main( string[] args )
        {
            Console.WriteLine( string.Format( "Program started!" ) );

            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory;
#if (DEBUG)
                using( Factory factory = new Factory( args[0], args[1], "sdk\\c-sharp\\stream\\stream-plugin\\bin\\debug\\stream-plugin.dll", "Stream.Plugin" ) )
#else
                using( Factory factory = new Factory( args[0], args[1], "sdk\\c-sharp\\stream\\stream-plugin\\bin\\release\\stream-plugin.dll", "Stream.Plugin" ) )
#endif
                    try
                    {
                        int blockSize = 32767;
                        byte[] buffer = new byte[blockSize];
                        int read;

                        FileStream orig = new FileStream( path + "\\" + args[2], FileMode.Open );
                        long max = orig.Length;
                        int len = 0;
                        while( ( read = orig.Read( buffer, 0, blockSize ) ) > 0 )
                        {
                            factory.WriteBytes( new byte[] { 1 } );
                            byte[] block = new byte[read];
                            Buffer.BlockCopy( buffer, 0, block, 0, read );
                            factory.WriteBytes( block );

                            len += read;
                            Console.WriteLine( "<-- {0} / {1}", len, max );
                        }
                        factory.WriteBytes( new byte[] { 0 } );
                        orig.Close();
                        orig.Dispose();

                        FileStream test = new FileStream( path + "\\test-" + args[2], FileMode.Create );
                        byte[] h;
                        while( ( h = factory.ReadBytes() )[0] == 1 )
                        {
                            byte[] block = factory.ReadBytes();
                            test.Write( block, 0, block.Length );

                            len -= block.Length;
                            Console.WriteLine( "--> {0} / {1}", len, max );
                        }
                        test.Close();
                        test.Dispose();

                        if( len > 0 )
                        {
                            Console.WriteLine( "FAIL" );
                            Console.ReadKey();
                        }

                        Console.WriteLine( "Sleep 3000..." );
                        System.Threading.Thread.Sleep( 3000 );

                        byte[] origMD5 = null;
                        byte[] testMD5 = null;
                        byte[] tempMD5 = null;
                        using( var md5 = MD5.Create() )
                        {
                            using( var stream = File.OpenRead( path + "\\" + args[2] ) )
                            {
                                origMD5 = md5.ComputeHash( stream );
                            }
                            using( var stream = File.OpenRead( path + "\\test-" + args[2] ) )
                            {
                                testMD5 = md5.ComputeHash( stream );
                            }
#if (DEBUG)
                            using( var stream = File.OpenRead( path + "\\temp.dat" ) )
                            {
                                tempMD5 = md5.ComputeHash( stream );
                            }
#else
                            tempMD5 = testMD5;    
#endif
                        }

                        if( origMD5 != null && testMD5 != null )
                            if( origMD5.SequenceEqual( testMD5 ) && testMD5.SequenceEqual( tempMD5 ) )
                                Console.WriteLine( "MATCH" );
                    }
                    catch( Exception e )
                    {
                        Console.WriteLine( string.Format( "Program catch an error at Main \"{0}\"", e ) );
                    }
            }
            catch( Exception e )
            {
                Console.WriteLine( e );
            }

            Console.WriteLine( string.Format( "Program finished!" ) );
        }
    }
}

sdk\c-sharp\stream\stream-plugin\Plugin.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;

using RobotNET.Factory;

namespace Stream
{
    public class Plugin : MarshalByRefObject, IPlugin
    {
        public void Worker( object _pipe )
        {
            // Поток должен завершиться, если завершилось выполнение кода или был разрушен пайп
            IPipe pipe = ( IPipe )_pipe;

            Console.WriteLine( string.Format( "Plugin started!" ) );

            string path = AppDomain.CurrentDomain.BaseDirectory;

            int blockSize = 8192;
            byte[] buffer = new byte[blockSize];
            int read;

            FileStream test = new FileStream( path + "\\temp.dat", FileMode.Create );
            int len = 0;
            while( ( pipe.ReadBytes() )[0] == 1 )
            {
                byte[] block = pipe.ReadBytes();
                test.Write( block, 0, block.Length );

                len += block.Length;
                Console.WriteLine( "--> {0} / ?", len );
            }
            test.Close();
            test.Dispose();

            FileStream orig = new FileStream( path + "\\temp.dat", FileMode.Open );
            long max = orig.Length;
            while( ( read = orig.Read( buffer, 0, blockSize ) ) > 0 )
            {
                pipe.WriteBytes( new byte[] { 1 } );
                byte[] block = new byte[read];
                Buffer.BlockCopy( buffer, 0, block, 0, read );
                pipe.WriteBytes( block );

                len -= read;
                Console.WriteLine( "<-- {0} / {1}", len, max );
            }
            pipe.WriteBytes( new byte[] { 0 } );
            orig.Close();
            orig.Dispose();

            Console.WriteLine( string.Format( "Plugin finished!" ) );
        }
    }
}