Advertisement
Guest User

Untitled

a guest
Sep 6th, 2011
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1. void sendmsg(zmq::socket_t &socket, DFHackMessaging::DFMessage &msg, string uuid) {
  2.     DFHack::Console &con = DFHack::Core::getInstance().con;
  3.  
  4.     zmq::message_t zmsguuid(uuid.size());
  5.     memcpy(zmsguuid.data(), uuid.data(), uuid.size());
  6.  
  7.     // Serialize protobuf message and copy to ZMQ message ready for transfer:
  8.     string dataSerialized;
  9.     msg.SerializePartialToString(&dataSerialized);
  10.     zmq::message_t zmsgdata(dataSerialized.size());
  11.     memcpy(zmsgdata.data(), dataSerialized.data(), dataSerialized.size());
  12.  
  13.     //con << "SERVER: Sending message to " << uuid << std::endl;
  14.     //msgcount++;
  15.     //con << msgcount << endl;
  16.  
  17.     try {
  18.         socket.send(zmsguuid, ZMQ_SNDMORE); // Send the uuid identifier so routers knows who to send to.
  19.         socket.send(zmsgdata); // Finally, send the data.
  20.     } catch (std::exception &e) {
  21.         con.print("Error: %s\n", zmq_strerror(errno));
  22.     }
  23. }
  24.  
  25. void recvMsg(zmq::socket_t &socket) {
  26.     DFHack::Console &con = DFHack::Core::getInstance().con;
  27.  
  28.     int pollint;
  29.     pollint = zmq::poll(items, 1, 0);
  30.  
  31.     while(items[0].revents & ZMQ_POLLIN) {
  32.             DFHackMessaging::DFMessage  dfmsg;
  33.             std::string                 uuid;
  34.  
  35.         while(true) {
  36.             zmq::message_t      zmsg;
  37.             //std::string           zmsgdata(static_cast<char*>(zmsg.data()));
  38.             std::string         zmsgdata;
  39.            
  40.             socket.recv(&zmsg); // Retrieve message part
  41.  
  42.             /****************************** Debugging *******************************/
  43.             //con << "----------------------------------------" << std::endl;
  44.             /*con << "           Incoming Message             " << std::endl;
  45.             bool                is_text = true;
  46.             int                 char_nbr;
  47.             unsigned char       byte;
  48.             for (char_nbr = 0; char_nbr < zmsg.size(); char_nbr++) {
  49.                 byte = zmsgdata[char_nbr];
  50.                 if (byte < 32 || byte > 127)
  51.                     is_text = false;
  52.             }
  53.  
  54.             con.print("[%03d] ", zmsg.size());
  55.  
  56.             for (char_nbr = 0; char_nbr < zmsg.size(); char_nbr++) {
  57.                 if (is_text)
  58.                     con.print("%c", zmsgdata[char_nbr]);
  59.                 else
  60.                     con.print("%02X", (unsigned char) zmsgdata[char_nbr]);
  61.             }
  62.             con.print("\n");*/
  63.             /*************************************************************************/
  64.  
  65.             // Must be the first message if uuid is empty. Get UUID:
  66.             if(uuid.size() == 0) {
  67.                 uuid.assign(static_cast<char*>(zmsg.data()), zmsg.size());
  68.             } else { // Must be the actual message. Parse it.
  69.                 /* Here we find out what the message is and perform any actions required. */
  70.                 dfmsg.ParseFromArray(zmsg.data(), zmsg.size());
  71.                 //dfmsg.set_uuid(uuid); // Rewrite the uuid to the proper value. This should be rewritten at some point.
  72.                 /********* Find out what message came in and action it ********************/
  73.                 if(dfmsg.has_dftile()) {
  74.                     con << "Action: DFTile" << std::endl;
  75.                 }
  76.                 if(dfmsg.has_subtozlevel()) {
  77.                     con << "Action: subscribeToMapZLevel" << std::endl;
  78.                     subscribeToMapZLevel(uuid, dfmsg.subtozlevel().zlevel());
  79.                 }
  80.             }
  81.  
  82.             // Multipart detection:
  83.             int64_t more;          
  84.             size_t more_size = sizeof(more);
  85.             socket.getsockopt(ZMQ_RCVMORE, &more, &more_size);
  86.  
  87.             if (!more)
  88.                 break;      //  Last message part
  89.         }
  90.  
  91.         zmq::poll(&items [0], 1, 0);
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement