1; // extensions length public byte[] Encode() { var size = MinEncodedSize; if (ExtensionIds != null) { foreach (var extensionId in ExtensionIds) { size += 1 + extensionId.Length; } } byte[] data = new byte[size]; P2ptpCommon.EncodeHeader(data, PacketType.hello); int index = P2ptpCommon.HeaderSize; PeerId.Encode(FromPeerId, data, ref index); StreamId.Encode(StreamId, data, ref index); PeerId.Encode(ToPeerId, data, ref index); P2ptpCommon.EncodeUInt32(data, ref index, LibraryVersion); P2ptpCommon.EncodeUInt16(data, ref index, ProtocolVersion); data[index++] = (byte)Status; P2ptpCommon.EncodeUInt32(data, ref index, RequestTime32); data[index++] = RoleFlags; data[index++] = (byte)(ExtensionIds?.Length ?? 0); if (ExtensionIds != null) { foreach (var extensionId in ExtensionIds) { P2ptpCommon.EncodeString1ASCII(data, ref index, extensionId); } } return(data); }
public static int InitializePayloadPacketForExtension(byte[] extensionHeader, byte[] data, StreamId streamId) { Array.Copy(extensionHeader, data, extensionHeader.Length); var index = PacketHeaders.SubtPayload.Length; StreamId.Encode(streamId, data, ref index); return(index); }
internal void Encode(byte[] data, ref int index) { StreamId.Encode(FromSocketAtStreamId, data, ref index); PeerId.Encode(ToPeerId, data, ref index); if (ToEndPoint.Address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork) { throw new NotSupportedException("only IPv4 is supported"); } #pragma warning disable CS0618 // Type or member is obsolete BinaryProcedures.EncodeUInt32(data, ref index, (uint)ToEndPoint.Address.Address); #pragma warning restore CS0618 // Type or member is obsolete BinaryProcedures.EncodeUInt16(data, ref index, (ushort)ToEndPoint.Port); }
public byte[] Encode() { var data = new byte[EncodedSizeMinimum + SharedPeers.Length * PeersListPacket_SharedPeerIpv4.EncodedSize]; P2ptpCommon.EncodeHeader(data, PacketTypes.peersListIpv4); var index = P2ptpCommon.HeaderSize; PeerId.Encode(FromPeerId, data, ref index); StreamId.Encode(StreamId, data, ref index); data[index++] = (byte)SharedPeers.Length; foreach (var peer in SharedPeers) { peer.Encode(data, ref index); } return(data); }
//const int MinEncodedSize = P2ptpCommon.HeaderSize + // PeerId.EncodedSize + StreamId.EncodedSize + // PeerId.EncodedSize + // 4 + 2 + // library, protocol version // 1 + // status // 4 + // requesttime // 1 + // role flags // 1; // extensions length //const int OptionalEncodedSize = 2 + // RequestSequenceNumber // 2 +//ResponseCpuDelayMs // 1; // RequestedFromIp length public byte[] Encode() { // var size = MinEncodedSize; // if (ExtensionIds != null) // foreach (var extensionId in ExtensionIds) // size += 1 + extensionId.Length; var requestedFromIp = RequestedFromIp ?? ""; FlagIshareMyIpLocation = (IpLocationData != null); // size += OptionalEncodedSize + requestedFromIp.Length; // byte[] data = new byte[size]; PacketProcedures.CreateBinaryWriter(out var ms, out var writer); P2ptpCommon.EncodeHeader(writer, PacketTypes.hello); // int index = P2ptpCommon.HeaderSize; PeerId.Encode(writer, FromPeerId); StreamId.Encode(writer, StreamId); PeerId.Encode(writer, ToPeerId); writer.Write(LibraryVersion); writer.Write(ProtocolVersion); writer.Write((byte)Status); writer.Write(RequestTime32); writer.Write(Flags); writer.Write((byte)(ExtensionIds?.Length ?? 0)); if (ExtensionIds != null) { foreach (var extensionId in ExtensionIds) { PacketProcedures.EncodeString1ASCII(writer, extensionId); } } writer.Write(RequestSequenceNumber ?? 0); writer.Write(ResponseCpuDelayMs ?? 0); PacketProcedures.EncodeString1ASCII(writer, requestedFromIp); if (FlagIshareMyIpLocation) { IpLocationData.Encode(writer); } return(ms.ToArray()); }
public static void InitializeExtensionSignalingPacket(BinaryWriter writer, PeerId fromPeerId, PeerId toPeerId, StreamId streamId, string extensionId) { if (fromPeerId == null) { throw new ArgumentNullException(nameof(fromPeerId)); } if (toPeerId == null) { throw new ArgumentNullException(nameof(toPeerId)); } if (streamId == null) { throw new ArgumentNullException(nameof(streamId)); } P2ptpCommon.EncodeHeader(writer, PacketType.extensionSignaling); PeerId.Encode(writer, fromPeerId); PeerId.Encode(writer, toPeerId); StreamId.Encode(writer, streamId); P2ptpCommon.EncodeString1ASCII(writer, extensionId); }