Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void sendmsg(zmq::socket_t &socket, DFHackMessaging::DFMessage &msg, string uuid) {
- DFHack::Console &con = DFHack::Core::getInstance().con;
- zmq::message_t zmsguuid(uuid.size());
- memcpy(zmsguuid.data(), uuid.data(), uuid.size());
- // Serialize protobuf message and copy to ZMQ message ready for transfer:
- string dataSerialized;
- msg.SerializePartialToString(&dataSerialized);
- zmq::message_t zmsgdata(dataSerialized.size());
- memcpy(zmsgdata.data(), dataSerialized.data(), dataSerialized.size());
- //con << "SERVER: Sending message to " << uuid << std::endl;
- //msgcount++;
- //con << msgcount << endl;
- try {
- socket.send(zmsguuid, ZMQ_SNDMORE); // Send the uuid identifier so routers knows who to send to.
- socket.send(zmsgdata); // Finally, send the data.
- } catch (std::exception &e) {
- con.print("Error: %s\n", zmq_strerror(errno));
- }
- }
- void recvMsg(zmq::socket_t &socket) {
- DFHack::Console &con = DFHack::Core::getInstance().con;
- int pollint;
- pollint = zmq::poll(items, 1, 0);
- while(items[0].revents & ZMQ_POLLIN) {
- DFHackMessaging::DFMessage dfmsg;
- std::string uuid;
- while(true) {
- zmq::message_t zmsg;
- //std::string zmsgdata(static_cast<char*>(zmsg.data()));
- std::string zmsgdata;
- socket.recv(&zmsg); // Retrieve message part
- /****************************** Debugging *******************************/
- //con << "----------------------------------------" << std::endl;
- /*con << " Incoming Message " << std::endl;
- bool is_text = true;
- int char_nbr;
- unsigned char byte;
- for (char_nbr = 0; char_nbr < zmsg.size(); char_nbr++) {
- byte = zmsgdata[char_nbr];
- if (byte < 32 || byte > 127)
- is_text = false;
- }
- con.print("[%03d] ", zmsg.size());
- for (char_nbr = 0; char_nbr < zmsg.size(); char_nbr++) {
- if (is_text)
- con.print("%c", zmsgdata[char_nbr]);
- else
- con.print("%02X", (unsigned char) zmsgdata[char_nbr]);
- }
- con.print("\n");*/
- /*************************************************************************/
- // Must be the first message if uuid is empty. Get UUID:
- if(uuid.size() == 0) {
- uuid.assign(static_cast<char*>(zmsg.data()), zmsg.size());
- } else { // Must be the actual message. Parse it.
- /* Here we find out what the message is and perform any actions required. */
- dfmsg.ParseFromArray(zmsg.data(), zmsg.size());
- //dfmsg.set_uuid(uuid); // Rewrite the uuid to the proper value. This should be rewritten at some point.
- /********* Find out what message came in and action it ********************/
- if(dfmsg.has_dftile()) {
- con << "Action: DFTile" << std::endl;
- }
- if(dfmsg.has_subtozlevel()) {
- con << "Action: subscribeToMapZLevel" << std::endl;
- subscribeToMapZLevel(uuid, dfmsg.subtozlevel().zlevel());
- }
- }
- // Multipart detection:
- int64_t more;
- size_t more_size = sizeof(more);
- socket.getsockopt(ZMQ_RCVMORE, &more, &more_size);
- if (!more)
- break; // Last message part
- }
- zmq::poll(&items [0], 1, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement