udp_node.h 1020 B

1234567891011121314151617181920212223242526272829303132333435
  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. const ylib::AddressPort& local() { return m_local_ap; }
  25. friend class node_lst;
  26. private:
  27. std::function<void(ylib::network::udp::node*, const ylib::AddressPort&, const char*, uint32)> m_callback_recv;
  28. ylib::AddressPort m_local_ap;
  29. IUdpNode* m_node = nullptr;
  30. ylib::network::udp::node_lst* m_node_listener = nullptr;
  31. };
  32. }
  33. }
  34. }
  35. #endif