public void AddUpdatable(IUpdatable newUpdatable) { var newHandle = new DisposeHandle(newUpdatable); newUpdatable.handle = newHandle; _disposeHandles.Add(newHandle); _updateQueue.Add(newUpdatable); }
private void ProcessHandshakeAsync(Socket sckRead) { using (var h = DisposeHandle.Get(sckListen)) { ThreadManager.NewThread(() => HandshakeThread(sckRead), () => sckRead.Close(), "Incoming connection " + NetTools.GetRemoteIP(sckRead).ToString()); h.Disengage(); } }
public void BeginTransaction(bool readOnly) { CheckNotInTransaction(); if (wasInTransaction) { throw new InvalidOperationException("DeferredChainStateCursor may only be used for a single transaction"); } wasInTransaction = true; parentHandle = storageManager.OpenChainStateCursor(); try { parentCursor = parentHandle.Item; parentCursor.BeginTransaction(); inTransaction = true; this.chainTip = parentCursor.ChainTip; this.unspentTxCount = parentCursor.UnspentTxCount; this.unspentOutputCount = parentCursor.UnspentOutputCount; this.totalTxCount = parentCursor.TotalTxCount; this.totalInputCount = parentCursor.TotalInputCount; this.totalOutputCount = parentCursor.TotalOutputCount; this.readOnly = readOnly; } catch (Exception ex) { inTransaction = false; // ensure utxo applier has completed, but do not propagate exceptions ((IDataflowBlock)unspentTxes.WorkQueue).Fault(ex); ((IDataflowBlock)utxoApplier).Fault(ex); try { utxoApplier.Completion.Wait(); } catch (Exception) { } ((IDataflowBlock)unspentTxOutputs.WorkQueue).Fault(ex); ((IDataflowBlock)utxoApplier2).Fault(ex); try { utxoApplier2.Completion.Wait(); } catch (Exception) { } parentHandle.Dispose(); parentHandle = null; parentCursor = null; utxoApplier = null; throw; } }
//Action<Exception> onHandshakeError; public SocketListener(ActionSyncronizerProxy sync, Action <Handshake, Socket> processConnection, Socket sckListen) { using (var h = DisposeHandle.Get(sckListen)) { this.sckListen = sckListen; this.processConnection = sync.Convert(processConnection); //this.onHandshakeError = onHandshakeError; ThreadManager.NewThread(() => ProcessThread(), () => TerminateThread(), "SocketListener " + NetTools.GetLocalIP(sckListen).ToString()); h.Disengage(); } }
public SocketReader(ActionSyncronizerProxy sync, Action <MemoryStream> messageProcessor, Action <IOException> errorResponse, Action onSoftDisconnect, Socket socketRead) { using (var h = DisposeHandle.Get(socketRead)) { this.onSoftDisconnect = sync.Convert(onSoftDisconnect); this.messageProcessor = sync.Convert(messageProcessor); this.errorResponse = sync.Convert(errorResponse); this.socketRead = socketRead; ThreadManager.NewThread(() => this.ProcessThread(), () => TerminateThread(), "SocketReader " + NetTools.GetRemoteIP(socketRead).ToString()); h.Disengage(); } }
private void HandshakeThread(Socket sckRead) { try { using (var h = DisposeHandle.Get(sckRead)) using (NetworkStream connectionStream = new NetworkStream(sckRead, false)) { int nTp = connectionStream.ReadByte(); if (nTp != (int)NetworkMessageType.HANDSHAKE) { //Log.LogWriteLine("Invalid incoming connection message (expecting handshake): type {0} {1}", nTp, (MessageType)nTp); sckRead.Close(); throw new Exception("Invalid incoming connection message (expecting handshake): type " + nTp + " " + (NetworkMessageType)nTp); } Handshake info = Serializer.Deserialize <Handshake>(Serializer.DeserializeChunk(connectionStream)); // swap OverlayEndpoint remote = info.local; OverlayEndpoint local = info.remote; info.remote = remote; info.local = local; // read extra information if (info.hasExtraInfo) { info.ExtraInfo = Serializer.DeserializeChunk(connectionStream); } processConnection.Invoke(info, sckRead); h.Disengage(); } } catch (Exception e) { Log.Console("Error while processing handshake: {0}", e.Message); //onHandshakeError(e); throw; } }
public ChainStateBuilder(Logger logger, IBlockchainRules rules, CoreStorage coreStorage) { this.logger = logger; this.sha256 = new SHA256Managed(); this.rules = rules; this.coreStorage = coreStorage; this.blockValidator = new BlockValidator(this.coreStorage, this.rules, this.logger); this.chainStateCursorHandle = coreStorage.OpenChainStateCursor(); this.chainStateCursor = this.chainStateCursorHandle.Item; this.chain = new ChainBuilder(chainStateCursor.ReadChain()); this.utxoBuilder = new UtxoBuilder(chainStateCursor, logger); this.commitLock = new ReaderWriterLockSlim(); this.stats = new BuilderStats(); }
public void BeginTransaction(bool readOnly) { CheckNotInTransaction(); if (wasInTransaction) throw new InvalidOperationException("DeferredChainStateCursor may only be used for a single transaction"); wasInTransaction = true; parentHandle = storageManager.OpenChainStateCursor(); try { parentCursor = parentHandle.Item; parentCursor.BeginTransaction(); inTransaction = true; this.chainTip = parentCursor.ChainTip; this.unspentTxCount = parentCursor.UnspentTxCount; this.unspentOutputCount = parentCursor.UnspentOutputCount; this.totalTxCount = parentCursor.TotalTxCount; this.totalInputCount = parentCursor.TotalInputCount; this.totalOutputCount = parentCursor.TotalOutputCount; this.readOnly = readOnly; } catch (Exception ex) { inTransaction = false; // ensure utxo applier has completed, but do not propagate exceptions ((IDataflowBlock)unspentTxes.WorkQueue).Fault(ex); ((IDataflowBlock)utxoApplier).Fault(ex); try { utxoApplier.Completion.Wait(); } catch (Exception) { } ((IDataflowBlock)unspentTxOutputs.WorkQueue).Fault(ex); ((IDataflowBlock)utxoApplier2).Fault(ex); try { utxoApplier2.Completion.Wait(); } catch (Exception) { } parentHandle.Dispose(); parentHandle = null; parentCursor = null; utxoApplier = null; throw; } }
public void BeginTransaction(bool readOnly, bool pruneOnly) { if (inTransaction) throw new InvalidOperationException(); parentHandle = storageManager.OpenChainStateCursor(); parentCursor = parentHandle.Item; changesApplied = false; try { parentCursor.BeginTransaction(); this.ChainTip = parentCursor.ChainTip; this.UnspentTxCount = UnspentTxCount; this.UnspentOutputCount = UnspentOutputCount; this.TotalTxCount = TotalTxCount; this.TotalInputCount = TotalInputCount; this.TotalOutputCount = TotalOutputCount; utxoApplier = new ActionBlock<WorkQueueDictionary<UInt256, UnspentTx>.WorkItem>( workItem => { workItem.Consume( (operation, unspentTxHash, unspentTx) => { switch (operation) { case WorkQueueOperation.Nothing: break; case WorkQueueOperation.Add: if (!parentCursor.TryAddUnspentTx(unspentTx)) throw new InvalidOperationException(); break; case WorkQueueOperation.Update: if (!parentCursor.TryUpdateUnspentTx(unspentTx)) throw new InvalidOperationException(); break; case WorkQueueOperation.Remove: if (!parentCursor.TryRemoveUnspentTx(unspentTxHash)) throw new InvalidOperationException(); break; default: throw new InvalidOperationException(); } }); }); unspentTxes.WorkQueue.LinkTo(utxoApplier, new DataflowLinkOptions { PropagateCompletion = true }); inTransaction = true; } finally { if (!inTransaction) { parentHandle.Dispose(); parentHandle = null; parentCursor = null; utxoApplier = null; } } }
public void RollbackTransaction() { if (!inTransaction) throw new InvalidOperationException(); parentCursor.RollbackTransaction(); parentHandle.Dispose(); parentHandle = null; parentCursor = null; utxoApplier = null; inTransaction = false; }