public static bool TryParse(BDictionary message, out FindNodeResponse response) { response = null; if (message.TryGetValue("t", out BString transactionId) && transactionId.Length > 0 && message.TryGetValue("a", out BDictionary arguments) && arguments.TryGetValue("id", out BString nodeId) && nodeId.Length == NodeID.Size) { IPv4Node[] nodes = null; IPv6Node[] nodes6 = null; if (arguments.TryGetValue("nodes", out BString nodeBytes)) { if (!IPv4Node.TryParseCompactNodeInfos(nodeBytes.Bytes, out nodes)) { return(false); } } if (arguments.TryGetValue("nodes6", out BString node6Bytes)) { if (!IPv6Node.TryParseCompactNodeInfos(nodeBytes.Bytes, out nodes6)) { return(false); } } if (nodes != null) { if (nodes6 != null) { response = new FindNodeResponse(new TransactionID(transactionId), new NodeID(nodeId), nodes, nodes6); } else { response = new FindNodeResponse(new TransactionID(transactionId), new NodeID(nodeId), nodes); } } else if (nodes6 != null) { response = new FindNodeResponse(new TransactionID(transactionId), new NodeID(nodeId), nodes6); } } return(response != null); }
public static bool TryParse(BDictionary message, out GetPeersResponse response) { response = null; if (message.TryGetValue("t", out BString transactionId) && transactionId.Length > 0 && message.TryGetValue("a", out BDictionary arguments) && arguments.TryGetValue("id", out BString nodeId) && nodeId.Length == NodeID.Size && arguments.TryGetValue("token", out BString token) && token.Length > 0) { if (arguments.TryGetValue("values", out BList values)) { PeerContact[] peers = new PeerContact[values.Count]; for (int i = 0; i < peers.Length; i++) { BObject value = values[i]; if (value is BString peer) { if (peer.Length == IPv4PeerContact.CompactInfoSize) { peers[i] = new IPv4PeerContact(peer); } else if (peer.Length == IPv6PeerContact.CompactInfoSize) { peers[i] = new IPv6PeerContact(peer); } else { return(false); } } else { return(false); } } response = new GetPeersResponse(new TransactionID(transactionId), new NodeID(nodeId), new PeerToken(token), peers); } else { IPv4Node[] nodes = null; IPv6Node[] nodes6 = null; if (arguments.TryGetValue("nodes", out BString nodeBytes)) { if (!IPv4Node.TryParseCompactNodeInfos(nodeBytes.Bytes, out nodes)) { return(false); } } if (arguments.TryGetValue("nodes6", out BString node6Bytes)) { if (!IPv6Node.TryParseCompactNodeInfos(nodeBytes.Bytes, out nodes6)) { return(false); } } if (nodes != null) { if (nodes6 != null) { response = new GetPeersResponse(new TransactionID(transactionId), new NodeID(nodeId), new PeerToken(token), nodes, nodes6); } else { response = new GetPeersResponse(new TransactionID(transactionId), new NodeID(nodeId), new PeerToken(token), nodes); } } else if (nodes6 != null) { response = new GetPeersResponse(new TransactionID(transactionId), new NodeID(nodeId), new PeerToken(token), nodes6); } } } return(response != null); }