|
@@ -10,16 +10,21 @@ CServer::CServer()
|
|
|
|
|
|
|
|
CServer::~CServer()
|
|
CServer::~CServer()
|
|
|
{
|
|
{
|
|
|
|
|
+ LOG_ERROR("start ~CServer");
|
|
|
delete acceptor_;
|
|
delete acceptor_;
|
|
|
|
|
|
|
|
if (m_db != NULL)
|
|
if (m_db != NULL)
|
|
|
{
|
|
{
|
|
|
sqlite3_close(m_db);
|
|
sqlite3_close(m_db);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ LOG_ERROR("end ~CServer");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void CServer::Init()
|
|
void CServer::Init()
|
|
|
{
|
|
{
|
|
|
|
|
+ LOG_ERROR("start Init()");
|
|
|
|
|
+
|
|
|
//初始化数据库
|
|
//初始化数据库
|
|
|
InitSqlLite();
|
|
InitSqlLite();
|
|
|
|
|
|
|
@@ -31,7 +36,6 @@ void CServer::Init()
|
|
|
start_accept();
|
|
start_accept();
|
|
|
|
|
|
|
|
int n_cpu = CSystem::get_CPU_core_num();
|
|
int n_cpu = CSystem::get_CPU_core_num();
|
|
|
- LOG_INFO("cpu num:" << n_cpu);
|
|
|
|
|
|
|
|
|
|
//开始接受和处理mns的消息
|
|
//开始接受和处理mns的消息
|
|
|
for (int i = 0; i < n_cpu * 2; i++)
|
|
for (int i = 0; i < n_cpu * 2; i++)
|
|
@@ -61,8 +65,19 @@ void CServer::Init()
|
|
|
t.detach();
|
|
t.detach();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //开始异步执行
|
|
|
|
|
- io_context_.run();
|
|
|
|
|
|
|
+ LOG_ERROR("ready to run");
|
|
|
|
|
+
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ //开始异步执行
|
|
|
|
|
+ io_context_.run();
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (std::exception& e)
|
|
|
|
|
+ {
|
|
|
|
|
+ LOG_ERROR("io_context Exception: " << e.what());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ LOG_ERROR("stop run");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void CServer::InitSqlLite()
|
|
void CServer::InitSqlLite()
|
|
@@ -82,20 +97,20 @@ void CServer::InitSqlLite()
|
|
|
m_rc = sqlite3_open(s_path.c_str(), &m_db);
|
|
m_rc = sqlite3_open(s_path.c_str(), &m_db);
|
|
|
#else
|
|
#else
|
|
|
std::string db_path = CSystem::getAbsopath() + "../db/pos.db";
|
|
std::string db_path = CSystem::getAbsopath() + "../db/pos.db";
|
|
|
- LOG_INFO("db path:" << db_path.c_str());
|
|
|
|
|
|
|
+ //LOG_INFO("db path:" << db_path.c_str());
|
|
|
|
|
|
|
|
m_rc = sqlite3_open(db_path.c_str(), &m_db);
|
|
m_rc = sqlite3_open(db_path.c_str(), &m_db);
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|
|
|
if (m_rc)
|
|
if (m_rc)
|
|
|
{
|
|
{
|
|
|
- LOG_INFO("Can't open database: " << sqlite3_errmsg(m_db));
|
|
|
|
|
|
|
+ LOG_ERROR("Can't open database: " << sqlite3_errmsg(m_db));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- LOG_INFO("Opened database successfully");
|
|
|
|
|
|
|
+ //LOG_INFO("Opened database successfully");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//初始化数据表
|
|
//初始化数据表
|
|
@@ -281,12 +296,15 @@ void CServer::start_accept()
|
|
|
void CServer::handle_accept(CClientSession* new_session,
|
|
void CServer::handle_accept(CClientSession* new_session,
|
|
|
const boost::system::error_code& error)
|
|
const boost::system::error_code& error)
|
|
|
{
|
|
{
|
|
|
|
|
+ //LOG_INFO("new client, handle_accept!");
|
|
|
|
|
+
|
|
|
if (!error)
|
|
if (!error)
|
|
|
{
|
|
{
|
|
|
new_session->start();
|
|
new_session->start();
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
|
|
+ LOG_ERROR("handle_accept error");
|
|
|
delete new_session;
|
|
delete new_session;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -301,15 +319,24 @@ void CServer::BindUsername(std::string username, CClientSession* session)
|
|
|
{
|
|
{
|
|
|
m_map_mutex.lock();
|
|
m_map_mutex.lock();
|
|
|
|
|
|
|
|
- if (m_clients_map.find(username) != m_clients_map.end() && m_clients_map[username]->GetNum() < session->GetNum())
|
|
|
|
|
|
|
+ if (m_clients_map.find(username) != m_clients_map.end())
|
|
|
{
|
|
{
|
|
|
- //之前已经存在了一个,先把之前的关闭掉(这个是服务器端主动的关闭)
|
|
|
|
|
- m_clients_map[username]->stop();
|
|
|
|
|
|
|
+ if (m_clients_map[username]->GetNum() < session->GetNum())
|
|
|
|
|
+ {
|
|
|
|
|
+ //之前已经存在了一个,先把之前的关闭掉(这个是服务器端主动的关闭)
|
|
|
|
|
+ m_clients_map[username]->stop();
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ LOG_ERROR("异常情况,不应该出现");
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
m_clients_map[username] = session;
|
|
m_clients_map[username] = session;
|
|
|
|
|
|
|
|
m_map_mutex.unlock();
|
|
m_map_mutex.unlock();
|
|
|
|
|
+
|
|
|
|
|
+ LOG_INFO("绑定新客户端成功,当前客户端数量:" << m_clients_map.size());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void CServer::DeleteClient(std::string username)
|
|
void CServer::DeleteClient(std::string username)
|
|
@@ -322,6 +349,8 @@ void CServer::DeleteClient(std::string username)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
m_map_mutex.unlock();
|
|
m_map_mutex.unlock();
|
|
|
|
|
+
|
|
|
|
|
+ LOG_INFO("删除客户端成功,当前客户端数量:" << m_clients_map.size());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
/*
|
|
@@ -337,7 +366,7 @@ void CServer::ReceiveMNSMessage()
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- LOG_INFO("Init AliyunMNS success!");
|
|
|
|
|
|
|
+ //LOG_INFO("Init AliyunMNS success!");
|
|
|
|
|
|
|
|
while(true)
|
|
while(true)
|
|
|
{
|
|
{
|
|
@@ -348,7 +377,7 @@ void CServer::ReceiveMNSMessage()
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //LOG_INFO("get new message:" << message.c_str());
|
|
|
|
|
|
|
+ //LOG_INFO("new msn message:" << message.c_str());
|
|
|
|
|
|
|
|
//获取到了新的消息,开始进行处理
|
|
//获取到了新的消息,开始进行处理
|
|
|
rapidjson::Document document;
|
|
rapidjson::Document document;
|
|
@@ -356,6 +385,7 @@ void CServer::ReceiveMNSMessage()
|
|
|
if(!document.IsObject())
|
|
if(!document.IsObject())
|
|
|
{
|
|
{
|
|
|
LOG_INFO("message 非法!");
|
|
LOG_INFO("message 非法!");
|
|
|
|
|
+ LOG_INFO("get new message:" << message.c_str());
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -389,7 +419,7 @@ void CServer::ReceiveMNSMessage()
|
|
|
if (time(NULL) > atoi(timestamp.c_str()) + atoi(use_time.c_str()))
|
|
if (time(NULL) > atoi(timestamp.c_str()) + atoi(use_time.c_str()))
|
|
|
{
|
|
{
|
|
|
//消息过了有效期,直接丢弃
|
|
//消息过了有效期,直接丢弃
|
|
|
- LOG_INFO("message due date!");
|
|
|
|
|
|
|
+ //LOG_INFO("message due date!");
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -445,7 +475,7 @@ void CServer::SendMessageToClient()
|
|
|
{
|
|
{
|
|
|
m_map_mutex.unlock();
|
|
m_map_mutex.unlock();
|
|
|
|
|
|
|
|
- LOG_INFO("can not find client, save to db, username:"<<username.c_str());
|
|
|
|
|
|
|
+ //LOG_INFO("can not find client, save to db, username:"<<username.c_str());
|
|
|
|
|
|
|
|
//客户端不在线,操作存数据库
|
|
//客户端不在线,操作存数据库
|
|
|
AddMessageToDB(username, due_time, data);
|
|
AddMessageToDB(username, due_time, data);
|
|
@@ -460,13 +490,13 @@ void CServer::SendMessageToClient()
|
|
|
bool ret = session->send_message(data);
|
|
bool ret = session->send_message(data);
|
|
|
if (ret == false)
|
|
if (ret == false)
|
|
|
{
|
|
{
|
|
|
- LOG_INFO("send to client fail, save it to db, username:" << username.c_str());
|
|
|
|
|
|
|
+ //LOG_INFO("send to client fail, save it to db, username:" << username.c_str());
|
|
|
//如果发送失败了,把消息存回到数据库
|
|
//如果发送失败了,把消息存回到数据库
|
|
|
AddMessageToDB(username, due_time, data);
|
|
AddMessageToDB(username, due_time, data);
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- LOG_INFO("send to client success, username:" << username.c_str());
|
|
|
|
|
|
|
+ //LOG_INFO("send to client success, username:" << username.c_str());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -485,4 +515,4 @@ void CServer::HandleCleanupDueMessage()
|
|
|
|
|
|
|
|
CSystem::my_sleep(60 * 30);
|
|
CSystem::my_sleep(60 * 30);
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+}
|