how to distinguish different Message Types in tcp byte stream

29 views
Skip to first unread message

Alex Syrnikov

unread,
Oct 25, 2022, 10:45:03 PM10/25/22
to Cap'n Proto
Hello.

I have (working on) Rust service and clients ( Rust and Node.js). I want service<->client cap'n'proto message exchange via Tcp socket (no HTTP). Messages will go via tcp connection, but I have different message types (each message is structs Login, or CommandGetData or Error...). So how can I distinguish which message Type I got from tcp bytes stream (and I need to know it's size too).

I decided to prefix each message with UInt32 type and size (8 bytes total). Probably JS can read raw bytes. Or I can prefix Messages with fixed size unpacked message (struct - 24 bytes).
struct MessageHeader {
 type @0: UInt32;
 size @1: UInt32;
}

So what is good solution for this task? I think it was already solved many times. I understand, that RPC will solve this as JSON messages. But I do not want use RPC right now.

Ian Denhardt

unread,
Oct 25, 2022, 11:18:15 PM10/25/22
to Alex Syrnikov, Cap'n Proto
For distinguishing the type, I would recommend wrapping your messages in
a common union type, e.g:

struct Message {
union {
login @0 :Login;
commandGetData @1 :CommandGetData;
# etc.
}
}

The rpc protocol itself does something similar on the wire; see
rpc.capnp.

For determining message size, you shouldn't need to do any extra
framing, as capnp's stream serialization already makes it possible to
determine the size of a message:

https://capnproto.org/encoding.html#serialization-over-a-stream

I'm not familiar enough with the rust & node APIs to provide code
examples, but hopefully this is helpful.

-Ian

Quoting Alex Syrnikov (2022-10-25 22:45:02)
> --
> You received this message because you are subscribed to the Google
> Groups "Cap'n Proto" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [1]capnproto+...@googlegroups.com.
> To view this discussion on the web visit
> [2]https://groups.google.com/d/msgid/capnproto/7b42273c-8e4b-4a70-b661-
> d6dd7b014367n%40googlegroups.com.
>
> Verweise
>
> 1. mailto:capnproto+...@googlegroups.com
> 2. https://groups.google.com/d/msgid/capnproto/7b42273c-8e4b-4a70-b661-d6dd7b014367n%40googlegroups.com?utm_medium=email&utm_source=footer
Reply all
Reply to author
Forward
0 new messages