zeromq / netmq

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

NetMQ to CppZmq empty request #992

Closed amastrobera closed 2 years ago

amastrobera commented 3 years ago

Environment

NetMQ Version:    4.0.1.6
CppZmq: https://zeromq.org/get-started/?language=cpp&library=cppzmq# latest
Operating System:  Windows 10 (.Net) / Ubuntu 18.04 (cpp)
.NET Version:     Framework 4.8

Expected behaviour

I am sending a protocol buffer message from .Net client to Cpp server. Both are using ZeroMQ but with different APIs.

The message looks something like this

req.proto

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

this is the .Net client

using NetMQ;
using NetMQ.Sockets;
/// ....
      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();
        }

this is the cpp server

#include <zmq.hpp>
// ... 
        zmq::context_t ctx;
        zmq::socket_t socket(ctx, zmq::socket_type::rep);
        socket.bind("tcp://localhost:8000");

        while (true) {
          zmq::message_t msg_req;
          auto ret = socket.recv(msg_req, zmq::recv_flags::none);
          std::string msg_req_buf = std::string(static_cast<char*>(msg_req.data()), msg_req.size());

          // handle response
          PBRequest req;
          req.ParseFromString(msg_req_buf);
          std::cout << "received request." << std::endl;
          std::cout << "   > num: " << req.num() << std::endl;
          std::cout << "   > text: " << req.text() << std::endl;
        }

should (obviously) spin and print

received request
  > num = 10
  > text = ciao

Actual behaviour

received request
  > num = 0
  > text = 

Steps to reproduce the behaviour

  1. install CppZMQ like in the link at the top of the issue.
    
    # I. install cppzmq
    git clone https://github.com/zeromq/libzmq.git
    cd libzmq
    mkdir build
    cd build
    cmake ..
    sudo make -j4 install

II. install cppzmq wrapper

git clone https://github.com/zeromq/cppzmq.git cd cppzmq mkdir build cd build cmake .. sudo make -j4 install

2. install protobuf on linux

sudo apt-get install \ libprotobuf-dev \ protobuf-compiler

3. add the protofile, write the code of the server as above
4. compile and run
5. use NetMQ with these libraries
<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" />

6. add the protofile, build and run
dxdjgl commented 3 years ago

See answer in #991