开发者

EndianBinaryReader - Contious update of the input stream?

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 c开发者_运维知识库an just create a single stream that I can just keep updateing the inputstream with the data from the udp client?


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
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜