zeromq / netmq

A 100% native C# implementation of ZeroMQ for .NET
Other
2.95k stars 744 forks source link

Protocol message contained a tag with an invalid wire type. #991

Closed amastrobera closed 2 years ago

amastrobera commented 3 years ago

Environment

NetMQ Version:    4.0.1.6
Operating System: Windows 10
.NET Version:     Framework 4.8

Expected behaviour

I am sending a message from .Net MQ client to a .Net MQ server.

The message is a protocol buffer class structure.

req.proto

message PBRequest {
    int num = 1;
    string text = 2;
}

client

        PBRequest req = new PBRequest(); 
        req.Num = 10;
        req.Text = "ciao";
        Console.WriteLine("sending request" + req.ToString());

        using (var socket = new RequestSocket()) {

          socket.Connect("tcp://localhost:8000"); // any address would do, as long as it's available
          socket.Options.HeartbeatTimeout = TimeSpan.FromMilliseconds(10*1000);
          socket.Options.Linger = TimeSpan.Zero;  // do not block on time out

          NetMQMessage msg = new NetMQMessage();
          msg.Append(req.ToString(), Encoding.UTF8);
          socket.SendFrame(msg.ToString());
          msg.Clear();
        }

server

      using (var socket = new ResponseSocket()) {
        socket.Bind("tcp://localhost:8000");
        while (true) {
            var smsg = socket.ReceiveFrameString(); // there is only one frame
            PBRequest req = PBRequest.Parser.ParseFrom(Encoding.UTF8.GetBytes(smsg)); // protobuf function
            Console.WriteLine("received request" + req.ToString());
        }
     } 

Actual behaviour

The server is receiving something that throws an error like

Protocol message contained a tag with an invalid wire type.

Steps to reproduce the behaviour

  1. create 1 project "client", and 1 project "server"
  2. add the protocol buffer libraries and netmq to both in .csproj

    <PackageReference Include="Google.Protobuf" Version="3.15.6" />
    <PackageReference Include="Grpc" Version="2.36.4" />
    <PackageReference Include="Grpc.Tools" Version="2.36.4"/> 
    
    <PackageReference Include="NetMQ" Version="4.0.1.6" />
  3. write the code client and server as above, you will need to reference the proto file before compiling the solution
  4. compile the solution (or project by project)
  5. run the server first
  6. run the client 8 check the result
dxdjgl commented 3 years ago

This is not a NetMQ issue this is related to protobuf. However I am quite sure you are not serializing you message into protobuf since tostring returns a json representation of the object. You should most likely use ToByteString or ToByteArray instead, for further questions please address the protobuf community.