Added buffer pool with leak check
[pithos-ms-client] / trunk / Pithos.Network / MD5BlockCalculator.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Diagnostics;\r
4 using System.Linq;\r
5 using System.Security.Cryptography;\r
6 using System.Text;\r
7 using System.Threading;\r
8 using System.Threading.Tasks;\r
9 using System.Threading.Tasks.Dataflow;\r
10 using OpenSSL.Core;\r
11 using OpenSSL.Crypto;\r
12 \r
13 \r
14 namespace Pithos.Network\r
15 {\r
16     //public class MD5BlockCalculator:IDisposable\r
17     //{\r
18     //    private MessageDigestContext _hasher ;\r
19 \r
20     //    private ActionBlock<Tuple<long, byte[],int>>  _actionBlock;\r
21 \r
22     //    private long _currentBlock = 0;\r
23 \r
24     //    public MD5BlockCalculator(int capacity)\r
25     //    {\r
26     //        var opt = new ExecutionDataflowBlockOptions { BoundedCapacity = capacity };\r
27     //        _actionBlock = new ActionBlock<Tuple<long, byte[], int>>(t => ProcessBlock(t), opt);\r
28     //        _hasher = new MessageDigestContext(MessageDigest.CreateByName("md5"));\r
29     //        _hasher.Init();\r
30     //    }\r
31 \r
32     //    public MD5BlockCalculator()\r
33     //    {\r
34     //        _actionBlock = new ActionBlock<Tuple<long, byte[], int>>(t => ProcessBlock(t));\r
35     //        _hasher = new MessageDigestContext(MessageDigest.CreateByName("md5"));\r
36     //        _hasher.Init();\r
37     //    }\r
38 \r
39     //    private void ProcessBlock(Tuple<long,byte[],int> tuple)\r
40     //    {\r
41     //        if (tuple.Item1 == _currentBlock)\r
42     //        {\r
43     //            var size = tuple.Item3;\r
44 \r
45     //            var buffer= tuple.Item2;\r
46     //            if (size == buffer.Length)\r
47     //            {\r
48     //                _hasher.Update(buffer);\r
49     //            }\r
50     //            else\r
51     //            {\r
52     //                var block = new byte[size];\r
53     //                Buffer.BlockCopy(buffer, 0, block, 0, size);\r
54     //                _hasher.Update(block);\r
55     //            }\r
56     //            Interlocked.Increment(ref _currentBlock);\r
57     //        }\r
58     //        else\r
59     //        {\r
60     //            _actionBlock.Post(tuple);\r
61     //        }\r
62     //    }\r
63 \r
64     //    public void PostBlock(long blockIndex,byte[] buffer,int size)\r
65     //    {\r
66     //        _actionBlock.Post(Tuple.Create(blockIndex, buffer, size));\r
67     //    }\r
68 \r
69     //    public async Task<string> GetHash()\r
70     //    {\r
71     //        _actionBlock.Complete();\r
72     //        await _actionBlock.Completion;\r
73     //        Debug.Assert(_actionBlock.InputCount == 0);\r
74     //        var hashBytes=_hasher.DigestFinal();\r
75     //        var hash=hashBytes.ToHashString();\r
76     //        return hash;\r
77     //    }\r
78 \r
79     //    public void Dispose()\r
80     //    {\r
81     //        Dispose(true);\r
82     //        GC.SuppressFinalize(this);\r
83     //    }\r
84 \r
85     //    protected virtual void Dispose(bool disposing)\r
86     //    {\r
87     //        if (disposing && _hasher!=null)\r
88     //            _hasher.Dispose();\r
89     //        _hasher = null;            \r
90     //    }\r
91     //}\r
92 }\r