예제 #1
0
        public void TestAdd()
        {
            HashCollection <int> hashCollection = new HashCollection <int>();

            Assert.IsTrue(hashCollection.Add(3));
            Assert.IsFalse(hashCollection.Add(3));
        }
    /// <summary>
    /// 获取下一个题目
    /// </summary>
    /// <param name="token">Hash 客户端信息</param>
    /// <returns>Hash 结果信息</returns>
    public static Hash Assign(Hash token)
    {
        Random         random = new Random();
        Hash           data   = ClientDinosaurData.Assign(token.ToInt("clientId"));
        HashCollection names  = DinosaurData.AllName(0).ToHashCollection("data");

        data["length"]   = names.Count;
        data["duration"] = token.ToInt("duration");
        data["score"]    = token.ToInt("score");
        data["cards"]    = token.ToInt("cards");
        data["status"]   = token.ToInt("status");

        int indexAt = random.Next(0, 4);

        if (data.ToInt("dinosaurId") > 0)
        {
            HashCollection options = new HashCollection();

            //  剔除当前题目
            for (int i = 0; i < names.Count; i++)
            {
                if (names[i].ToString("name") == data.ToString("name"))
                {
                    names.RemoveAt(i);
                }
            }

            //  添加选项
            for (int i = 0; i < 4; i++)
            {
                if (i == indexAt)
                {
                    options.Add(new Hash("{\"name\":\"" + data.ToString("name") + "\"}"));
                }
                else
                {
                    int index = random.Next(0, names.Count);
                    options.Add(names[i]);
                    names.RemoveAt(index);
                }
            }

            data["options"] = options;

            return(new Hash((int)CodeType.OK, "成功", data));
        }
        return(new Hash((int)CodeType.OK, "所有题目都答完了", data));
    }
예제 #3
0
 private void btnWriteHashes_Click(object sender, EventArgs e)
 {
     if (saveFileDialog.ShowDialog() == DialogResult.OK)
     {
         var hashes = new HashCollection();
         var files  = Directory.GetFiles(txtImageFolder.Text);
         foreach (var file in files)
         {
             int[] hash;
             using (Bitmap bmp = new Bitmap(file)) {
                 hash = bmp.GetHash(_scaleSize);
             }
             hashes.Add(new HashEntry {
                 Filename = file, Hash = hash
             });
         }
         hashes.Write(saveFileDialog.FileName);
     }
 }
예제 #4
0
        public void TestISetMethods()
        {
            HashCollection <int> hashCollection = new HashCollection <int>();
            ISet set = hashCollection;

            set.Add(1);
            Assert.IsTrue(hashCollection.Contains(1));
            Assert.IsFalse(hashCollection.Contains(2));

            set.UnionWith(new[] { 2 });
            Assert.IsTrue(hashCollection.Contains(1));
            Assert.IsTrue(hashCollection.Contains(2));

            set.IntersectWith(new[] { 2 });
            Assert.IsFalse(hashCollection.Contains(1));
            Assert.IsTrue(hashCollection.Contains(2));

            hashCollection.Add(1);
            set.SymmetricExceptWith(new[] { 2, 3 });
            Assert.IsTrue(hashCollection.Contains(1));
            Assert.IsFalse(hashCollection.Contains(2));
            Assert.IsTrue(hashCollection.Contains(3));

            set.ExceptWith(new[] { 2, 3 });
            Assert.IsTrue(hashCollection.Contains(1));
            Assert.IsFalse(hashCollection.Contains(2));
            Assert.IsFalse(hashCollection.Contains(3));

            Assert.IsTrue(set.IsSupersetOf(Array <int> .Empty));
            Assert.IsFalse(set.IsSupersetOf(new[] { 2 }));
            Assert.IsTrue(set.IsSubsetOf(new[] { 1, 2 }));
            Assert.IsFalse(set.IsSubsetOf(new[] { 2 }));

            Assert.IsTrue(set.IsProperSupersetOf(Array <int> .Empty));
            Assert.IsFalse(set.IsProperSupersetOf(set));
            Assert.IsTrue(set.IsProperSubsetOf(new[] { 1, 2 }));
            Assert.IsFalse(set.IsProperSubsetOf(set));

            Assert.IsTrue(set.Overlaps(new[] { 1, 2 }));
            Assert.IsFalse(set.Overlaps(new[] { 3, 4 }));
        }
예제 #5
0
    /// <summary>
    /// 获取排行榜信息
    /// </summary>
    /// <param name="token">Hash 客户端信息</param>
    /// <returns>Hash 排行榜</returns>
    public static Hash Rank(Hash token)
    {
        Hash           data   = new Hash();
        HashCollection all    = ClientData.GetRank(token.ToInt("clientId")).ToHashCollection("data");
        HashCollection global = new HashCollection();
        HashCollection friend = new HashCollection();

        for (int i = 0; i < all.Count; i++)
        {
            if (i < 100)
            {
                global.Add(all[i]);
            }
            if (all[i].ToInt("relateType") > 0 || all[i].ToInt("clientId") == token.ToInt("clientId"))
            {
                friend.Add(all[i]);
            }
        }
        data["global"] = global;
        data["friend"] = friend;
        return(new Hash((int)CodeType.OK, "成功", data));
    }
예제 #6
0
        private IEnumerable <Hash> ParityEncoding(IEnumerable <ArraySegment <byte> > buffers, HashAlgorithm hashAlgorithm, CorrectionAlgorithm correctionAlgorithm, CancellationToken token)
        {
            lock (_parityEncodingLockObject)
            {
                if (correctionAlgorithm == CorrectionAlgorithm.ReedSolomon8)
                {
                    if (buffers.Count() > 128)
                    {
                        throw new ArgumentOutOfRangeException(nameof(buffers));
                    }

                    var createBuffers = new List <ArraySegment <byte> >();

                    try
                    {
                        var targetBuffers = new ArraySegment <byte> [buffers.Count()];
                        var parityBuffers = new ArraySegment <byte> [buffers.Count()];

                        int blockLength = buffers.Max(n => n.Count);

                        // Normalize
                        {
                            int index = 0;

                            foreach (var buffer in buffers)
                            {
                                token.ThrowIfCancellationRequested();

                                if (buffer.Count < blockLength)
                                {
                                    var tempBuffer = new ArraySegment <byte>(_bufferManager.TakeBuffer(blockLength), 0, blockLength);
                                    Unsafe.Copy(buffer.Array, buffer.Offset, tempBuffer.Array, tempBuffer.Offset, buffer.Count);
                                    Unsafe.Zero(tempBuffer.Array, tempBuffer.Offset + buffer.Count, tempBuffer.Count - buffer.Count);

                                    createBuffers.Add(tempBuffer);

                                    targetBuffers[index] = tempBuffer;
                                }
                                else
                                {
                                    targetBuffers[index] = buffer;
                                }

                                index++;
                            }
                        }

                        for (int i = 0; i < parityBuffers.Length; i++)
                        {
                            parityBuffers[i] = new ArraySegment <byte>(_bufferManager.TakeBuffer(blockLength), 0, blockLength);
                        }

                        var indexes = new int[parityBuffers.Length];

                        for (int i = 0; i < parityBuffers.Length; i++)
                        {
                            indexes[i] = targetBuffers.Length + i;
                        }

                        using (var reedSolomon = new ReedSolomon8(targetBuffers.Length, targetBuffers.Length + parityBuffers.Length, _threadCount, _bufferManager))
                        {
                            reedSolomon.Encode(targetBuffers, parityBuffers, indexes, blockLength, token).Wait();
                        }

                        token.ThrowIfCancellationRequested();

                        var parityHashes = new HashCollection();

                        for (int i = 0; i < parityBuffers.Length; i++)
                        {
                            Hash hash;

                            if (hashAlgorithm == HashAlgorithm.Sha256)
                            {
                                hash = new Hash(HashAlgorithm.Sha256, Sha256.Compute(parityBuffers[i]));
                            }
                            else
                            {
                                throw new NotSupportedException();
                            }

                            _blocksManager.Lock(hash);
                            _blocksManager.Set(hash, parityBuffers[i]);

                            parityHashes.Add(hash);
                        }

                        return(parityHashes);
                    }
                    finally
                    {
                        foreach (var buffer in createBuffers)
                        {
                            if (buffer.Array == null)
                            {
                                continue;
                            }

                            _bufferManager.ReturnBuffer(buffer.Array);
                        }
                    }
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
        }
예제 #7
0
        private void DecodingThread(CancellationToken token)
        {
            for (; ;)
            {
                if (token.WaitHandle.WaitOne(1000))
                {
                    return;
                }

                DownloadItemInfo item = null;

                lock (_lockObject)
                {
                    item = CollectionUtils.Unite(_volatileDownloadItemInfoManager, _downloadItemInfoManager)
                           .Where(n => !_workingItems.Contains(n))
                           .Where(n => n.State == DownloadState.Decoding || n.State == DownloadState.ParityDecoding)
                           .OrderBy(n => (n.Depth == n.Metadata.Depth) ? 0 : 1)
                           .OrderBy(n => (n.State == DownloadState.Decoding) ? 0 : 1)
                           .FirstOrDefault();

                    if (item != null)
                    {
                        _workingItems.Add(item);
                    }
                }

                if (item == null)
                {
                    continue;
                }

                try
                {
                    if ((item.Depth == 0 && !_cacheManager.Contains(item.Metadata.Hash)) ||
                        (item.Depth > 0 && !item.Index.Groups.All(n => _existManager.GetCount(n, true) >= n.Hashes.Count() / 2)))
                    {
                        item.State = DownloadState.Downloading;
                    }
                    else
                    {
                        var hashes      = new HashCollection();
                        var totalHashes = new HashCollection();

                        if (item.Depth == 0)
                        {
                            hashes.Add(item.Metadata.Hash);
                            totalHashes.Add(item.Metadata.Hash);
                        }
                        else
                        {
                            try
                            {
                                foreach (var group in item.Index.Groups)
                                {
                                    if (item.State == DownloadState.Error)
                                    {
                                        throw new OperationCanceledException();
                                    }

                                    hashes.AddRange(_cacheManager.ParityDecoding(group, token).Result);
                                }

                                totalHashes.AddRange(item.Index.Groups.SelectMany(n => n.Hashes));
                            }
                            catch (OperationCanceledException)
                            {
                                continue;
                            }

                            item.State = DownloadState.Decoding;
                        }

                        if (item.Depth < item.Metadata.Depth)
                        {
                            Index index;

                            try
                            {
                                using (var stream = _cacheManager.Decoding(hashes))
                                    using (var progressStream = new ProgressStream(stream, null, 1024 * 1024, token))
                                    {
                                        if (item.State == DownloadState.Error)
                                        {
                                            throw new OperationCanceledException();
                                        }
                                        if (progressStream.Length > item.MaxLength)
                                        {
                                            throw new ArgumentException();
                                        }

                                        index = Index.Import(progressStream, _bufferManager);
                                    }
                            }
                            catch (OperationCanceledException)
                            {
                                continue;
                            }

                            lock (_lockObject)
                            {
                                if (item.Path != null)
                                {
                                    _protectCacheInfoManager.Add(new ProtectedCacheInfo(DateTime.UtcNow, totalHashes));
                                }

                                this.CheckState(index);
                                this.UncheckState(item.Index);

                                item.Index = index;

                                item.Depth++;

                                item.State = DownloadState.Downloading;
                            }
                        }
                        else
                        {
                            if (item.Path != null)
                            {
                                string filePath = null;

                                try
                                {
                                    token.ThrowIfCancellationRequested();

                                    string targetPath;

                                    if (Path.IsPathRooted(item.Path))
                                    {
                                        targetPath = item.Path;
                                    }
                                    else
                                    {
                                        targetPath = Path.GetFullPath(Path.Combine(_basePath, item.Path));

                                        // ディレクトリトラバーサル対策
                                        if (!targetPath.StartsWith(Path.GetFullPath(_basePath)))
                                        {
                                            targetPath = Path.GetFullPath(Path.Combine(_basePath, Path.GetFileName(item.Path)));
                                        }
                                    }

                                    Directory.CreateDirectory(Path.GetDirectoryName(targetPath));

                                    using (var inStream = _cacheManager.Decoding(hashes))
                                        using (var outStream = DownloadManager.GetUniqueFileStream(targetPath + ".tmp"))
                                            using (var safeBuffer = _bufferManager.CreateSafeBuffer(1024 * 1024))
                                            {
                                                filePath = outStream.Name;

                                                int readLength;

                                                while ((readLength = inStream.Read(safeBuffer.Value, 0, safeBuffer.Value.Length)) > 0)
                                                {
                                                    if (item.State == DownloadState.Error)
                                                    {
                                                        throw new OperationCanceledException();
                                                    }
                                                    token.ThrowIfCancellationRequested();

                                                    outStream.Write(safeBuffer.Value, 0, readLength);
                                                }
                                            }

                                    File.Move(filePath, DownloadManager.GetUniqueFilePath(targetPath));
                                }
                                catch (OperationCanceledException)
                                {
                                    if (filePath != null)
                                    {
                                        File.Delete(filePath);
                                    }

                                    continue;
                                }
                            }

                            lock (_lockObject)
                            {
                                if (item.Path != null)
                                {
                                    _protectCacheInfoManager.Add(new ProtectedCacheInfo(DateTime.UtcNow, totalHashes));
                                }

                                item.ResultHashes.AddRange(hashes);

                                item.State = DownloadState.Completed;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    item.State = DownloadState.Error;

                    Log.Error(e);
                }
                finally
                {
                    _workingItems.Remove(item);
                }
            }
        }
예제 #8
0
        private void BeginValidation()
        {
            ThreadStart beginValidation = delegate
            {
                while (true)
                {
                    // Wait if there is no work to do
                    this.queueResetEvent.WaitOne();

                    ISupportValidation obj = null;
                    lock (this.validationQueue)
                    {
                        if (this.validationQueue.Count == 0)
                        {
                            this.Status = ValidationStatus.Ready;
                            this.queueResetEvent.Reset();
                            continue;
                        }
                        else
                        {
                            obj = this.validationQueue[0];
                            this.validationQueue.RemoveAt(0);
                        }
                    }

                    lock (this.validationResults)
                    {
                        // Try to get the validation results (they may have been removed by a different
                        // thread calling RemoveObject)
                        HashCollection <ValidationResult> oldValidationResults;
                        if (this.validationResults.TryGetValue(obj, out oldValidationResults))
                        {
                            HashCollection <ValidationResult> newValidationResults = new HashCollection <ValidationResult> ();
                            foreach (ValidationResult validationResult in obj.Validate())
                            {
                                if (!newValidationResults.Contains(validationResult))
                                {
                                    newValidationResults.Add(validationResult);
                                }
                            }

                            foreach (ValidationResult validationResult in new List <ValidationResult> (oldValidationResults))
                            {
                                if (!newValidationResults.Contains(validationResult))
                                {
                                    oldValidationResults.Remove(validationResult);
                                    ValidationResult   currentValidationResult = validationResult;
                                    SendOrPostCallback raiseRemovedEvent       = delegate
                                    {
                                        this.RaiseValidationResultRemovedEvent(currentValidationResult);
                                    };
                                    this.asyncOperation.Post(raiseRemovedEvent, null);
                                }
                            }

                            foreach (ValidationResult validationResult in newValidationResults)
                            {
                                if (!oldValidationResults.Contains(validationResult))
                                {
                                    oldValidationResults.Add(validationResult);
                                    ValidationResult   currentValidationResult = validationResult;
                                    SendOrPostCallback raiseAddedEvent         = delegate
                                    {
                                        this.RaiseValidationResultAddedEvent(currentValidationResult);
                                    };
                                    this.asyncOperation.Post(raiseAddedEvent, null);
                                }
                            }
                        }
                    }
                }
            };

            Thread thread = new Thread(beginValidation);

            thread.Name         = "Validation Manager";
            thread.IsBackground = true;
            thread.Priority     = ThreadPriority.BelowNormal;
            thread.Start();
        }