private void TakeFromPeerPool(CancellationToken token) { PeerHandler handler = null; while (handler == null) { var peer = _peerPool.Acquire(token); if (peer == null) { _myPool.Add(null); break; } if (!_connectedIps.Contains(peer.IpAddress.ToString())) { handler = ConnectTo(peer); if (handler != null) { handler.SendBitfield(_client.PieceStorage.GetValidPieces().ToByteArray()); AwaitBitfield(handler); handler.StartListening(); return; } else { _peerPool.Realese(peer); } } } }
public void WaitAndRealese(PeerHandler handler, IResourcePool <PeerHandler> returnPool, int waitSeconds) { Task.Factory.StartNew(() => { PeerHandler h = handler; Thread.Sleep(waitSeconds); returnPool.Realese(h); }); }
public void AwaitHandler(ManualResetEventSlim eventReset, PeerHandler handler, IResourcePool <PeerHandler> returnPool, int awaitSeconds, int triesCount = 1) { Task.Factory.StartNew(() => { PeerHandler h = handler; for (int i = 0; i < triesCount; i++) { if (eventReset.Wait(awaitSeconds * 1000)) { returnPool.Realese(h); return; } } returnPool.Dispose(h); }); }