EndianBinaryReader - 输入流的连续更新?

发布于 2024-08-18 15:24:45 字数 897 浏览 14 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁虫ゝ 2024-08-25 15:24:46

我不记得 EndianBinaryReader 是否缓冲 - 您可以覆盖单个 MemoryStream 吗?但说实话,这里额外的对象带来的开销非常小。数据包有多大? (将其放入 MemoryStream 将克隆 byte[])。

我很想使用最简单的可行方法,看看是否存在真正的问题。也许我想要做的一个改变是引入 using (因为它们是 IDisposable):

using(var memoryStream = new MemoryStream(data))
using(var endianbinaryReader = ..blah..) {
    // use it
}

I can't remember whether EndianBinaryReader buffers - you could overwrite a single MemoryStream? But to be honest there is very little overhead from an extra object here. How big are the packets? (putting it into a MemoryStream will clone the byte[]).

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 are IDisposable):

using(var memoryStream = new MemoryStream(data))
using(var endianbinaryReader = ..blah..) {
    // use it
}
天涯沦落人 2024-08-25 15:24:46

您最好的选择可能是重写 .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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文