Ejemplo n.º 1
0
        public ClientEngine(EngineSettings settings, PeerListener listener, PieceWriter writer, string peerId = null)
        {
            Check.Settings(settings);
            Check.Listener(listener);
            Check.Writer(writer);

            this.listener = listener;
            this.settings = settings;

            if (settings.FastResumePath != null && File.Exists(settings.FastResumePath))
            {
                var encodedListData = File.ReadAllBytes(settings.FastResumePath);
                var encodedList     = (BEncodedList)BEncodedValue.Decode(encodedListData);

                _fastResume = encodedList.Cast <BEncodedDictionary>()
                              .Select(x => new FastResume(x));
            }

            connectionManager = new ConnectionManager(this);
            RegisterDht(new NullDhtEngine());
            diskManager   = new DiskManager(this, writer);
            listenManager = new ListenManager(this);
            MainLoop.QueueTimeout(TimeSpan.FromMilliseconds(TickLength), () =>
            {
                if (IsRunning && !disposed)
                {
                    LogicTick();
                }
                return(!disposed);
            });
            torrents         = new List <TorrentManager>();
            torrentsReadonly = new ReadOnlyCollection <TorrentManager>(torrents);
            CreateRateLimiters();
            this.peerId = peerId ?? GeneratePeerId();

            localPeerListener      = new LocalPeerListener(this);
            localPeerManager       = new LocalPeerManager();
            LocalPeerSearchEnabled = SupportsLocalPeerDiscovery;
            listenManager.Register(listener);
            // This means we created the listener in the constructor
            if (listener.Endpoint.Port == 0)
            {
                listener.ChangeEndpoint(new IPEndPoint(IPAddress.Any, settings.ListenPort));
            }
        }
Ejemplo n.º 2
0
        internal DiskManager(ClientEngine engine, PieceWriter writer)
        {
            _bufferedReads  = new Queue <BufferedIO>();
            _bufferedWrites = new Queue <BufferedIO>();
            _cache          = new Cache <BufferedIO>(true).Synchronize();
            _engine         = engine;
            ReadLimiter     = new RateLimiter();
            _readMonitor    = new SpeedMonitor();
            _writeMonitor   = new SpeedMonitor();
            WriteLimiter    = new RateLimiter();
            _writer         = writer;

            _loopTask = delegate {
                if (_disposed)
                {
                    return;
                }

                while (_bufferedWrites.Count > 0 && WriteLimiter.TryProcess(_bufferedWrites.Peek().buffer.Length / 2048))
                {
                    BufferedIO write;
                    lock (_bufferLock)
                        write = _bufferedWrites.Dequeue();
                    try
                    {
                        PerformWrite(write);
                        _cache.Enqueue(write);
                    }
                    catch (Exception ex)
                    {
                        if (write.Manager != null)
                        {
                            SetError(write.Manager, Reason.WriteFailure, ex);
                        }
                    }
                }

                while (_bufferedReads.Count > 0 && ReadLimiter.TryProcess(_bufferedReads.Peek().Count / 2048))
                {
                    BufferedIO read;
                    lock (_bufferLock)
                        read = _bufferedReads.Dequeue();

                    try
                    {
                        PerformRead(read);
                        _cache.Enqueue(read);
                    }
                    catch (Exception ex)
                    {
                        if (read.Manager != null)
                        {
                            SetError(read.Manager, Reason.ReadFailure, ex);
                        }
                    }
                }
            };

            IOLoop.QueueTimeout(TimeSpan.FromSeconds(1), () =>
            {
                if (_disposed)
                {
                    return(false);
                }

                _readMonitor.Tick();
                _writeMonitor.Tick();
                _loopTask();
                return(true);
            });
        }