Node.js, protobuffer, buffer.lentgh.. how to send structured buffer/message trough tcp?

℡╲_俬逩灬. 提交于 2020-01-24 12:00:46

问题


I had wrote a node/ssjs program that makes:

  • Tcp connection to data server(Apache MIMA), using TLS module. (OK)
  • Encode/decode(serialize/deserialize) messages through protobuffer module.. (OK)
  • Send serialized message to server and get response. (Not OK).

The server´s manual types:

Structure of message:
[ Length | Header length | Header (| Body length |Body) ]
Length – message length = fixed size (4 bytes). Note that this is only the size of
         following message (not the prefix itself);
Header  length – fixed size (4 bytes);
Header – contains message metadata (e.g. messageId);
Body length – fixed size (4 bytes, optional). If body is not required, body size is 
              not serialized. In such case, message is 4 (message length) + 4 
              (header length) + length(header) bytes long.
Body – the message payload (optional). If body is not present (e.g. response
       notification without data) it is not serialized.

Length [bytes]  Content    
4               Whole message length       
4               Header message length (H_LEN)      
H_LEN           Protocol Buffers encoded MessageHeaderProto    
4               Body message length (B_LEN), optional      
B_LEN           Protocol Buffers encoded Message

My questions are:

  • How to get the 4-bytes-length of a protobuffer?
  • How to concatenate those buffers before send?
  • How to send?
  • Is it possible to do with JavaScript? Observation: The server uses Little Endian byte order, client side too.

Thank you all...

来源:https://stackoverflow.com/questions/17973201/node-js-protobuffer-buffer-lentgh-how-to-send-structured-buffer-message-trou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!