CMessagePush.cpp 2.9 KB

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