Example #1
0
/// Send messages between a client and server synchronously. 
///
/// @see overview of zmq socket types https://sachabarbs.wordpress.com/2014/08/21/zeromq-2-the-socket-types-2/
/// @see bounce is based on https://github.com/zeromq/azmq/blob/master/test/socket/main.cpp
/// @see flatbuffers https://google.github.io/flatbuffers/md__cpp_usage.html
void bounce(azmq::socket & server, azmq::socket & client, bool send_only = true) {
	
	flatbuffers::FlatBufferBuilder fbb;
	std::array<uint8_t, 512> buf;
	for (int x = 0; x<100; ++x) {
		
		/////////////////////////
		// Client sends to server
		
		grl::flatbuffer::Vector3d rv(x,0,0);
	    auto controlPoint = grl::flatbuffer::CreateVrepControlPoint(fbb,&rv);
		grl::flatbuffer::FinishVrepControlPointBuffer(fbb, controlPoint);
        client.send(boost::asio::buffer(fbb.GetBufferPointer(), fbb.GetSize()));
                std::cout << "sent: " << rv.x() << "\n";
		
		//////////////////////////////
		// Server receives from client
		if(! send_only) {
            auto size = server.receive(boost::asio::buffer(buf));
            auto verifier = flatbuffers::Verifier(buf.begin(),buf.size());
            auto bufOK = grl::flatbuffer::VerifyVrepControlPointBuffer(verifier);
            
            if(size == fbb.GetSize() && bufOK){
                const grl::flatbuffer::VrepControlPoint* VCPin = grl::flatbuffer::GetVrepControlPoint(buf.begin());
                std::cout << "received: " << VCPin->position()->x() << "\n";
            } else {
                std::cout << "wrong size or failed verification. size: "<< size <<" bufOk: " <<bufOK << "\n";
            }
		}
		fbb.Clear();
    }
}
Example #2
0
 void start()
 {
     socket_.async_receive([this](boost::system::error_code const& ec,
                                  azmq::message & msg, size_t) {
             if (ec)
                 return;
             event_t event;
             msg.buffer_copy(boost::asio::buffer(&event, sizeof(event)));
             events_.push_back(event);
             socket_.flush();
             start();
         });
 }
void ChannelServer::nextMonitorEvent(StreamIdx idx, azmq::socket &socket) {
    auto self = shared_from_this();
    socket.async_receive([this, self, idx, &socket](
        const error_code &ec, azmq::message &msg, size_t) {
        if (ec == errc::operation_canceled) {
            // We are shutting down.
            return;
        }

        if (ec) {
            BOOST_LOG_TRIVIAL(error) << "Error receiving streaming monitor "
                                        "event; should not happen unless there "
                                        "is a ZeroMQ-internal problem?";
            nextMonitorEvent(idx, socket);
            return;
        }

        if (!msg.more()) {
            BOOST_LOG_TRIVIAL(error) << "Streaming monitor event only consists "
                                        "of one part; should not happen unless "
                                        "there is a ZeroMQ-internal problem?";
            nextMonitorEvent(idx, socket);
            return;
        }

        MonitorEvent event;
        msg.buffer_copy(buffer(&event, sizeof(event)));

        if (event.type == ZMQ_EVENT_ACCEPTED) {
            addStreamSubscription(idx);
        } else if (event.type == ZMQ_EVENT_DISCONNECTED) {
            removeStreamSubscription(idx);
        } else {
            // This might be triggered if the ZeroMQ API is changed in the
            // future. Have a look at the API docs to see if we need to handle
            // that event type then. Currently, all the other events should not
            // be triggered during normal operation of the server.
            BOOST_LOG_TRIVIAL(info)
                << "Ignoring streaming socket monitor event: type = "
                << event.type << ", value = " << event.value;
        }

        socket.flush();
        nextMonitorEvent(idx, socket);
    });
}
Example #4
0
 void cancel()
 {
     socket_.cancel();
 }
Example #5
0
 monitor_handler(boost::asio::io_service & ios, azmq::socket& s, std::string role)
     : socket_(s.monitor(ios, ZMQ_EVENT_ALL))
     , role_(std::move(role))
 { }