コード例 #1
0
        /// <summary>
        /// Decodes the message.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="message">The message.</param>
        /// <returns>
        /// True if decoding was successful; false otherwise.
        /// </returns>
        public static bool TryDecode(byte[] buffer, int offset, out ScrapeResponseMessage message)
        {
            int action;
            int transactionId;
            int seeds;
            int completed;
            int leechers;
            List <ScrapeDetails> scrapeInfo = new List <ScrapeDetails>();

            message = null;

            if (buffer != null &&
                buffer.Length >= offset + ActionLength + TransactionIdLength &&
                offset >= 0)
            {
                action        = Message.ReadInt(buffer, ref offset);
                transactionId = Message.ReadInt(buffer, ref offset);

                if (action == (int)TrackingAction.Scrape &&
                    transactionId >= 0)
                {
                    while (offset <= buffer.Length - SeedersLength - CompletedLength - LeechersLength)
                    {
                        seeds     = Message.ReadInt(buffer, ref offset);
                        completed = Message.ReadInt(buffer, ref offset);
                        leechers  = Message.ReadInt(buffer, ref offset);

                        scrapeInfo.Add(new ScrapeDetails(seeds, leechers, completed));
                    }

                    message = new ScrapeResponseMessage(transactionId, scrapeInfo);
                }
            }

            return(message != null);
        }
コード例 #2
0
        /// <summary>
        /// Tries to decode the message.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="messageType">Type of the message.</param>
        /// <param name="message">The decoded message.</param>
        /// <returns>
        /// True if message was successfully decoded; false otherwise.
        /// </returns>
        public static bool TryDecode(byte[] buffer, int offset, MessageType messageType, out TrackerMessage message)
        {
            int action;

            message = null;

            if (buffer.IsNotNullOrEmpty())
            {
                action = messageType == MessageType.Request ? Message.ReadInt(buffer, ref offset) : Message.ReadInt(buffer, ref offset);
                offset = 0;

                if (action == (int)TrackingAction.Connect)
                {
                    if (messageType == MessageType.Request)
                    {
                        ConnectMessage message2;
                        ConnectMessage.TryDecode(buffer, offset, out message2);

                        message = message2;
                    }
                    else
                    {
                        ConnectResponseMessage message2;
                        ConnectResponseMessage.TryDecode(buffer, offset, out message2);

                        message = message2;
                    }
                }
                else if (action == (int)TrackingAction.Announce)
                {
                    if (messageType == MessageType.Request)
                    {
                        AnnounceMessage message2;
                        AnnounceMessage.TryDecode(buffer, offset, out message2);

                        message = message2;
                    }
                    else
                    {
                        AnnounceResponseMessage message2;
                        AnnounceResponseMessage.TryDecode(buffer, offset, out message2);

                        message = message2;
                    }
                }
                else if (action == (int)TrackingAction.Scrape)
                {
                    if (messageType == MessageType.Request)
                    {
                        ScrapeMessage message2;
                        ScrapeMessage.TryDecode(buffer, offset, out message2);

                        message = message2;
                    }
                    else
                    {
                        ScrapeResponseMessage message2;
                        ScrapeResponseMessage.TryDecode(buffer, offset, out message2);

                        message = message2;
                    }
                }
                else if (action == (int)TrackingAction.Error)
                {
                    ErrorMessage message2;
                    ErrorMessage.TryDecode(buffer, offset, out message2);

                    message = message2;
                }
                else
                {
                    // could not decode UDP message
                }
            }

            return(message != null);
        }