tcp_client.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #pragma once
  2. #include "define.h"
  3. #ifdef USE_NET_TCP_CLIENT
  4. #include <string>
  5. #include <functional>
  6. #include "base/buffer.h"
  7. #include "base/error.h"
  8. class ITcpClient;
  9. namespace ylib
  10. {
  11. namespace network
  12. {
  13. namespace tcp
  14. {
  15. class client_lst;
  16. class client :public ylib::error_base
  17. {
  18. public:
  19. client(ylib::receive_model model = ylib::PUSH_DEFAULT);
  20. ~client();
  21. // 通过 IClient 继承
  22. bool start();
  23. bool start(const ylib::AddressPort& remote_ap);
  24. bool disconn();
  25. bool close(uint32 wait_msec = 0);
  26. bool connect(const ylib::AddressPort& remote_ap, int32 wait_msec = -1);
  27. bool send(const char* buff, uint32 len);
  28. inline const ITcpClient* getHP() { return m_client; }
  29. friend class client_lst;
  30. public:
  31. void on_recv(std::function<void(ylib::network::tcp::client*, const char*, uint32)> callback) { m_callback_onrecv = callback; }
  32. void on_accept(std::function<void(ylib::network::tcp::client*)> callback) { m_callback_onaccept = callback; }
  33. void on_close(std::function<void(ylib::network::tcp::client*)> callback) { m_callback_onclose = callback; }
  34. void on_filter(std::function<void(ylib::network::tcp::client*, uint32)> callback) { m_callback_onfilter = callback; }
  35. private:
  36. std::function<void(ylib::network::tcp::client*)> m_callback_onaccept;
  37. std::function<void(ylib::network::tcp::client*)> m_callback_onclose;
  38. std::function<void(ylib::network::tcp::client*, const char*, uint32)> m_callback_onrecv;
  39. std::function<void(ylib::network::tcp::client*, uint32)> m_callback_onfilter;
  40. ylib::AddressPort m_remote_ap;
  41. // 是否PULL模型
  42. bool m_ispull = false;
  43. ITcpClient* m_client = nullptr;
  44. client_lst* m_client_listener = nullptr;
  45. ylib::receive_model m_model = ylib::PUSH_DEFAULT;
  46. };
  47. }
  48. }
  49. }
  50. #endif