Ejemplo n.º 1
0
 protected void Page_Load(object sender, EventArgs e)
 {
     try
     {
         StoreManager SM    = new StoreManager();
         StoreTBx     store = new StoreTBx();
         store.status     = 1;
         store.name       = Request["name"];
         store.phone      = Request["phone"];
         store.lat        = Request["lat"];
         store.@long      = Request["long"];
         store.email      = Request["email"];
         store.close_time = Request["close"];
         store.open_time  = Request["open"];
         store.address    = Request["address"];
         SM.Add(store);
         Response.Write(JsonConvert.SerializeObject(new
         {
             success = 1
         }));
     }
     catch (Exception ex)
     {
         Response.Write(JsonConvert.SerializeObject(new
         {
             success = -1,
             error   = ex
         }));
     }
 }
Ejemplo n.º 2
0
        private static void StartDownTorrent()
        {
            if (DownTaskList.Count >= MaxDownTaskNum)
            {
                return;
            }
            Task downTask = Task.Factory.StartNew(() =>
            {
                while (running)
                {
                    try
                    {
                        if (!DownLoadQueue.TryTake(out var info))
                        {
                            lock (InfoStore)
                            {
                                if (!DownLoadQueue.TryTake(out info))
                                {
                                    var infos = InfoStore.ReadLast(Math.Min(MaxDownTaskNum, DownLoadQueue.BoundedCapacity - DownLoadQueue.Count));
                                    if (infos.Count > 0)
                                    {
                                        for (int i = 0; i < infos.Count - 1; i++)
                                        {
                                            var item = infos[i];
                                            if (!DownLoadQueue.TryAdd(item))
                                            {
                                                InfoStore.Add(item);
                                            }
                                        }
                                        info = infos.Last();
                                    }
                                }
                            }
                        }
                        if (info == null)
                        {
                            if (!DownLoadQueue.TryTake(out info, DownTimeOutSeconds))
                            {
                                if (DownTaskList.Count > MinDownTaskNum)
                                {
                                    return;
                                }
                                continue;
                            }
                        }
                        if (DownlaodedSet.Contains(info.Value))
                        {
                            continue;
                        }
                        watchLog.Info($"thread {Task.CurrentId:x2} downloading {info.Value}");
                        foreach (var peer in info.Peers.Where(p => p.Address.IsPublic()))
                        {
                            if (DownlaodedSet.Contains(info.Value))
                            {
                                break;
                            }
                            var longPeer = peer.ToInt64();
                            try
                            {
                                if (BadAddress.TryGetValue(longPeer, out var expireTime))
                                {
                                    if (expireTime > DateTime.Now)
                                    {
                                        continue;
                                    }
                                    BadAddress.TryRemove(longPeer, out expireTime);
                                }
                                using (var client = new WireClient(peer))
                                {
                                    var meta = client.GetMetaData(new global::BitTorrent.InfoHash(info.Bytes), out var netError);
                                    if (meta == null)
                                    {
                                        if (netError)
                                        {
                                            BadAddress.AddOrUpdate(longPeer, DateTime.Now.AddDays(1),
                                                                   (ip, before) => DateTime.Now.AddDays(1));
                                        }
                                        continue;
                                    }
                                    DownlaodedSet.Add(info.Value);
                                    var torrent      = ParseBitTorrent(meta);
                                    torrent.InfoHash = info.Value;
                                    var subdirectory = TorrentDirectory.CreateSubdirectory(DateTime.Now.ToString("yyyy-MM-dd"));
                                    var path         = Path.Combine(subdirectory.FullName, torrent.InfoHash + ".json");
                                    var hasLock      = false;
                                    writeLock.Enter(ref hasLock);
                                    try
                                    {
                                        File.WriteAllText(Path.Combine(TorrentPath, path), torrent.ToJson());
                                        File.AppendAllText(DownloadInfoPath, torrent.InfoHash + Environment.NewLine);
                                    }
                                    finally
                                    {
                                        if (hasLock)
                                        {
                                            writeLock.Exit(false);
                                        }
                                    }
                                    watchLog.Info($"download {torrent.InfoHash} success");
                                }
                                break;
                            }
                            catch (SocketException)
                            {
                                BadAddress.AddOrUpdate(longPeer, DateTime.Now.AddDays(1), (ip, before) => DateTime.Now.AddDays(1));
                            }
                            catch (IOException)
                            {
                                BadAddress.AddOrUpdate(longPeer, DateTime.Now.AddDays(1), (ip, before) => DateTime.Now.AddDays(1));
                            }
                            catch (Exception ex)
                            {
                                log.Error("下载失败", ex);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("并行下载时错误", ex);
                    }
                }
            }, TaskCreationOptions.LongRunning);

            downTask.ContinueWith(t => DownTaskList.Remove(t));
            DownTaskList.Add(downTask);
        }