public async void EnqueueTask(HyperServerTask task) { try { Interlocked.Increment(ref _activeOperations); var normalizedPath = task.Path.Replace("/", "\\"); if (normalizedPath.StartsWith("/")) { normalizedPath = normalizedPath.Remove(0, 1); } string tth; lock (_pathIndex) { if (!_pathIndex.TryGetValue(normalizedPath, out tth)) { Logger.Error($"Error when requesting hyper segment {normalizedPath} No such file in share index yet"); return; } } var ci = _share.SearchByTth(tth); if (ci == null) { Logger.Error($"TTH {tth} not found in share"); return; } if (task.IsSegmentRequest) { if (!_cacheManager.ReadCacheSegment(tth, task.Offset, task.Length, task.Buffer.Object, 0)) { if (ci.Value.SystemPath.StartsWith("hyp://")) { var segment = await _manager.DownloadSegment(task.Path, task.Offset, task.Length); task.Buffer.Dispose(); task.Buffer = segment; } else if (ci.Value.SystemPath.StartsWith("http://")) { using (var stream = await HttpHelper.GetHttpChunkAsync(ci.Value.SystemPath, task.Offset, task.Length)) using (var ms = new MemoryStream(task.Buffer.Object)) { stream.CopyTo(ms); } } else { throw new InvalidOperationException("Not supported source type " + ci.Value.SystemPath); } SegmentServiceProxy.Update(task.SinceCreatedMs); SegmentsPerSecondProxy.Update(1); } else { SegmentServiceCached.Update(task.SinceCreatedMs); SegmentsPerSecondCached.Update(1); } SegmentService.Update(task.SinceCreatedMs); SegmentsPerSecond.Update(1); } else { task.FileLength = ci.Value.Magnet.Size; } } catch (Exception x) { Logger.Error($"Error when requesting hyper segment {task.Path} {x.Message}"); } finally { Interlocked.Decrement(ref _activeOperations); task.Done(); } }