This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jun 15th, 2011  |  syntax: None  |  size: 2.66 KB  |  views: 119  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.     try
  2.     {
  3.         boost::asio::io_service ioService;
  4.         boost::asio::ip::udp::endpoint serverEndpoint;
  5.         boost::asio::ip::udp::socket socket(ioService);
  6.         boost::asio::ip::udp::resolver resolver(ioService);
  7.         boost::asio::streambuf sb;
  8.         std::ostream os(&sb);
  9.         std::istream is(&sb);
  10.         MSG_T CITE_REQUEST_MSG;
  11.  
  12.         initVariables(serverEndpoint, socket, resolver, CITE_REQUEST_MSG);
  13.  
  14.         os.write((char*)&CITE_REQUEST_MSG, sizeof(CITE_REQUEST_MSG));
  15.         size_t sentBytes = socket.send_to(sb.data(), serverEndpoint);
  16.         sb.consume(sentBytes);
  17.  
  18.  
  19.         uint8_t msgType;
  20.         uint16_t quotePartsNumber = -1;
  21.         CITE_MSG_T replyMsg;
  22.         replyMsg.text.data = NULL;
  23.         boost::asio::streambuf::mutable_buffers_type mutableBuf =
  24.             sb.prepare(sizeof(replyMsg) + MAX_PART_SIZE);
  25.  
  26.         do
  27.         {
  28.             timed_wait_on_socket(socket, (quotePartsNumber > 0),
  29.                 boost::bind(&QaSProtocolClient::timeoutHandler, this, _1, _2));
  30.             size_t receivedBytes = socket.receive_from(mutableBuf,
  31.                 serverEndpoint);
  32.             sb.commit(receivedBytes);
  33.             is.read((char*)&msgType, sizeof(msgType));
  34.         //    sb.consume(is.gcount());
  35.             if (msgType == 0)
  36.             {
  37.                 uint8_t errno_;
  38.                 is.read((char*)errno_, sizeof(errno_));
  39.                 sb.consume(receivedBytes);
  40.                 errno = __builtin_bswap64(errno_);
  41.                 throw QaSProtocolClientException("Server returned error");
  42.             }
  43.  
  44.             replyMsg.msg_id = msgType;
  45.             is.read((char*)&replyMsg.key, sizeof(replyMsg.key));
  46.             is.read((char*)&replyMsg.index, sizeof(replyMsg.index));
  47.             is.read((char*)&replyMsg.parts, sizeof(replyMsg.parts));
  48.             is.read((char*)&replyMsg.text.data_length,
  49.                 sizeof(replyMsg.text.data_length));
  50.             if (replyMsg.text.data != NULL)
  51.             {
  52.                 delete[] replyMsg.text.data;
  53.             }
  54.             replyMsg.text.data = new char[replyMsg.text.data_length];
  55.             is.read(replyMsg.text.data, htons(replyMsg.text.data_length));
  56.             sb.consume(receivedBytes);
  57.             qDebug() << msgType << " " << replyMsg.index << " " << replyMsg.parts << " " << replyMsg.text.data_length << " "<< replyMsg.text.data;
  58.  
  59.             processReply(replyMsg, quotePartsNumber);
  60.         }
  61.         while(--quotePartsNumber > 0);
  62.         delete[] replyMsg.text.data;
  63.  
  64.         aggregate_result(result);
  65.         quoteParts.clear();
  66.         quotePartsNumber = -1;
  67.     }
  68.     catch(std::exception& e)
  69.     {
  70.         throw QaSProtocolClientException(e.what());
  71.     }
  72.     return result;
clone this paste RAW Paste Data