znet.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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; //分站:0,WEB:1
  10. virtual int handle()=0;
  11. virtual void close()=0;
  12. virtual void send(std::vector<char>&&b)=0;
  13. virtual ~client(){}
  14. };
  15. struct service_callback;
  16. struct service_handle
  17. {
  18. virtual int run(int port)=0;
  19. virtual void stop()=0;
  20. virtual void boardcast(const std::vector<char>&msg)=0;
  21. static service_handle*instance(service_callback*sc);
  22. };
  23. struct service_callback:service_handle
  24. {
  25. service_handle*m_handle{nullptr};
  26. void set_handle(service_handle*handle)
  27. {
  28. m_handle=handle;
  29. }
  30. int run(int port)
  31. {
  32. return m_handle->run(port);
  33. }
  34. void stop()
  35. {
  36. m_handle->stop();
  37. }
  38. void boardcast(const std::vector<char>&msg)
  39. {
  40. m_handle->boardcast(msg);
  41. }
  42. virtual void on_connect(std::shared_ptr<client> clt)
  43. {
  44. }
  45. virtual void on_close(std::shared_ptr<client> clt)
  46. {
  47. log_info("client(%s) closed",clt->name().c_str());
  48. }
  49. virtual void on_error(std::shared_ptr<client> clt,const char*error)
  50. {
  51. log_info("client(%s) error with:%s",clt->name().c_str(),error);
  52. }
  53. virtual void on_send_timeout(std::shared_ptr<client> clt)
  54. {
  55. log_info("client(%s) send time out",clt->name().c_str());
  56. }
  57. virtual void on_recv_timeout(std::shared_ptr<client> clt)
  58. {
  59. log_info("client(%s) recv time out",clt->name().c_str());
  60. }
  61. virtual void on_message(std::shared_ptr<client> clt,const char*data,size_t len)=0;
  62. virtual ~service_callback(){}
  63. };
  64. #endif