protected override void AppendBitfieldMessage(PeerId id, MessageBundle bundle) { if (id.SupportsFastPeer) bundle.Messages.Add(new HaveNoneMessage()); else bundle.Messages.Add(new BitfieldMessage(zero)); }
public void ChunkedRequest() { if (requests.Messages.Count != 0) rig.Manager.PieceManager.Picker.CancelRequests(id); requests = rig.Manager.PieceManager.Picker.PickPiece(id, new List<PeerId>(), 256); byte[] sendBuffer = requests.Encode(); int offset = 0; amountSent = Math.Min(sendBuffer.Length - offset, 2048); IAsyncResult sendResult = connection.BeginSend(sendBuffer, offset, amountSent, null, null); while (sendResult.AsyncWaitHandle.WaitOne(10, true)) { Assert.AreEqual(amountSent, connection.EndSend(sendResult), "#1." + amountSent); offset += amountSent; amountSent = Math.Min(sendBuffer.Length - offset, 2048); if (amountSent == 0) Assert.Fail("This should never happen"); sendResult = connection.BeginSend(sendBuffer, offset, amountSent, null, null); } byte[] buffer = new byte[1024 * 1024 * 3]; IAsyncResult receiveResult = connection.BeginReceive(buffer, 0, 4, null, null); CompleteSendOrReceiveFirst(buffer, receiveResult, sendResult); }
public void Setup() { requestedUrl.Clear(); partialData = false; int i; for (i = 0; i < 1000; i++) { try { listener = new HttpListener(); listener.Prefixes.Add(string.Format(listenerURL, i)); listener.Start(); break; } catch { } } listener.BeginGetContext(GotContext, null); rig = TestRig.CreateMultiFile(); connection = new HttpConnection(new Uri(string.Format(listenerURL, i))); connection.Manager = rig.Manager; id = new PeerId(new Peer("this is my id", connection.Uri), rig.Manager); id.Connection = connection; id.IsChoking = false; id.AmInterested = true; id.BitField.SetAll(true); id.MaxPendingRequests = numberOfPieces; requests = rig.Manager.PieceManager.Picker.PickPiece(id, new List<PeerId>(), numberOfPieces); }
public void AddConnectionToStoppedManager() { var bundle = new MessageBundle(); // Create the handshake and bitfield message bundle.Messages.Add(new HandshakeMessage(rig.Manager.InfoHash, "11112222333344445555", VersionInfo.ProtocolStringV100)); bundle.Messages.Add(new BitfieldMessage(rig.Torrent.Pieces.Count)); var data = bundle.Encode(); // Add the 'incoming' connection to the engine and send our payload rig.Listener.Add(rig.Manager, conn.Incoming); conn.Outgoing.EndSend(conn.Outgoing.BeginSend(data, 0, data.Length, null, null)); try { conn.Outgoing.EndReceive(conn.Outgoing.BeginReceive(data, 0, data.Length, null, null)); } catch { Assert.False(conn.Incoming.Connected); // Assert.False(conn.Outgoing.Connected); return; } Assert.True(false, "The outgoing connection should've thrown an exception"); }
void SendHaveMessagesToAll() { for (int i = 0; i < manager.Peers.ConnectedPeers.Count; i++) { if (manager.Peers.ConnectedPeers[i].Connection == null) continue; MessageBundle bundle = new MessageBundle(); foreach (int pieceIndex in manager.finishedPieces) { // If the peer has the piece already, we need to recalculate his "interesting" status. bool hasPiece = manager.Peers.ConnectedPeers[i].BitField[pieceIndex]; if (hasPiece) { bool isInteresting = manager.PieceManager.IsInteresting(manager.Peers.ConnectedPeers[i]); SetAmInterestedStatus(manager.Peers.ConnectedPeers[i], isInteresting); } // Check to see if have supression is enabled and send the have message accordingly if (!hasPiece || (hasPiece && !manager.Engine.Settings.HaveSupressionEnabled)) bundle.Messages.Add(new HaveMessage(pieceIndex)); } manager.Peers.ConnectedPeers[i].Enqueue(bundle); } manager.finishedPieces.Clear(); }
protected virtual void AppendBitfieldMessage(PeerId id, MessageBundle bundle) { if (id.SupportsFastPeer && ClientEngine.SupportsFastPeer) { if (manager.Bitfield.AllFalse) bundle.Messages.Add(new HaveNoneMessage()); else if (manager.Bitfield.AllTrue) bundle.Messages.Add(new HaveAllMessage()); else bundle.Messages.Add(new BitfieldMessage(manager.Bitfield)); } else { bundle.Messages.Add(new BitfieldMessage(manager.Bitfield)); } }
protected virtual void AppendFastPieces(PeerId id, MessageBundle bundle) { // Now we will enqueue a FastPiece message for each piece we will allow the peer to download // even if they are choked if (ClientEngine.SupportsFastPeer && id.SupportsFastPeer) for (int i = 0; i < id.AmAllowedFastPieces.Count; i++) bundle.Messages.Add(new AllowedFastMessage(id.AmAllowedFastPieces[i])); }
protected virtual void AppendExtendedHandshake(PeerId id, MessageBundle bundle) { if (id.SupportsLTMessages && ClientEngine.SupportsExtended) bundle.Messages.Add(new ExtendedHandshakeMessage(manager.HasMetadata ? manager.Torrent.Metadata.Length : 0)); }
public virtual void HandlePeerConnected(PeerId id, Direction direction) { MessageBundle bundle = new MessageBundle(); AppendBitfieldMessage(id, bundle); AppendExtendedHandshake(id, bundle); AppendFastPieces(id, bundle); id.Enqueue(bundle); }
protected override void AppendBitfieldMessage(PeerId id, MessageBundle bundle) { // We can't send a bitfield message in metadata mode as // we don't know what size the bitfield is }
public void SingleFileTorrent() { rig.Dispose(); rig = TestRig.CreateSingleFile(); string url = rig.Torrent.GetRightHttpSeeds[0]; connection = new HttpConnection(new Uri (url)); connection.Manager = rig.Manager; id = new PeerId(new Peer("this is my id", connection.Uri), rig.Manager); id.Connection = connection; id.IsChoking = false; id.AmInterested = true; id.BitField.SetAll(true); id.MaxPendingRequests = numberOfPieces; requests = rig.Manager.PieceManager.Picker.PickPiece(id, new List<PeerId>(), numberOfPieces); RecieveFirst(); Assert.AreEqual(url, requestedUrl[0]); }
private void PeerHandshakeReceived(bool succeeded, int count, object state) { PeerId id = (PeerId)state; string reason = null; bool cleanUp = false; PeerMessage msg; try { // If the connection is closed, just return if (!succeeded) { CleanupSocket(id, "Handshaking failed"); return; } // Decode the handshake and handle it id.Decryptor.Decrypt(id.recieveBuffer.Array, id.recieveBuffer.Offset, count); msg = new HandshakeMessage(); msg.Decode(id.recieveBuffer, 0, count); msg.Handle(id); Logger.Log(id.Connection, "ConnectionManager - Handshake recieved"); if (id.SupportsFastPeer && ClientEngine.SupportsFastPeer) { if (id.TorrentManager.Bitfield.AllFalse || id.TorrentManager.IsInitialSeeding) msg = new HaveNoneMessage(); else if (id.TorrentManager.Bitfield.AllTrue) msg = new HaveAllMessage(); else msg = new BitfieldMessage(id.TorrentManager.Bitfield); } else if (id.TorrentManager.IsInitialSeeding) { BitField btfld = new BitField(id.TorrentManager.Bitfield.Length); btfld.SetAll(false); msg = new BitfieldMessage(btfld); } else { msg = new BitfieldMessage(id.TorrentManager.Bitfield); } if (id.SupportsLTMessages && ClientEngine.SupportsExtended) { MessageBundle bundle = new MessageBundle(); bundle.Messages.Add(new ExtendedHandshakeMessage()); bundle.Messages.Add(msg); msg = bundle; } //ClientEngine.BufferManager.FreeBuffer(ref id.recieveBuffer); SendMessage(id, msg, this.bitfieldSentCallback); } catch (TorrentException) { Logger.Log(id.Connection, "ConnectionManager - Couldn't decode the message"); reason = "Couldn't decode handshake"; cleanUp = true; return; } finally { if (cleanUp) CleanupSocket(id, reason); } }