udp_client-windows.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. // Helper RAII over winsock udp client socket.
  5. // Will throw on construction if socket creation failed.
  6. #include <spdlog/common.h>
  7. #include <spdlog/details/os.h>
  8. #include <spdlog/details/windows_include.h>
  9. #include <winsock2.h>
  10. #include <ws2tcpip.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string>
  14. #pragma comment(lib, "Ws2_32.lib")
  15. #pragma comment(lib, "Mswsock.lib")
  16. #pragma comment(lib, "AdvApi32.lib")
  17. namespace spdlog {
  18. namespace details {
  19. class udp_client
  20. {
  21. static constexpr int TX_BUFFER_SIZE = 1024 * 10;
  22. SOCKET socket_ = INVALID_SOCKET;
  23. sockaddr_in addr_ = {0};
  24. static void init_winsock_()
  25. {
  26. WSADATA wsaData;
  27. auto rv = ::WSAStartup(MAKEWORD(2, 2), &wsaData);
  28. if (rv != 0)
  29. {
  30. throw_winsock_error_("WSAStartup failed", ::WSAGetLastError());
  31. }
  32. }
  33. static void throw_winsock_error_(const std::string &msg, int last_error)
  34. {
  35. char buf[512];
  36. ::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, last_error,
  37. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, (sizeof(buf) / sizeof(char)), NULL);
  38. throw_spdlog_ex(fmt_lib::format("udp_sink - {}: {}", msg, buf));
  39. }
  40. void cleanup_()
  41. {
  42. if (socket_ != INVALID_SOCKET)
  43. {
  44. ::closesocket(socket_);
  45. }
  46. socket_ = INVALID_SOCKET;
  47. ::WSACleanup();
  48. }
  49. public:
  50. udp_client(const std::string &host, uint16_t port)
  51. {
  52. init_winsock_();
  53. addr_.sin_family = PF_INET;
  54. addr_.sin_port = htons(port);
  55. addr_.sin_addr.s_addr = INADDR_ANY;
  56. if (InetPtonA(PF_INET, host.c_str(), &addr_.sin_addr.s_addr) != 1)
  57. {
  58. int last_error = ::WSAGetLastError();
  59. ::WSACleanup();
  60. throw_winsock_error_("error: Invalid address!", last_error);
  61. }
  62. socket_ = ::socket(PF_INET, SOCK_DGRAM, 0);
  63. if (socket_ == INVALID_SOCKET)
  64. {
  65. int last_error = ::WSAGetLastError();
  66. ::WSACleanup();
  67. throw_winsock_error_("error: Create Socket failed", last_error);
  68. }
  69. int option_value = TX_BUFFER_SIZE;
  70. if (::setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<const char *>(&option_value), sizeof(option_value)) < 0)
  71. {
  72. int last_error = ::WSAGetLastError();
  73. cleanup_();
  74. throw_winsock_error_("error: setsockopt(SO_SNDBUF) Failed!", last_error);
  75. }
  76. }
  77. ~udp_client()
  78. {
  79. cleanup_();
  80. }
  81. SOCKET fd() const
  82. {
  83. return socket_;
  84. }
  85. void send(const char *data, size_t n_bytes)
  86. {
  87. socklen_t tolen = sizeof(struct sockaddr);
  88. if (::sendto(socket_, data, static_cast<int>(n_bytes), 0, (struct sockaddr *)&addr_, tolen) == -1)
  89. {
  90. throw_spdlog_ex("sendto(2) failed", errno);
  91. }
  92. }
  93. };
  94. } // namespace details
  95. } // namespace spdlog