1
0

znet.h 1.6 KB

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