views:

47

answers:

1

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.

+1  A: 

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.

Marc Gravell
The serializer/deserializer methods in Serializer is typed, right?. I don't know which protobuf message I will receive and can therefore not use them, are there any alternatives?
jgauffin
Marc's answer is correct. Buffering to a "variant" length is not easy, though.
Stephen Cleary
@jgauffin - there is a mechanism via `Serializer.NonGeneric` that is designed **exactly** for this, with a mechanism to provide the type on-the-fly. There was a recent question with an example of this here on SO.
Marc Gravell
excellent. thanks. i'm looking at WCF over TCP with protbuf-net now.
jgauffin