Advertisement
Guest User

Untitled

a guest
Sep 6th, 2011
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.79 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. }
  94.  
  95. DFhackCExport command_result dfserver (DFHack::Core * c, vector <string> & parameters)
  96. {
  97.     // Prepare our context and socket
  98.     zmq::context_t context(1);
  99.     zmq::socket_t socket (context, ZMQ_ROUTER);
  100.     socket.bind ("tcp://*:5555");
  101.    
  102.     // Initialize poll set
  103.     items[0].socket = socket;
  104.     items[0].events = ZMQ_POLLIN;
  105.    
  106.     while (1) {
  107.         recvMsg(socket);
  108.     }
  109. }
  110.  
  111.  
  112. /********************************* CLIENT CODE *********************************/
  113. // MAIN
  114.     // Prepare our context and socket
  115.     zmq::context_t context (1);
  116.     zmq::socket_t socket (context, ZMQ_DEALER);
  117.    
  118.  
  119.     // Initialize poll set
  120.     items[0].socket = socket;
  121.     items[0].events = ZMQ_POLLIN;
  122.  
  123.     cout << "CLIENT: Connecting to DF server..." << std::endl;
  124.     socket.connect("tcp://localhost:5555");
  125.  
  126. while(1) {
  127. recvMsg(socket);
  128. }
  129.  
  130.  
  131. // END MAIN
  132.  
  133. void sendmsg(zmq::socket_t &socket, DFHackMessaging::DFMessage &msg) {
  134.     string dataSerialized;
  135.  
  136.     // Serialize protobuf message and copy to ZMQ message ready for transfer:
  137.     msg.SerializePartialToString(&dataSerialized);
  138.     zmq::message_t msgdata(dataSerialized.size());
  139.     memcpy(msgdata.data(), dataSerialized.data(), dataSerialized.size());
  140.  
  141.     try {
  142.         socket.send(msgdata); // Finally, send the data.
  143.     } catch (std::exception &e) {
  144.         printf ("Error: %s\n", zmq_strerror(errno));
  145.     }
  146. }
  147.  
  148. void recvMsg(zmq::socket_t &socket) {
  149.     int pollint;
  150.     pollint = zmq::poll(items, 1, 0);
  151.  
  152.     while(items[0].revents & ZMQ_POLLIN) {
  153.         DFHackMessaging::DFMessage  dfmsg;
  154.         std::string                 uuid;
  155.         zmq::message_t              zmsg;
  156.         std::string                 zmsgdata;
  157.        
  158.         msgcount++;
  159.            
  160.         socket.recv(&zmsg); // Retrieve message part
  161.  
  162.         /****************************** Debugging *******************************/
  163.         //cout << "----------------------------------------" << std::endl;
  164.         //cout << "           Incoming Message             " << std::endl;
  165.         //bool              is_text = true;
  166.         //int                   char_nbr;
  167.         //unsigned char     byte;
  168.         //for (char_nbr = 0; char_nbr < zmsg.size(); char_nbr++) {
  169.         //  byte = zmsgdata[char_nbr];
  170.         //  if (byte < 32 || byte > 127)
  171.         //      is_text = false;
  172.         //}
  173.  
  174.         //printf("[%03d] ", zmsg.size());
  175.  
  176.         //for (char_nbr = 0; char_nbr < zmsg.size(); char_nbr++) {
  177.         //  if (is_text)
  178.         //      printf("%c", zmsgdata[char_nbr]);
  179.         //  else
  180.         //      printf("%02X", (unsigned char) zmsgdata[char_nbr]);
  181.         //}
  182.         //printf("\n");
  183.         /*************************************************************************/
  184.  
  185.  
  186.         /* Here we find out what the message is and perform any actions required. */
  187.         dfmsg.ParseFromArray(zmsg.data(), zmsg.size());
  188.         //dfmsg.set_uuid(uuid); // Rewrite the uuid to the proper value. This should be rewritten at some point.
  189.         /********* Find out what message came in and action it ********************/
  190.         if(dfmsg.has_dftile()) {
  191.             //cout << "[msg " << msgcount << "] Action: DFTile" << std::endl;
  192.             DFHack::DFCoord tilecoords(dfmsg.dftile().xgcoord(), dfmsg.dftile().ygcoord(), dfmsg.dftile().zgcoord());
  193.             multimap<DFHack::DFCoord, LEO::Cube*>::iterator iter = blockMap.find(tilecoords);
  194.             if(iter == blockMap.end()) {
  195.                 if( dfmsg.dftile().shape() != DFHack::TileShape::EMPTY) {
  196.                     //AppLog("Creating a cube...");
  197.                     //Cube *box = new Cube(CREATENOW);
  198.                     //box->SetOcclusionMode(true);
  199.                     //box->Move(tilecoords.y, 0, tilecoords.x);
  200.                     //if( dfmsg.dftile().shape() == DFHack::TileShape::WALL)
  201.                     //  box->SetColor(Vec4(0,0,1,1)); // Blue
  202.                     Cube box = masterCube->Copy();
  203.                     box.SetOcclusionMode(true);
  204.                     box.Move(tilecoords.y, 0, tilecoords.x);
  205.                     if( dfmsg.dftile().shape() == DFHack::TileShape::WALL)
  206.                         box.SetColor(Vec4(0,0,1,1)); // Blue
  207.  
  208.                     //blockMap.insert( pair<DFHack::DFCoord, LEO::Cube*>(tilecoords, box) );
  209.                 }
  210.             }
  211.         }
  212.  
  213.         zmq::poll(&items [0], 1, 0);
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement