예제 #1
0
        public byte[] Serialize()
        {
            var dict = new BDictionary
            {
                ["msg_type"] = new BNumber((int)RequestType),
                ["piece"]    = new BNumber(PieceIndex)
            };

            if (RequestType == Type.Data)
            {
                dict["total_size"] = new BNumber(TotalSize);
            }

            using (var ms = new MemoryStream())
            {
                dict.EncodeTo(ms);

                if (RequestType == Type.Data)
                {
                    ms.Write(PieceData, 0, PieceData.Length);
                }

                return(ms.ToArray());
            }
        }
예제 #2
0
        /// <summary>
        /// Calculates the hash of the 'info'-dictionary.
        /// The info hash is a 20-byte SHA1 hash of the 'info'-dictionary of the torrent
        /// used to uniquely identify it and it's contents.
        ///
        /// <para>Example: 6D60711ECF005C1147D8973A67F31A11454AB3F5</para>
        /// </summary>
        /// <param name="info">The 'info'-dictionary of a torrent.</param>
        /// <returns>A byte-array of the 20-byte SHA1 hash.</returns>
        public static byte[] CalculateInfoHashBytes(BDictionary info)
        {
            using (var sha1 = SHA1.Create())
                using (var stream = new MemoryStream()) {
                    info.EncodeTo(stream);
                    stream.Position = 0;

                    return(sha1.ComputeHash(stream));
                }
        }
예제 #3
0
        public void SaveSession(BitArray pieces)
        {
            var    dictionary = new BDictionary();
            string state      = "";

            for (int i = 0; i < pieces.Count; i++)
            {
                state += pieces[i] ? "1" : "0";
            }
            dictionary.Add(Path.GetFileName(torrentCopyName), new BString(state));
            infoFile.SetLength(0);
            dictionary.EncodeTo(infoFile);
        }
예제 #4
0
        public byte[] Serialize()
        {
            var dict = new BDictionary
            {
                ["added"]   = EncodeEndPoints(Added),
                ["dropped"] = EncodeEndPoints(Dropped),
            };

            using (var ms = new MemoryStream())
            {
                dict.EncodeTo(ms);
                return(ms.ToArray());
            }
        }
예제 #5
0
        public async Task WriteToPipeWriter()
        {
            var dict = new BDictionary {
                { "key", "value" }
            };

            var(reader, writer) = new Pipe();

            dict.EncodeTo(writer);
            await writer.FlushAsync();

            reader.TryRead(out var readResult);

            var result = Encoding.UTF8.GetString(readResult.Buffer.First.Span.ToArray());

            result.Should().Be("d3:key5:valuee");
        }