Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-09 08:47:30

0001 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
0002 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
0003 
0004 #pragma once
0005 
0006 // Helper RAII over winsock udp client socket.
0007 // Will throw on construction if socket creation failed.
0008 
0009 #include <spdlog/common.h>
0010 #include <spdlog/details/os.h>
0011 #include <spdlog/details/windows_include.h>
0012 #include <stdio.h>
0013 #include <stdlib.h>
0014 #include <string>
0015 #include <winsock2.h>
0016 #include <ws2tcpip.h>
0017 
0018 #if defined(_MSC_VER)
0019     #pragma comment(lib, "Ws2_32.lib")
0020     #pragma comment(lib, "Mswsock.lib")
0021     #pragma comment(lib, "AdvApi32.lib")
0022 #endif
0023 
0024 namespace spdlog {
0025 namespace details {
0026 class udp_client {
0027     static constexpr int TX_BUFFER_SIZE = 1024 * 10;
0028     SOCKET socket_ = INVALID_SOCKET;
0029     sockaddr_in addr_ = {};
0030 
0031     static void init_winsock_() {
0032         WSADATA wsaData;
0033         auto rv = ::WSAStartup(MAKEWORD(2, 2), &wsaData);
0034         if (rv != 0) {
0035             throw_winsock_error_("WSAStartup failed", ::WSAGetLastError());
0036         }
0037     }
0038 
0039     static void throw_winsock_error_(const std::string &msg, int last_error) {
0040         char buf[512];
0041         ::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
0042                          last_error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf,
0043                          (sizeof(buf) / sizeof(char)), NULL);
0044 
0045         throw_spdlog_ex(fmt_lib::format("udp_sink - {}: {}", msg, buf));
0046     }
0047 
0048     void cleanup_() {
0049         if (socket_ != INVALID_SOCKET) {
0050             ::closesocket(socket_);
0051         }
0052         socket_ = INVALID_SOCKET;
0053         ::WSACleanup();
0054     }
0055 
0056 public:
0057     udp_client(const std::string &host, uint16_t port) {
0058         init_winsock_();
0059 
0060         addr_.sin_family = PF_INET;
0061         addr_.sin_port = htons(port);
0062         addr_.sin_addr.s_addr = INADDR_ANY;
0063         if (InetPtonA(PF_INET, host.c_str(), &addr_.sin_addr.s_addr) != 1) {
0064             int last_error = ::WSAGetLastError();
0065             ::WSACleanup();
0066             throw_winsock_error_("error: Invalid address!", last_error);
0067         }
0068 
0069         socket_ = ::socket(PF_INET, SOCK_DGRAM, 0);
0070         if (socket_ == INVALID_SOCKET) {
0071             int last_error = ::WSAGetLastError();
0072             ::WSACleanup();
0073             throw_winsock_error_("error: Create Socket failed", last_error);
0074         }
0075 
0076         int option_value = TX_BUFFER_SIZE;
0077         if (::setsockopt(socket_, SOL_SOCKET, SO_SNDBUF,
0078                          reinterpret_cast<const char *>(&option_value), sizeof(option_value)) < 0) {
0079             int last_error = ::WSAGetLastError();
0080             cleanup_();
0081             throw_winsock_error_("error: setsockopt(SO_SNDBUF) Failed!", last_error);
0082         }
0083     }
0084 
0085     ~udp_client() { cleanup_(); }
0086 
0087     SOCKET fd() const { return socket_; }
0088 
0089     void send(const char *data, size_t n_bytes) {
0090         socklen_t tolen = sizeof(struct sockaddr);
0091         if (::sendto(socket_, data, static_cast<int>(n_bytes), 0, (struct sockaddr *)&addr_,
0092                      tolen) == -1) {
0093             throw_spdlog_ex("sendto(2) failed", errno);
0094         }
0095     }
0096 };
0097 }  // namespace details
0098 }  // namespace spdlog