Пример #1
0
        protected virtual void OnUploadItemDispose(UploadItemEventArgs e)
        {
            e.Transfer = this;
            var handler = UploadItemDispose;

            if (handler != null)
            {
                handler(this, e);
            }
        }
Пример #2
0
        private void OnUploadItemNeeded(UploadItemEventArgs e)
        {
            e.Transfer = this;
            var handler = UploadItemNeeded;

            if (handler != null)
            {
                handler(this, e);
            }
        }
Пример #3
0
        public override void Dispose()
        {
            if (_disposed)
            {
                return;
            }

            if (_isResponding)
            {
                _fails.Update(1);
            }

            ReleaseSegment();
            DownloadItem = null;
            if (UploadItem != null)
            {
                var ea = new UploadItemEventArgs();

                try
                {
                    OnUploadItemDispose(ea);
                }
                catch (Exception x)
                {
                    Logger.Error("Exception when disposing transfer {0} {1}", x.Message, x.StackTrace);
                }

                if (!ea.Handled)
                {
                    var ui = UploadItem;

                    if (ui != null)
                    {
                        ui.Dispose();
                    }

                    UploadItem = null;
                }
            }

            DisconnectAsync();
            _disposed = true;
        }
Пример #4
0
        private async void OnMessageAdcget(ADCGETMessage adcgetMessage)
        {
            var reqItem = new ContentItem();

            if (adcgetMessage.Type == ADCGETType.Tthl)
            {
                SendMessageAsync(new ErrorMessage {
                    Error = "File Not Available"
                }.Raw);
                return;
            }

            if (adcgetMessage.Type == ADCGETType.File)
            {
                if (adcgetMessage.Request.StartsWith("TTH/"))
                {
                    reqItem.Magnet = new Magnet {
                        TTH = adcgetMessage.Request.Remove(0, 4)
                    };
                }
                else
                {
                    reqItem.Magnet = new Magnet {
                        FileName = adcgetMessage.Request
                    };
                }
            }

            _requests.Update(1);
            _isResponding = true;

            if (!SlotUsed)
            {
                var ea = new CancelEventArgs();
                OnSlotRequest(ea);

                if (ea.Cancel)
                {
                    Logger.Info("Can't start upload to {0}, no slots available", Source);
                    SendMessageAsync(new MaxedOutMessage().Raw);
                    return;
                }

                SlotUsed = true;
            }

            if (UploadItem == null || UploadItem.Content.Magnet.TTH != reqItem.Magnet.TTH)
            {
                var ea = new UploadItemEventArgs {
                    Transfer = this, Content = reqItem
                };
                OnUploadItemNeeded(ea);

                if (UploadItem != null)
                {
                    var uea = new UploadItemEventArgs();
                    OnUploadItemDispose(uea);
                    if (!uea.Handled)
                    {
                        UploadItem.Dispose();
                    }
                }

                UploadItem = ea.UploadItem;
                if (ea.UploadItem == null)
                {
                    SendMessageAsync(new ErrorMessage {
                        Error = "File Not Available"
                    }.Raw);
                    return;
                }
            }

            if (adcgetMessage.Start >= UploadItem.Content.Magnet.Size)
            {
                SendMessageAsync(new ErrorMessage {
                    Error = "File Not Available"
                }.Raw);
                return;
            }

            if (adcgetMessage.Start + adcgetMessage.Length > UploadItem.Content.Magnet.Size)
            {
                Logger.Warn("Trim ADCGET length to file actual length {0}/{1}",
                            adcgetMessage.Start + adcgetMessage.Length, UploadItem.Content.Magnet.Size);
                adcgetMessage.Length = UploadItem.Content.Magnet.Size - adcgetMessage.Start;
            }


            var uploadItem = UploadItem;

            if (_disposed || uploadItem == null)
            {
                return;
            }

            var sw = PerfTimer.StartNew();

            await SendAsync(new ADCSNDMessage
            {
                Type    = ADCGETType.File,
                Request = adcgetMessage.Request,
                Start   = adcgetMessage.Start,
                Length  = adcgetMessage.Length
            }.Raw + "|").ConfigureAwait(false);

            try
            {
                if (_disposed)
                {
                    return;
                }

                await uploadItem.SendChunkAsync(this, adcgetMessage.Start, (int)adcgetMessage.Length).ConfigureAwait(false);

                Stream.Flush();
                sw.Stop();
                _isResponding = false;
                ServiceTime.Update((int)sw.ElapsedMilliseconds);
            }
            catch (Exception x)
            {
                Logger.Error("Upload read error {0} (L:{1}) {2} {3} ms",
                             x.Message,
                             adcgetMessage.Length,
                             uploadItem.Content.SystemPath,
                             sw.ElapsedMilliseconds);
                Dispose();
            }
        }