//private void PlayNew(MyWebRequest req, ArcStream url)
        //{
        //    string cid = url.source;
        //    if (url.Type != SourceType.ContentId)
        //    {
        //        TorrentStream ts1 = new TorrentStream(req.Client);
        //        ts1.Connect();
        //        var respData = ts1.ReadTorrent(url.source, url.Type);
        //        cid = ts1.GetContentId(respData);
        //    }
        //    var stream = _device.Proxy.Broadcaster.GetSource(cid);
        //    req.GetResponse().SendFile(stream);
        //}

        public override void Play(MyWebRequest req)
        {
            locker.isSet = true;
            int id = int.Parse(req.Parameters["id"].Split("#".ToCharArray(), 2)[0]);
            var url = new ArcStream(id).Run(_device.Proxy.SessionState.session);
            if (!url.IsSuccess)
            {
                while (!_device.Proxy.Login() && _device.Proxy.SessionState.Error == ApiError.noconnect)
                {

                }
                if (!_device.Proxy.SessionState.IsSuccess)
                    throw new Exception("No authorized");
                Play(req);
                return;
            }
            var ts = _device.Proxy.GetTsClient(url.source);
            Task<string> waiter;
            
            try
            {
                if (ts == null)
                {
                    if (!req.Client.Connected)
                        return;
                    ts = new TorrentStream(req.Client);
                    ts.Connect();
                    waiter = ts.Play(url.source, url.Type);

                    if (waiter != null)
                        _device.Proxy.AddToTsPool(ts);
                }
                else
                {
                    waiter = ts.GetPlayTask();
                    ts.Owner[0].Close();
                    ts.Owner.Add(req.Client);
                    ts.Owner.RemoveAt(0);
                
                }
                locker.isSet = false;
                if (waiter != null && !waiter.IsCompleted)
                    waiter.Wait();
                else if (waiter == null)
                    throw new FileNotFoundException();

                if (string.IsNullOrEmpty(waiter.Result))
                {
                    _device.Proxy.RemoveFromTsPoos(ts);
                }
                while (plaing) Thread.Sleep(256);
                plaing = true;
                req.GetResponse().SendFile(waiter.Result);
                plaing = false;
                do Thread.Sleep(512);
                while (locker.isSet); 
                if (ts.Owner.All(c => !c.Connected))
                {
                        
                    ts.Disconnect();
                    _device.Proxy.RemoveFromTsPoos(ts);
                    //GC.Collect();
                }
                
            }
            catch (Exception ex)
            {
                P2pProxyApp.Log.Write(ex.Message, TypeMessage.Error);
                ts.Disconnect();
                _device.Proxy.RemoveFromTsPoos(ts);
                plaing = false;
            }
            
        }
        //private void PlayNew(MyWebRequest req, ArcStream url)
        //{
        //    string cid = url.source;
        //    if (url.Type != SourceType.ContentId)
        //    {
        //        TorrentStream ts1 = new TorrentStream(req.Client);
        //        ts1.Connect();
        //        var respData = ts1.ReadTorrent(url.source, url.Type);
        //        cid = ts1.GetContentId(respData);
        //    }
        //    var stream = _device.Proxy.Broadcaster.GetSource(cid);
        //    req.GetResponse().SendFile(stream);
        //}

        public override void Play(MyWebRequest req)
        {
            locker.isSet = true;
            int id  = int.Parse(req.Parameters["id"].Split("#".ToCharArray(), 2)[0]);
            var url = new ArcStream(id).Run(_device.Proxy.SessionState.session);

            if (!url.IsSuccess)
            {
                while (!_device.Proxy.Login() && _device.Proxy.SessionState.Error == ApiError.noconnect)
                {
                }
                if (!_device.Proxy.SessionState.IsSuccess)
                {
                    throw new Exception("No authorized");
                }
                Play(req);
                return;
            }
            var           ts = _device.Proxy.GetTsClient(url.source);
            Task <string> waiter;

            try
            {
                if (ts == null)
                {
                    if (!req.Client.Connected)
                    {
                        return;
                    }
                    ts = new TorrentStream(req.Client);
                    ts.Connect();
                    waiter = ts.Play(url.source, url.Type);

                    if (waiter != null)
                    {
                        _device.Proxy.AddToTsPool(ts);
                    }
                }
                else
                {
                    waiter = ts.GetPlayTask();
                    ts.Owner[0].Close();
                    ts.Owner.Add(req.Client);
                    ts.Owner.RemoveAt(0);
                }
                locker.isSet = false;
                if (waiter != null && !waiter.IsCompleted)
                {
                    waiter.Wait();
                }
                else if (waiter == null)
                {
                    throw new FileNotFoundException();
                }

                if (string.IsNullOrEmpty(waiter.Result))
                {
                    _device.Proxy.RemoveFromTsPoos(ts);
                }
                while (plaing)
                {
                    Thread.Sleep(256);
                }
                plaing = true;
                req.GetResponse().SendFile(waiter.Result);
                plaing = false;
                do
                {
                    Thread.Sleep(512);
                }while (locker.isSet);
                if (ts.Owner.All(c => !c.Connected))
                {
                    ts.Disconnect();
                    _device.Proxy.RemoveFromTsPoos(ts);
                    //GC.Collect();
                }
            }
            catch (Exception ex)
            {
                P2pProxyApp.Log.Write(ex.Message, TypeMessage.Error);
                ts.Disconnect();
                _device.Proxy.RemoveFromTsPoos(ts);
                plaing = false;
            }
        }