|
|
@@ -2,13 +2,38 @@
|
|
|
|
|
|
#include "../helper/CAliyunMNS.h"
|
|
|
|
|
|
+void CServer::Init()
|
|
|
+{
|
|
|
+ //TCP服务器默认使用9001端口
|
|
|
+ m_port = 9001;
|
|
|
+
|
|
|
+ acceptor_ = new tcp::acceptor(io_context_, tcp::endpoint(tcp::v4(), m_port));
|
|
|
+
|
|
|
+ //开始监听客户端的消息
|
|
|
+ start_accept();
|
|
|
+
|
|
|
+ //开始接受和处理mns的消息
|
|
|
+ start_aliyun_mns();
|
|
|
+
|
|
|
+ //任务队列,发送消息给客户端
|
|
|
+ std::thread t(&CServer::SendMessageToClient, this);
|
|
|
+ t.detach();
|
|
|
+
|
|
|
+ //开始异步执行
|
|
|
+ io_context_.run();
|
|
|
+}
|
|
|
+
|
|
|
void CServer::start_accept()
|
|
|
{
|
|
|
//新建一个客户端会话
|
|
|
CClientSession* new_session = new CClientSession(io_context_);
|
|
|
new_session->SetServer(this);
|
|
|
|
|
|
- acceptor_.async_accept(new_session->socket(),
|
|
|
+ //记录客户端的顺序
|
|
|
+ m_nClientCount++;
|
|
|
+ new_session->SetNum(m_nClientCount);
|
|
|
+
|
|
|
+ acceptor_->async_accept(new_session->socket(),
|
|
|
boost::bind(&CServer::handle_accept, this, new_session,
|
|
|
boost::asio::placeholders::error));
|
|
|
}
|
|
|
@@ -25,12 +50,38 @@ void CServer::handle_accept(CClientSession* new_session,
|
|
|
delete new_session;
|
|
|
}
|
|
|
|
|
|
+ //继续监听下一个客户端
|
|
|
start_accept();
|
|
|
}
|
|
|
|
|
|
+/*
|
|
|
+ *将客户端的用户名和socket进行绑定,每个用户名只能绑定一个socket,后登陆的会删掉前面先登录的
|
|
|
+ **/
|
|
|
void CServer::BindUsername(std::string username, CClientSession* session)
|
|
|
{
|
|
|
+ m_map_mutex.lock();
|
|
|
+
|
|
|
+ if (m_clients_map.find(username) != m_clients_map.end() && m_clients_map[username]->GetNum() < session->GetNum())
|
|
|
+ {
|
|
|
+ //之前已经存在了一个,先把直接的关闭掉
|
|
|
+ m_clients_map[username]->stop();
|
|
|
+ }
|
|
|
+
|
|
|
m_clients_map[username] = session;
|
|
|
+
|
|
|
+ m_map_mutex.unlock();
|
|
|
+}
|
|
|
+
|
|
|
+void CServer::DeleteClient(std::string username)
|
|
|
+{
|
|
|
+ m_map_mutex.lock();
|
|
|
+
|
|
|
+ if (m_clients_map.find(username) != m_clients_map.end())
|
|
|
+ {
|
|
|
+ m_clients_map.erase(username);
|
|
|
+ }
|
|
|
+
|
|
|
+ m_map_mutex.unlock();
|
|
|
}
|
|
|
|
|
|
void CServer::start_aliyun_mns()
|
|
|
@@ -79,18 +130,28 @@ void CServer::ReceiveMNSMessage()
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- //处理消息类型 { "username":"zhangyang","order_id":"86425730"}
|
|
|
- int type = document["type"].GetInt();
|
|
|
+ //处理消息类型
|
|
|
+ int type = document["msg_type"].GetInt();
|
|
|
+ std::string timestamp = document["timestamp"].GetString();
|
|
|
+ std::string use_time = document["use_time"].GetString();
|
|
|
std::string username = document["username"].GetString();
|
|
|
- std::string order_id = document["order_id"].GetString();
|
|
|
- std::string order_no = document["order_no"].GetString();
|
|
|
+ std::string waimai_order_id = document["waimai_order_id"].GetString();
|
|
|
+ std::string waimai_order_no = document["waimai_order_no"].GetString();
|
|
|
|
|
|
- //把消息放进队列,队列发给客户端
|
|
|
+ //判断是否过期
|
|
|
+ if (abs(time(NULL) - atoi(timestamp.c_str())) > 1800)
|
|
|
+ {
|
|
|
+ //如果消息已经超过了30分钟,直接丢弃
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ //把消息放进队列
|
|
|
CClientMessage newMessage;
|
|
|
- newMessage.m_type = type;
|
|
|
+ newMessage.msg_type = type;
|
|
|
+ newMessage.timestamp = to_string((NULL));
|
|
|
newMessage.m_username = username;
|
|
|
- newMessage.m_order_id = order_id;
|
|
|
- newMessage.m_order_no = order_no;
|
|
|
+ newMessage.m_order_id = waimai_order_id;
|
|
|
+ newMessage.m_order_no = waimai_order_no;
|
|
|
|
|
|
m_queue_mutex.lock();
|
|
|
m_message_queue.push(newMessage);
|
|
|
@@ -123,13 +184,24 @@ void CServer::SendMessageToClient()
|
|
|
|
|
|
std::string username = msg.m_username;
|
|
|
|
|
|
- //返回给接入层的消息
|
|
|
- std::string res_msg = msg.toJson();
|
|
|
+ //判断消息对应的用户是否在线,如果不在线就消息暂存在数据库
|
|
|
+ m_map_mutex.lock();
|
|
|
+ if (m_clients_map.find(username) == m_clients_map.end())
|
|
|
+ {
|
|
|
+ m_map_mutex.unlock();
|
|
|
|
|
|
- //找到username对应的socket,然后发消息过去
|
|
|
- if (m_clients_map.find(username) != m_clients_map.end())
|
|
|
+ //客户端不在线,操作存数据库
|
|
|
+ }
|
|
|
+ else
|
|
|
{
|
|
|
- m_clients_map[username]->send_message(res_msg);
|
|
|
+ CClientSession* session = m_clients_map[username];
|
|
|
+
|
|
|
+ m_map_mutex.unlock();
|
|
|
+
|
|
|
+ //返回给接入层的消息
|
|
|
+ std::string res_msg = msg.toJson();
|
|
|
+
|
|
|
+ session->send_message(res_msg);
|
|
|
}
|
|
|
}
|
|
|
|