Asynchronous BinaryReader and BinaryWriter in .Net?

时光怂恿深爱的人放手 提交于 2020-01-11 05:01:04

问题


I'd like to read and write bytes and structured value types, asynchronously, without having to worry about decoders and byte-shifting: is there something out there that would allow me to do that?


回答1:


It's not possible with BinaryReader or BinaryWriter. You could read concurrently from the underlying BaseStream, but the documentation states the following:

Using the underlying stream while reading or while using the BinaryReader can cause data loss and corruption. For example, the same bytes might be read more than once, bytes might be skipped, or character reading might become unpredictable.

Therefore the only way is to roll your own implementation. But the benefit of doing that is debatable. Marco Greg from Microsoft added the following comment to the blog article Should I expose asynchronous wrappers for synchronous methods?:

Jon: The reason that the BinaryReader/Writer do not have XxxAsync methods is that the methods on those types typically read/write only very few bytes from an underlying stream that has been previously opened. In practice, the data is frequently cached and the time required to fetch the data from the underlying source is typically so small that it is not worth it doing it asynchronously.

Notably, there are some methods on these types that in some circumstances may transfer larger amounts of data (e.g. ReadString). Further down the line, Async versions for those methods may or may not be added, but it is unlikely it will happen in the immediate future.

In general, you should only consider Async IO methods if the amount of data you are reading is significant (at least several hundreds or thousands of bytes), or if you are accessing a resource for the first time (e.g. a first read from a file may require to spin up the disk even if you are reading one byte).

This sounds reasonable. If you do need a solution there are several workarounds apart from rolling your own BinaryReader/BinaryWriter. You can run it in a separate thread (which can be inefficient) or if you're willing to change the file format or wire protocol you could use this pattern (pseudo-code):

//read packet length
await stream.ReadAsync(buffer);
var packetLength=convertToInt(buffer);

//read complete packet asynchronously
await stream.ReadAsync(buffer,packetLength);

//process packet with BinaryReader
using(var br=new BinaryReader(new MemoryStream(buffer))
{
  //...
}

Please note that this pattern is only useful if the complete buffer easily fits in memory and that performance might suffer.



来源:https://stackoverflow.com/questions/10315316/asynchronous-binaryreader-and-binarywriter-in-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!