本文整理汇总了C#中System.IO.BinaryReader.Read4Bytes方法的典型用法代码示例。如果您正苦于以下问题:C# BinaryReader.Read4Bytes方法的具体用法?C# BinaryReader.Read4Bytes怎么用?C# BinaryReader.Read4Bytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.BinaryReader
的用法示例。
在下文中一共展示了BinaryReader.Read4Bytes方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DecodeBlockHeader
public static BlockHeader DecodeBlockHeader(Stream stream, UInt256? blockHash = null)
{
using (var reader = new BinaryReader(stream, Encoding.ASCII, leaveOpen: true))
{
return new BlockHeader
(
version: reader.Read4Bytes(),
previousBlock: reader.Read32Bytes(),
merkleRoot: reader.Read32Bytes(),
time: reader.Read4Bytes(),
bits: reader.Read4Bytes(),
nonce: reader.Read4Bytes(),
hash: blockHash
);
}
}
示例2: DecodeGetBlocksPayload
public static GetBlocksPayload DecodeGetBlocksPayload(Stream stream)
{
using (var reader = new BinaryReader(stream, Encoding.ASCII, leaveOpen: true))
{
return new GetBlocksPayload
(
Version: reader.Read4Bytes(),
BlockLocatorHashes: reader.DecodeList(() => reader.Read32Bytes()),
HashStop: reader.Read32Bytes()
);
}
}
示例3: ReadBlockchain
public Data.Blockchain ReadBlockchain(BlockchainKey blockchainKey)
{
CheckDatabaseFolder();
var blockListBuilder = ImmutableList.CreateBuilder<ChainedBlock>();
var utxoBuilder = ImmutableDictionary.CreateBuilder<UInt256, UnspentTx>();
var connString = @"Server=localhost; Database=BitSharp_Blockchains; Trusted_Connection=true;";
using (var conn = new SqlConnection(connString))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = @"
SELECT BlockHash, PreviousBlockHash, Height, TotalWork
FROM ChainedBlocks
WHERE Guid = @guid AND RootBlockHash = @rootBlockHash
ORDER BY Height ASC";
cmd.Parameters.SetValue("@guid", SqlDbType.Binary, 16).Value = blockchainKey.Guid.ToByteArray();
cmd.Parameters.SetValue("@rootBlockHash", SqlDbType.Binary, 32).Value = blockchainKey.RootBlockHash.ToDbByteArray();
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var blockHash = reader.GetUInt256(0);
var previousBlockHash = reader.GetUInt256(1);
var height = reader.GetInt32(2);
var totalWork = reader.GetBigInteger(3);
blockListBuilder.Add(new ChainedBlock(blockHash, previousBlockHash, height, totalWork));
}
}
}
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = @"
SELECT UtxoChunkBytes
FROM UtxoData
WHERE Guid = @guid AND RootBlockHash = @rootBlockHash";
cmd.Parameters.SetValue("@guid", SqlDbType.Binary, 16).Value = blockchainKey.Guid.ToByteArray();
cmd.Parameters.SetValue("@rootBlockHash", SqlDbType.Binary, 32).Value = blockchainKey.RootBlockHash.ToDbByteArray();
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var chunkBytes = reader.GetBytes(0);
var chunkStream = new MemoryStream(chunkBytes);
using (var chunkReader = new BinaryReader(chunkStream))
{
var chunkLength = chunkReader.Read4Bytes().ToIntChecked();
var outputs = new TxOutputKey[chunkLength];
for (var i = 0; i < chunkLength; i++)
{
var prevTxHash = chunkReader.Read32Bytes();
var prevTxOutputIndex = chunkReader.Read4Bytes();
outputs[i] = new TxOutputKey(prevTxHash, prevTxOutputIndex);
}
//TODO utxoBuilder.UnionWith(outputs);
}
}
}
}
}
return new Data.Blockchain(blockListBuilder.ToImmutable(), blockListBuilder.Select(x => x.BlockHash).ToImmutableHashSet(), utxoBuilder.ToImmutable());
}
示例4: DecodeInventoryVector
public static InventoryVector DecodeInventoryVector(Stream stream)
{
using (var reader = new BinaryReader(stream, Encoding.ASCII, leaveOpen: true))
{
return new InventoryVector
(
Type: reader.Read4Bytes(),
Hash: reader.Read32Bytes()
);
}
}
示例5: DecodeVersionPayload
public static VersionPayload DecodeVersionPayload(Stream stream, int payloadLength)
{
using (var reader = new BinaryReader(stream, Encoding.ASCII, leaveOpen: true))
{
var position = stream.Position;
var versionPayload = new VersionPayload
(
ProtocolVersion: reader.Read4Bytes(),
ServicesBitfield: reader.Read8Bytes(),
UnixTime: reader.Read8Bytes(),
RemoteAddress: DecodeNetworkAddress(stream),
LocalAddress: DecodeNetworkAddress(stream),
Nonce: reader.Read8Bytes(),
UserAgent: reader.ReadVarString(),
StartBlockHeight: reader.Read4Bytes(),
Relay: false
);
var readCount = stream.Position - position;
if (versionPayload.ProtocolVersion >= VersionPayload.RELAY_VERSION && payloadLength - readCount == 1)
versionPayload = versionPayload.With(Relay: reader.ReadBool());
return versionPayload;
}
}
示例6: DecodeTransaction
public static Transaction DecodeTransaction(Stream stream, UInt256? txHash = null)
{
using (var reader = new BinaryReader(stream, Encoding.ASCII, leaveOpen: true))
{
return new Transaction
(
version: reader.Read4Bytes(),
inputs: reader.DecodeList(() => DecodeTxInput(stream)),
outputs: reader.DecodeList(() => DecodeTxOutput(stream)),
lockTime: reader.Read4Bytes(),
hash: txHash
);
}
}
示例7: DecodeTxInput
public static TxInput DecodeTxInput(Stream stream)
{
using (var reader = new BinaryReader(stream, Encoding.ASCII, leaveOpen: true))
{
return new TxInput
(
previousTxOutputKey: new TxOutputKey
(
txHash: reader.Read32Bytes(),
txOutputIndex: reader.Read4Bytes()
),
scriptSignature: reader.ReadVarBytes().ToImmutableArray(),
sequence: reader.Read4Bytes()
);
}
}
示例8: DecodeNetworkAddressWithTime
public static NetworkAddressWithTime DecodeNetworkAddressWithTime(Stream stream)
{
using (var reader = new BinaryReader(stream, Encoding.ASCII, leaveOpen: true))
{
return new NetworkAddressWithTime
(
Time: reader.Read4Bytes(),
NetworkAddress: DecodeNetworkAddress(stream)
);
}
}
示例9: DecodeMessage
public static Message DecodeMessage(Stream stream)
{
using (var reader = new BinaryReader(stream, Encoding.ASCII, leaveOpen: true))
{
var magic = reader.Read4Bytes();
var command = reader.ReadFixedString(12);
var payloadSize = reader.Read4Bytes();
var payloadChecksum = reader.Read4Bytes();
var payload = reader.ReadBytes(payloadSize.ToIntChecked()).ToImmutableArray();
return new Message
(
Magic: magic,
Command: command,
PayloadSize: payloadSize,
PayloadChecksum: payloadChecksum,
Payload: payload
);
}
}
示例10: WireDecodeMessage
private Message WireDecodeMessage(UInt32 magic, Stream stream)
{
byte[] payload;
Message message;
using (var reader = new BinaryReader(stream, Encoding.ASCII, leaveOpen: true))
{
var command = reader.ReadFixedString(12);
var payloadSize = reader.Read4Bytes();
var payloadChecksum = reader.Read4Bytes();
payload = reader.ReadBytes(payloadSize.ToIntChecked());
if (!Messaging.VerifyPayloadChecksum(payloadChecksum, payload))
throw new Exception(string.Format("Checksum failed for {0}", command));
message = new Message
(
Magic: magic,
Command: command,
PayloadSize: payloadSize,
PayloadChecksum: payloadChecksum,
Payload: payload.ToImmutableArray()
);
}
switch (message.Command)
{
case "addr":
{
var addressPayload = NetworkEncoder.DecodeAddressPayload(payload.ToMemoryStream());
var handler = this.OnReceivedAddresses;
if (handler != null)
handler(addressPayload.NetworkAddresses);
}
break;
case "alert":
{
var alertPayload = NetworkEncoder.DecodeAlertPayload(payload.ToMemoryStream());
}
break;
case "block":
{
var block = NetworkEncoder.DecodeBlock(payload.ToMemoryStream());
var handler = this.OnBlock;
if (handler != null)
handler(block);
}
break;
case "getblocks":
{
var getBlocksPayload = NetworkEncoder.DecodeGetBlocksPayload(payload.ToMemoryStream());
var handler = this.OnGetBlocks;
if (handler != null)
handler(getBlocksPayload);
}
break;
case "getheaders":
{
var getHeadersPayload = NetworkEncoder.DecodeGetBlocksPayload(payload.ToMemoryStream());
var handler = this.OnGetHeaders;
if (handler != null)
handler(getHeadersPayload);
}
break;
case "headers":
{
var headerStream = payload.ToMemoryStream();
using (var reader = new BinaryReader(headerStream))
{
var headerCount = reader.ReadVarInt().ToIntChecked();
for (var i = 0; i < headerCount; i++)
{
var blockHeader = NetworkEncoder.DecodeBlockHeader(headerStream);
//TODO wiki says this is a byte and a var int, which is it?
var txCount = reader.ReadVarInt();
var handler = this.OnBlockHeader;
if (handler != null)
handler(blockHeader);
}
}
}
break;
case "inv":
{
var invPayload = NetworkEncoder.DecodeInventoryPayload(payload.ToMemoryStream());
var handler = this.OnInventoryVectors;
if (handler != null)
//.........这里部分代码省略.........
示例11: ExecuteOps
private bool ExecuteOps(ImmutableArray<byte> scriptPubKey, Transaction tx, int inputIndex, byte[] script, out Stack stack, out Stack altStack)
{
stack = new Stack();
altStack = new Stack();
using (var opReader = new BinaryReader(script.ToMemoryStream()))
{
while (opReader.BaseStream.Position < script.Length)
{
var opByte = opReader.ReadByte();
var op = (ScriptOp)Enum.ToObject(typeof(ScriptOp), opByte);
//logger.LogTrace("Executing {0} with stack count: {1}", OpName(opByte), stack.Count);
switch (op)
{
// Constants
case ScriptOp.OP_PUSHDATA1:
{
if (opReader.BaseStream.Position + 1 >= script.Length)
return false;
var length = opReader.ReadByte();
stack.PushBytes(opReader.ReadBytes(length));
}
break;
case ScriptOp.OP_PUSHDATA2:
{
if (opReader.BaseStream.Position + 2 >= script.Length)
return false;
var length = opReader.Read2Bytes();
stack.PushBytes(opReader.ReadBytes(length));
}
break;
case ScriptOp.OP_PUSHDATA4:
{
if (opReader.BaseStream.Position + 4 >= script.Length)
return false;
var length = opReader.Read4Bytes();
stack.PushBytes(opReader.ReadBytes(length.ToIntChecked()));
}
break;
// Flow control
case ScriptOp.OP_NOP:
{
}
break;
// Stack
case ScriptOp.OP_DROP:
{
if (stack.Count < 1)
return false;
var value = stack.PopBytes();
//logger.LogTrace("{0} dropped {1}", OpName(opByte), value);
}
break;
case ScriptOp.OP_DUP:
{
if (stack.Count < 1)
return false;
var value = stack.PeekBytes();
stack.PushBytes(value);
//logger.LogTrace("{0} duplicated {2}", OpName(opByte), value);
}
break;
// Splice
// Bitwise logic
case ScriptOp.OP_EQUAL:
case ScriptOp.OP_EQUALVERIFY:
{
if (stack.Count < 2)
return false;
var value1 = stack.PopBytes();
var value2 = stack.PopBytes();
var result = value1.SequenceEqual(value2);
stack.PushBool(result);
// logger.LogTrace(
//@"{0} compared values:
//value1: {1}
//value2: {2}
//result: {3}", OpName(opByte), value1, value2, result);
if (op == ScriptOp.OP_EQUALVERIFY)
{
//.........这里部分代码省略.........