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();
    }
}