如何使用C#使用Protobuf进行非阻塞套接字读取? [英] How to do non blocking socket reads with Protobuf using C#?

查看:347
本文介绍了如何使用C#使用Protobuf进行非阻塞套接字读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我想不阻止从网络套接字读取数据. 我可以异步等待套接字读取x字节,一切都很好.

Lets say I want to do non blocking reads from a network socket. I can async await for the socket to read x bytes and all is fine.

但是我该如何通过protobuf将其与反序列化结合起来?

But how do I combine this with deserialization via protobuf?

从流中读取对象必须被阻止吗?也就是说,如果流中的解析器数据太少,则必须在后台进行一些阻止,以便读取器可以获取其所需的所有字节.

Reading objects from a stream must be blocking? that is, if the stream contains too little data for the parser, then there has to be some blocking going on behind the scenes so that the reader can fetch all the bytes it needs.

我想我可以使用lengthprefix分隔符读取第一个字节,然后找出在解析之前必须获取的最小字节数,这是正确的方法吗? 例如如果我的缓冲区是500字节,则等待这500字节,然后解析lengthprefix,如果长度超过500,则再次等待,直到全部读取.

I guess I can use lengthprefix delimiters and read the first bytes and then figure out how many bytes I have to fetch minimum before I parse, is this the right way to go about it? e.g. if my buffer is 500 bytes, then await those 500 bytes, and parse the lengthprefix and if the length is more than 500 then wait again, untill all of it is read.

结合无阻塞IO和protobuf解析的惯用方式是什么?

What is the idiomatic way to combine non blocking IO and protobuf parsing?

(我现在正在使用Jon Skeet的实现 http://code.google.com/p/protobuf-csharp-port/)

(I'm using Jon Skeet's implementation right now http://code.google.com/p/protobuf-csharp-port/)

推荐答案

作为一般规则,序列化程序通常不包含DeserializeAsync方法,因为这确实很难做到(真的至少有效).如果数据大小适中,那么我建议使用异步代码缓冲所需数量的数据-然后在所有所需数据可用时反序列化.如果数据非常大,并且您不想将所有内容都缓存在内存中,请考虑在工作线程上使用常规的同步反序列化.

As a general rule, serializers don't often contain a DeserializeAsync method, because that is really really hard to do (at least, efficiently). If the data is of moderate size, then I would advise to buffer the required amount of data using asynchronous code - and then deserialize when all of the required data is available. If the data is very large and you don't want to have to buffer everything in memory, then consider using a regular synchronous deserialize on a worker thread.

(请注意,这是特定于实现的,但是如果您使用的序列化器 支持异步反序列化,请确保使用该序列化器)

(note that note of this is implementation specific, but if the serializer you are using does support an async deserialize: then sure, use that)

这篇关于如何使用C#使用Protobuf进行非阻塞套接字读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆