public override void Handle(DhtEngine engine, Node node) { base.Handle(engine, node); if (!engine.Torrents.ContainsKey(InfoHash)) engine.Torrents.Add(InfoHash, new List<Node>()); Message response; if (engine.TokenManager.VerifyToken(node, Token)) { engine.Torrents[InfoHash].Add(node); response = new AnnouncePeerResponse(engine.RoutingTable.LocalNode.Id, TransactionId); } else response = new ErrorMessage(ErrorCode.ProtocolError, "Invalid or expired token received"); engine.MessageLoop.EnqueueSend(response, node.EndPoint); }
public static bool TryDecodeMessage(BEncodedDictionary dictionary, out Message message, out string error) { message = null; error = null; if (dictionary[MessageTypeKey].Equals(QueryMessage.QueryType)) { 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; }