znet.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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(std::shared_ptr<client> clt)
  45. {
  46. }
  47. virtual void on_close(std::shared_ptr<client> clt)
  48. {
  49. log_info("client(%s) closed",clt->name().c_str());
  50. }
  51. virtual void on_error(std::shared_ptr<client> clt,const char*error)
  52. {
  53. log_info("client(%s) error with:%s",clt->name().c_str(),error);
  54. }
  55. virtual void on_send_timeout(std::shared_ptr<client> clt)
  56. {
  57. log_info("client(%s) send time out",clt->name().c_str());
  58. }
  59. virtual void on_recv_timeout(std::shared_ptr<client> clt)
  60. {
  61. log_info("client(%s) recv time out",clt->name().c_str());
  62. }
  63. virtual void on_message(std::shared_ptr<client> clt,const char*data,size_t len)=0;
  64. virtual void on_timer()=0;
  65. virtual ~service_callback(){}
  66. };
  67. #endif