1
0

service_position.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #ifndef __service_position_h__
  2. #define __service_position_h__
  3. #include <string>
  4. #include <list>
  5. #include <boost/asio.hpp>
  6. #include <boost/thread.hpp>
  7. #include <boost/enable_shared_from_this.hpp>
  8. #include <boost/atomic.hpp>
  9. #include <chrono>
  10. #include <boost/serialization/singleton.hpp>
  11. struct info_data{
  12. boost::shared_ptr<std::string> p_msg;
  13. std::chrono::system_clock::time_point time_receive;
  14. };
  15. typedef std::shared_ptr<info_data> info_data_ptr;
  16. class info_send{
  17. public:
  18. std::string id;
  19. boost::posix_time::ptime time_last_send;
  20. std::list<info_data_ptr> data;
  21. std::shared_ptr<boost::asio::deadline_timer> p_timer;
  22. double feq;
  23. std::size_t count_max = 3;
  24. };
  25. typedef std::shared_ptr<info_send> info_send_ptr;
  26. class service_position: public boost::enable_shared_from_this<service_position>{
  27. public:
  28. typedef boost::shared_ptr<boost::asio::io_service> service_ptr;
  29. typedef boost::shared_ptr<boost::asio::ip::tcp::socket> socket_ptr;
  30. typedef boost::shared_ptr<boost::asio::ip::tcp::acceptor> acceptor_ptr;
  31. typedef boost::shared_ptr<std::string> buffer_ptr;
  32. public:
  33. virtual void notify(const std::string& msg, const std::string& id, const double& feq);
  34. virtual void set_port(const int port);
  35. virtual bool start();
  36. virtual void stop();
  37. protected:
  38. int m_port;
  39. service_ptr m_service;
  40. std::list<socket_ptr> m_sockets;
  41. acceptor_ptr m_acceptor;
  42. boost::thread m_thread;
  43. boost::atomic_bool m_stop;
  44. std::map<std::string, info_send_ptr> m_deques;
  45. std::mutex m_mutex;
  46. std::shared_ptr<boost::asio::deadline_timer> mp_timer;
  47. protected:
  48. virtual void handle_thread(service_ptr p_service);
  49. virtual void handle_acceptor(const boost::system::error_code& ec, socket_ptr p_socket);
  50. virtual void handle_stop();
  51. virtual void handle_notify(buffer_ptr pBuffer);
  52. virtual void handle_write(const boost::system::error_code& ec, unsigned int bytes_transferred, socket_ptr p_socket, buffer_ptr p_buffer);
  53. virtual void handle_buffer(buffer_ptr p_buffer, std::string id, double feq, service_ptr p_service);
  54. virtual void handle_timer(const boost::system::error_code& ec, std::weak_ptr<info_send> p_info);
  55. virtual std::string to_str(const std::chrono::system_clock::time_point& time);
  56. virtual void init_cache(info_send_ptr& p_info);
  57. };
  58. typedef boost::shared_ptr<service_position> service_position_ptr;
  59. #endif