private DhtRpcPacket Query(DhtRpcPacket query, NodeContact contact) { Stream s = null; try { if (contact.IsCurrentNode) { return(ProcessQuery(query, contact.NodeEP)); } s = _manager.GetConnection(contact.NodeEP); //set timeout s.WriteTimeout = _queryTimeout; s.ReadTimeout = _queryTimeout; //send query query.WriteTo(new BinaryWriter(s)); s.Flush(); Debug.Write(this.GetType().Name, "query sent to: " + contact.ToString()); //read response DhtRpcPacket response = new DhtRpcPacket(new BinaryReader(s)); Debug.Write(this.GetType().Name, "response received from: " + contact.ToString()); //auto add contact or update last seen time { NodeContact bucketContact = _routingTable.FindContact(contact.NodeId); if (bucketContact == null) { contact.UpdateLastSeenTime(); _routingTable.AddContact(contact); } else { bucketContact.UpdateLastSeenTime(); } } return(response); } catch (Exception ex) { Debug.Write(this.GetType().Name, ex); contact.IncrementRpcFailCount(); return(null); } finally { if (s != null) { s.Dispose(); } } }
public void AddNode(NodeContact contact) { if (contact.NodeEP.AddressFamily != _currentNode.NodeEP.AddressFamily) { return; } if (_routingTable.AddContact(contact)) { Debug.Write(this.GetType().Name, "node contact added: " + contact.ToString()); ThreadPool.QueueUserWorkItem(delegate(object state) { Query(DhtRpcPacket.CreatePingPacket(_currentNode), contact); }); } }