znet.h 1.7 KB

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