예제 #1
0
                                   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);
        }
예제 #2
0
        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
            P2ptpCommon.EncodeUInt32(data, ref index, (uint)ToEndPoint.Address.Address);
#pragma warning restore CS0618 // Type or member is obsolete
            P2ptpCommon.EncodeUInt16(data, ref index, (ushort)ToEndPoint.Port);
        }