Esempio n. 1
0
        static IStreamable <Empty, Payload> Where_Bench(StreamCache <Empty, Payload> stream)
        {
            var result = stream
                         .Where(e => WhereOps(e));

            return(result);
        }
Esempio n. 2
0
        public ShardedCacheObserver(StreamCache <TKey, TPayload> cache, StreamProperties <TKey, TPayload> sourceProps)
        {
            this.cache       = cache;
            this.sourceProps = sourceProps;

            this.elements = new List <QueuedMessage <StreamMessage <TKey, TPayload> > >();
        }
Esempio n. 3
0
        static IStreamable <Empty, Payload> Join_Bench(StreamCache <Empty, Payload> streamA, StreamCache <Empty, Payload> streamB)
        {
            var result = streamA
                         .Join(streamB, (l, r) => JoinOps(l, r));

            return(result);
        }
Esempio n. 4
0
 public MediaStreamApiController(
     ApplicationDbContext dbContext,
     StreamCache streamCache)
 {
     _dbContext   = dbContext;
     _streamCache = streamCache;
 }
Esempio n. 5
0
 public StreamCacheStream(StreamCache cache, long start, long length)
 {
     m_start  = start;
     m_length = length;
     m_cache  = cache;
     Position = m_start;
 }
Esempio n. 6
0
        public void TestCacheRecoverAbandondStream()
        {
            Stream stream;

            using (StreamCache cache = new StreamCache(new SharedMemoryStream(), 1))
            {
                if (true)
                {
                    stream = cache.Open();
                    stream.Write(new byte[100], 25, 55);
                    stream = null;//simulated "accidental" object abandonment... i.e. someone does something stupid.
                }

                GC.Collect(0, GCCollectionMode.Forced);
                GC.WaitForPendingFinalizers();

                Thread t = new Thread(
                    delegate()
                {
                    using (stream = cache.Open(FileAccess.Read))
                    {
                        Assert.AreEqual(new byte[55], IOStream.ReadAllBytes(stream));
                    }
                }
                    );
                t.IsBackground = true;
                t.Start();
                Assert.IsTrue(t.Join(1000));
            }
        }
Esempio n. 7
0
        public void TestStreamCacheDisposes()
        {
            Stream stream;

            using (StreamCache cache = new StreamCache(new SharedMemoryStream(), 1))
            {
                stream = cache.Open();
                Assert.IsTrue(stream.CanRead && stream.CanWrite);
            }

            Assert.IsFalse(stream.CanRead || stream.CanWrite);
            try
            {
                stream.ReadByte();
                Assert.Fail();
            }/* why InvalidOperation?, the underlying stream was disposed, not the stream itself */
            catch (InvalidOperationException) { }

            stream.Dispose();
            try
            {
                stream.WriteByte(1);
                Assert.Fail();
            }/* Now it's been disposed */
            catch (ObjectDisposedException) { }
        }
Esempio n. 8
0
        public void TestStreamCanI()
        {
            Stream stream;

            using (StreamCache cache = new StreamCache(new SharedMemoryStream(), 1))
            {
                using (stream = cache.Open())
                {
                    Assert.IsTrue(stream.CanRead);
                    Assert.IsTrue(stream.CanWrite);
                    Assert.IsTrue(stream.CanSeek);
                }
                using (stream = ((IFactory <Stream>)cache).Create())
                {
                    Assert.IsTrue(stream.CanRead);
                    Assert.IsTrue(stream.CanWrite);
                    Assert.IsTrue(stream.CanSeek);
                }
                using (stream = cache.Open(FileAccess.Read))
                {
                    Assert.IsTrue(stream.CanRead);
                    Assert.IsFalse(stream.CanWrite);
                    Assert.IsTrue(stream.CanSeek);
                }
                using (stream = cache.Open(FileAccess.Write))
                {
                    Assert.IsFalse(stream.CanRead);
                    Assert.IsTrue(stream.CanWrite);
                    Assert.IsTrue(stream.CanSeek);
                }
            }
        }
        public void TestStreamCacheDisposes()
        {
            Stream stream;
            using (StreamCache cache = new StreamCache(new SharedMemoryStream(), 1))
            {
                stream = cache.Open();
                Assert.IsTrue(stream.CanRead && stream.CanWrite);
            }

            Assert.IsFalse(stream.CanRead || stream.CanWrite);
            try
            {
                stream.ReadByte();
                Assert.Fail();
            }/* why InvalidOperation?, the underlying stream was disposed, not the stream itself */
            catch (InvalidOperationException) { }

            stream.Dispose();
            try
            {
                stream.WriteByte(1);
                Assert.Fail();
            }/* Now it's been disposed */
            catch (ObjectDisposedException) { }
        }
Esempio n. 10
0
        static IStreamable <Empty, Payload> Select_Bench(StreamCache <Empty, Payload> stream)
        {
            var result = stream
                         .Select(e => SelectOps(e));

            return(result);
        }
 public void TestStreamCanI()
 {
     Stream stream;
     using (StreamCache cache = new StreamCache(new SharedMemoryStream(), 1))
     {
         using (stream = cache.Open())
         {
             Assert.IsTrue(stream.CanRead);
             Assert.IsTrue(stream.CanWrite);
             Assert.IsTrue(stream.CanSeek);
         }
         using (stream = ((IFactory<Stream>)cache).Create())
         {
             Assert.IsTrue(stream.CanRead);
             Assert.IsTrue(stream.CanWrite);
             Assert.IsTrue(stream.CanSeek);
         }
         using (stream = cache.Open(FileAccess.Read))
         {
             Assert.IsTrue(stream.CanRead);
             Assert.IsFalse(stream.CanWrite);
             Assert.IsTrue(stream.CanSeek);
         }
         using (stream = cache.Open(FileAccess.Write))
         {
             Assert.IsFalse(stream.CanRead);
             Assert.IsTrue(stream.CanWrite);
             Assert.IsTrue(stream.CanSeek);
         }
     }
 }
Esempio n. 12
0
        static IStreamable <Empty, long> Agg_Bench(StreamCache <Empty, Payload> stream)
        {
            var result = stream
                         .TumblingWindowLifetime(10000)
                         .Aggregate(w => w.Sum(e => AggOps(e)));

            return(result);
        }
 public void Close()
 {
     disposed = true;
     stream.Close();
     root = null;
     StreamCache.Clear();
     BStreamCache.Clear();
 }
Esempio n. 14
0
 internal byte[] InternalRender(string format, bool allowInternalRenderers, string deviceInfo, PageCountMode pageCountMode, out string mimeType, out string encoding, out string fileNameExtension, out string[] streams, out Warning[] warnings)
 {
     lock (m_syncObject)
     {
         using (StreamCache streamCache = new StreamCache())
         {
             InternalRender(format, allowInternalRenderers, deviceInfo, pageCountMode, streamCache.StreamCallback, out warnings);
             streams = new string[0];
             return(streamCache.GetMainStream(out encoding, out mimeType, out fileNameExtension));
         }
     }
 }
Esempio n. 15
0
        public Task InvokeFinal(IOwinContext context, string prefix, string suffix, string plugin, string path, Func <IOwinContext, Task> noMatchHandler, double expires, IDictionary <string, string> addonHttpHeaders)
        {
            //加前缀
            if (!String.IsNullOrEmpty(prefix))
            {
                path = $"{prefix}/{path}";
            }

            List <String> resourceUrlList = new List <string>();

            resourceUrlList.Add($"resource://{plugin}/{path}");
            //加后缀
            if (!String.IsNullOrEmpty(suffix))
            {
                path = $"{path}{suffix}";
            }
            resourceUrlList.Add($"resource://{plugin}/{path}");

            //先尝试从缓存中获取
            if (UseMemoryCache)
            {
                foreach (var url in resourceUrlList)
                {
                    var streamCache = StreamCacheManager.GetCache(url);
                    if (streamCache != null)
                    {
                        return(handleResource(context, streamCache.GetStream(), null, url, expires, addonHttpHeaders));
                    }
                }
            }

            Stream stream;
            ResourceWebResponse resourceResponse;

            foreach (var url in resourceUrlList)
            {
                stream = getUrlStream(url, out resourceResponse);
                if (stream != null)
                {
                    if (UseMemoryCache)
                    {
                        var streamCache = new StreamCache(stream);
                        StreamCacheManager.AddCache(url, streamCache);
                        stream.Close();
                        stream.Dispose();
                        stream = streamCache.GetStream();
                    }
                    return(handleResource(context, stream, resourceResponse.LastModified, resourceResponse.Uri.LocalPath, expires, addonHttpHeaders));
                }
            }
            return(noMatchHandler(context));
        }
        async Task <AssetEntry <ISyncModel> > DownloadSyncModel(StreamKey streamKey, string hash, CancellationToken token)
        {
            StreamCache cache;

            lock (m_StreamCaches)
            {
                if (!m_StreamCaches.TryGetValue(streamKey, out cache))
                {
                    m_StreamCaches[streamKey] = cache = new StreamCache(streamKey);
                }
            }

            Task <ISyncModel> task;
            var isTaskOwner = false;

            lock (cache)
            {
                if (cache.model != null && cache.hash == hash)
                {
                    return(new AssetEntry <ISyncModel>(streamKey, cache.model));
                }

                if (cache.task != null)
                {
                    task = cache.task;
                }
                else
                {
                    isTaskOwner = true;
                    cache.model = null;
                    cache.hash  = hash;
                    cache.task  = task = m_Client.GetSyncModelAsync(cache.streamKey, cache.hash); // TODO Cancellation Token
                }
            }

            token.ThrowIfCancellationRequested();

            var syncModel = await task;

            if (isTaskOwner)
            {
                lock (cache)
                {
                    cache.model = syncModel;
                    cache.task  = null;
                }
            }

            return(new AssetEntry <ISyncModel>(streamKey, syncModel));
        }
                public BytesToRead(Client client, int threadLimit, Guid transferId, Salt sessionKey, string location, StreamCache streams, long offset, int count)
                {
                    _client     = client;
                    _transferId = transferId;
                    _sessionKey = sessionKey;
                    _location   = location;
                    _streams    = streams;
                    _offset     = offset;
                    _count      = count;

                    _throttle = new Semaphore(threadLimit, threadLimit, GetType().FullName);
                    _throttle.WaitOne();
                    _aquired = true;
                }
            /// <summary>
            /// Called to send a specific length of bytes to a server identified by serverKey.  The transfer
            /// is a blocking call and returns on success or raises an exception.  If Abort() is called durring
            /// the transfer, or if a ProgressChanged event handler raises the OperationCanceledException, the
            /// transfer is silently terminated and the method will return false.
            /// </summary>
            /// <param name="location">A string of up to 1024 bytes in length</param>
            /// <param name="filename">The name of the file to write to</param>
            /// <returns>True if the file was successfully received by the server</returns>
            public bool Download(string location, string filename)
            {
                FileStreamFactory file = new FileStreamFactory(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);

                using (TempFile temp = TempFile.Attach(filename))
                    using (StreamCache cache = new StreamCache(file, LimitThreads))
                    {
                        temp.Length = 0;
                        if (Download(location, cache))
                        {
                            temp.Detatch();
                            return(true);
                        }
                    }
                return(false);
            }
Esempio n. 19
0
        public void TestCacheLimit()
        {
            bool finished = false;

            using (SharedMemoryStream shared = new SharedMemoryStream())
                using (StreamCache cache = new StreamCache(shared, 1))
                {
                    ManualResetEvent ready   = new ManualResetEvent(false);
                    ManualResetEvent release = new ManualResetEvent(false);
                    Thread           t       = new Thread(
                        delegate()
                    {
                        using (Stream stream = cache.Open(FileAccess.ReadWrite))
                        {
                            stream.Write(new byte[50], 0, 50);
                            ready.Set();
                            release.WaitOne();
                            stream.Write(new byte[50], 0, 50);
                            GC.KeepAlive(stream);
                            finished = true;
                        }
                    }
                        );
                    t.IsBackground = true;
                    t.Start();

                    Assert.IsTrue(ready.WaitOne());
                    Assert.AreEqual(50, shared.Length);

                    new Thread(
                        delegate()
                    {
                        Thread.Sleep(10);
                        release.Set();
                    }
                        ).Start();

                    Assert.IsFalse(finished);
                    using (Stream stream = cache.Open())
                    {
                        Assert.IsTrue(finished);
                        Assert.IsTrue(release.WaitOne(0, false));
                        Assert.AreEqual(100, stream.Read(new byte[100], 0, 100));
                    }
                    Assert.IsTrue(t.Join(1000));
                }
        }
Esempio n. 20
0
        public void TestStreamReadWrite()
        {
            Stream stream;

            using (StreamCache cache = new StreamCache(new SharedMemoryStream(), 1))
            {
                using (stream = cache.Open())
                {
                    stream.Write(new byte[100], 25, 55);
                    stream.Close();
                }
                using (stream = cache.Open(FileAccess.Read))
                {
                    Assert.AreEqual(new byte[55], IOStream.ReadAllBytes(stream));
                }
            }
        }
        public void TestFileStreamCache()
        {
            Stream stream;
            using (TempFile tempFile = new TempFile())
            {
                using (StreamCache cache = new StreamCache(new FileStreamFactory(tempFile.TempPath, FileMode.Open)))
                {
                    using (stream = cache.Open())
                    {
                        stream.SetLength(100);
                        stream.WriteByte(1);
                    }
                }

                Assert.AreEqual(100, tempFile.Length);
                using (stream = tempFile.Open())
                    Assert.AreEqual(1, stream.ReadByte());
            }
        }
Esempio n. 22
0
        public void TestFileStreamCache()
        {
            Stream stream;

            using (TempFile tempFile = new TempFile())
            {
                using (StreamCache cache = new StreamCache(new FileStreamFactory(tempFile.TempPath, FileMode.Open)))
                {
                    using (stream = cache.Open())
                    {
                        stream.SetLength(100);
                        stream.WriteByte(1);
                    }
                }

                Assert.AreEqual(100, tempFile.Length);
                using (stream = tempFile.Open())
                    Assert.AreEqual(1, stream.ReadByte());
            }
        }
Esempio n. 23
0
        public void TestCacheRecoverAbandondMutex()
        {
            Stream stream = null;

            using (StreamCache cache = new StreamCache(new SharedMemoryStream(), 1))
            {
                Thread t = new Thread(delegate() { stream = cache.Open(FileAccess.Read); });
                t.Start();
                t.Join(); //The exit of this thread releases the stream...

                using (Stream another = cache.Open())
                {
                    Assert.AreEqual(0, another.Position);
                    // Another thread can then objtain the same underlying stream... we can demonstrate
                    // this by the fact that the Position property affects both streams.
                    stream.Position = 100;
                    Assert.AreEqual(100, another.Position);
                }
            }
        }
Esempio n. 24
0
        public override Task InvokeNotMatch(IOwinContext context)
        {
            var requestPath       = context.Request.Path.ToString();
            var isRootContextPath = Server.Instance.IsRootContextPath;
            var contextPath       = Server.Instance.ContextPath;

            if (!isRootContextPath &&
                requestPath.StartsWith(contextPath) &&
                requestPath.Length > contextPath.Length)
            {
                requestPath = requestPath.Substring(Server.Instance.ContextPath.Length - 1);
            }
            var url = $"resource://0{requestPath}";

            if (UseMemoryCache)
            {
                var streamCache = StreamCacheManager.GetCache(url);
                if (streamCache != null)
                {
                    return(handleResource(context, streamCache.GetStream(), null, url, Expires, AddonHttpHeaders));
                }
            }
            ResourceWebResponse resourceResponse;
            var stream = getUrlStream(url, out resourceResponse);

            if (stream == null)
            {
                return(base.InvokeNotMatch(context));
            }
            if (UseMemoryCache)
            {
                var streamCache = new StreamCache(stream);
                StreamCacheManager.AddCache(url, streamCache);
                stream.Close();
                stream.Dispose();
                stream = streamCache.GetStream();
            }
            return(handleResource(context, stream, resourceResponse.LastModified, resourceResponse.Uri.LocalPath, Expires, AddonHttpHeaders));
        }
            private void ReadByteRange(Guid transferId, Salt sessionKey, string location, StreamCache streams, long offset, int count)
            {
                using (Message req = new Message(TransferState.DownloadBytesRequest, transferId, _publicKey, s => sessionKey))
                {
                    req.Write(location);
                    req.Write(offset);
                    req.Write(count);

                    Stream response = SendPayload(req, location, req.ToStream(_privateKey));
                    using (Message rsp = new Message(response, _privateKey, s => sessionKey))
                    {
                        Check.Assert <InvalidOperationException>(rsp.State == TransferState.DownloadBytesResponse);
                        byte[] bytes = rsp.ReadBytes(100 * 1000 * 1024);
                        Check.Assert <InvalidOperationException>(bytes.Length == count);

                        rsp.VerifySignature(_publicKey);

                        using (Stream io = streams.Open(FileAccess.Write))
                        {
                            io.Seek(offset, SeekOrigin.Begin);
                            io.Write(bytes, 0, count);
                        }
                    }
                }
            }
        public void TestCacheLimit()
        {
            bool finished = false;
            using (SharedMemoryStream shared = new SharedMemoryStream())
            using (StreamCache cache = new StreamCache(shared, 1))
            {
                ManualResetEvent ready = new ManualResetEvent(false);
                ManualResetEvent release = new ManualResetEvent(false);
                Thread t = new Thread(
                    delegate()
                    {
                        using (Stream stream = cache.Open(FileAccess.ReadWrite))
                        {
                            stream.Write(new byte[50], 0, 50);
                            ready.Set();
                            release.WaitOne();
                            stream.Write(new byte[50], 0, 50);
                            GC.KeepAlive(stream);
                            finished = true;
                        }
                    }
                );
                t.IsBackground = true;
                t.Start();

                Assert.IsTrue(ready.WaitOne());
                Assert.AreEqual(50, shared.Length);

                new Thread(
                    delegate()
                    {
                        Thread.Sleep(10);
                        release.Set();
                    }
                ).Start();

                Assert.IsFalse(finished);
                using (Stream stream = cache.Open())
                {
                    Assert.IsTrue(finished);
                    Assert.IsTrue(release.WaitOne(0, false));
                    Assert.AreEqual(100, stream.Read(new byte[100], 0, 100));
                }
                Assert.IsTrue(t.Join(1000));
            }
        }
        public void TestCacheRecoverAbandondStream()
        {
            Stream stream;
            using (StreamCache cache = new StreamCache(new SharedMemoryStream(), 1))
            {
                if(true)
                {
                    stream = cache.Open();
                    stream.Write(new byte[100], 25, 55);
                    stream = null;//simulated "accidental" object abandonment... i.e. someone does something stupid.
                }

                GC.Collect(0, GCCollectionMode.Forced);
                GC.WaitForPendingFinalizers();

                Thread t = new Thread(
                    delegate()
                    {
                        using (stream = cache.Open(FileAccess.Read))
                        {
                            Assert.AreEqual(new byte[55], IOStream.ReadAllBytes(stream));
                        }
                    }
                );
                t.IsBackground = true;
                t.Start();
                Assert.IsTrue(t.Join(1000));
            }
        }
        public void TestCacheRecoverAbandondMutex()
        {
            Stream stream = null;
            using (StreamCache cache = new StreamCache(new SharedMemoryStream(), 1))
            {
                Thread t = new Thread( delegate() { stream = cache.Open(FileAccess.Read); } );
                t.Start();
                t.Join(); //The exit of this thread releases the stream...

                using (Stream another = cache.Open())
                {
                    Assert.AreEqual(0, another.Position);
                    // Another thread can then objtain the same underlying stream... we can demonstrate
                    // this by the fact that the Position property affects both streams.
                    stream.Position = 100;
                    Assert.AreEqual(100, another.Position);
                }
            }
        }
 public void TestStreamReadWrite()
 {
     Stream stream;
     using (StreamCache cache = new StreamCache(new SharedMemoryStream(), 1))
     {
         using (stream = cache.Open())
         {
             stream.Write(new byte[100], 25, 55);
             stream.Close();
         }
         using (stream = cache.Open(FileAccess.Read))
         {
             Assert.AreEqual(new byte[55], IOStream.ReadAllBytes(stream));
         }
     }
 }
            private void ReadByteRange(Guid transferId, Salt sessionKey, string location, StreamCache streams, long offset, int count)
            {
                using (Message req = new Message(TransferState.DownloadBytesRequest, transferId, _publicKey, s=>sessionKey))
                {
                    req.Write(location);
                    req.Write(offset);
                    req.Write(count);

                    Stream response = SendPayload(req, location, req.ToStream(_privateKey));
                    using (Message rsp = new Message(response, _privateKey, s=>sessionKey))
                    {
                        Check.Assert<InvalidOperationException>(rsp.State == TransferState.DownloadBytesResponse);
                        byte[] bytes = rsp.ReadBytes(100 * 1000 * 1024);
                        Check.Assert<InvalidOperationException>(bytes.Length == count);
                        
                        rsp.VerifySignature(_publicKey);

                        using(Stream io = streams.Open(FileAccess.Write))
                        {
                            io.Seek(offset, SeekOrigin.Begin);
                            io.Write(bytes, 0, count);
                        }
                    }
                }
            }
                public BytesToRead(Client client, int threadLimit, Guid transferId, Salt sessionKey, string location, StreamCache streams, long offset, int count)
                {
                    _client = client;
                    _transferId = transferId;
                    _sessionKey = sessionKey;
                    _location = location;
                    _streams = streams;
                    _offset = offset;
                    _count = count;

                    _throttle = new Semaphore(threadLimit, threadLimit, GetType().FullName);
                    _throttle.WaitOne();
                    _aquired = true;
                }
 /// <summary>
 /// Called to send a specific length of bytes to a server identified by serverKey.  The transfer
 /// is a blocking call and returns on success or raises an exception.  If Abort() is called durring
 /// the transfer, or if a ProgressChanged event handler raises the OperationCanceledException, the
 /// transfer is silently terminated and the method will return false.
 /// </summary>
 /// <param name="location">A string of up to 1024 bytes in length</param>
 /// <param name="filename">The name of the file to write to</param>
 /// <returns>True if the file was successfully received by the server</returns>
 public bool Download(string location, string filename)
 {
     FileStreamFactory file = new FileStreamFactory(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
     using (TempFile temp = TempFile.Attach(filename))
     using (StreamCache cache = new StreamCache(file, LimitThreads))
     {
         temp.Length = 0;
         if (Download(location, cache))
         {
             temp.Detatch();
             return true;
         }
     }
     return false;
 }
            private bool Download(string location, StreamCache output)
            {
                int maxMessageLength;
                long fileLength, bytesReceived = 0;
                Guid transferId = Guid.NewGuid();
                byte[] serverKeyBits;
                byte[] nonce = GetNonce(transferId, location, out serverKeyBits);
                Hash hnonce = Hash.SHA256(nonce);

                //STEP 2: Create and send session key
                Salt clientKeyBits = new Salt(Salt.Size.b256);
                Salt sessionKey = SessionSecret(clientKeyBits, serverKeyBits);

                using (Message req = new Message(TransferState.DownloadRequest, transferId, _publicKey, NoSession))
                {
                    req.Write(hnonce.ToArray());
                    req.Write(location);
                    req.Write(clientKeyBits.ToArray());

                    Stream response = SendPayload(req, location, req.ToStream(_privateKey));
                    using (Message rsp = new Message(response, _privateKey, s=>sessionKey))
                    {
                        Check.Assert<InvalidOperationException>(rsp.State == TransferState.DownloadResponse);
                        maxMessageLength = Check.InRange(rsp.ReadInt32(), 0, int.MaxValue);
                        fileLength = Check.InRange(rsp.ReadInt64(), 0, 0x00FFFFFFFFFFFFFFL);
                        byte[] bytes = rsp.ReadBytes(100 * 1000 * 1024);
                        rsp.VerifySignature(_publicKey);

                        using(Stream io = output.Open(FileAccess.Write))
                        {
                            io.SetLength(fileLength);
                            if (bytes.Length > 0)
                            {
                                io.Seek(0, SeekOrigin.Begin);
                                io.Write(bytes, 0, bytes.Length);
                                bytesReceived += bytes.Length;
                            }
                        }
                    }
                }
                //STEP 3...n: Continue downloading other chunks of the file
                if (bytesReceived < fileLength)
                {
                    bool[] failed = new bool[1];
                    using (WorkQueue queue = new WorkQueue(LimitThreads))
                    {
                        queue.OnError += (o, e) => { failed[0] = true; };
                        while (bytesReceived < fileLength && !failed[0] && !_abort.WaitOne(0, false))
                        {
                            int len = (int) Math.Min(fileLength - bytesReceived, maxMessageLength);
                            BytesToRead task = new BytesToRead(
                                this, LimitThreads, transferId, sessionKey, location, output, bytesReceived, len);
                            queue.Enqueue(task.Send);
                            OnProgressChanged(location, bytesReceived, fileLength);
                            bytesReceived += len;
                        }

                        queue.Complete(true, failed[0] ? 5000 : 7200000);
                    }

                    if (_abort.WaitOne(0, false))
                        return false;
                    Check.Assert<InvalidDataException>(failed[0] == false);

                    // STEP 4: Complete the transfer
                    using (Message req = new Message(TransferState.DownloadCompleteRequest, transferId, _publicKey, s => sessionKey))
                    {
                        SendPayload(req, location, req.ToStream(_privateKey)).Dispose();
                    }
                }
                OnProgressChanged(location, fileLength, fileLength);
                return true;
            }
Esempio n. 34
0
 internal DataBlock(int blockSize, StreamCache cache, DataBlock next)
 {
     m_data  = new byte[blockSize];
     m_next  = next;
     m_cache = cache;
 }
Esempio n. 35
0
 public MoviesController(ApplicationDbContext dbContext, StreamCache fileRepo)
 {
     _dbContext      = dbContext;
     _fileRepository = fileRepo;
 }
        /// <summary> Internal use to specify aligned IO when using NoBuffering file option </summary>
        protected FragmentedFile(IFactory<Stream> streamFactory, int blockSize, int growthRate, int cacheLimit, FileOptions options)
        {
            _useAlignedIo = (options & NoBuffering) == NoBuffering;
            _streamCache = new StreamCache(streamFactory, cacheLimit);
            _header = new FileBlock(blockSize, _useAlignedIo);
            _syncFreeBlock = new object();
            try
            {
                long fallocated;
                bool canWrite;

                using (Stream s = _streamCache.Open(FileAccess.ReadWrite))
                {
                    canWrite = s.CanWrite;
                    if (!s.CanRead)
                        throw new InvalidOperationException("The stream does not support Read access.");

                    _header.Read(s, blockSize);

                    if ((_header.Flags & ~BlockFlags.HeaderFilter) != BlockFlags.HeaderFlags)
                        throw new InvalidDataException();

                    _nextFree = _header.NextBlockId;
                    SetBlockSize(_header.Length, out _blockSize, out _maskVersion, out _maskOffset);
                    if (blockSize != _blockSize) throw new ArgumentOutOfRangeException("blockSize");
                    fallocated = LastAllocated(s);
                    _reallocSize = growthRate * _blockSize;

                    if (canWrite)
                    {
                        s.Position = 0;
                        _header.NextBlockId = long.MinValue;
                        _header.Write(s, FileBlock.HeaderSize);
                    }
                }

                if (canWrite)
                {
                    if ((_header.Flags & BlockFlags.ResizingFile) == BlockFlags.ResizingFile && _nextFree > 0)
                        ResizeFile(_nextFree, Math.Max(fallocated, _nextFree + _reallocSize));

                    if (_nextFree == long.MinValue)
                        _nextFree = RecoverFreeBlocks();
                }
            }
            catch
            {
                _streamCache.Dispose();
                throw;
            }
        }
Esempio n. 37
0
 public StreamCacheDecompressor(StreamCache input, int blockSize)
     : base(blockSize)
 {
     m_input      = input;
     m_maxWorkers = 1;       // TODO
 }
            private bool Download(string location, StreamCache output)
            {
                int  maxMessageLength;
                long fileLength, bytesReceived = 0;
                Guid transferId = Guid.NewGuid();

                byte[] serverKeyBits;
                byte[] nonce  = GetNonce(transferId, location, out serverKeyBits);
                Hash   hnonce = Hash.SHA256(nonce);

                //STEP 2: Create and send session key
                Salt clientKeyBits = new Salt(Salt.Size.b256);
                Salt sessionKey    = SessionSecret(clientKeyBits, serverKeyBits);

                using (Message req = new Message(TransferState.DownloadRequest, transferId, _publicKey, NoSession))
                {
                    req.Write(hnonce.ToArray());
                    req.Write(location);
                    req.Write(clientKeyBits.ToArray());

                    Stream response = SendPayload(req, location, req.ToStream(_privateKey));
                    using (Message rsp = new Message(response, _privateKey, s => sessionKey))
                    {
                        Check.Assert <InvalidOperationException>(rsp.State == TransferState.DownloadResponse);
                        maxMessageLength = Check.InRange(rsp.ReadInt32(), 0, int.MaxValue);
                        fileLength       = Check.InRange(rsp.ReadInt64(), 0, 0x00FFFFFFFFFFFFFFL);
                        byte[] bytes = rsp.ReadBytes(100 * 1000 * 1024);
                        rsp.VerifySignature(_publicKey);

                        using (Stream io = output.Open(FileAccess.Write))
                        {
                            io.SetLength(fileLength);
                            if (bytes.Length > 0)
                            {
                                io.Seek(0, SeekOrigin.Begin);
                                io.Write(bytes, 0, bytes.Length);
                                bytesReceived += bytes.Length;
                            }
                        }
                    }
                }
                //STEP 3...n: Continue downloading other chunks of the file
                if (bytesReceived < fileLength)
                {
                    bool[] failed = new bool[1];
                    using (WorkQueue queue = new WorkQueue(LimitThreads))
                    {
                        queue.OnError += (o, e) => { failed[0] = true; };
                        while (bytesReceived < fileLength && !failed[0] && !_abort.WaitOne(0, false))
                        {
                            int         len  = (int)Math.Min(fileLength - bytesReceived, maxMessageLength);
                            BytesToRead task = new BytesToRead(
                                this, LimitThreads, transferId, sessionKey, location, output, bytesReceived, len);
                            queue.Enqueue(task.Send);
                            OnProgressChanged(location, bytesReceived, fileLength);
                            bytesReceived += len;
                        }

                        queue.Complete(true, failed[0] ? 5000 : 7200000);
                    }

                    if (_abort.WaitOne(0, false))
                    {
                        return(false);
                    }
                    Check.Assert <InvalidDataException>(failed[0] == false);

                    // STEP 4: Complete the transfer
                    using (Message req = new Message(TransferState.DownloadCompleteRequest, transferId, _publicKey, s => sessionKey))
                    {
                        SendPayload(req, location, req.ToStream(_privateKey)).Dispose();
                    }
                }
                OnProgressChanged(location, fileLength, fileLength);
                return(true);
            }