CMessagePush.cpp 3.8 KB

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