public KadHashTable(KadNode node, KadContactNode knownNode = null, KadHashTableConfiguration kadHashTableConfig = null) { Owner = node; if (knownNode != null) { Owner.BucketList.Put(knownNode); } if (kadHashTableConfig != null) { ReplicationCount = kadHashTableConfig.ReplicationCount ?? ReplicationCount; MaxConcurrentThreads = kadHashTableConfig.MaxConcurrentThreads ?? MaxConcurrentThreads; ReplicationInSecs = kadHashTableConfig.ReplicationInSecs ?? ReplicationInSecs; RepublicationInSecs = kadHashTableConfig.RepublicationInSecs ?? RepublicationInSecs; BucketsRefreshInSecs = kadHashTableConfig.BucketsRefreshInSecs ?? BucketsRefreshInSecs; } InitTable(); }
private void ListenIncoming(KadNode ownerNode) { IPEndPoint incomingIpEndPoint = new IPEndPoint(IPAddress.Any, EndPoint.Port); while (!_stopped) { try { byte[] rawMsg = _udpClient.Receive(ref incomingIpEndPoint); NodeMessage incomingMsg = new NodeMessage.Builder(rawMsg).Build(); //TODO: msg validation ulong msgIdx = GetMessageId(incomingIpEndPoint.Address, incomingMsg.Seq); //TODO: two spare bytes for port if (_messages.ContainsKey(msgIdx)) { NodeMessage rqMsg = null; if (_messages.TryRemove(msgIdx, out rqMsg)) { rqMsg.ProcessResponse(incomingMsg); } } else { if (incomingMsg.IsRequest) { ownerNode.ProcessRequest(incomingMsg, incomingIpEndPoint); _messages.AddOrUpdate(msgIdx, incomingMsg, (k, v) => v); } } } catch (SocketException se) { if (se.SocketErrorCode != SocketError.Interrupted) { throw se; } } incomingIpEndPoint = new IPEndPoint(IPAddress.Any, EndPoint.Port); } }
public void Start(KadNode ownerNode) { _udpClient = new UdpClient(EndPoint); _listenerThread = new Thread(ListenIncomingObj); _listenerThread.Start(ownerNode); }