2

I'm trying to write a c++ class for communicating between two computers via ZeroMQ.

To be able to handle errors I am trying to read the return values of the .recv()- and .send()- methods but I get the following error

error: cannot convert 'zmq::send_result_t' {aka 'zmq::detail::trivial_optional<unsigned int>'} to 'int' in assignment ret = msocket.send(reply, zmq::send_flags::none);

The code looks like this:

Publisher::Publisher(dataHandler & mdatahandler) :datahandler(mdatahandler)
{
    // construct a REP (reply) socket and bind to interface

    socketState.bind("tcp://*:5555");
    //socketAngles.bind("tcp://*:5556");
    //socketCurrents.bind("tcp://*:5557");
}

Publisher::~Publisher()
{
    socketState.close();
    //socketAngles.close();
    //socketCurrents.close();
}

std::string Publisher::transfer(zmq::socket_t& msocket, std::string replyString,
    int receiveFlag = 0)
{
    zmq::send_result_t ret = 0;
    if (receiveFlag)
    {
        zmq::message_t receivedData;
        ret = msocket.recv(receivedData, zmq::recv_flags::none);
        if (verbose)
        {
            std::cout << "Received " << receivedData.to_string() << std::endl;
        }
        return receivedData.to_string();
    }
    zmq::message_t reply{ replyString.cbegin(), replyString.cend() };

    // send the reply to the client
    ret = msocket.send(reply, zmq::send_flags::none);

    if (ret == -1)
    {
        std::cout << zmq_strerror(errno) << std::endl;
    }
}

the socket is defined as

zmq::context_t context{ 1 };
zmq::socket_t socketState{ context, ZMQ_REP };

How can I reliably catch errors and is there a better way of handling errors if they occur?

Edit:
I added the zmq::send_result_t but how can I do anything with it? I can't compare it to anything and I can't print it either.

user3666197
  • 1
  • 6
  • 50
  • 92
FR_MPI
  • 111
  • 1
  • 9

2 Answers2

1

zmq::recv_result_t is based on trivial_optional<unsigned int>.

trivial_optional<T> is a class template that encapsulates that it may or may not contain a value. Instances of type trivial_optional<unsigned int> are interrogated with bool trivial_optional<unsigned int>::has_value() to see if there is a value.

If there is a value it is extracted using T trivial_optional<unsigned int>::operator*() or T trivial_optional<unsigned int>::value().

zmq::recv_result_t ret(msocket.recv(receivedData, zmq::recv_flags::none));
if (ret.has_value() && (EAGAIN == ret.value()))
{
    // msocket had nothing to read and recv() timed out
    ....
}
-2

Q : "How can I reliably catch errors and is there a better way of handling errors if they occur?"

This can be split into two questions:


Part A: "How can I reliably catch errors"

First understand the language tools. There are exceptions related tools and best-practices and other Do-s and Don't-s. Obey them.

Part B: "a better way of handling errors"

The best way of handling errors is by avoiding them completely - this does not save the Planet ( you can read about Ms. Margaret Hamilton ( she saved lives and national pride on doing this correctly for the Apollo Guidance Computer software ) and her genuine methodology, that saves unavoidable principally colliding cases ).

The next, a lot weaker strategy is to design architectures ( then code ), that thoroughly inspects the state of the system ( return values, RTT-times and other factors ), so as to be continuously ready to handle Exception, as it happens and in full-context with the state of the system ( not to find yourself surprised as standing as uninformed as a blind person in the middle of the crossroads, once the Exception was thrown ... and it will be thrown, at some later time, so be prepared a-priori, not panicking ex-post, resorting to but chaotic ad-hoc options )

Solution :

Step 1)
Understand and master the language tools.

Step 2)
Understand and master the ZeroMQ tools ( No REP can ever start with .send() )

Step 3)
Understand and master the published ZeroMQ API, there are all details on details needed for successful Exception handling details ( preventive error-state indications - hidden gems in { EINVAL | ETERM | ENOTSOCK | EINTR | ... } error-states, explained for each and every API call method, in due context for each one such method.

If still not convinced, at least read the fabulous Pieter Hintjens' book "Code Connected, Volume 1", there one will get the roots of what the Zen-of-Zero is all about.

halfer
  • 19,824
  • 17
  • 99
  • 186
user3666197
  • 1
  • 6
  • 50
  • 92