public static bool TryDecodeMessage(BEncodedDictionary dictionary, out DhtMessage message, out string error) { message = null; error = null; if (!dictionary.TryGetValue(MessageTypeKey, out BEncodedValue messageType)) { message = null; error = "The BEncodedDictionary did not contain the 'q' key, so the message type could not be identified"; return(false); } if (messageType.Equals(QueryMessage.QueryType)) { message = queryDecoders[(BEncodedString)dictionary[QueryNameKey]](dictionary); } else if (messageType.Equals(ErrorMessage.ErrorType)) { message = new ErrorMessage(dictionary); messages.Remove(message.TransactionId); } else { QueryMessage query; BEncodedString key = (BEncodedString)dictionary[TransactionIdKey]; if (messages.TryGetValue(key, out query)) { messages.Remove(key); try { message = query.CreateResponse(dictionary); } catch { error = "Response dictionary was invalid"; } } else { error = "Response had bad transaction ID"; } } // If the transaction ID is null, or invalid, we should bail out if (message != null && message.TransactionId == null) { error = "Response had a null transation ID"; } // If the node ID is null, or invalid, we should bail out if (message != null && message.Id == null) { error = "Response had a null node ID"; } return(error == null && message != null); }
public static bool TryDecodeMessage(BEncodedDictionary dictionary, out DhtMessage message) { string error; return(TryDecodeMessage(dictionary, out message, out error)); }