Advertisement
Guest User

Untitled

a guest
Aug 26th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #include "ZeusBaseClass.h"
  2.  
  3. #include <boost/range/algorithm/remove_if.hpp>
  4. #include <boost/algorithm/string/classification.hpp>
  5.  
  6. void PlainUser::Send(std::string message)
  7. {
  8. message.append("\r\n");
  9. mtx.lock();
  10. Queue.append(std::move(message));
  11. mtx.unlock();
  12. if (finish == true) {
  13. finish = false;
  14. write();
  15. }
  16. }
  17.  
  18. void PlainUser::write() {
  19. if (!Queue.empty()) {
  20. if (Socket.is_open()) {
  21. boost::asio::async_write(Socket, boost::asio::buffer(Queue, Queue.length()), boost::asio::bind_executor(strand, boost::bind(&PlainUser::handleWrite, shared_from_this(), _1, _2)));
  22. }
  23. } else
  24. finish = true;
  25. }
  26.  
  27. void PlainUser::handleWrite(const boost::system::error_code& error, std::size_t bytes) {
  28. if (error) {
  29. Queue.clear();
  30. finish = true;
  31. return;
  32. }
  33. mtx.lock();
  34. Queue.erase(0, bytes);
  35. mtx.unlock();
  36. if (!Queue.empty())
  37. write();
  38. else {
  39. finish = true;
  40. Queue.clear();
  41. }
  42. }
  43.  
  44. void PlainUser::Close()
  45. {
  46. if(Socket.is_open()) {
  47. Exit();
  48. Socket.cancel();
  49. Socket.close();
  50. }
  51. }
  52.  
  53. std::string PlainUser::ip()
  54. {
  55. try {
  56. if (Socket.is_open())
  57. return Socket.remote_endpoint().address().to_string();
  58. } catch (boost::system::system_error &e) {
  59. std::cout << "ERROR getting IP in plain mode" << std::endl;
  60. }
  61. return "127.0.0.0";
  62. }
  63.  
  64. void PlainUser::start()
  65. {
  66. read();
  67. deadline.cancel();
  68. deadline.expires_from_now(boost::posix_time::seconds(60));
  69. deadline.async_wait(boost::bind(&PlainUser::check_ping, this, boost::asio::placeholders::error));
  70. mHost = ip();
  71. }
  72.  
  73. void PlainUser::check_ping(const boost::system::error_code &e) {
  74. if (!e) {
  75. if (bPing + 200 < time(0)) {
  76. deadline.cancel();
  77. Close();
  78. } else {
  79. deadline.cancel();
  80. deadline.expires_from_now(boost::posix_time::seconds(60));
  81. deadline.async_wait(boost::bind(&PlainUser::check_ping, this, boost::asio::placeholders::error));
  82. }
  83. }
  84. }
  85.  
  86. void PlainUser::read() {
  87.  
  88. if (Socket.is_open()) {
  89. boost::asio::async_read_until(Socket, mBuffer, '\n', boost::asio::bind_executor(strand,
  90. boost::bind(&PlainUser::handleRead, shared_from_this(),
  91. boost::asio::placeholders::error,
  92. boost::asio::placeholders::bytes_transferred)));
  93. }
  94. }
  95.  
  96. void PlainUser::handleRead(const boost::system::error_code& error, std::size_t bytes) {
  97. if (!error) {
  98. std::string message;
  99. std::istream istream(&mBuffer);
  100. std::getline(istream, message);
  101.  
  102. message.erase(boost::remove_if(message, boost::is_any_of("\r\n")), message.end());
  103.  
  104. if (message.length() > 1024)
  105. message.substr(0, 1024);
  106.  
  107. boost::asio::post(strand, boost::bind(&PlainUser::Parse, shared_from_this(), message));
  108.  
  109. read();
  110. } else
  111. Close();
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement