public EngineTestRig(string savePath, int piecelength = 256*1024, PieceWriter writer = null) { if (writer == null) writer = new MemoryWriter(new NullWriter()); _listener = new CustomListener(); _engine = new ClientEngine(new EngineSettings(), _listener, writer); _torrentDict = CreateTorrent(piecelength); _torrent = Torrent.Load(_torrentDict); _manager = new TorrentManager(_torrent, savePath, new TorrentSettings()); _engine.Register(_manager); //manager.Start(); }
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; }); }
public EngineTestRig(string savePath, PieceWriter writer) : this(savePath, 256 * 1024, writer) { }
public EngineTestRig(string savePath, PieceWriter writer) : this(savePath, 256*1024, writer) { }
public ClientEngine(EngineSettings settings, PieceWriter writer) : this(settings, new PeerListener(new IPEndPoint(IPAddress.Any, settings.ListenPort)), writer) { }
public ClientEngine(EngineSettings settings, PieceWriter writer) : this(settings, new SocketListener(new IPEndPoint(IPAddress.Any, 0)), writer) { }
void CreateSingleFileTorrent(BEncodedDictionary dictionary, List <TorrentFile> mappings, PieceWriter writer, string name) { BEncodedDictionary infoDict = (BEncodedDictionary)dictionary ["info"]; infoDict.Add("length", new BEncodedNumber(mappings [0].Length)); if (mappings [0].MD5 != null) { infoDict ["md5sum"] = (BEncodedString)mappings [0].MD5; } }
void CreateMultiFileTorrent(BEncodedDictionary dictionary, List <TorrentFile> mappings, PieceWriter writer, string name) { BEncodedDictionary info = (BEncodedDictionary)dictionary ["info"]; #if NETSTANDARD1_5 List <BEncodedValue> files = mappings.ConvertAll <TorrentFile, BEncodedValue>(ToFileInfoDict).ToList(); #else List <BEncodedValue> files = mappings.ConvertAll <BEncodedValue>(ToFileInfoDict); #endif info.Add("files", new BEncodedList(files)); }
byte [] CalcPiecesHash(List <TorrentFile> files, PieceWriter writer) { byte [] buffer = null; int bufferRead = 0; long fileRead = 0; long overallRead = 0; long overallTotal = 0; MD5 md5Hasher = null; SHA1 shaHasher = null; List <byte> torrentHashes = null; shaHasher = HashAlgoFactory.Create <SHA1> (); torrentHashes = new List <byte> (); overallTotal = Toolbox.Accumulate <TorrentFile> (files, delegate(TorrentFile m) { return(m.Length); }); long pieceLength = PieceLength; buffer = new byte [pieceLength]; if (StoreMD5) { md5Hasher = HashAlgoFactory.Create <MD5> (); } try { foreach (TorrentFile file in files) { fileRead = 0; if (md5Hasher != null) { md5Hasher.Initialize(); } while (fileRead < file.Length) { int toRead = (int)Math.Min(buffer.Length - bufferRead, file.Length - fileRead); int read = writer.Read(file, fileRead, buffer, bufferRead, toRead); if (md5Hasher != null) { md5Hasher.TransformBlock(buffer, bufferRead, read, buffer, bufferRead); } shaHasher.TransformBlock(buffer, bufferRead, read, buffer, bufferRead); bufferRead += read; fileRead += read; overallRead += read; if (bufferRead == buffer.Length) { bufferRead = 0; shaHasher.TransformFinalBlock(buffer, 0, 0); torrentHashes.AddRange(shaHasher.Hash); shaHasher.Initialize(); } RaiseHashed(new TorrentCreatorEventArgs(file.Path, fileRead, file.Length, overallRead, overallTotal)); } if (md5Hasher != null) { md5Hasher.TransformFinalBlock(buffer, 0, 0); md5Hasher.Initialize(); file.MD5 = md5Hasher.Hash; } } if (bufferRead > 0) { shaHasher.TransformFinalBlock(buffer, 0, 0); torrentHashes.AddRange(shaHasher.Hash); } } finally { if (shaHasher != null) { shaHasher.Clear(); } if (md5Hasher != null) { md5Hasher.Clear(); } } return(torrentHashes.ToArray()); }
void CreateMultiFileTorrent(BEncodedDictionary dictionary, List <TorrentFile> mappings, PieceWriter writer, string name) { BEncodedDictionary info = (BEncodedDictionary)dictionary ["info"]; List <BEncodedValue> files = mappings.ConvertAll <BEncodedValue> (ToFileInfoDict); info.Add("files", new BEncodedList(files)); }
// PUT: api/PieceWriter/5 public void Put(Guid id, PieceWriter pieceWriter) { pieceWriter.Update(); }
// POST: api/PieceWriter public void Post(PieceWriter pieceWriter) { pieceWriter.Insert(); }
byte [] CalcPiecesHash(List <TorrentFile> files, PieceWriter writer, CancellationToken token) { int bufferRead = 0; long fileRead = 0; long overallRead = 0; MD5 md5Hasher = null; var shaHasher = HashAlgoFactory.Create <SHA1> (); var torrentHashes = new List <byte> (); var overallTotal = files.Sum(t => t.Length); var buffer = new byte [PieceLength]; if (StoreMD5) { md5Hasher = HashAlgoFactory.Create <MD5> (); } try { foreach (TorrentFile file in files) { fileRead = 0; md5Hasher?.Initialize(); while (fileRead < file.Length) { int toRead = (int)Math.Min(buffer.Length - bufferRead, file.Length - fileRead); int read = writer.Read(file, fileRead, buffer, bufferRead, toRead); token.ThrowIfCancellationRequested(); md5Hasher?.TransformBlock(buffer, bufferRead, read, buffer, bufferRead); shaHasher.TransformBlock(buffer, bufferRead, read, buffer, bufferRead); bufferRead += read; fileRead += read; overallRead += read; if (bufferRead == buffer.Length) { bufferRead = 0; shaHasher.TransformFinalBlock(buffer, 0, 0); torrentHashes.AddRange(shaHasher.Hash); shaHasher.Initialize(); } RaiseHashed(new TorrentCreatorEventArgs(file.Path, fileRead, file.Length, overallRead, overallTotal)); } md5Hasher?.TransformFinalBlock(buffer, 0, 0); md5Hasher?.Initialize(); file.MD5 = md5Hasher?.Hash; } if (bufferRead > 0) { shaHasher.TransformFinalBlock(buffer, 0, 0); torrentHashes.AddRange(shaHasher.Hash); } } finally { shaHasher.Dispose(); md5Hasher?.Dispose(); } return(torrentHashes.ToArray()); }
public ClientEngine(EngineSettings settings, PieceWriter writer, string peerId = null) : this(settings, new SocketListener(new IPEndPoint(IPAddress.Any, 0)), writer, peerId) { }
internal DiskManager(ClientEngine engine, PieceWriter writer) { this.bufferedReads = new Queue <BufferedIO>(); this.bufferedWrites = new Queue <BufferedIO>(); this.cache = new Cache <BufferedIO>(true).Synchronize(); this.engine = engine; this.readLimiter = new RateLimiter(); this.readMonitor = new SpeedMonitor(); this.writeMonitor = new SpeedMonitor(); this.writeLimiter = new RateLimiter(); this.writer = writer; LoopTask = delegate { if (disposed) { return; } while (this.bufferedWrites.Count > 0 && writeLimiter.TryProcess(bufferedWrites.Peek().buffer.Length / 2048)) { BufferedIO write; lock (bufferLock) write = this.bufferedWrites.Dequeue(); try { PerformWrite(write); cache.Enqueue(write); } catch (Exception ex) { logger.Error("Write error: {0}", ex.Message); if (write.Manager != null) { SetError(write.Manager, Reason.WriteFailure, ex); } } } while (this.bufferedReads.Count > 0 && readLimiter.TryProcess(bufferedReads.Peek().Count / 2048)) { BufferedIO read; lock (bufferLock) read = this.bufferedReads.Dequeue(); try { PerformRead(read); cache.Enqueue(read); } catch (Exception ex) { logger.Error("Read error: {0}", ex.Message); if (read.Manager != null) { SetError(read.Manager, Reason.ReadFailure, ex); } } } }; IOLoop.QueueTimeout(TimeSpan.FromSeconds(1), delegate { if (disposed) { return(false); } readMonitor.Tick(); writeMonitor.Tick(); LoopTask(); return(true); }); }
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)); }
byte [] CalcPiecesHash(List <TorrentFile> files, PieceWriter writer) { byte [] buffer = null; int bufferRead = 0; long fileRead = 0; long overallRead = 0; long overallTotal = 0; MD5 md5Hasher = null; SHA1 shaHasher = null; List <byte> torrentHashes = null; #if NETSTANDARD1_5 shaHasher = SHA1.Create(); #else shaHasher = HashAlgoFactory.Create <SHA1> (); #endif torrentHashes = new List <byte> (); overallTotal = Toolbox.Accumulate <TorrentFile> (files, delegate(TorrentFile m) { return(m.Length); }); long pieceLength = PieceLength; buffer = new byte [pieceLength]; if (StoreMD5) #if NETSTANDARD1_5 { md5Hasher = MD5.Create(); //HashAlgoFactory.Create<MD5> (); } #else { md5Hasher = HashAlgoFactory.Create <MD5> (); } #endif try { foreach (TorrentFile file in files) { fileRead = 0; if (md5Hasher != null) { md5Hasher.Initialize(); } while (fileRead < file.Length) { int toRead = (int)Math.Min(buffer.Length - bufferRead, file.Length - fileRead); int read = writer.Read(file, fileRead, buffer, bufferRead, toRead); if (md5Hasher != null) { #if NETSTANDARD1_5 var hash = md5Hasher.ComputeHash(buffer, bufferRead, read); hash.CopyTo(buffer, bufferRead); #else md5Hasher.TransformBlock(buffer, bufferRead, read, buffer, bufferRead); #endif } #if NETSTANDARD1_5 var shaHash = shaHasher.ComputeHash(buffer, bufferRead, read); shaHash.CopyTo(buffer, bufferRead); #else shaHasher.TransformBlock(buffer, bufferRead, read, buffer, bufferRead); #endif bufferRead += read; fileRead += read; overallRead += read; if (bufferRead == buffer.Length) { bufferRead = 0; #if NETSTANDARD1_5 var hash = shaHasher.ComputeHash(buffer, 0, 0); torrentHashes.AddRange(hash); #else shaHasher.TransformFinalBlock(buffer, 0, 0); torrentHashes.AddRange(shaHasher.Hash); #endif shaHasher.Initialize(); } RaiseHashed(new TorrentCreatorEventArgs(file.Path, fileRead, file.Length, overallRead, overallTotal)); } if (md5Hasher != null) { #if NETSTANDARD1_5 var hash = md5Hasher.ComputeHash(buffer, 0, 0); md5Hasher.Initialize(); file.MD5 = hash; #else md5Hasher.TransformFinalBlock(buffer, 0, 0); md5Hasher.Initialize(); file.MD5 = md5Hasher.Hash; #endif } } if (bufferRead > 0) { #if NETSTANDARD1_5 var hash = shaHasher.ComputeHash(buffer, 0, 0); torrentHashes.AddRange(hash); #else shaHasher.TransformFinalBlock(buffer, 0, 0); torrentHashes.AddRange(shaHasher.Hash); #endif } } finally { #if NETSTANDARD1_5 if (shaHasher != null) { shaHasher.Dispose(); } if (md5Hasher != null) { md5Hasher.Dispose(); } #else if (shaHasher != null) { shaHasher.Clear(); } if (md5Hasher != null) { md5Hasher.Clear(); } #endif } return(torrentHashes.ToArray()); }