znet.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #ifndef __EVIO_HPP_
  2. #define __EVIO_HPP_
  3. #include <string>
  4. #include <vector>
  5. #include <memory>
  6. struct client:std::enable_shared_from_this<client>
  7. {
  8. virtual std::string name()=0;
  9. virtual int type()=0; //分站:1,模拟客户端:2
  10. virtual int handle()=0;
  11. virtual void set_conn_type(int type)=0; //设置分站类型1:site,2:虚拟推送客户端
  12. virtual void close()=0;
  13. virtual void send(std::vector<char>&&b)=0;
  14. // virtual bool check_timestamp(const char*)=0;
  15. virtual ~client(){}
  16. };
  17. struct service_callback;
  18. struct service_handle
  19. {
  20. virtual int run(int port)=0;
  21. virtual void stop()=0;
  22. virtual void boardcast(const std::vector<char>&msg)=0;
  23. virtual ~service_handle(){}
  24. static service_handle*instance(service_callback*sc);
  25. };
  26. struct service_callback:service_handle
  27. {
  28. service_handle*m_handle{nullptr};
  29. void set_handle(service_handle*handle)
  30. {
  31. m_handle=handle;
  32. }
  33. int run(int port)
  34. {
  35. return m_handle->run(port);
  36. }
  37. void stop()
  38. {
  39. m_handle->stop();
  40. }
  41. void boardcast(const std::vector<char>&msg)
  42. {
  43. m_handle->boardcast(msg);
  44. }
  45. virtual void on_connect(const std::shared_ptr<client>& clt)
  46. {
  47. log_info("client(%s) connected",clt->name().c_str());
  48. }
  49. virtual void on_close(const std::shared_ptr<client> &clt)
  50. {
  51. log_info("client(%s) closed",clt->name().c_str());
  52. }
  53. virtual void on_error(const std::shared_ptr<client> &clt,const char*error)
  54. {
  55. log_info("client(%s) error with:%s",clt->name().c_str(),error);
  56. }
  57. virtual void on_send_timeout(const std::shared_ptr<client> &clt)
  58. {
  59. log_info("client(%s) send time out",clt->name().c_str());
  60. }
  61. virtual void on_recv_timeout(const std::shared_ptr<client> &clt)
  62. {
  63. log_info("client(%s) recv time out",clt->name().c_str());
  64. }
  65. virtual void on_message(const std::shared_ptr<client>& clt,const char*data,size_t len)=0;
  66. virtual void on_timer()=0;
  67. virtual ~service_callback(){}
  68. };
  69. #endif