Asynchronous approach to using streams with sockets
I've always built my own serializers/deserializers to be able to use BeginReceive/EndReceive with sockets to avoid using one thread per connected socket. It's a quite time consuming task.
I've started to look at protobuf-net and want to used it 开发者_运维问答with async sockets without having to write serialization/deserialization myself. I would prefer if I don't have to use one thread per socket to wrap blocking operations.
Looking at the quickstart they do exactly that (one thread per client). Are there no other way?
My question isn't really about protobuf-net, but about serialization and sockets in general. But examples for protobuf-net is most welcome.
That is just a basic example. protobuf-net is the serialization layer, not the messaging layer. If you have a specific way you want to handle the messaging: go for it. All you need to do is, during your stream processing, buffer up an entire message (perhaps into a MemoryStream
), then process it via protobuf-net when the data is available.
The difference would be that instead of using the *WithLengthPrefix
methods directly, you might want to handle to payload-length separately, so that your code knows how much data to buffer before processing.
精彩评论