0

I Have a simple class with two fields in proto file: (proto3)

enum MaestroMsgType
{
    EVAL = 0;
    GET_ACK = 1;
    GET_AN = 2;
}

message MaestroMsg
{
    MaestroMsgType msgType = 1;
    string maestroMsg = 2;
}

I'm trying to send (with netMQ but that less matter) the class form C# to python. But in python when trying to make the data back into class format it fails.

The sending in C#:

  MaestroMsg maestroMsg = new MaestroMsg
  {
       MaestroMsg_ = "someMessage",
       MsgType = MaestroMsgType.GET_AN,
   };
   string messageToSend = maestroMsg.ToString();
   NetMQMessage msg = new NetMQMessage();
   msg.Append(messageToSend);
   _pubSocket.SendMultipartMessage(msg);

The code in python receiving the message:

 received_message = sub_socket.recv_multipart()
 maestroMsg_object = MaestroMsg()
 maestroMsg_object.ParseFromString(received_message.encode())

And I get this error message:

 google.protobuf.message.DecodeError: Error parsing message

I got no idea what I'm doing wrong. Thanks for answers.

Izik
  • 746
  • 1
  • 9
  • 25
  • Is it possible to post the code for the `CreateNetMqMessage` method? – kurtmkurtm May 03 '19 at 05:28
  • 1
    a: your example code doesn't use `messageToSend ` - is that a typo - is `msg` the same thing? b: which format are you using? There are two protobuf formats: JSON and binary, and your use of `ToString()` is confusing to me - is that generating the JSON? (it certainly can't be the binary). So I suspect that you're trying to use the JSON on one side and the binary on the other, or: you're simply using the wrong serialization methods, and `ToString()` here is simply wrong, and/or you need to tell the python code to expect JSON instead of binary. Do you have an example of the actual payload here? – Marc Gravell May 03 '19 at 11:19
  • @MarcGravell Thanks. I did used the wrong serialization. In python SerializeToString() return binary format and not string so I got confused with the C#, whereas I just need to send it as ByteArray. about the "msg" it is no t a typo but I edited the API to something more relevant for NetMQ API. – Izik May 06 '19 at 15:25

1 Answers1

1

So thanks go to @MarcGravell, the answer was just wrong use of API:

C#:

var maestroMsg = new MaestroMsg
{
    MaestroMsg_ = maestroMsgStr,
    MsgType = maestroMsgType,
};

var messageToSend = maestroMsg.ToByteArray();
var msg = new NetMQMessage();
msg.Append(messageToSend);
_pubSocket.SendMultipartMessage(msg);

python:

received_message = sub_socket.recv_multipart()
maestro_object = MaestroMsg()
maestro_object.ParseFromString(received_message)
g t
  • 7,287
  • 7
  • 50
  • 85
Izik
  • 746
  • 1
  • 9
  • 25