|
|
@@ -3,32 +3,23 @@
|
|
|
|
|
|
#include "../tool/CPosPrinter.h"
|
|
|
|
|
|
-CMessagePush::CMessagePush()
|
|
|
-{
|
|
|
- std::thread t(&CMessagePush::Init, this);
|
|
|
- t.detach();
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
CMessagePush::~CMessagePush()
|
|
|
{
|
|
|
+
|
|
|
}
|
|
|
|
|
|
-void CMessagePush::Init()
|
|
|
+void CMessagePush::Start()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- boost::asio::io_context io_context;
|
|
|
-
|
|
|
char host[] = "192.168.3.29";
|
|
|
char port[] = "9001";
|
|
|
|
|
|
- tcp::resolver resolver(io_context);
|
|
|
+ tcp::resolver resolver(m_io_context);
|
|
|
tcp::resolver::results_type endpoints =
|
|
|
resolver.resolve(tcp::v4(), host, port);
|
|
|
|
|
|
- tcp::socket s(io_context);
|
|
|
- boost::asio::connect(s, endpoints);
|
|
|
+ boost::asio::connect(socket_, endpoints);
|
|
|
|
|
|
//发送身份信息
|
|
|
rapidjson::Document doc;
|
|
|
@@ -44,35 +35,54 @@ void CMessagePush::Init()
|
|
|
//返回给接入层的消息
|
|
|
std::string m_login_msg = buffer.GetString();
|
|
|
|
|
|
- boost::asio::write(s, boost::asio::buffer(m_login_msg, m_login_msg.length()));
|
|
|
+ //异步发送消息
|
|
|
+ PushMessage(m_login_msg);
|
|
|
|
|
|
- char reply[max_length];
|
|
|
- size_t reply_length = boost::asio::read(s,
|
|
|
- boost::asio::buffer(reply, max_length));
|
|
|
+ //同时启动一个异步接受消息
|
|
|
+ socket_.async_read_some(boost::asio::buffer(data_, max_length),
|
|
|
+ boost::bind(&CMessagePush::handle_read, this,
|
|
|
+ boost::asio::placeholders::error,
|
|
|
+ boost::asio::placeholders::bytes_transferred));
|
|
|
+ }
|
|
|
+ catch (std::exception& e)
|
|
|
+ {
|
|
|
+ std::cerr << "Exception: " << e.what() << "\n";
|
|
|
+ }
|
|
|
|
|
|
- //判断服务器返回的是不是ok,如果是的那么
|
|
|
- string s_reply = reply;
|
|
|
+ CSystem::my_sleep(100);
|
|
|
+}
|
|
|
|
|
|
+void CMessagePush::PushMessage(std::string msg)
|
|
|
+{
|
|
|
+ boost::asio::async_write(socket_,
|
|
|
+ boost::asio::buffer(msg.c_str(), msg.length()),
|
|
|
+ boost::bind(&CMessagePush::handle_write, this,
|
|
|
+ boost::asio::placeholders::error));
|
|
|
+}
|
|
|
+
|
|
|
+void CMessagePush::HandleMessage(std::string msg)
|
|
|
+{
|
|
|
+ try
|
|
|
+ {
|
|
|
+ //收到服务器的消息,对服务器的消息进行处理
|
|
|
rapidjson::Document document;
|
|
|
- document.Parse(s_reply.c_str());
|
|
|
+ document.Parse(msg.c_str());
|
|
|
if (!document.IsObject())
|
|
|
{
|
|
|
LOG_INFO("message 非法!");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- std::string status = document["status"].GetString();
|
|
|
+ int type = document["type"].GetInt();
|
|
|
+ std::string username = document["username"].GetString();
|
|
|
+ std::string order_id = document["order_id"].GetString();
|
|
|
+ std::string order_no = document["order_no"].GetString();
|
|
|
|
|
|
- if (status == "ok")
|
|
|
+ if (type == 1)
|
|
|
{
|
|
|
- //这个表示登录成功了
|
|
|
- while (m_is_work)
|
|
|
- {
|
|
|
- ReceiveMessage(s);
|
|
|
- }
|
|
|
+ CPosPrinter printer;
|
|
|
+ printer.PrintWaimaiOrder(order_id, order_no);
|
|
|
}
|
|
|
-
|
|
|
- return;
|
|
|
}
|
|
|
catch (std::exception& e)
|
|
|
{
|
|
|
@@ -80,17 +90,13 @@ void CMessagePush::Init()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void CMessagePush::ReceiveMessage(tcp::socket& s)
|
|
|
+void CMessagePush::handle_read(const boost::system::error_code& error,
|
|
|
+ size_t bytes_transferred)
|
|
|
{
|
|
|
- try
|
|
|
+ if (!error)
|
|
|
{
|
|
|
- char reply[max_length];
|
|
|
- size_t reply_length = boost::asio::read(s,
|
|
|
- boost::asio::buffer(reply, max_length));
|
|
|
+ string s_reply = data_;
|
|
|
|
|
|
- string s_reply = reply;
|
|
|
-
|
|
|
- //收到服务器的消息,对服务器的消息进行处理
|
|
|
rapidjson::Document document;
|
|
|
document.Parse(s_reply.c_str());
|
|
|
if (!document.IsObject())
|
|
|
@@ -99,19 +105,41 @@ void CMessagePush::ReceiveMessage(tcp::socket& s)
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- std::string type = document["type"].GetString();
|
|
|
- std::string username = document["username"].GetString();
|
|
|
- std::string order_id = document["order_id"].GetString();
|
|
|
- std::string order_no = document["order_no"].GetString();
|
|
|
+ int type = document["type"].GetInt();
|
|
|
+ if (type == 0)
|
|
|
+ {
|
|
|
+ std::string status = document["status"].GetString();
|
|
|
|
|
|
- if (type == "1")
|
|
|
+ if (status == "ok")
|
|
|
+ {
|
|
|
+ //表示连接服务器成功了
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
{
|
|
|
- CPosPrinter printer;
|
|
|
- printer.PrintWaimaiOrder(order_id, order_no);
|
|
|
+ HandleMessage(s_reply);
|
|
|
}
|
|
|
+
|
|
|
+ memset(data_, 0, max_length);
|
|
|
+ socket_.async_read_some(boost::asio::buffer(data_, max_length),
|
|
|
+ boost::bind(&CMessagePush::handle_read, this,
|
|
|
+ boost::asio::placeholders::error,
|
|
|
+ boost::asio::placeholders::bytes_transferred));
|
|
|
}
|
|
|
- catch (std::exception& e)
|
|
|
+ else
|
|
|
{
|
|
|
- std::cerr << "Exception: " << e.what() << "\n";
|
|
|
+ delete this;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void CMessagePush::handle_write(const boost::system::error_code& error)
|
|
|
+{
|
|
|
+ if (!error)
|
|
|
+ {
|
|
|
+ LOG_INFO("send success!");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ delete this;
|
|
|
}
|
|
|
}
|