public async Task <bool> CopyChunkToTransferAsync(TransferConnection transfer, string url, long filePos, long length) { var parsedUrl = new HttpUrl(url); var pool = GetPool(parsedUrl.Server); var task = new HttpTask { Url = url, Transfer = transfer, FilePosition = filePos, Length = length }; await pool.ExecuteTaskAsync(task).ConfigureAwait(false); if (task.Completed) { SegmentDelay.Update((int)task.ExecutionTime.TotalMilliseconds); } Interlocked.Add(ref _totalDownloaded, length); if (!task.Completed) { logger.Error("Unable to complete the task"); } return(task.Completed); }
private void HubOutgoingConnectionRequest(object sender, OutgoingConnectionRequestEventArgs e) { var hubConnection = (HubConnection)sender; var ea = new ConnectionRequestEventArgs { UserNickname = "", Address = e.Message.SenderAddress, HubConnection = hubConnection }; OnConnectionRequest(ea); if (ea.Cancel) { return; } TransferConnection transfer; try { transfer = new TransferConnection(e.Message.SenderAddress) { AllowedToConnect = true, Source = new Source { HubAddress = hubConnection.RemoteAddressString } }; if (Settings.NetworkInterface != null) { transfer.LocalAddress = new IPEndPoint(Settings.NetworkInterface, 0); } } catch (Exception x) { Logger.Error("Unable to create outgoing transfer thread {0}", x.Message); return; } TransferManager.AddTransfer(transfer); transfer.FirstMessages = new[] { new MyNickMessage { Nickname = hubConnection.Settings.Nickname }.Raw, new LockMessage { ExtendedProtocol = true }.Raw }; transfer.StartAsync(); }
public void AddTransfer(TransferConnection transfer) { lock (_synRoot) { _connections.Add(transfer); } transfer.ConnectionStatusChanged += TransferConnectionStatusChanged; transfer.UploadItemNeeded += TransferUploadItemNeededHandler; transfer.UploadItemDispose += TransferUploadItemDisposeHandler; transfer.DirectionChanged += TransferDirectionChanged; transfer.DownloadItemNeeded += TransferDownloadItemNeeded; transfer.Authorization += TransferAuthorizationHandler; transfer.Error += TransferError; transfer.SlotRequest += TransferSlotRequest; OnTransferAdded(new TransferEventArgs { Transfer = transfer }); }
private void TcpConnectionListenerIncomingConnection(object sender, IncomingConnectionEventArgs e) { if (e.Socket.Connected) { var connLimit = Settings.ConnectionsLimit; if (connLimit != 0 && TransferManager.TransfersCount >= connLimit) { Logger.Warn("Connection limit {0} reached, dropping incoming connection", connLimit); return; } e.Handled = true; var transfer = new TransferConnection(e.Socket); TransferManager.AddTransfer(transfer); transfer.StartAsync(); } else { Logger.Warn("We have disconnected incoming socket"); } }
public virtual async Task <long> SendChunkAsync(TransferConnection transfer, long filePos, int bytesRequired) { if (EnableRequestEventFire) { OnRequest(new UploadItemEventArgs()); } using (new PerfLimit("Send fs chunk time", 2000)) { try { return(await InternalCopyChunk(transfer.Stream, filePos, bytesRequired).ConfigureAwait(false)); } catch (Exception x) { OnError(new UploadItemEventArgs { Exception = x }); Logger.Error("Unable to read the data for upload: " + x.Message); return(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); }
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); }