1
0

UdpClient.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef HV_UDP_CLIENT_HPP_
  2. #define HV_UDP_CLIENT_HPP_
  3. #include "hsocket.h"
  4. #include "EventLoopThread.h"
  5. #include "Channel.h"
  6. namespace hv {
  7. template<class TSocketChannel = SocketChannel>
  8. class UdpClientTmpl {
  9. public:
  10. typedef std::shared_ptr<TSocketChannel> TSocketChannelPtr;
  11. UdpClientTmpl() {
  12. #if WITH_KCP
  13. enable_kcp = false;
  14. #endif
  15. }
  16. virtual ~UdpClientTmpl() {
  17. }
  18. const EventLoopPtr& loop() {
  19. return loop_thread.loop();
  20. }
  21. //@retval >=0 sockfd, <0 error
  22. int createsocket(int port, const char* host = "127.0.0.1") {
  23. hio_t* io = hloop_create_udp_client(loop_thread.hloop(), host, port);
  24. if (io == NULL) return -1;
  25. channel.reset(new TSocketChannel(io));
  26. return channel->fd();
  27. }
  28. // closesocket thread-safe
  29. void closesocket() {
  30. if (channel) {
  31. channel->close(true);
  32. }
  33. }
  34. int startRecv() {
  35. assert(channel != NULL);
  36. channel->onread = [this](Buffer* buf) {
  37. if (onMessage) {
  38. onMessage(channel, buf);
  39. }
  40. };
  41. channel->onwrite = [this](Buffer* buf) {
  42. if (onWriteComplete) {
  43. onWriteComplete(channel, buf);
  44. }
  45. };
  46. #if WITH_KCP
  47. if (enable_kcp) {
  48. hio_set_kcp(channel->io(), &kcp_setting);
  49. }
  50. #endif
  51. return channel->startRead();
  52. }
  53. void start(bool wait_threads_started = true) {
  54. loop_thread.start(wait_threads_started, std::bind(&UdpClientTmpl::startRecv, this));
  55. }
  56. // stop thread-safe
  57. void stop(bool wait_threads_stopped = true) {
  58. loop_thread.stop(wait_threads_stopped);
  59. }
  60. // sendto thread-safe
  61. int sendto(const void* data, int size, struct sockaddr* peeraddr = NULL) {
  62. if (channel == NULL) return -1;
  63. std::lock_guard<std::mutex> locker(sendto_mutex);
  64. if (peeraddr) hio_set_peeraddr(channel->io(), peeraddr, SOCKADDR_LEN(peeraddr));
  65. return channel->write(data, size);
  66. }
  67. int sendto(Buffer* buf, struct sockaddr* peeraddr = NULL) {
  68. return sendto(buf->data(), buf->size(), peeraddr);
  69. }
  70. int sendto(const std::string& str, struct sockaddr* peeraddr = NULL) {
  71. return sendto(str.data(), str.size(), peeraddr);
  72. }
  73. #if WITH_KCP
  74. void setKcp(kcp_setting_t* setting) {
  75. if (setting) {
  76. enable_kcp = true;
  77. kcp_setting = *setting;
  78. } else {
  79. enable_kcp = false;
  80. }
  81. }
  82. #endif
  83. public:
  84. TSocketChannelPtr channel;
  85. #if WITH_KCP
  86. bool enable_kcp;
  87. kcp_setting_t kcp_setting;
  88. #endif
  89. // Callback
  90. std::function<void(const TSocketChannelPtr&, Buffer*)> onMessage;
  91. std::function<void(const TSocketChannelPtr&, Buffer*)> onWriteComplete;
  92. private:
  93. std::mutex sendto_mutex;
  94. EventLoopThread loop_thread;
  95. };
  96. typedef UdpClientTmpl<SocketChannel> UdpClient;
  97. }
  98. #endif // HV_UDP_CLIENT_HPP_