CMessagePush.cpp 3.0 KB

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