Exemple #1
0
        /// <summary>
        /// Parse meta-info from torrent file.
        /// </summary>
        /// <param name="fileName">The filename of torrent</param>
        /// <returns>Return the metainfo</returns>
        public static MetaInfo Parse(string fileName)
        {
            MetaInfo result = null;
            DictNode rootNode;

            byte[] source;

            if (File.Exists(fileName))
            {
                source   = File.ReadAllBytes(fileName);
                rootNode = BEncodingFactory.Decode(source) as DictNode;
                Debug.Assert(rootNode != null);
            }
            else
            {
                return(null);
            }

            DictNode infoNode = rootNode["info"] as DictNode;

            Debug.Assert(infoNode != null);

            if (infoNode.ContainsKey("length"))
            {
                result = GetSingleFileMetaInfo(rootNode, infoNode);
            }
            else
            {
                result = GetMultiFileMetaInfo(rootNode, infoNode);
            }

            result.InfoHash = Globals.GetSha1Hash(BEncodingFactory.ByteArrayEncode(rootNode["info"]));//Globals.Sha1.ComputeHash(BEncodingFactory.ByteArrayEncode(rootNode["info"]));

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Node字典类的解码函数
        /// </summary>
        /// <param name="source">待解码的字节数组</param>
        /// <param name="index">字节数组的解码位置</param>
        /// <returns>解码的字节数组长度</returns>
        public override int Decode(byte[] source, ref int index)
        {
            //保存初始位置
            int start = index;

            //跳过字符'd'
            index++;

            try
            {
                //当遇到'e'(ASCII码为101),解析结束
                while (source[index] != 101)
                {
                    //解析字符串
                    BytesNode keyNode = new BytesNode();
                    keyNode.Decode(source, ref index);
                    if (keyNode.ByteArray.LongLength == 0)
                    {
                        throw new BitTorrentException("待添加的字符串长度为0");
                    }

                    //解析Handler
                    BEncodedNode valueNode = BEncodingFactory.Decode(source, ref index);

                    //'e'(ASCII码为101),解析结束
                    if (valueNode == null)
                    {
                        throw new BitTorrentException("待添加的Handler节点为空");
                    }

                    //字典添加key和value
                    _dict.Add(keyNode.StringText, valueNode);
                }
            }

            //当捕捉IndexOutOfRangeException,抛出BitTorrentException
            catch (IndexOutOfRangeException)
            {
                throw new BitTorrentException("BEncode字典类的字符串长度异常");
            }

            //当捕捉ArgumentException,抛出BitTorrentException
            catch (ArgumentException)
            {
                throw new BitTorrentException("BEncode字典类中包含相同的字符串关键字");
            }

            //跳过字符'e'
            index++;

            //返回所解析的数组长度
            return(index - start);
        }
Exemple #3
0
        /// <summary>
        /// Handler列表类的解码函数
        /// </summary>
        /// <param name="source">待解码的字节数组</param>
        /// <param name="index">字节数组的解码位置</param>
        /// <returns>解码的字节数组长度</returns>
        public override int Decode(byte[] source, ref int index)
        {
            //保存初始位置
            int start = index;

            //跳过字符'l'
            index++;

            try
            {
                //当遇到'e'(ASCII码为101),解析结束
                while (source[index] != 101)
                {
                    BEncodedNode node = BEncodingFactory.Decode(source, ref index);

                    //当遇到'e'(ASCII码为101),解析结束
                    if (node == null)
                    {
                        break;
                    }

                    //列表添加handler
                    _items.Add(node);
                }
            }

            //当捕捉IndexOutOfRangeException,抛出BitTorrentException
            catch (IndexOutOfRangeException)
            {
                throw new BitTorrentException("BEnocde列表类的字节数组长度异常");
            }

            //跳过字符'e'
            index++;

            //返回所解析的数组长度
            return(index - start);
        }
Exemple #4
0
 public override string ToString()
 {
     return(BEncodingFactory.StringEncode(this));
 }