简体   繁体   English

写入时的 MonoTorrent 事件

[英]MonoTorrent Event when piece written

I've been trying to use the C# MonoTorrent Library, but the lack of a documentation isn't helping.我一直在尝试使用 C# MonoTorrent 库,但缺少文档并没有帮助。 I'm trying to stream a file, but to do that, I somehow need an event whenever a Piece is written to file, or something similar.我正在尝试 stream 一个文件,但要做到这一点,每当将 Piece 写入文件或类似的东西时,我都需要一个事件。

I know there is an event which gets triggered whenever a Piece has been hashed, but it's not so useful when I need the actual content.我知道只要对 Piece 进行散列处理,就会触发一个事件,但是当我需要实际内容时它就不是那么有用了。

So I want to ask how I can know when a piece has been written to a file, so I can parse that and then stream that movie.所以我想问我怎么知道一个片段何时被写入文件,所以我可以解析它,然后 stream 那部电影。

I already looked at the TorrentManager the ClientEngine the DiskManager and I haven't found anything useful in any of these classes nor any other Manager class.我已经查看了TorrentManagerClientEngineDiskManager ,但在这些类中的任何一个以及任何其他 Manager class 中都没有发现任何有用的东西。 Now is this feature just hidden somewhere or do I have to do something different to get the pieces that were downloaded?现在这个功能只是隐藏在某个地方还是我必须做一些不同的事情才能获得下载的作品?

The PieceHashed event is what you need. PieceHashed 事件是您所需要的。 When that event is raised you can be guaranteed that the data associated with that piece has been received, validated and written to the DiskManager.当该事件引发时,您可以保证与该片段相关的数据已被接收、验证并写入 DiskManager。

If a MemoryWriter is being used that data may not be written to the underlying harddrive/SSD when the event is raised.如果正在使用MemoryWriter ,则在引发事件时,数据可能不会写入底层硬盘驱动器/SSD。 To guarantee that you'll need to call the FlushAsync method, passing in the TorrentManager and piece index.为保证您需要调用FlushAsync方法,传入 TorrentManager 和片段索引。 If a piece is 256kB in size and there are three files of length 200kb, 50kb and 6kb contained within piece 6, all three of those files will be flushed if you pass in '6' as the piece index.如果一个片段大小为 256kB,并且片段 6 中包含三个长度分别为 200kb、50kb 和 6kb 的文件,那么如果您传入“6”作为片段索引,所有这三个文件都将被刷新。 If you call the overload which doesn't take a PieceIndex it will instead flush every file.如果您调用不采用 PieceIndex 的重载,它将改为刷新每个文件。

If you're writing something like a SlidingWindowPicker then you should probably only call FlushAsync when a piece in the high priority set has been downloaded.如果您正在编写类似SlidingWindowPicker的东西,那么您应该只在下载了高优先级集中的片段时才调用 FlushAsync。 Any call to flush will flush all pending data for a given file (or all files).任何对 flush 的调用都会刷新给定文件(或所有文件)的所有待处理数据。 If the only data you have is from the very end of the torrent then flushing it immediately won't impact your ability to stream, but it may increase the overheads.如果您拥有的唯一数据来自洪流的最后,那么立即刷新它不会影响您使用 stream 的能力,但可能会增加开销。

There is an alternative, which is to implement IPieceWriter and create a wrapper which flushes whenever an appropriate piece is written.还有一种替代方法,即实现 IPieceWriter 并创建一个包装器,该包装器在写入适当的片段时刷新。

The existing MemoryWriter shows how to create a wrapper.现有的 MemoryWriter 展示了如何创建包装器。 It's Write method is implemented as follows: https://github.com/mono/monotorrent/blob/2209922c4159e394242c6c337401571312642b6e/src/MonoTorrent/MonoTorrent.Client.PieceWriters/MemoryWriter.cs#L118-L123 .它的Write方法实现如下: https://github.com/mono/monotorrent/blob/2209922c4159e394242c6c337401571312642b6e/src/MonoTorrent/MonoTorrent.Client.PieceWriters-L123.Writer .

If you wanted to write something to automatically flush pieces you'd do something like:如果你想写一些东西来自动刷新碎片,你会做这样的事情:

public void Write(TorrentFile file, long offset, byte[] buffer, int bufferOffset, int count, bool forceWrite)
{
    Writer.Write(file, offset, buffer, bufferOffset, count);
    Writer.Flush(file);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM