Exemple #1
0
        public MessageLoop(DhtEngine engine, TransferMonitor monitor)
        {
            Engine                  = engine ?? throw new ArgumentNullException(nameof(engine));
            Monitor                 = monitor;
            DhtMessageFactory       = new DhtMessageFactory();
            Listener                = new NullDhtListener();
            ReceiveQueue            = new Queue <KeyValuePair <IPEndPoint, DhtMessage> > ();
            SendQueue               = new Queue <SendDetails> ();
            Timeout                 = TimeSpan.FromSeconds(15);
            WaitingResponse         = new Dictionary <BEncodedValue, SendDetails> ();
            WaitingResponseTimedOut = new List <SendDetails> ();

            Task sendTask = null;

            DhtEngine.MainLoop.QueueTimeout(TimeSpan.FromMilliseconds(5), () => {
                monitor.ReceiveMonitor.Tick();
                monitor.SendMonitor.Tick();

                if (engine.Disposed)
                {
                    return(false);
                }
                try {
                    if (sendTask == null || sendTask.IsCompleted)
                    {
                        sendTask = SendMessages();
                    }

                    while (ReceiveQueue.Count > 0)
                    {
                        ReceiveMessage();
                    }

                    TimeoutMessages();
                } catch (Exception ex) {
                    Debug.WriteLine("Error in DHT main loop:");
                    Debug.WriteLine(ex);
                }

                return(!engine.Disposed);
            });
        }
Exemple #2
0
        public DhtEngine()
        {
            var monitor = new TransferMonitor();

            BucketRefreshTimeout = TimeSpan.FromMinutes(15);
            MessageLoop          = new MessageLoop(this, monitor);
            Monitor      = monitor;
            RoutingTable = new RoutingTable();
            State        = DhtState.NotReady;
            TokenManager = new TokenManager();
            Torrents     = new Dictionary <NodeId, List <Node> > ();

            MainLoop.QueueTimeout(TimeSpan.FromMinutes(5), () => {
                if (!Disposed)
                {
                    TokenManager.RefreshTokens();
                }
                return(!Disposed);
            });
        }