udp_client-windows.h 3.1 KB

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