Esempio n. 1
0
 static HaveAllMessage()
 {
     Instance = new HaveAllMessage();
 }
Esempio n. 2
0
        /// <summary>
        /// Parse the message from the buffer pool
        /// </summary>
        /// <param name="bufferPool">The buffer pool that saves the bytes from network</param>
        /// <param name="booleansLength">The length of booleans</param>
        /// <returns>Return the message that is parsed from the buffer pool</returns>
        public static Message Parse(BufferPool bufferPool, int booleansLength)
        {
            Message result = null;

            if (bufferPool.Length < 4)
            {
                return(null);
            }

            byte firstByte = bufferPool.GetFirstByte();

            if (firstByte == 19)
            {
                if (bufferPool.Length >= 68)
                {
                    byte[] buffer = new byte[68];
                    result = new HandshakeMessage();
                    bufferPool.Read(buffer, 0, 68);
                    result.Parse(buffer);
                    return(result);
                }
                return(null);
            }

            byte[] lengthBytes = new byte[4];

            bufferPool.Read(lengthBytes, 0, 4);

            int length = GetLength(lengthBytes, 0);

            Debug.Assert(length >= 0);

            if (length == 0)
            {
                result = new KeepAliveMessage();
                return(result);
            }

            if (bufferPool.Length < length)
            {
                bufferPool.Seek(-4);
                return(null);
            }

            byte[] contentBytes = new byte[length];
            bufferPool.Read(contentBytes, 0, length);

            switch ((MessageType)contentBytes[0])
            {
            case MessageType.Choke:
                result = new ChokeMessage();
                break;

            case MessageType.Unchoke:
                result = new UnchokeMessage();
                break;

            case MessageType.Interested:
                result = new InterestedMessage();
                break;

            case MessageType.NotInterested:
                result = new NotInterestedMessage();
                break;

            case MessageType.Have:
                result = new HaveMessage();
                break;

            case MessageType.BitField:
                result = new BitfieldMessage(booleansLength);
                break;

            case MessageType.Request:
                result = new RequestMessage();
                break;

            case MessageType.Piece:
                result = new PieceMessage();
                break;

            case MessageType.Cancel:
                result = new CancelMessage();
                break;

            case MessageType.Port:
                result = new PortMessage();
                break;

            case MessageType.SuggestPiece:
                result = new SuggestPieceMessage();
                break;

            case MessageType.HaveAll:
                result = new HaveAllMessage();
                break;

            case MessageType.HaveNone:
                result = new HaveNoneMessage();
                break;

            case MessageType.RejectRequest:
                result = new RejectRequestMessage();
                break;

            case MessageType.AllowedFast:
                result = new AllowedFastMessage();
                break;

            case MessageType.ExtendedList:
                result = new ExtendedListMessage();
                break;
            }

            if (result != null)
            {
                result.Parse(contentBytes);
            }
            return(result);
        }