private void EncodeThread() { for (;;) { Thread.Sleep(1000 * 1); if (this.State == ManagerState.Stop) return; BackgroundUploadItem item = null; try { lock (_thisLock) { if (_settings.UploadItems.Count > 0) { item = _settings.UploadItems .Where(n => n.State == BackgroundUploadState.Encoding) .FirstOrDefault(); } } } catch (Exception) { return; } if (item == null) continue; try { if (item.Groups.Count == 0 && item.Keys.Count == 0) { Stream stream = null; try { if (item.Scheme == "Broadcast") { if (item.Type == "Link") { var value = item.Link; if (value == null) throw new FormatException(); stream = ContentConverter.ToStream(value); } else if (item.Type == "Profile") { var value = item.Profile; if (value == null) throw new FormatException(); stream = ContentConverter.ToStream(value); } else if (item.Type == "Store") { var value = item.Store; if (value == null) throw new FormatException(); stream = ContentConverter.ToStream(value); } } else if (item.Scheme == "Unicast") { if (item.Type == "Message") { var value = item.Message; if (value == null) throw new FormatException(); stream = ContentConverter.ToCryptoStream(value, item.ExchangePublicKey); } } else if (item.Scheme == "Multicast") { if (item.Type == "Message") { var value = item.Message; if (value == null) throw new FormatException(); stream = ContentConverter.ToStream(value); } } else { throw new FormatException(); } if (stream.Length == 0) { lock (_thisLock) { if (item.Scheme == "Broadcast") { _connectionsManager.Upload(new BroadcastMetadata(item.Type, item.CreationTime, null, item.DigitalSignature)); } else if (item.Scheme == "Unicast") { _connectionsManager.Upload(new UnicastMetadata(item.Type, item.Signature, item.CreationTime, null, item.DigitalSignature)); } else if (item.Scheme == "Multicast") { _connectionsManager.Upload(new MulticastMetadata(item.Type, item.Tag, item.CreationTime, null, null, item.DigitalSignature)); } item.State = BackgroundUploadState.Completed; } } else { KeyCollection keys = null; try { using (ProgressStream encodingProgressStream = new ProgressStream(stream, (object sender, long readSize, long writeSize, out bool isStop) => { isStop = (this.State == ManagerState.Stop || !_settings.UploadItems.Contains(item)); }, 1024 * 1024, true)) { if (stream.Length == 0) throw new InvalidOperationException("Stream Length"); encodingProgressStream.Seek(0, SeekOrigin.Begin); item.State = BackgroundUploadState.Encoding; keys = _cacheManager.Encoding(encodingProgressStream, CompressionAlgorithm.None, CryptoAlgorithm.None, null, item.BlockLength, item.HashAlgorithm); } } catch (StopIoException) { continue; } lock (_thisLock) { if (!_settings.UploadItems.Contains(item)) { foreach (var key in keys) { _cacheManager.Unlock(key); } continue; } foreach (var key in keys) { item.UploadKeys.Add(key); item.LockedKeys.Add(key); } item.Keys.AddRange(keys); } } } finally { if (stream != null) stream.Dispose(); } } else if (item.Groups.Count == 0 && item.Keys.Count == 1) { BroadcastMetadata broadcastMetadata = null; UnicastMetadata unicastMetadata = null; MulticastMetadata multicastMetadata = null; { var metadata = new Metadata(item.Depth, item.Keys[0], CompressionAlgorithm.None, CryptoAlgorithm.None, null); if (item.Scheme == "Broadcast") { broadcastMetadata = new BroadcastMetadata(item.Type, item.CreationTime, metadata, item.DigitalSignature); } else if (item.Scheme == "Unicast") { unicastMetadata = new UnicastMetadata(item.Type, item.Signature, item.CreationTime, metadata, item.DigitalSignature); } else if (item.Scheme == "Multicast") { var miner = new Miner(CashAlgorithm.Version1, item.MiningLimit, item.MiningTime); try { var task = Task.Run(() => { return new MulticastMetadata(item.Type, item.Tag, item.CreationTime, metadata, miner, item.DigitalSignature); }); while (!task.IsCompleted) { if (this.State == ManagerState.Stop) miner.Cancel(); lock (_thisLock) { if (!_settings.UploadItems.Contains(item)) { miner.Cancel(); } } Thread.Sleep(1000); } multicastMetadata = task.Result; } catch (Exception) { continue; } } } lock (_thisLock) { if (!_settings.UploadItems.Contains(item)) continue; if (item.Scheme == "Broadcast") { _connectionsManager.Upload(broadcastMetadata); } else if (item.Scheme == "Unicast") { _connectionsManager.Upload(unicastMetadata); } else if (item.Scheme == "Multicast") { _connectionsManager.Upload(multicastMetadata); } item.Keys.Clear(); foreach (var key in item.UploadKeys) { _connectionsManager.Upload(key); } item.State = BackgroundUploadState.Uploading; this.CheckState(item); } } else if (item.Keys.Count > 0) { var length = Math.Min(item.Keys.Count, 128); var keys = new KeyCollection(item.Keys.Take(length)); Group group = null; try { using (var tokenSource = new CancellationTokenSource()) { var task = _cacheManager.ParityEncoding(keys, item.HashAlgorithm, item.BlockLength, item.CorrectionAlgorithm, tokenSource.Token); while (!task.IsCompleted) { if (this.State == ManagerState.Stop || !_settings.UploadItems.Contains(item)) tokenSource.Cancel(); Thread.Sleep(1000); } group = task.Result; } } catch (Exception) { continue; } lock (_thisLock) { if (!_settings.UploadItems.Contains(item)) { foreach (var key in group.Keys.Skip(group.InformationLength)) { _cacheManager.Unlock(key); } continue; } foreach (var key in group.Keys.Skip(group.InformationLength)) { item.UploadKeys.Add(key); item.LockedKeys.Add(key); } item.Groups.Add(group); item.Keys.RemoveRange(0, length); } } else if (item.Groups.Count > 0 && item.Keys.Count == 0) { var index = new Index(); index.Groups.AddRange(item.Groups); KeyCollection keys = null; try { using (var stream = index.Export(_bufferManager)) using (ProgressStream encodingProgressStream = new ProgressStream(stream, (object sender, long readSize, long writeSize, out bool isStop) => { isStop = (this.State == ManagerState.Stop || !_settings.UploadItems.Contains(item)); }, 1024 * 1024, true)) { encodingProgressStream.Seek(0, SeekOrigin.Begin); item.State = BackgroundUploadState.Encoding; keys = _cacheManager.Encoding(encodingProgressStream, CompressionAlgorithm.None, CryptoAlgorithm.None, null, item.BlockLength, item.HashAlgorithm); } } catch (StopIoException) { continue; } lock (_thisLock) { if (!_settings.UploadItems.Contains(item)) { foreach (var key in keys) { _cacheManager.Unlock(key); } continue; } foreach (var key in keys) { item.UploadKeys.Add(key); item.LockedKeys.Add(key); } item.Keys.AddRange(keys); item.Depth++; item.Groups.Clear(); } } } catch (Exception e) { item.State = BackgroundUploadState.Error; Log.Error(e); this.Remove(item); } } }
public void Test_Miner() { //{ // var key = NetworkConverter.FromHexString("e0ee19d617ee6ea9ea592afbdf71bafba6eecde2beba0d3cdc51419522fe5dbdf18f6830081be1615969b1fe43344fac3c312cd86a487cb1bd04f2c44cddca11"); // var value = NetworkConverter.FromHexString("01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101"); // var count = Verify_1(key, value); //} { Miner miner = new Miner(CashAlgorithm.Version1, -1, new TimeSpan(0, 0, 1)); Cash cash = null; Stopwatch sw = new Stopwatch(); sw.Start(); using (MemoryStream stream = new MemoryStream(NetworkConverter.FromHexString("0101010101010101"))) { cash = miner.Create(stream); } sw.Stop(); Assert.IsTrue(sw.ElapsedMilliseconds < 1000 * 30); } { Miner miner = new Miner(CashAlgorithm.Version1, 20, new TimeSpan(1, 0, 0)); Cash cash = null; using (MemoryStream stream = new MemoryStream(NetworkConverter.FromHexString("0101010101010101"))) { cash = miner.Create(stream); stream.Seek(0, SeekOrigin.Begin); Assert.IsTrue(Miner.Verify(cash, stream) >= 20); } } { Miner miner = new Miner(CashAlgorithm.Version1, 0, TimeSpan.Zero); Cash cash = null; using (MemoryStream stream = new MemoryStream(NetworkConverter.FromHexString("0101010101010101"))) { cash = miner.Create(stream); } Assert.IsTrue(cash == null); } { Stopwatch sw = new Stopwatch(); sw.Start(); Assert.Throws<AggregateException>(() => { Miner miner = new Miner(CashAlgorithm.Version1, -1, new TimeSpan(1, 0, 0)); var task = Task.Factory.StartNew(() => { Cash cash = null; using (MemoryStream stream = new MemoryStream(NetworkConverter.FromHexString("0101010101010101"))) { cash = miner.Create(stream); } }); Thread.Sleep(1000); miner.Cancel(); task.Wait(); }); sw.Stop(); Assert.IsTrue(sw.ElapsedMilliseconds < 1000 * 30); } }
private void UploadThread() { for (; ; ) { Thread.Sleep(1000 * 1); if (this.State == ManagerState.Stop) return; { UploadItem item = null; lock (this.ThisLock) { if (_settings.UploadItems.Count > 0) { item = _settings.UploadItems[0]; } } try { if (item != null) { ArraySegment<byte> buffer = new ArraySegment<byte>(); try { if (item.Type == "Profile") { buffer = ContentConverter.ToProfileBlock(item.Profile); } else if (item.Type == "SignatureMessage") { buffer = ContentConverter.ToSignatureMessageBlock(item.SignatureMessage, item.ExchangePublicKey); } else if (item.Type == "WikiDocument") { buffer = ContentConverter.ToWikiDocumentBlock(item.WikiDocument); } else if (item.Type == "ChatMessage") { buffer = ContentConverter.ToChatMessageBlock(item.ChatMessage); } Key key = null; { if (_hashAlgorithm == HashAlgorithm.Sha256) { key = new Key(Sha256.ComputeHash(buffer), _hashAlgorithm); } this.Lock(key); } _cacheManager[key] = buffer; _connectionsManager.Upload(key); var miner = new Miner(CashAlgorithm.Version1, item.MiningLimit, item.MiningTime); var task = Task.Factory.StartNew(() => { if (item.Type == "Profile") { var metadata = new ProfileMetadata(item.Profile.CreationTime, key, item.DigitalSignature); _connectionsManager.Upload(metadata); } else if (item.Type == "SignatureMessage") { var metadata = new SignatureMessageMetadata(item.SignatureMessage.Signature, item.SignatureMessage.CreationTime, key, miner, item.DigitalSignature); _connectionsManager.Upload(metadata); } else if (item.Type == "WikiDocument") { var metadata = new WikiDocumentMetadata(item.WikiDocument.Tag, item.WikiDocument.CreationTime, key, miner, item.DigitalSignature); _connectionsManager.Upload(metadata); } else if (item.Type == "ChatMessage") { var metadata = new ChatMessageMetadata(item.ChatMessage.Tag, item.ChatMessage.CreationTime, key, miner, item.DigitalSignature); _connectionsManager.Upload(metadata); } }); while (!task.IsCompleted) { if (this.State == ManagerState.Stop) miner.Cancel(); lock (this.ThisLock) { if (!_settings.UploadItems.Contains(item)) { miner.Cancel(); } } Thread.Sleep(1000); } if (task.Exception != null) throw task.Exception; lock (this.ThisLock) { _settings.UploadItems.Remove(item); } } finally { if (buffer.Array != null) { _bufferManager.ReturnBuffer(buffer.Array); } } } } catch (Exception) { } } } }