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 AddConnectionToStoppedManager() { MessageBundle 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)); byte[] 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.IsFalse(conn.Incoming.Connected, "#1"); // Assert.IsFalse(conn.Outgoing.Connected, "#2"); return; } Assert.Fail ("The outgoing connection should've thrown an exception"); }
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]); }
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); }