EndianBinaryReader - 输入流的连续更新?
我正在尝试使用 Jon Skeet 作为他的 misc utils 库。它非常适合我对它的两种用途。
从网络流 (TCPClient
) 中进行第一次读取,我坐在循环中读取传入的数据。我可以创建一个 EndianBinaryReader
,然后将其处理掉在应用程序关闭时。我通过传入 TCPClient.GetStream
构造 EndianBinaryReader
。
我现在尝试在从 UdpClient 读取时执行相同的操作,但这没有流。连接少。所以我得到的数据就像这样
byte[] data = udpClientSnapShot.Receive(ref endpoint);
我可以将这些数据放入内存流中
var memoryStream = new MemoryStream(data);
,然后创建 EndianBinaryReader
var endianbinaryReader = new EndianBinaryReader(
new BigEndianBitConverter(), memoryStream,Encoding.ASCII);
但这意味着我每次执行读取时都必须创建一个新的 endian 读取器。有没有一种方法可以让我只创建一个流,然后用来自 udp 客户端的数据不断更新输入流?
I am trying to use the EndianBinaryReader
and EndianBinaryWriter
that Jon Skeet wrote as part of his misc utils lib. It works great for the two uses I have made of it.
The first reading from a Network Stream (TCPClient
) where I sit in a loop reading the data as it comes in. I can create a single EndianBinaryReader
and then just dispose of it on the shut down of the application. I construct the EndianBinaryReader
by passing the TCPClient.GetStream
in.
I am now trying to do the same thing when reading from a UdpClient but this does not have a stream as it is connection less. so I get the data like so
byte[] data = udpClientSnapShot.Receive(ref endpoint);
I could put this data into a memory stream
var memoryStream = new MemoryStream(data);
and then create the EndianBinaryReader
var endianbinaryReader = new EndianBinaryReader(
new BigEndianBitConverter(), memoryStream,Encoding.ASCII);
but this means I have to create a new endian reader every time I do a read. Id there a way where I can just create a single stream that I can just keep updateing the inputstream with the data from the udp client?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不记得
EndianBinaryReader
是否缓冲 - 您可以覆盖单个MemoryStream
吗?但说实话,这里额外的对象带来的开销非常小。数据包有多大? (将其放入MemoryStream
将克隆byte[]
)。我很想使用最简单的可行方法,看看是否存在真正的问题。也许我想要做的一个改变是引入
using
(因为它们是IDisposable
):I can't remember whether
EndianBinaryReader
buffers - you could overwrite a singleMemoryStream
? But to be honest there is very little overhead from an extra object here. How big are the packets? (putting it into aMemoryStream
will clone thebyte[]
).I'd be tempted to use the simplest thing that works and see if there is a real problem. Probably the one change I would make is to introduce
using
(since they areIDisposable
):您最好的选择可能是重写 .NET Stream 类以提供自定义功能。该类被设计为可通过自定义行为覆盖。
由于成员数量众多,这可能看起来令人畏惧,但它比看起来容易。有许多布尔属性,例如“CanWrite”等。覆盖它们并让它们全部返回“false”,除了您的读者需要的功能(可能 CanRead 是您唯一需要为 true 的功能。)
然后,只需覆盖Stream 的帮助 并让不受支持的方法返回“UnsupportedException”(而不是默认的“NotImplementedException”)。
实现 Read 方法以从缓冲的 UDP 数据包中返回数据(可能使用缓冲区链接列表) ,当您读取已用缓冲区时将其设置为“null”,以便内存占用不会无限制地增长。
Your best option is probably an override of the .NET Stream class to provide your custom functionality. The class is designed to be overridable with custom behavior.
It may look daunting because of the number of members, but it is easier than it looks. There are a number of boolean properties like "CanWrite", etc. Override them and have them all return "false" except for the functionality that your reader needs (probably CanRead is the only one you need to be true.)
Then, just override all of the methods that start with the phrase "When overridden in a derived class" in the help for Stream and have the unsupported methods return an "UnsupportedException" (instead of the default "NotImplementedException".
Implement the Read method to return data from your buffered UDP packets using perhaps a linked list of buffers, setting used buffers to "null" as you read past them so that the memory footprint doesn't grow unbounded.