ContainerStack.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace System.Fire.Container {
  7. public class ContainerStack {
  8. private struct StackPositions {
  9. public byte ContainerGroup;
  10. public byte Container;
  11. public ushort ChunkId;
  12. public StackPositions(byte Group, byte Container, ushort ChunkId) {
  13. this.ContainerGroup = Group;
  14. this.Container = Container;
  15. this.ChunkId = ChunkId;
  16. }
  17. }
  18. private List<byte> _stack = new List<byte>();
  19. private int _position;
  20. private List<StackPositions> _stackPosition = new List<StackPositions>();
  21. private bool _done = false;
  22. private long _size;
  23. private long _writtenBytes;
  24. private string _fileName;
  25. private long startTime;
  26. private long endTime;
  27. private long expectedSize;
  28. private long lastRecivedTime;
  29. private double lastBytesPerTick;
  30. public ContainerStack() {
  31. this.startTime = DateTime.Now.Ticks;
  32. this.lastRecivedTime = DateTime.Now.Ticks;
  33. }
  34. public ContainerStack(string FileName) {
  35. this._fileName = FileName;
  36. this.startTime = DateTime.Now.Ticks;
  37. this.lastRecivedTime = DateTime.Now.Ticks;
  38. }
  39. public ContainerStack(long expectedSize) {
  40. this.startTime = DateTime.Now.Ticks;
  41. this.expectedSize = expectedSize;
  42. this.lastRecivedTime = DateTime.Now.Ticks;
  43. }
  44. public ContainerStack(string FileName, long expectedSize) {
  45. this._fileName = FileName;
  46. this.expectedSize = expectedSize;
  47. this.startTime = DateTime.Now.Ticks;
  48. this.lastRecivedTime = DateTime.Now.Ticks;
  49. }
  50. public void AddStackPosition(Container c, ushort chunkId) {
  51. this._stackPosition.Add(new StackPositions(c.GroupId, c.ContainerId, chunkId));
  52. }
  53. public void AddStackPosition(byte GroupId, byte ContainerId, ushort chunkId) {
  54. this._stackPosition.Add(new StackPositions(GroupId, ContainerId, chunkId));
  55. }
  56. public void add(byte[] bytes) {
  57. this._stack.AddRange(bytes);
  58. this._size += bytes.Length;
  59. this._writtenBytes += bytes.Length;
  60. long tmp = DateTime.Now.Ticks - this.lastRecivedTime;
  61. if (tmp == 0) tmp = 1;
  62. this.lastBytesPerTick = bytes.Length / tmp;
  63. this.lastRecivedTime = DateTime.Now.Ticks;
  64. }
  65. public void add(byte b) {
  66. this._stack.Add(b);
  67. this._size++;
  68. this._writtenBytes++;
  69. this.lastBytesPerTick = 1 / (DateTime.Now.Ticks - this.lastRecivedTime);
  70. this.lastRecivedTime = DateTime.Now.Ticks;
  71. }
  72. public byte[] read(int length) {
  73. int c = this._position;
  74. this._position += length;
  75. int stackLen = Math.Min(length, this._stack.Count);
  76. byte[] b = this._stack.GetRange(0, stackLen).ToArray();
  77. this._stack.RemoveRange(0, stackLen);
  78. this._size -= length;
  79. return b;
  80. }
  81. public long Size {
  82. get {
  83. return this._size;
  84. }
  85. }
  86. public long WrittenBytes {
  87. get {
  88. return this._writtenBytes;
  89. }
  90. }
  91. public string FileName {
  92. get {
  93. return this._fileName;
  94. }
  95. set {
  96. this._fileName = value;
  97. }
  98. }
  99. /// <summary>
  100. /// Returns the upload duration in miliseconds
  101. /// </summary>
  102. public double UploadDuration {
  103. get {
  104. if (this.endTime == 0)
  105. return (DateTime.Now.Ticks - this.startTime) / (TimeSpan.TicksPerMillisecond / 1000);
  106. else
  107. return (this.endTime - this.startTime) / (TimeSpan.TicksPerMillisecond / 1000);
  108. }
  109. }
  110. /// <summary>
  111. /// Returns the remaining expected upload time in seconds
  112. /// </summary>
  113. public double ExpectedTimeRemaining {
  114. get {
  115. if (this.endTime != 0 && this.expectedSize>0) {
  116. if(this.lastBytesPerTick>0) {
  117. // Long version
  118. //double bytesPerTick = ((this._writtenBytes / (DateTime.Now.Ticks - this.startTime)) + this.lastBytesPerTick) / 2;
  119. //long remaining = this.expectedSize - this._writtenBytes;
  120. //return bytesPerTick * remaining * TimeSpan.TicksPerSecond;
  121. // Short version
  122. return (((this._writtenBytes / (DateTime.Now.Ticks - this.startTime)) + this.lastBytesPerTick) / 2) * (this.expectedSize - this._writtenBytes) * TimeSpan.TicksPerSecond;
  123. } else {
  124. // Long version
  125. //double bytesPerTick = (this._writtenBytes / (DateTime.Now.Ticks - this.startTime));
  126. //long remaining = this.expectedSize - this._writtenBytes;
  127. //return bytesPerTick * remaining * TimeSpan.TicksPerSecond;
  128. // Short version
  129. return (this._writtenBytes / (DateTime.Now.Ticks - this.startTime)) * (this.expectedSize - this._writtenBytes) * TimeSpan.TicksPerSecond;
  130. }
  131. } else
  132. return 0;
  133. }
  134. }
  135. public byte[] getStackPositions() {
  136. byte[] b = new byte[this._stackPosition.Count * 4];
  137. for(int i = 0, c = this._stackPosition.Count; i<c;i++) {
  138. b[(i * 4)] = this._stackPosition[i].ContainerGroup;
  139. b[(i * 4) + 1] = this._stackPosition[i].Container;
  140. b[(i * 4) + 2] = (byte)(this._stackPosition[i].ChunkId >> 8);
  141. b[(i * 4) + 3] = (byte)(this._stackPosition[i].ChunkId % 256);
  142. }
  143. return b;
  144. }
  145. public float PercentLoaded {
  146. get {
  147. if (this.expectedSize == 0 && this.endTime == 0)
  148. return 0.0f;
  149. if (this.endTime > 0)
  150. return 1.0f;
  151. return this._writtenBytes / this.expectedSize;
  152. }
  153. }
  154. public float PercentWritten {
  155. get {
  156. if (this.expectedSize == 0)
  157. return 0.0f;
  158. return (this._writtenBytes - this._size) / this.expectedSize;
  159. }
  160. }
  161. public bool IsDone {
  162. get {
  163. return this._done;
  164. }
  165. set {
  166. this.endTime = DateTime.Now.Ticks;
  167. this._done = value;
  168. }
  169. }
  170. }
  171. }