当前位置: 首页>>代码示例>>C#>>正文


C# String.Aggregate方法代码示例

本文整理汇总了C#中String.Aggregate方法的典型用法代码示例。如果您正苦于以下问题:C# String.Aggregate方法的具体用法?C# String.Aggregate怎么用?C# String.Aggregate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在String的用法示例。


在下文中一共展示了String.Aggregate方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Scrape

        public IDictionary<String, ScrapeInfo> Scrape(String url, String[] hashes)
        {
            Dictionary<String, ScrapeInfo> returnVal = new Dictionary<string, ScrapeInfo>();

            ValidateInput(url, hashes, ScraperType.UDP);

            Int32 trasactionId = Random.Next(0, 65535);

            UdpClient udpClient = new UdpClient(Tracker, Port)
                {
                    Client =
                        {
                            SendTimeout = Timeout*1000, 
                            ReceiveTimeout = Timeout*1000
                        }
                };

            byte[] sendBuf = _currentConnectionId.Concat(Pack.Int32(0)).Concat(Pack.Int32(trasactionId)).ToArray();
            udpClient.Send(sendBuf, sendBuf.Length);

            IPEndPoint endPoint = null;
            byte[] recBuf = udpClient.Receive(ref endPoint);

            if(recBuf == null) throw new NoNullAllowedException("udpClient failed to receive");
            if(recBuf.Length < 0) throw new InvalidOperationException("udpClient received no response");
            if(recBuf.Length < 16) throw new InvalidOperationException("udpClient did not receive entire response");

            UInt32 recAction = Unpack.UInt32(recBuf, 0, Unpack.Endianness.Big);
            UInt32 recTrasactionId = Unpack.UInt32(recBuf, 4, Unpack.Endianness.Big);

            if (recAction != 0 || recTrasactionId != trasactionId)
            {
                throw new Exception("Invalid response from tracker");
            }

            _currentConnectionId = CopyBytes(recBuf, 8, 8);

            byte[] hashBytes = new byte[0];
            hashBytes = hashes.Aggregate(hashBytes, (current, hash) => current.Concat(Pack.Hex(hash)).ToArray());

            int expectedLength = 8 + (12 * hashes.Length);

            sendBuf = _currentConnectionId.Concat(Pack.Int32(2)).Concat(Pack.Int32(trasactionId)).Concat(hashBytes).ToArray();
            udpClient.Send(sendBuf, sendBuf.Length);

            recBuf = udpClient.Receive(ref endPoint);

            if (recBuf == null) throw new NoNullAllowedException("udpClient failed to receive");
            if (recBuf.Length < 0) throw new InvalidOperationException("udpClient received no response");
            if (recBuf.Length < expectedLength) throw new InvalidOperationException("udpClient did not receive entire response");

            recAction = Unpack.UInt32(recBuf, 0, Unpack.Endianness.Big);
            recTrasactionId = Unpack.UInt32(recBuf, 4, Unpack.Endianness.Big);

            _currentConnectionId = CopyBytes(recBuf, 8, 8);

            if (recAction != 2 || recTrasactionId != trasactionId)
            {
                throw new Exception("Invalid response from tracker");
            }

            Int32 startIndex = 8;
            foreach (String hash in hashes)
            {
                UInt32 seeders = Unpack.UInt32(recBuf, startIndex, Unpack.Endianness.Big);
                UInt32 completed = Unpack.UInt32(recBuf, startIndex + 4, Unpack.Endianness.Big);
                UInt32 leachers = Unpack.UInt32(recBuf, startIndex + 8, Unpack.Endianness.Big);

                returnVal.Add(hash, new ScrapeInfo(seeders, completed, leachers, ScraperType.UDP));

                startIndex += 12;
            }

            udpClient.Close();

            return returnVal;
        }
开发者ID:alex-kir,项目名称:System.Net.Torrent,代码行数:77,代码来源:UDPTrackerClient.cs


注:本文中的String.Aggregate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。