Exemplo n.º 1
0
        public void Connecting(TorrentModel model)
        {
            var ls = new List <AnnounceItem>();

            ls.AddRange(model.Announce_list.SelectMany(m => m));
            if (!string.IsNullOrEmpty(model.Announce.Url))
            {
                ls.Add(model.Announce);
            }


            var us = ls.Where(m => m.Url.StartsWith("udp"));

            _logger.LogInformation("udp announce数量:" + us.Count());
            foreach (var item in us)
            {
                var u  = new Uri(item.Url);
                var ip = GetIp(u);
                if (ip == null)
                {
                    continue;
                }
                IPEndPoint iPEndPoint = new IPEndPoint(ip, u.Port);
                var        ids        = ConnecttionId_TransactionId.CreateNext();
                _logger.LogInformation($"对{item.Url}发送请求,Ids:{ids}");
                _socket.SendTo(ids.ToArray(), iPEndPoint);


                _dic.Add(ids, model);
                _connectingLs.Add(new ReplayItem {
                    Ids = ids, EndPoint = iPEndPoint
                }, 0);
            }
        }
Exemplo n.º 2
0
        private void HandleTrack(byte[] buf, int receiveLen, EndPoint remoteEP)
        {
            using (var ms = new MemoryStream(buf, 0, receiveLen))
                using (var br = new BinaryReader(ms))
                {
                    var action = (ActionsType)IPAddress.NetworkToHostOrder(br.ReadInt32());
                    if (action == ActionsType.Connect)
                    {
                        var transaction_id = IPAddress.NetworkToHostOrder(br.ReadInt32());
                        var connection_id  = IPAddress.NetworkToHostOrder(br.ReadInt64());
                        _logger.LogInformation($"收到字节长度:{receiveLen},i32:{transaction_id},i64:{connection_id}");

                        var ids = ConnecttionId_TransactionId.Create(transaction_id, connection_id);
                        if (_dic.ContainsKey(ids))
                        {
                            var model = _dic[ids];
                            //_logger.LogInformation(model.Info.Name ?? model.Info.Files.FirstOrDefault()?.Path.FirstOrDefault());

                            Announcing(model, connection_id, transaction_id, remoteEP);
                            //Scraping(model, connection_id, transaction_id, remoteEP);
                        }
                        var rk = new ReplayItem()
                        {
                            EndPoint = remoteEP, Ids = ids
                        };
                        if (_connectingLs.ContainsKey(rk))
                        {
                            //_logger.LogInformation("已获取udp返回的值,从循环数据源删除相关数据");
                            _connectingLs.Remove(rk);
                        }
                        _connectingLs.Remove(new ReplayItem {
                            Ids = ids, EndPoint = remoteEP
                        });
                    }
                    else if (action == ActionsType.Announce)
                    {
                        var transaction_id = IPAddress.NetworkToHostOrder(br.ReadInt32());
                        var interval       = IPAddress.NetworkToHostOrder(br.ReadInt32());
                        var leechers       = IPAddress.NetworkToHostOrder(br.ReadInt32());
                        var seeders        = IPAddress.NetworkToHostOrder(br.ReadInt32());

                        var ls = new List <IPEndPoint>();
                        while (ms.Position != ms.Length)
                        {
                            var ip         = br.ReadBytes(4);
                            var port       = br.ReadUInt16();
                            var ipendpoint = new IPEndPoint(new IPAddress(ip), port);
                            ls.Add(ipendpoint);
                        }
                        _logger.LogInformation("收到响应 transaction_id:" + transaction_id + ",ip-" + ls.Aggregate("", (s, i) => s + i + ";"));
                        var ids = ConnecttionId_TransactionId.Create(transaction_id);
                        if (_dic.ContainsKey(ids))
                        {
                            var model = _dic[ids];
                            var tr    = new TrackerResponse(remoteEP as IPEndPoint)
                            {
                                Peers      = ls.ToArray(),
                                Interval   = interval,
                                Complete   = seeders,
                                Incomplete = leechers
                            };
                            model.Download(tr);
                            IsOk = true;
                            _dic.Remove(ids);

                            if (!model.IsFinish)
                            {
                                _ = Task.Delay(TimeSpan.FromSeconds(tr.Interval))
                                    .ContinueWith(t =>
                                {
                                    _socket.SendTo(ids.ToArray(), remoteEP);
                                    _dic.Add(ids, model);
                                    _connectingLs.Add(new ReplayItem {
                                        Ids = ids, EndPoint = remoteEP
                                    }, 0);
                                });
                            }
                        }
                    }
                    else if (action == ActionsType.Scrape)
                    {
                        var transaction_id = IPAddress.NetworkToHostOrder(br.ReadInt32());
                        var ids            = ConnecttionId_TransactionId.Create(transaction_id);

                        var complete   = IPAddress.NetworkToHostOrder(br.ReadInt32());
                        var downloaded = IPAddress.NetworkToHostOrder(br.ReadInt32());
                        var incomplete = IPAddress.NetworkToHostOrder(br.ReadInt32());

                        // todo do something for Scrape
                    }
                    else if (action == ActionsType.Error)
                    {
                        br.ReadInt32();
                        var  i1        = br.BaseStream.Length;
                        var  i2        = br.BaseStream.Position;
                        bool ishavestr = i1 != i2;
                        if (ishavestr)
                        {
                            var strbuf = br.ReadBytes((int)(i1 - i2));
                            var errMsg = Encoding.UTF8.GetString(strbuf);
                            _logger.LogWarnning($"{remoteEP}:udp errmsg:" + errMsg);
                        }
                    }
                    else
                    {
                        _logger.LogInformation("收到某些响应。。。");
                    }
                }
        }