Exemplo n.º 1
0
        public static bool TryNoTraceDecodeMessage(BEncodedDictionary dictionary, out DhtMessage message, out string error)
        {
            message = null;
            error   = null;

            if (dictionary[MessageTypeKey].Equals(QueryMessage.QueryType))
            {
                if (!queryDecoders.ContainsKey((BEncodedString)dictionary[QueryNameKey]))
                {
                    error = ((BEncodedString)dictionary[QueryNameKey]).Text;
                    return(false);
                }
                message = queryDecoders[(BEncodedString)dictionary[QueryNameKey]](dictionary);
            }
            else if (dictionary[MessageTypeKey].Equals(ErrorMessage.ErrorType))
            {
                message = new ErrorMessage(dictionary);
            }
            else
            {
                QueryMessage   query = new NoTraceMessage();
                BEncodedString key   = (BEncodedString)dictionary[TransactionIdKey];
                try
                {
                    message = query.ResponseCreator(dictionary, query);

                    //BEncodedValue q;
                    //if (message == null && dictionary.TryGetValue("q", out q))
                    //{
                    //    error = q.ToString();
                    //}
                    if (message == null && dictionary.ContainsKey("q"))
                    {
                        dictionary[MessageTypeKey] = QueryMessage.QueryType;
                        TryNoTraceDecodeMessage(dictionary, out message, out error);
                    }
                }
                catch (Exception ex)
                {
                    error = ex.ToString();
                }
            }

            return(error == null && message != null);
        }
Exemplo n.º 2
0
        public static bool TryDecodeMessage(BEncodedDictionary dictionary, out DhtMessage message, out string error)
        {
            message = null;
            error   = null;

            if (dictionary[MessageTypeKey].Equals(QueryMessage.QueryType))
            {
                if (!queryDecoders.ContainsKey((BEncodedString)dictionary[QueryNameKey]))
                {
                    error = ((BEncodedString)dictionary[QueryNameKey]).Text;
                    return(false);
                }
                message = queryDecoders[(BEncodedString)dictionary[QueryNameKey]](dictionary);
            }
            else if (dictionary[MessageTypeKey].Equals(ErrorMessage.ErrorType))
            {
                message = new ErrorMessage(dictionary);
            }
            else
            {
                QueryMessage   query;
                BEncodedString key = (BEncodedString)dictionary[TransactionIdKey];
                if (messages.TryGetValue(key, out query))
                {
                    messages.Remove(key);
                    try
                    {
                        message = query.ResponseCreator(dictionary, query);
                    }
                    catch
                    {
                        error = "Response dictionary was invalid";
                    }
                }
                else
                {
                    error = "Response had bad transaction ID";
                }
            }

            return(error == null && message != null);
        }
Exemplo n.º 3
0
        public static bool TryNoTraceDecodeMessage(BEncodedDictionary dictionary, out DhtMessage message)
        {
            string error;

            return(TryNoTraceDecodeMessage(dictionary, out message, out error));
        }