CClientSession.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "CClientSession.h"
  2. #include "CServer.h"
  3. #include <sqlite3/sqlite3.h>
  4. #include "PosMessage.h"
  5. CClientSession::~CClientSession()
  6. {
  7. }
  8. void CClientSession::start()
  9. {
  10. boost::asio::async_read(socket_, boost::asio::buffer(&m_pos_message, sizeof(PosMessage)),
  11. boost::bind(&CClientSession::handle_read, this,
  12. boost::asio::placeholders::error,
  13. boost::asio::placeholders::bytes_transferred));
  14. }
  15. /*
  16. *服务器端主动关闭,才能调用这个函数
  17. **/
  18. void CClientSession::stop()
  19. {
  20. //先给客户端发送一个下线通知
  21. rapidjson::Document doc;
  22. doc.SetObject();
  23. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); //获取分配器
  24. std::string timestamp = to_string(time(NULL));
  25. doc.AddMember("msg_type", "0", allocator);
  26. doc.AddMember("timestamp", StringRef(timestamp.c_str(), timestamp.length()), allocator);
  27. rapidjson::StringBuffer buffer;
  28. rapidjson::Writer<StringBuffer> writer(buffer);
  29. doc.Accept(writer);
  30. //返回给接入层的消息
  31. std::string login_out_msg = buffer.GetString();
  32. send_message(login_out_msg);
  33. //然后关闭socket
  34. socket_.close();
  35. //注意这里不能调用delete this,因为后面会异步返回,要判断状态,如果这里delete了,后面就会内存越界
  36. }
  37. /*
  38. *同步发送消息,返回true表示发送成功,返回false表示发送失败
  39. **/
  40. bool CClientSession::send_message(std::string msg)
  41. {
  42. try
  43. {
  44. PosMessage new_msg;
  45. new_msg.m_length = (int)msg.length();
  46. memcpy(new_msg.data, msg.c_str(), msg.length());
  47. new_msg.data[new_msg.m_length] = '\0';
  48. socket_.write_some(boost::asio::buffer(&new_msg, sizeof(PosMessage)));
  49. }
  50. catch (const std::exception& e)
  51. {
  52. std::string err = e.what();
  53. //注意这里是同步发送消息,这里不要处理socket的关闭和delete操作,让后面的异步读错误处理
  54. return false;
  55. }
  56. return true;
  57. }
  58. void CClientSession::SetServer(CServer* server)
  59. {
  60. m_server = server;
  61. }
  62. void CClientSession::handle_read(const boost::system::error_code& error,
  63. size_t bytes_transferred)
  64. {
  65. if(!error)
  66. {
  67. //读到客户端发过来的内容,进行绑定
  68. std::string msg = m_pos_message.data;
  69. LOG_INFO("client handle read msg:" << msg.c_str());
  70. rapidjson::Document document;
  71. document.Parse(msg.c_str());
  72. if(!document.IsObject())
  73. {
  74. LOG_INFO("message 非法!");
  75. return;
  76. }
  77. std::string username = document["username"].GetString();
  78. std::string timestamp = document["timestamp"].GetString();
  79. std::string is_login = document["is_login"].GetString();
  80. //这个用于判断是否成功绑定用户名了
  81. m_username = username;
  82. if (is_login == "1")
  83. {
  84. //获取到用户名了,进行绑定
  85. m_server->BindUsername(username, this);
  86. //绑定完了检查这个用户有没有离线消息
  87. m_server->CheckOfflineMessage(username);
  88. }
  89. //处理完了,重新读取
  90. socket_.async_read_some(boost::asio::buffer(&m_pos_message, sizeof(PosMessage)),
  91. boost::bind(&CClientSession::handle_read, this,
  92. boost::asio::placeholders::error,
  93. boost::asio::placeholders::bytes_transferred));
  94. }
  95. else
  96. {
  97. std::string err = error.message();
  98. if (socket_.is_open())
  99. {
  100. //这个是客户端断开了链接
  101. if (m_username == "")
  102. {
  103. //说明这个socket还没获取过用户名
  104. delete this;
  105. }
  106. else
  107. {
  108. m_server->DeleteClient(m_username);
  109. delete this;
  110. }
  111. }
  112. else
  113. {
  114. //这个是服务端断开了链接
  115. delete this;
  116. }
  117. }
  118. }