Ejemplo n.º 1
0
        public async override Task <long> SendChunkAsync(TransferConnection transfer, long filePos, int bytesRequired)
        {
            var startTime = DateTime.UtcNow;

            if (IsSegmentNeededAttached)
            {
                var ea = new UploadItemSegmentEventArgs
                {
                    UploadItem = this,
                    Magnet     = Content.Magnet,
                    Position   = filePos,
                    Length     = (int)bytesRequired
                };

                OnSegmentNeeded(ea);

                if (ea.FromCache)
                {
                    await transfer.SendAsync(ea.Stream).ConfigureAwait(false);

                    Interlocked.Add(ref _uploadedBytes, ea.Length);
                    return(bytesRequired);
                }
            }

            long bytesCopied = 0;

            var sw = PerfTimer.StartNew();

            try
            {
                if (UploadDelay != 0)
                {
                    await Task.Delay(UploadDelay);
                }

                using (new PerfLimit(() => $"Slow HYPER request {SystemPath} pos: {filePos} len: {bytesRequired} filelen: {Content.Magnet.Size}", 4000))
                    using (var buffer = await Manager.DownloadSegment(SystemPath, filePos, (int)bytesRequired))
                    {
                        if (buffer.Object != null)
                        {
                            await transfer.SendAsync(buffer.Object, 0, buffer.Object.Length).ConfigureAwait(false);
                        }
                    }
                bytesCopied = bytesRequired;

                Interlocked.Add(ref _uploadedBytes, bytesCopied);
            }
            catch (Exception x)
            {
                Logger.Error("DownloadChunk error: {0}", x.Message);
            }

            sw.Stop();

            //HttpHelper.RegisterDownloadTime((int)sw.ElapsedMilliseconds);

            if (IsSegmentDownloadedAttached)
            {
                OnSegmentDownloaded(new UploadItemSegmentEventArgs
                {
                    UploadItem      = this,
                    Magnet          = Content.Magnet,
                    Position        = filePos,
                    Length          = bytesRequired,
                    RequestedAt     = startTime,
                    DownloadingTime = sw.Elapsed
                });
            }

            return(bytesCopied);
        }
Ejemplo n.º 2
0
 protected static void OnSegmentNeeded(UploadItemSegmentEventArgs e)
 {
     SegmentNeeded?.Invoke(null, e);
 }
Ejemplo n.º 3
0
        public async override Task <long> SendChunkAsync(TransferConnection transfer, long filePos, int bytesRequired)
        {
            var startTime = DateTime.UtcNow;

            if (IsSegmentNeededAttached)
            {
                var ea = new UploadItemSegmentEventArgs
                {
                    UploadItem = this,
                    Magnet     = Content.Magnet,
                    Position   = filePos,
                    Length     = bytesRequired
                };

                OnSegmentNeeded(ea);

                if (ea.FromCache)
                {
                    await transfer.SendAsync(ea.Stream).ConfigureAwait(false);

                    Interlocked.Add(ref _uploadedBytes, ea.Length);
                    return(bytesRequired);
                }
            }

            long bytesCopied = 0;

            if (UploadDelay != 0)
            {
                await Task.Delay(UploadDelay).ConfigureAwait(false);
            }

            var sw = PerfTimer.StartNew();

            using (new PerfLimit(() =>
                                 $"Slow http request {SystemPath} pos: {filePos} len: {bytesRequired} filelen: {Content.Magnet.Size}", 4000))
            {
                try
                {
                    // custom http connections pool
                    await Manager.CopyChunkToTransferAsync(transfer, SystemPath, filePos, bytesRequired).ConfigureAwait(false);

                    // default http connections pool
                    //var responseStream = await HttpHelper.GetHttpChunkAsync(SystemPath, filePos, bytesRequired).ConfigureAwait(false);
                    //await transfer.SendAsync(responseStream).ConfigureAwait(false);

                    bytesCopied = bytesRequired;

                    Interlocked.Add(ref _uploadedBytes, bytesCopied);
                }
                catch (Exception x)
                {
                    Logger.Error("DownloadChunk error: {0}", x.Message);
                }
            }
            sw.Stop();

            HttpHelper.RegisterDownloadTime((int)sw.ElapsedMilliseconds);

            if (IsSegmentDownloadedAttached)
            {
                OnSegmentDownloaded(new UploadItemSegmentEventArgs
                {
                    UploadItem      = this,
                    Magnet          = Content.Magnet,
                    Position        = filePos,
                    Length          = bytesRequired,
                    RequestedAt     = startTime,
                    DownloadingTime = sw.Elapsed
                });
            }

            return(bytesCopied);
        }