Dequeue() приватный Метод

private Dequeue ( ) : PeerMessage
Результат PeerMessage
Пример #1
0
        /// <param name="id"> The peer whose message queue you want to start processing </param>
        internal void ProcessQueue(PeerId id)
        {
            if (id.QueueLength == 0)
            {
                id.ProcessingQueue = false;
                return;
            }

            var message      = id.Dequeue();
            var pieceMessage = message as PieceMessage;

            if (pieceMessage != null)
            {
                using (var handle = new ManualResetEvent(false))
                {
                    pieceMessage.Data = BufferManager.EmptyBuffer;
                    ClientEngine.BufferManager.GetBuffer(ref pieceMessage.Data, pieceMessage.ByteLength);
                    _engine.DiskManager.QueueRead(id.TorrentManager,
                                                  pieceMessage.StartOffset +
                                                  ((long)pieceMessage.PieceIndex * id.TorrentManager.Torrent.PieceLength), pieceMessage.Data,
                                                  pieceMessage.RequestLength, successful => handle.Set());
                    handle.WaitOne();
                    id.PiecesSent++;
                }
            }
            try
            {
                SendMessage(id, message, _messageSentCallback);
            }
            catch (Exception e)
            {
                CleanupSocket(id, "Exception calling SendMessage: " + e.Message);
            }
        }
Пример #2
0
        /// <summary>
        ///   Checks the send queue of the peer to see if there are any outstanding pieces which they requested
        ///   and rejects them as necessary
        /// </summary>
        /// <param name="Peer"> </param>
        private void RejectPendingRequests(PeerId Peer)
        {
            var length = Peer.QueueLength;

            for (var i = 0; i < length; i++)
            {
                var message      = Peer.Dequeue();
                var pieceMessage = message as PieceMessage;
                if (pieceMessage == null)
                {
                    Peer.Enqueue(message);
                    continue;
                }

                // If the peer doesn't support fast peer, then we will never requeue the message
                if (!(Peer.SupportsFastPeer && ClientEngine.SupportsFastPeer))
                {
                    Peer.IsRequestingPiecesCount--;
                    continue;
                }

                // If the peer supports fast peer, queue the message if it is an AllowedFast piece
                // Otherwise send a reject message for the piece
                if (Peer.AmAllowedFastPieces.Contains(pieceMessage.PieceIndex))
                {
                    Peer.Enqueue(pieceMessage);
                }
                else
                {
                    Peer.IsRequestingPiecesCount--;
                    Peer.Enqueue(new RejectRequestMessage(pieceMessage));
                }
            }
        }
Пример #3
0
        protected virtual void HandleCancelMessage(PeerId id, CancelMessage message)
        {
            for (var i = 0; i < id.QueueLength; i++)
            {
                var msg = id.Dequeue();
                if (!(msg is PieceMessage))
                {
                    id.Enqueue(msg);
                    continue;
                }

                var piece = msg as PieceMessage;
                if (
                    !(piece.PieceIndex == message.PieceIndex && piece.StartOffset == message.StartOffset &&
                      piece.RequestLength == message.RequestLength))
                {
                    id.Enqueue(msg);
                }
                else
                {
                    id.IsRequestingPiecesCount--;
                }
            }

            for (var i = 0; i < id.PieceReads.Count; i++)
            {
                if (id.PieceReads[i].PieceIndex == message.PieceIndex &&
                    id.PieceReads[i].StartOffset == message.StartOffset &&
                    id.PieceReads[i].RequestLength == message.RequestLength)
                {
                    id.IsRequestingPiecesCount--;
                    id.PieceReads.RemoveAt(i);
                    break;
                }
            }
        }
Пример #4
0
        protected virtual void HandleCancelMessage(PeerId id, CancelMessage message)
        {
            for (var i = 0; i < id.QueueLength; i++)
            {
                var msg = id.Dequeue();
                if (!(msg is PieceMessage))
                {
                    id.Enqueue(msg);
                    continue;
                }

                var piece = msg as PieceMessage;
                if (
                    !(piece.PieceIndex == message.PieceIndex && piece.StartOffset == message.StartOffset &&
                      piece.RequestLength == message.RequestLength))
                {
                    id.Enqueue(msg);
                }
                else
                {
                    id.IsRequestingPiecesCount--;
                }
            }

            for (var i = 0; i < id.PieceReads.Count; i++)
            {
                if (id.PieceReads[i].PieceIndex == message.PieceIndex &&
                    id.PieceReads[i].StartOffset == message.StartOffset &&
                    id.PieceReads[i].RequestLength == message.RequestLength)
                {
                    id.IsRequestingPiecesCount--;
                    id.PieceReads.RemoveAt(i);
                    break;
                }
            }
        }
Пример #5
0
        /// <param name="id"> The peer whose message queue you want to start processing </param>
        internal void ProcessQueue(PeerId id)
        {
            if (id.QueueLength == 0)
            {
                id.ProcessingQueue = false;
                return;
            }

            var message = id.Dequeue();
            var pieceMessage = message as PieceMessage;
            if (pieceMessage != null)
            {
                using (var handle = new ManualResetEvent(false))
                {
                    pieceMessage.Data = BufferManager.EmptyBuffer;
                    ClientEngine.BufferManager.GetBuffer(ref pieceMessage.Data, pieceMessage.ByteLength);
                    _engine.DiskManager.QueueRead(id.TorrentManager,
                                                  pieceMessage.StartOffset +
                                                  ((long) pieceMessage.PieceIndex*id.TorrentManager.Torrent.PieceLength), pieceMessage.Data,
                                                  pieceMessage.RequestLength, successful => handle.Set());
                    handle.WaitOne();
                    id.PiecesSent++;
                }
            }
            try
            {
                SendMessage(id, message, _messageSentCallback);
            }
            catch (Exception e)
            {
                CleanupSocket(id, "Exception calling SendMessage: " + e.Message);
            }
        }
Пример #6
0
        /// <summary>
        ///   Checks the send queue of the peer to see if there are any outstanding pieces which they requested
        ///   and rejects them as necessary
        /// </summary>
        /// <param name="Peer"> </param>
        private void RejectPendingRequests(PeerId Peer)
        {
            var length = Peer.QueueLength;

            for (var i = 0; i < length; i++)
            {
                var message = Peer.Dequeue();
                var pieceMessage = message as PieceMessage;
                if (pieceMessage == null)
                {
                    Peer.Enqueue(message);
                    continue;
                }

                // If the peer doesn't support fast peer, then we will never requeue the message
                if (!(Peer.SupportsFastPeer && ClientEngine.SupportsFastPeer))
                {
                    Peer.IsRequestingPiecesCount--;
                    continue;
                }

                // If the peer supports fast peer, queue the message if it is an AllowedFast piece
                // Otherwise send a reject message for the piece
                if (Peer.AmAllowedFastPieces.Contains(pieceMessage.PieceIndex))
                    Peer.Enqueue(pieceMessage);
                else
                {
                    Peer.IsRequestingPiecesCount--;
                    Peer.Enqueue(new RejectRequestMessage(pieceMessage));
                }
            }
        }