示例#1
0
        protected override void TransferChunk(MegaChunk chunk, Action transferCompleteFn)
        {
            if (chunk.Handle.SkipChunks) { transferCompleteFn(); return; }
            var wc = new WebClient();
            wc.Proxy = transport.Proxy;
            chunk.Handle.ChunkTransferStarted(wc);
            wc.DownloadDataCompleted += (s, e) =>
            {
                transferCompleteFn();
                chunk.Handle.ChunkTransferEnded(wc);
                if (e.Cancelled) { return; }
                if (e.Error != null)
                {
                    chunk.Handle.BytesTransferred(0 - chunk.transferredBytes);
                    chunk.transferredBytes = 0;
                    EnqueueTransfer(new List<MegaChunk> { chunk }, true);
                }
                else 
                {
                    chunk.Data = e.Result;
                    EnqueueCrypt(new List<MegaChunk> { chunk });
                }
            };
            wc.DownloadProgressChanged += (s, e) => OnDownloadedBytes(chunk, e.BytesReceived);
            var url = ((DownloadHandle)chunk.Handle).DownloadUrl;
            url += String.Format("/{0}-{1}", chunk.Offset, chunk.Offset + chunk.Size - 1);
            wc.DownloadDataAsync(new Uri(url));

        }
示例#2
0
 protected override void CryptChunk(MegaChunk chunk)
 {
     byte[] data = new byte[chunk.Size];  
     var hndl = chunk.Handle;
     try
     {
         lock (streamLock)
         {
             hndl.Stream.Seek(chunk.Offset, SeekOrigin.Begin);
             hndl.Stream.Read(data, 0, data.Length);
         }
     }
     catch
     {
         hndl.CancelTransfer(MegaApiError.ESYSTEM);
         return;
     }
     chunk.Mac = Crypto.EncryptCtr(chunk.Handle.AesAlg, data, chunk.Handle.Nonce, chunk.Offset);
     chunk.Data = data;
     EnqueueTransfer(new List<MegaChunk>{chunk});
 }
 protected abstract void TransferChunk(MegaChunk megaChunk, Action transferCompleteFn);
 protected abstract void CryptChunk(MegaChunk chunk);
 protected void OnUploadedBytes(MegaChunk chunk, long bytes)
 {
     var delta = bytes - chunk.transferredBytes;
     chunk.transferredBytes = bytes;
     chunk.Handle.BytesTransferred(delta);
 }
示例#6
0
        protected override void CryptChunk(MegaChunk chunk)
        {
            var data = chunk.Data;
            var hndl = chunk.Handle;
            chunk.Mac = Crypto.DecryptCtr(chunk.Handle.AesAlg, data, chunk.Handle.Nonce, chunk.Offset);
            lock(streamLock)
            {
                hndl.Stream.Seek(chunk.Offset, SeekOrigin.Begin);
                hndl.Stream.Write(data, 0, chunk.Size);
            }
            hndl.chunksProcessed++;
            chunk.ClearData();

            if (chunk.Handle.chunksProcessed == chunk.Handle.Chunks.Count)
            {
                Util.StartThread(() =>
                {
                    FinishFile((DownloadHandle)chunk.Handle);
                }, "transfer_finish_file");
            }
        }
示例#7
0
        protected override void TransferChunk(MegaChunk chunk, Action transferCompleteFn)
        {
            if (chunk.Handle.SkipChunks) { transferCompleteFn(); return; }
            var wc = new WebClient();
            wc.Proxy = transport.Proxy;
            chunk.Handle.ChunkTransferStarted(wc);
            wc.UploadDataCompleted += (s, e) =>
            {
                chunk.Handle.ChunkTransferEnded(wc);
                transferCompleteFn();
                if (e.Cancelled) { return; }
                if (e.Error != null)
                {
                    chunk.Handle.BytesTransferred(0 - chunk.transferredBytes);
                    EnqueueTransfer(new List<MegaChunk>{chunk}, true);
                }
                else { OnUploadedChunk(chunk, e); }
            };
            wc.UploadProgressChanged += (s, e) => OnUploadedBytes(chunk, e.BytesSent);
            var url = String.Format(((UploadHandle)chunk.Handle).UploadUrl + "/{0}", chunk.Offset);
            wc.UploadDataAsync(new Uri(url), chunk.Data);

        }
示例#8
0
        private void OnUploadedChunk(MegaChunk chunk, UploadDataCompletedEventArgs e)
        {
            chunk.ClearData();

            chunk.Handle.chunksProcessed++;
            if (chunk.Handle.chunksProcessed == chunk.Handle.Chunks.Count)
            {
                Util.StartThread(() =>
                {
                    var uploadHandle = Encoding.UTF8.GetString(e.Result);
                    FinishFile((UploadHandle)chunk.Handle, uploadHandle);
                }, "transfer_finish_file");
            }
        }