udp_node.h 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #pragma once
  2. #include "net/define.h"
  3. #if USE_NET_UDP_NODE
  4. #include <functional>
  5. #include "base/error.h"
  6. class IUdpNode;
  7. namespace ylib
  8. {
  9. namespace network
  10. {
  11. namespace udp
  12. {
  13. class node_lst;
  14. class node :public ylib::error_base
  15. {
  16. public:
  17. node();
  18. ~node();
  19. // 通过 IServer 继承
  20. bool start(const ylib::AddressPort& bind_ap);
  21. bool close();
  22. bool send(const std::string& remote_ipaddress, ushort remote_port, const char* pData, uint32 len);
  23. void on_recv(std::function<void(ylib::network::udp::node* node, const ylib::AddressPort& remote_ap, const char* data, uint32 len)> callback) { m_callback_recv = callback; }
  24. ushort listen_port();
  25. const ylib::AddressPort& local() { return m_local_ap; }
  26. friend class node_lst;
  27. private:
  28. std::function<void(ylib::network::udp::node*, const ylib::AddressPort&, const char*, uint32)> m_callback_recv;
  29. ylib::AddressPort m_local_ap;
  30. IUdpNode* m_node = nullptr;
  31. ylib::network::udp::node_lst* m_node_listener = nullptr;
  32. };
  33. }
  34. }
  35. }
  36. #endif