Exemplo n.º 1
0
        /// <summary>
        ///     Adds the specified <paramref name="slimResponse"/> to the list of responses after applying the filters specified
        ///     in the search options.
        /// </summary>
        /// <param name="slimResponse">The response to add.</param>
        public void TryAddResponse(SearchResponseSlim slimResponse)
        {
            // ensure the search is still active, the token matches and that the response meets basic filtering criteria we check
            // the slim response for fitness prior to extracting the file list from it for performance reasons.
            if (!Disposed && State.HasFlag(SearchStates.InProgress) && slimResponse.Token == Token && SlimResponseMeetsOptionCriteria(slimResponse))
            {
                // extract the file list from the response and filter it
                var fullResponse  = SearchResponseFactory.FromSlimResponse(slimResponse);
                var filteredFiles = fullResponse.Files.Where(f => Options.FileFilter?.Invoke(f) ?? true);

                fullResponse = new SearchResponse(fullResponse.Username, fullResponse.Token, filteredFiles.Count(), fullResponse.FreeUploadSlots, fullResponse.UploadSpeed, fullResponse.QueueLength, filteredFiles);

                // ensure the filtered file count still meets the response criteria
                if ((Options.FilterResponses && fullResponse.FileCount < Options.MinimumResponseFileCount) || !(Options.ResponseFilter?.Invoke(fullResponse) ?? true))
                {
                    return;
                }

                Interlocked.Increment(ref responseCount);
                Interlocked.Add(ref fileCount, fullResponse.Files.Count);

                ResponseReceived?.Invoke(fullResponse);
                SearchTimeoutTimer.Reset();

                if (responseCount >= Options.ResponseLimit)
                {
                    Complete(SearchStates.ResponseLimitReached);
                }
                else if (fileCount >= Options.FileLimit)
                {
                    Complete(SearchStates.FileLimitReached);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Releases the managed and unmanaged resources used by the <see cref="SearchInternal"/>.
        /// </summary>
        /// <param name="disposing">A value indicating whether the object is in the process of disposing.</param>
        public void Dispose(bool disposing)
        {
            if (!Disposed)
            {
                if (disposing)
                {
                    SearchTimeoutTimer.Dispose();
                }

                Disposed = true;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Releases the managed and unmanaged resources used by the <see cref="SearchInternal"/>.
        /// </summary>
        /// <param name="disposing">A value indicating whether the object is in the process of disposing.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!Disposed)
            {
                if (disposing)
                {
                    SearchTimeoutTimer.Dispose();
                }

                Disposed = true;
            }
        }
Exemplo n.º 4
0
        private void Dispose(bool disposing)
        {
            if (!Disposed)
            {
                if (disposing)
                {
                    SearchTimeoutTimer.Dispose();
                    ResponseBag = default;
                }

                Disposed = true;
            }
        }
Exemplo n.º 5
0
        private void Dispose(bool disposing)
        {
            if (!Disposed)
            {
                if (disposing)
                {
                    SearchTimeoutTimer.Dispose();
                    ResponseBag = default(ConcurrentBag <SearchResponse>);
                }

                Disposed = true;
            }
        }
Exemplo n.º 6
0
        /// <summary>
        ///     Adds the specified <paramref name="response"/> to the list of responses after applying the filters specified in
        ///     the search options.
        /// </summary>
        /// <param name="response">The response to add.</param>
        public void TryAddResponse(SearchResponse response)
        {
            if (!Disposed && State.HasFlag(SearchStates.InProgress) && response.Token == Token)
            {
                if (!ResponseMeetsOptionCriteria(response))
                {
                    return;
                }

                if (Options.FilterResponses)
                {
                    // apply custom filter, if one was provided
                    if (!(Options.ResponseFilter?.Invoke(response) ?? true))
                    {
                        return;
                    }

                    // apply individual file filter, if one was provided
                    var filteredFiles       = response.Files.Where(f => Options.FileFilter?.Invoke(f) ?? true);
                    var filteredLockedFiles = response.LockedFiles.Where(f => Options.FileFilter?.Invoke(f) ?? true);

                    response = new SearchResponse(response, filteredFiles, filteredLockedFiles);

                    // ensure the filtered file count still meets the response criteria
                    if (response.FileCount + response.LockedFileCount < Options.MinimumResponseFileCount)
                    {
                        return;
                    }
                }

                Interlocked.Increment(ref responseCount);
                Interlocked.Add(ref fileCount, response.FileCount);
                Interlocked.Add(ref lockedFileCount, response.LockedFileCount);

                ResponseReceived?.Invoke(response);
                SearchTimeoutTimer.Reset();

                if (responseCount >= Options.ResponseLimit)
                {
                    Complete(SearchStates.ResponseLimitReached);
                }
                else if (fileCount >= Options.FileLimit)
                {
                    Complete(SearchStates.FileLimitReached);
                }
            }
        }
Exemplo n.º 7
0
 /// <summary>
 ///     Completes the search with the specified <paramref name="state"/>.
 /// </summary>
 /// <param name="state">The terminal state of the search.</param>
 internal void Complete(SearchStates state)
 {
     SearchTimeoutTimer.Stop();
     State = SearchStates.Completed | state;
     TaskCompletionSource.SetResult(0);
 }