internal async ReusableTask SetListener(IDhtListener listener) { await DhtEngine.MainLoop; Listener.MessageReceived -= MessageReceived; Listener = listener ?? new NullDhtListener(); Listener.MessageReceived += MessageReceived; }
public MessageLoop(DhtEngine engine, IDhtListener listener) { Engine = engine ?? throw new ArgumentNullException(nameof(engine)); Listener = listener ?? throw new ArgumentNullException(nameof(engine)); ReceiveQueue = new Queue <KeyValuePair <IPEndPoint, DhtMessage> >(); SendQueue = new Queue <SendDetails>(); Timeout = TimeSpan.FromSeconds(15); WaitingResponse = new Dictionary <BEncodedValue, SendDetails>(); WaitingResponseTimedOut = new List <SendDetails> (); listener.MessageReceived += MessageReceived; Task sendTask = null; DhtEngine.MainLoop.QueueTimeout(TimeSpan.FromMilliseconds(5), () => { if (engine.Disposed) { return(false); } try { if (sendTask == null || sendTask.IsCompleted) { sendTask = SendMessages(); } while (ReceiveQueue.Count > 0) { ReceiveMessage(); } TimeoutMessage(); } catch (Exception ex) { Debug.WriteLine("Error in DHT main loop:"); Debug.WriteLine(ex); } return(!engine.Disposed); }); }
/// <summary> /// 构造DHT引擎 /// </summary> /// <param name="listener">监听对象</param> public DhtEngine(IDhtListener listener) { if (listener == null) { throw new ArgumentNullException(nameof(listener)); } BucketRefreshTimeout = TimeSpan.FromMinutes(15); MessageLoop = new MessageLoop(this, listener); 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); }); }
public Task SetListenerAsync(IDhtListener listener) { return(Task.CompletedTask); }
public void SetListener(IDhtListener listener) { }
internal void SetListener(IDhtListener listener) { Listener.MessageReceived -= MessageReceived; Listener = listener ?? new NullDhtListener(); Listener.MessageReceived += MessageReceived; }
public async Task SetListenerAsync(IDhtListener listener) => await MessageLoop.SetListener(listener);
public static IDhtEngine Create(IDhtListener listener) => Creator(listener);
public void SetListener(IDhtListener listener) => MessageLoop.SetListener(listener);