CMessagePush.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "../pch/pch.h"
  2. #include "CMessagePush.h"
  3. #include "../tool/CPosPrinter.h"
  4. #include "mmsystem.h"
  5. CMessagePush::~CMessagePush()
  6. {
  7. }
  8. void CMessagePush::Start()
  9. {
  10. std::thread t(&CMessagePush::Run, this);
  11. t.detach();
  12. }
  13. void CMessagePush::Run()
  14. {
  15. try
  16. {
  17. char host[] = "127.0.0.1";
  18. char port[] = "9001";
  19. tcp::resolver resolver(m_io_context);
  20. tcp::resolver::results_type endpoints =
  21. resolver.resolve(tcp::v4(), host, port);
  22. boost::asio::connect(socket_, endpoints);
  23. //发送身份信息
  24. rapidjson::Document doc;
  25. doc.SetObject();
  26. rapidjson::Document::AllocatorType &allocator = doc.GetAllocator(); //获取分配器
  27. doc.AddMember("username", "zhangyang", allocator);
  28. rapidjson::StringBuffer buffer;
  29. rapidjson::Writer<StringBuffer> writer(buffer);
  30. doc.Accept(writer);
  31. //返回给接入层的消息
  32. std::string m_login_msg = buffer.GetString();
  33. //异步发送消息
  34. PushMessage(m_login_msg);
  35. //同时启动一个异步接受消息
  36. socket_.async_read_some(boost::asio::buffer(data_, max_length),
  37. boost::bind(&CMessagePush::handle_read, this,
  38. boost::asio::placeholders::error,
  39. boost::asio::placeholders::bytes_transferred));
  40. m_io_context.run();
  41. }
  42. catch (std::exception& e)
  43. {
  44. std::cerr << "Exception: " << e.what() << "\n";
  45. }
  46. }
  47. void CMessagePush::PushMessage(std::string msg)
  48. {
  49. boost::asio::async_write(socket_,
  50. boost::asio::buffer(msg.c_str(), msg.length()),
  51. boost::bind(&CMessagePush::handle_write, this,
  52. boost::asio::placeholders::error));
  53. }
  54. void CMessagePush::HandleMessage(std::string msg)
  55. {
  56. try
  57. {
  58. //收到服务器的消息,对服务器的消息进行处理
  59. rapidjson::Document document;
  60. document.Parse(msg.c_str());
  61. if (!document.IsObject())
  62. {
  63. LOG_INFO("message 非法!");
  64. return;
  65. }
  66. int type = document["type"].GetInt();
  67. if (type == 0)
  68. {
  69. std::string status = document["status"].GetString();
  70. if (status == "ok")
  71. {
  72. //表示连接服务器成功了
  73. }
  74. }
  75. else if (type == 1)
  76. {
  77. std::string username = document["username"].GetString();
  78. std::string order_id = document["order_id"].GetString();
  79. std::string order_no = document["order_no"].GetString();
  80. //新订单来了,首先判断是否要语音提醒
  81. if (CSetting::GetParam("setting_is_new_waimai_voice") == "1")
  82. {
  83. //需要语音提醒
  84. std::wstring filepath = L"music/new_wamai_order.mp3";
  85. std::wstring Open = L"OPEN " + filepath + L" ALIAS MUSIC";
  86. mciSendString(Open.c_str(), NULL, 0, 0);
  87. std::wstring Play = L"PLAY MUSIC FROM 0";
  88. mciSendString(Play.c_str(), NULL, 0, 0);
  89. std::wstring Close = L"CLOSE MUSIC";
  90. mciSendString(Close.c_str(), NULL, 0, 0);
  91. }
  92. CPosPrinter printer;
  93. printer.PrintWaimaiOrder(order_id, order_no);
  94. }
  95. }
  96. catch (std::exception& e)
  97. {
  98. std::cerr << "Exception: " << e.what() << "\n";
  99. }
  100. }
  101. void CMessagePush::handle_read(const boost::system::error_code& error,
  102. size_t bytes_transferred)
  103. {
  104. if (!error)
  105. {
  106. string s_reply = data_;
  107. HandleMessage(s_reply);
  108. memset(data_, 0, max_length);
  109. socket_.async_read_some(boost::asio::buffer(data_, max_length),
  110. boost::bind(&CMessagePush::handle_read, this,
  111. boost::asio::placeholders::error,
  112. boost::asio::placeholders::bytes_transferred));
  113. }
  114. else
  115. {
  116. delete this;
  117. }
  118. }
  119. void CMessagePush::handle_write(const boost::system::error_code& error)
  120. {
  121. if (!error)
  122. {
  123. LOG_INFO("send success!");
  124. }
  125. else
  126. {
  127. delete this;
  128. }
  129. }