Ejemplo n.º 1
0
        public static async ReusableTask ReceiveAsync(IConnection connection, ByteBuffer buffer, int offset, int count, IRateLimiter rateLimiter, SpeedMonitor peerMonitor, SpeedMonitor managerMonitor)
        {
            await MainLoop.SwitchToThreadpool();

            while (count > 0)
            {
                int  transferred;
                bool unlimited  = rateLimiter?.Unlimited ?? true;
                int  shouldRead = unlimited ? count : Math.Min(ChunkLength, count);

                if (rateLimiter != null && !unlimited && !rateLimiter.TryProcess(shouldRead))
                {
                    var tcs = new ReusableTaskCompletionSource <int> ();
                    lock (receiveQueue)
                        receiveQueue.Enqueue(new QueuedIO(connection, buffer, offset, shouldRead, rateLimiter, tcs));
                    transferred = await tcs.Task.ConfigureAwait(false);
                }
                else
                {
                    transferred = await connection.ReceiveAsync(buffer, offset, shouldRead).ConfigureAwait(false);
                }

                if (transferred == 0)
                {
                    throw new ConnectionClosedException("Socket receive returned 0, indicating the connection has been closed.");
                }

                peerMonitor?.AddDelta(transferred);
                managerMonitor?.AddDelta(transferred);

                offset += transferred;
                count  -= transferred;
            }
        }
Ejemplo n.º 2
0
        public static async ReusableTask SendAsync(IPeerConnection connection, SocketMemory buffer, IRateLimiter rateLimiter, SpeedMonitor peerMonitor, SpeedMonitor managerMonitor)
        {
            await MainLoop.SwitchToThreadpool();

            while (buffer.Length > 0)
            {
                int  transferred;
                bool unlimited  = rateLimiter?.Unlimited ?? true;
                int  shouldRead = unlimited ? buffer.Length : Math.Min(ChunkLength, buffer.Length);

                if (rateLimiter != null && !unlimited && !rateLimiter.TryProcess(shouldRead))
                {
                    var tcs = new ReusableTaskCompletionSource <int> ();
                    lock (sendQueue)
                        sendQueue.Enqueue(new QueuedIO(connection, buffer.Slice(0, shouldRead), rateLimiter, tcs));
                    transferred = await tcs.Task.ConfigureAwait(false);
                }
                else
                {
                    transferred = await connection.SendAsync(buffer.Slice(0, shouldRead)).ConfigureAwait(false);
                }

                if (transferred == 0)
                {
                    throw new ConnectionClosedException("Socket send returned 0, indicating the connection has been closed.");
                }

                peerMonitor?.AddDelta(transferred);
                managerMonitor?.AddDelta(transferred);

                buffer = buffer.Slice(transferred);
            }
        }
Ejemplo n.º 3
0
        public static async Task SendAsync(IConnection connection, byte [] buffer, int offset, int count, IRateLimiter rateLimiter, SpeedMonitor peerMonitor, SpeedMonitor managerMonitor)
        {
            await IOLoop;

            int remaining = count;

            while (remaining > 0)
            {
                int transferred;
                if (rateLimiter != null && !rateLimiter.Unlimited && !rateLimiter.TryProcess(Math.Min(ChunkLength, remaining)))
                {
                    var   tcs = new TaskCompletionSource <int> ();
                    await IOLoop;
                    sendQueue.Enqueue(new QueuedIO(connection, buffer, offset, Math.Min(ChunkLength, remaining), rateLimiter, tcs));
                    transferred = await tcs.Task.ConfigureAwait(false);
                }
                else
                {
                    transferred = await connection.SendAsync(buffer, offset, remaining).ConfigureAwait(false);
                }

                if (transferred == 0)
                {
                    throw new Exception("Socket is dead");
                }

                peerMonitor?.AddDelta(transferred);
                managerMonitor?.AddDelta(transferred);

                offset    += transferred;
                remaining -= transferred;
            }
        }
Ejemplo n.º 4
0
        public static async Task SendAsync(IConnection connection, byte [] buffer, int offset, int count, IRateLimiter rateLimiter, SpeedMonitor peerMonitor, SpeedMonitor managerMonitor)
        {
            await IOLoop;

            while (count > 0)
            {
                int  transferred;
                bool unlimited  = rateLimiter?.Unlimited ?? true;
                var  shouldRead = unlimited ? count : Math.Min(ChunkLength, count);

                if (rateLimiter != null && !unlimited && !rateLimiter.TryProcess(shouldRead))
                {
                    var   tcs = new TaskCompletionSource <int> ();
                    await IOLoop;
                    sendQueue.Enqueue(new QueuedIO(connection, buffer, offset, shouldRead, rateLimiter, tcs));
                    transferred = await tcs.Task.ConfigureAwait(false);
                }
                else
                {
                    transferred = await connection.SendAsync(buffer, offset, shouldRead).ConfigureAwait(false);
                }

                if (transferred == 0)
                {
                    throw new Exception("Socket is dead");
                }

                peerMonitor?.AddDelta(transferred);
                managerMonitor?.AddDelta(transferred);

                offset += transferred;
                count  -= transferred;
            }
        }
Ejemplo n.º 5
0
        public void ZeroAveragingPeriod()
        {
            var monitor = new SpeedMonitor(0);

            monitor.AddDelta(1000);
            monitor.Tick(1000);

            Assert.AreEqual(1000, monitor.Rate, "#1");
        }
Ejemplo n.º 6
0
        public void Tick()
        {
            var monitor = new SpeedMonitor();

            monitor.AddDelta(1000);
            monitor.Tick(1000);

            Assert.AreEqual(1000, monitor.Rate, "#1");
        }
Ejemplo n.º 7
0
 internal void BytesReceived(int bytesDownloaded, TransferType type)
 {
     lock (_locker)
     {
         if (type == TransferType.Data)
             _dataDown.AddDelta(bytesDownloaded);
         else
             _protocolDown.AddDelta(bytesDownloaded);
     }
 }
Ejemplo n.º 8
0
 internal void BytesSent(int bytesUploaded, TransferType type)
 {
     lock (_locker)
     {
         if (type == TransferType.Data)
             _dataUp.AddDelta(bytesUploaded);
         else
             _protocolUp.AddDelta(bytesUploaded);
     }
 }
Ejemplo n.º 9
0
        public void TickFull()
        {
            // Averaging period is 3, tick 3 times.
            var monitor = new SpeedMonitor(3);

            monitor.AddDelta(1200);
            monitor.Tick(1000);
            monitor.Tick(1000);
            monitor.Tick(1000);

            Assert.AreEqual(400, monitor.Rate, "#1");
        }
Ejemplo n.º 10
0
        public void TickTwice()
        {
            // Send 1000 bytes in 2000 milliseconds. Check
            // transfer rate is then 500bytes/second
            var monitor = new SpeedMonitor();

            monitor.AddDelta(1000);
            monitor.Tick(1000);
            monitor.Tick(1000);

            Assert.AreEqual(500, monitor.Rate, "#1");
        }
Ejemplo n.º 11
0
        public void Tick_AveragingTwo_TickThree()
        {
            // Send data only in the first tick. When we tick a third time
            // we should have a transfer rate of zero as the data should vanish.
            var monitor = new SpeedMonitor(2);

            monitor.AddDelta(1200);
            monitor.Tick(1000);
            monitor.Tick(1000);
            monitor.Tick(1000);

            Assert.AreEqual(0, monitor.Rate, "#1");
        }
Ejemplo n.º 12
0
        void HandlePeerMessageTransferred(object sender, PeerMessageEventArgs e)
        {
            if (e.Direction != Direction.Incoming)
            {
                return;
            }
            if (!(e.Message is MonoTorrent.Client.Messages.Standard.HaveMessage))
            {
                return;
            }

            swarmSpeed.AddDelta(manager.Torrent.PieceLength);
        }
Ejemplo n.º 13
0
        public void Start(string trackerAddress)
        {
            for (var i = 0; i < _threads.Length; i++)
            {
                _threads[i] = new Thread(() =>
                {
                    var sb      = new StringBuilder();
                    var torrent = 0;
                    while (true)
                    {
                        sb.Remove(0, sb.Length);

                        sb.Append(trackerAddress);
                        sb.Append("?info_hash=");
                        sb.Append(_hashes[torrent++]);
                        sb.Append("&peer_id=");
                        sb.Append("12345123451234512345");
                        sb.Append("&port=");
                        sb.Append("5000");
                        sb.Append("&uploaded=");
                        sb.Append("5000");
                        sb.Append("&downloaded=");
                        sb.Append("5000");
                        sb.Append("&left=");
                        sb.Append("5000");
                        sb.Append("&compact=");
                        sb.Append("1");

                        var request = WebRequest.Create(sb.ToString());
                        request.BeginGetResponse(r =>
                        {
                            try
                            {
                                request.EndGetResponse(r).Close();
                                _requests.AddDelta(1);
                            }
                            catch
                            {
                            }
                            finally
                            {
                                _requests.Tick();
                            }
                        }, null);

                        Thread.Sleep(_threadSleepTime);
                    }
                });
                _threads[i].Start();
            }
        }
Ejemplo n.º 14
0
 private void PerformWrite(BufferedIO io)
 {
     try {
         // Perform the actual write
         _writer.Write(io.Files, io.Offset, io.buffer, 0, io.Count, io.PieceLength, io.Manager.Torrent.Size);
         _writeMonitor.AddDelta(io.Count);
     } finally {
         io.Complete = true;
         if (io.Callback != null)
         {
             io.Callback(true);
         }
     }
 }
Ejemplo n.º 15
0
 private void PerformRead(BufferedIO io)
 {
     try
     {
         io.ActualCount = Writer.Read(io.Files, io.Offset, io.InternalBuffer, 0, io.Count, io.PieceLength,
                                      io.Manager.Torrent.Size)
             ? io.Count
             : 0;
         _readMonitor.AddDelta(io.ActualCount);
     }
     finally
     {
         io.Complete = true;
         io.Callback?.Invoke(io.ActualCount == io.Count);
     }
 }
Ejemplo n.º 16
0
        private void PerformWrite(BufferedIO io)
        {
            // Find the block that this data belongs to and set it's state to "Written"
            int index = io.PieceOffset / Piece.BlockSize;

            try {
                // Perform the actual write
                writer.Write(io.Files, io.Offset, io.buffer, 0, io.Count, io.PieceLength, io.Manager.Torrent.Size);
                writeMonitor.AddDelta(io.Count);
            } finally {
                io.Complete = true;
                if (io.Callback != null)
                {
                    io.Callback(true);
                }
            }
        }
Ejemplo n.º 17
0
        public void Start(string trackerAddress)
        {
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread((ThreadStart) delegate
                {
                    StringBuilder sb = new StringBuilder();
                    int torrent      = 0;
                    while (true)
                    {
                        sb.Remove(0, sb.Length);

                        int ipaddress = random.Next(0, hashes.Count);

                        sb.Append(trackerAddress);
                        sb.Append("?info_hash="); sb.Append(hashes[torrent++]);
                        sb.Append("&peer_id="); sb.Append("12345123451234512345");
                        sb.Append("&port="); sb.Append("5000");
                        sb.Append("&uploaded="); sb.Append("5000");
                        sb.Append("&downloaded="); sb.Append("5000");
                        sb.Append("&left="); sb.Append("5000");
                        sb.Append("&compact="); sb.Append("1");

                        WebRequest req = HttpWebRequest.Create(sb.ToString());
                        req.BeginGetResponse(delegate(IAsyncResult r)
                        {
                            try
                            {
                                req.EndGetResponse(r).Close();
                                requests.AddDelta(1);
                            }
                            catch
                            {
                            }
                            finally
                            {
                                requests.Tick();
                            }
                        }, null);

                        System.Threading.Thread.Sleep(threadSleepTime);
                    }
                });
                threads[i].Start();
            }
        }
Ejemplo n.º 18
0
 private void PerformRead(BufferedIO io)
 {
     try {
         if (writer.Read(io.Files, io.Offset, io.buffer, 0, io.Count, io.PieceLength, io.Manager.Torrent.Size))
         {
             io.ActualCount = io.Count;
         }
         else
         {
             io.ActualCount = 0;
         }
         readMonitor.AddDelta(io.ActualCount);
     } finally {
         io.Complete = true;
         if (io.Callback != null)
         {
             io.Callback(io.ActualCount == io.Count);
         }
     }
 }
Ejemplo n.º 19
0
        public void Start(string trackerAddress)
        {
            var client = new HttpClient();

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread((ThreadStart)async delegate {
                    StringBuilder sb = new StringBuilder();
                    int torrent      = 0;
                    while (true)
                    {
                        sb.Remove(0, sb.Length);

                        int ipaddress = random.Next(0, hashes.Count);

                        sb.Append(trackerAddress);
                        sb.Append("?info_hash=");
                        sb.Append(hashes[torrent++]);
                        sb.Append("&peer_id=");
                        sb.Append("12345123451234512345");
                        sb.Append("&port=");
                        sb.Append("5000");
                        sb.Append("&uploaded=");
                        sb.Append("5000");
                        sb.Append("&downloaded=");
                        sb.Append("5000");
                        sb.Append("&left=");
                        sb.Append("5000");
                        sb.Append("&compact=");
                        sb.Append("1");

                        await client.GetByteArrayAsync(sb.ToString());
                        requests.AddDelta(1);
                        requests.Tick();
                        await Task.Delay(threadSleepTime);
                    }
                });
                threads[i].Start();
            }
        }
 internal void ScrapeReceived()
 {
     lock (scrapes)
         scrapes.AddDelta(1);
 }
 internal void AnnounceReceived()
 {
     lock (announces)
         announces.AddDelta(1);
 }
Ejemplo n.º 22
0
 void Write(TorrentManager manager, long offset, byte [] buffer, int count)
 {
     writeMonitor.AddDelta(count);
     Writer.Write(manager.Torrent.Files, offset, buffer, 0, count, manager.Torrent.PieceLength, manager.Torrent.Size);
 }
Ejemplo n.º 23
0
 bool Read(TorrentManager manager, long offset, byte [] buffer, int count)
 {
     readMonitor.AddDelta(count);
     return(Writer.Read(manager.Torrent.Files, offset, buffer, 0, count, manager.Torrent.PieceLength, manager.Torrent.Size));
 }