Example #1
0
        private bool doReconn(NetworkStream stream, ulong writeCount, ulong readCount)
        {
            Thread thread        = null;
            bool   rereadSucceed = false;

            if (writeCount != _ReadCount)
            {
                thread = new Thread(() => {
                    int n         = (int)writeCount - (int)_ReadCount;
                    rereadSucceed = _Rereader.Reread(stream, n);
                });
                thread.Start();
            }

            if (_WriterCount != readCount)
            {
                if (!_Rewriter.Rewrite(stream, _WriterCount, readCount))
                {
                    return(false);
                }
            }

            if (thread != null)
            {
                thread.Join();
                if (!rereadSucceed)
                {
                    return(false);
                }
            }

            setBaseStream(stream);
            return(true);
        }
Example #2
0
        public void RewriterTest()
        {
            ulong writeCount = 0;
            ulong readCount  = 0;

            var w = new Rewriter(100);

            for (var i = 0; i < 100000; i++)
            {
                var a = RandBytes(100);
                var b = new byte[a.Length];
                w.Push(a, 0, a.Length);
                writeCount += (ulong)a.Length;

                var remind = a.Length;
                var offset = 0;
                while (remind > 0)
                {
                    var size = rand.Next(remind) + 1;

                    using (MemoryStream ms = new MemoryStream(b, offset, b.Length - offset)) {
                        Assert.True(w.Rewrite(ms, writeCount, readCount));
                    }

                    readCount += (ulong)size;
                    offset    += size;
                    remind    -= size;
                }

                Assert.True(BytesEquals(a, b));
            }
        }