public static byte[] EncodeVersionPayload(VersionPayload versionPayload, bool withRelay) { using (var stream = new MemoryStream()) using (var writer = new BinaryWriter(stream)) { EncodeVersionPayload(writer, versionPayload, withRelay); return(stream.ToArray()); } }
public static void EncodeVersionPayload(BinaryWriter writer, VersionPayload versionPayload, bool withRelay) { writer.WriteUInt32(versionPayload.ProtocolVersion); writer.WriteUInt64(versionPayload.ServicesBitfield); writer.WriteUInt64((ulong)versionPayload.Time.ToUnixTimeSeconds()); EncodeNetworkAddress(writer, versionPayload.RemoteAddress); EncodeNetworkAddress(writer, versionPayload.LocalAddress); writer.WriteUInt64(versionPayload.Nonce); writer.WriteVarString(versionPayload.UserAgent); writer.WriteUInt32(versionPayload.StartBlockHeight); if (withRelay) { writer.WriteBool(versionPayload.Relay); } }
public static void EncodeVersionPayload(Stream stream, VersionPayload versionPayload, bool withRelay) { using (var writer = new BinaryWriter(stream, Encoding.ASCII, leaveOpen: true)) { writer.Write4Bytes(versionPayload.ProtocolVersion); writer.Write8Bytes(versionPayload.ServicesBitfield); writer.Write8Bytes(versionPayload.UnixTime); EncodeNetworkAddress(stream, versionPayload.RemoteAddress); EncodeNetworkAddress(stream, versionPayload.LocalAddress); writer.Write8Bytes(versionPayload.Nonce); writer.WriteVarString(versionPayload.UserAgent); writer.Write4Bytes(versionPayload.StartBlockHeight); if (withRelay) { writer.WriteBool(versionPayload.Relay); } } }
public static byte[] EncodeVersionPayload(VersionPayload versionPayload, bool withRelay) { var stream = new MemoryStream(); EncodeVersionPayload(stream, versionPayload, withRelay); return stream.ToArray(); }
public static void EncodeVersionPayload(Stream stream, VersionPayload versionPayload, bool withRelay) { using (var writer = new BinaryWriter(stream, Encoding.ASCII, leaveOpen: true)) { writer.Write4Bytes(versionPayload.ProtocolVersion); writer.Write8Bytes(versionPayload.ServicesBitfield); writer.Write8Bytes(versionPayload.UnixTime); EncodeNetworkAddress(stream, versionPayload.RemoteAddress); EncodeNetworkAddress(stream, versionPayload.LocalAddress); writer.Write8Bytes(versionPayload.Nonce); writer.WriteVarString(versionPayload.UserAgent); writer.Write4Bytes(versionPayload.StartBlockHeight); if (withRelay) writer.WriteBool(versionPayload.Relay); } }
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; } }