Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jun 15th, 2011  |  syntax: None  |  size: 1.67 KB  |  views: 99  |  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.  
  2. void QaSProtocolServer::serverCore()
  3. {
  4.     boost::asio::ip::udp::socket socket(ioService,
  5.         boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(),
  6.             m_serverPort));
  7.     boost::asio::streambuf sb;
  8.     boost::asio::streambuf::mutable_buffers_type mutableBuf =
  9.         sb.prepare(sizeof(MSG_T));
  10.     size_t receivedBytes = socket.receive_from(mutableBuf, clientEndpoint);
  11.     sb.commit(receivedBytes);
  12.  
  13.     std::istream is(&sb);
  14.     uint8_t msg_type;
  15.     is.read((char*)&msg_type, sizeof(msg_type));
  16.     uint64_t key;
  17.     is.read((char*)&key, sizeof(key));
  18.     sb.consume(receivedBytes);
  19.  
  20.     std::ostream os(&sb);
  21.     CITE_MSG_T replyMsg;
  22.     std::vector<std::string> quoteParts;
  23.     try
  24.     {
  25.         quoteParts = prepareQuote(replyMsg);
  26.     }
  27.     catch (QaSProtocolServerException& e)
  28.     {
  29.         sendServerErrMsg(socket, clientEndpoint);
  30.         return;
  31.     }
  32.  
  33.     uint16_t index = 0;
  34.     replyMsg.msg_id = 2;
  35.     replyMsg.key = key;
  36.     replyMsg.text.data = NULL;
  37.     BOOST_FOREACH(std::string quotePart, quoteParts)
  38.     {
  39.         replyMsg.index = htons(index++);
  40.         if (replyMsg.text.data != NULL)
  41.         {
  42.             delete[] replyMsg.text.data;
  43.         }
  44.         replyMsg.text.data = new char[quotePart.size()];
  45.         strcpy(replyMsg.text.data, quotePart.c_str());
  46.         replyMsg.text.data_length = strlen(replyMsg.text.data);
  47.         qDebug() << replyMsg.text.data_length;
  48.         os.write((char*)&replyMsg,
  49.                  sizeof(replyMsg) - sizeof(replyMsg.text.data));
  50.         os.write(replyMsg.text.data, strlen(replyMsg.text.data));
  51.         size_t sent_bytes = socket.send_to(sb.data(), clientEndpoint);
  52.         sb.consume(sent_bytes);
  53.     }
  54.     socket.close();
  55. }