-
Notifications
You must be signed in to change notification settings - Fork 12
/
HashManager.cs
142 lines (117 loc) · 3.69 KB
/
HashManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CentralMine.NET
{
class HashManager
{
public class HashBlock
{
public uint Start;
public uint Count;
public Client Owner;
};
List<HashBlock> mFreeBlocks;
List<HashBlock> mBusyBlocks;
List<HashBlock> mDoneBlocks;
public uint mHashesDone = 0;
public uint mHashesTotal = 0xFFFFFFFF;
public uint mHashrate = 0;
public bool mExpires = false;
public DateTime mStartTime;
public HashManager()
{
mFreeBlocks = new List<HashBlock>();
mBusyBlocks = new List<HashBlock>();
mDoneBlocks = new List<HashBlock>();
// Add initial free block
HashBlock block = new HashBlock();
block.Owner = null;
block.Start = 0;
block.Count = mHashesTotal;
mFreeBlocks.Add(block);
mStartTime = DateTime.Now;
}
HashBlock FindFreeBlock(uint desiredSize)
{
HashBlock free = null;
foreach (HashBlock hb in mFreeBlocks)
{
if (hb.Count >= desiredSize)
{
free = hb;
break;
}
}
if (free == null && mFreeBlocks.Count > 0)
{
// Didnt find a large enough block, use the first block instead
free = mFreeBlocks[0];
}
return free;
}
public HashBlock Allocate(uint desired, Client c)
{
HashBlock block = null;
HashBlock free = FindFreeBlock(desired);
if (free != null)
{
if (free.Count > desired)
{
// Split this block
block = new HashBlock();
block.Owner = c;
block.Start = free.Start;
block.Count = desired;
mBusyBlocks.Add(block);
free.Start += desired;
free.Count -= desired;
}
else
{
// Remove the free block from the free list
mFreeBlocks.Remove(free);
// Assign the free block to the client
free.Owner = c;
// Add the block to the busy list
block = free;
mBusyBlocks.Add(block);
}
}
return block;
}
public void FinishBlock(HashBlock block)
{
mBusyBlocks.Remove(block);
mDoneBlocks.Add(block);
// Stat Tracking
{
mHashesDone = 0;
foreach (HashBlock hb in mDoneBlocks)
mHashesDone += hb.Count;
//Console.WriteLine("Block finished: " + mHashesDone + " (%" + ((float)mHashesDone / (float)mHashesTotal) * 100 + ")");
}
}
public void FreeBlock(HashBlock block)
{
if (block != null)
{
mBusyBlocks.Remove(block);
mFreeBlocks.Insert(0, block);
block.Owner = null;
}
}
public bool IsComplete()
{
return mFreeBlocks.Count == 0 && mBusyBlocks.Count == 0;
}
public bool IsExpired()
{
if (!mExpires)
return false;
TimeSpan span = DateTime.Now - mStartTime;
return (span.TotalSeconds > 30);
}
}
}