Example #1
0
        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();
        }
Example #2
0
        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);
            }
        }
Example #3
0
 public void Start(KadNode ownerNode)
 {
     _udpClient      = new UdpClient(EndPoint);
     _listenerThread = new Thread(ListenIncomingObj);
     _listenerThread.Start(ownerNode);
 }