udp_client-windows.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #if defined(_MSC_VER)
  15. # pragma comment(lib, "Ws2_32.lib")
  16. # pragma comment(lib, "Mswsock.lib")
  17. # pragma comment(lib, "AdvApi32.lib")
  18. #endif
  19. namespace spdlog {
  20. namespace details {
  21. class udp_client
  22. {
  23. static constexpr int TX_BUFFER_SIZE = 1024 * 10;
  24. SOCKET socket_ = INVALID_SOCKET;
  25. sockaddr_in addr_ = {};
  26. static void init_winsock_()
  27. {
  28. WSADATA wsaData;
  29. auto rv = ::WSAStartup(MAKEWORD(2, 2), &wsaData);
  30. if (rv != 0)
  31. {
  32. throw_winsock_error_("WSAStartup failed", ::WSAGetLastError());
  33. }
  34. }
  35. static void throw_winsock_error_(const std::string &msg, int last_error)
  36. {
  37. char buf[512];
  38. ::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, last_error,
  39. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, (sizeof(buf) / sizeof(char)), NULL);
  40. throw_spdlog_ex(fmt_lib::format("udp_sink - {}: {}", msg, buf));
  41. }
  42. void cleanup_()
  43. {
  44. if (socket_ != INVALID_SOCKET)
  45. {
  46. ::closesocket(socket_);
  47. }
  48. socket_ = INVALID_SOCKET;
  49. ::WSACleanup();
  50. }
  51. public:
  52. udp_client(const std::string &host, uint16_t port)
  53. {
  54. init_winsock_();
  55. addr_.sin_family = PF_INET;
  56. addr_.sin_port = htons(port);
  57. addr_.sin_addr.s_addr = INADDR_ANY;
  58. if (InetPtonA(PF_INET, host.c_str(), &addr_.sin_addr.s_addr) != 1)
  59. {
  60. int last_error = ::WSAGetLastError();
  61. ::WSACleanup();
  62. throw_winsock_error_("error: Invalid address!", last_error);
  63. }
  64. socket_ = ::socket(PF_INET, SOCK_DGRAM, 0);
  65. if (socket_ == INVALID_SOCKET)
  66. {
  67. int last_error = ::WSAGetLastError();
  68. ::WSACleanup();
  69. throw_winsock_error_("error: Create Socket failed", last_error);
  70. }
  71. int option_value = TX_BUFFER_SIZE;
  72. if (::setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<const char *>(&option_value), sizeof(option_value)) < 0)
  73. {
  74. int last_error = ::WSAGetLastError();
  75. cleanup_();
  76. throw_winsock_error_("error: setsockopt(SO_SNDBUF) Failed!", last_error);
  77. }
  78. }
  79. ~udp_client()
  80. {
  81. cleanup_();
  82. }
  83. SOCKET fd() const
  84. {
  85. return socket_;
  86. }
  87. void send(const char *data, size_t n_bytes)
  88. {
  89. socklen_t tolen = sizeof(struct sockaddr);
  90. if (::sendto(socket_, data, static_cast<int>(n_bytes), 0, (struct sockaddr *)&addr_, tolen) == -1)
  91. {
  92. throw_spdlog_ex("sendto(2) failed", errno);
  93. }
  94. }
  95. };
  96. } // namespace details
  97. } // namespace spdlog