Photon 1.0.0
Loading...
Searching...
No Matches
udp_client-windows.h
Go to the documentation of this file.
1// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2// Distributed under the MIT License (http://opensource.org/licenses/MIT)
3
4#pragma once
5
6// Helper RAII over winsock udp client socket.
7// Will throw on construction if socket creation failed.
8
9#include <spdlog/common.h>
10#include <spdlog/details/os.h>
12#include <winsock2.h>
13#include <ws2tcpip.h>
14#include <stdlib.h>
15#include <stdio.h>
16#include <string>
17
18#if defined(_MSC_VER)
19#pragma comment(lib, "Ws2_32.lib")
20#pragma comment(lib, "Mswsock.lib")
21#pragma comment(lib, "AdvApi32.lib")
22#endif
23
24namespace spdlog
25{
26 namespace details
27 {
29 {
30 static constexpr int TX_BUFFER_SIZE = 1024 * 10;
32 sockaddr_in addr_ = {};
33
34 static void init_winsock_()
35 {
36 WSADATA wsaData;
37 auto rv = ::WSAStartup(MAKEWORD(2, 2), &wsaData);
38 if (rv != 0)
39 {
40 throw_winsock_error_("WSAStartup failed", ::WSAGetLastError());
41 }
42 }
43
44 static void throw_winsock_error_(const std::string& msg, int last_error)
45 {
46 char buf[512];
47 ::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, last_error,
48 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, (sizeof(buf) / sizeof(char)), NULL);
49
50 throw_spdlog_ex(fmt_lib::format("udp_sink - {}: {}", msg, buf));
51 }
52
53 void cleanup_()
54 {
56 {
58 }
60 ::WSACleanup();
61 }
62
63 public:
64 udp_client(const std::string& host, uint16_t port)
65 {
67
68 addr_.sin_family = PF_INET;
69 addr_.sin_port = htons(port);
70 addr_.sin_addr.s_addr = INADDR_ANY;
71 if (InetPtonA(PF_INET, host.c_str(), &addr_.sin_addr.s_addr) != 1)
72 {
73 int last_error = ::WSAGetLastError();
74 ::WSACleanup();
75 throw_winsock_error_("error: Invalid address!", last_error);
76 }
77
78 socket_ = ::socket(PF_INET, SOCK_DGRAM, 0);
80 {
81 int last_error = ::WSAGetLastError();
82 ::WSACleanup();
83 throw_winsock_error_("error: Create Socket failed", last_error);
84 }
85
86 int option_value = TX_BUFFER_SIZE;
87 if (::setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<const char*>(&option_value), sizeof(option_value)) < 0)
88 {
89 int last_error = ::WSAGetLastError();
90 cleanup_();
91 throw_winsock_error_("error: setsockopt(SO_SNDBUF) Failed!", last_error);
92 }
93 }
94
96 {
97 cleanup_();
98 }
99
100 SOCKET fd() const
101 {
102 return socket_;
103 }
104
105 void send(const char* data, size_t n_bytes)
106 {
107 socklen_t tolen = sizeof(struct sockaddr);
108 if (::sendto(socket_, data, static_cast<int>(n_bytes), 0, (struct sockaddr*)&addr_, tolen) == -1)
109 {
110 throw_spdlog_ex("sendto(2) failed", errno);
111 }
112 }
113 };
114 } // namespace details
115} // namespace spdlog
#define PHOTON_CLOSE
Definition Socket.hpp:38
#define WSAGetLastError()
Definition Socket.hpp:30
Definition udp_client-windows.h:29
static constexpr int TX_BUFFER_SIZE
Definition udp_client-windows.h:30
static void init_winsock_()
Definition udp_client-windows.h:34
void cleanup_()
Definition udp_client-windows.h:53
SOCKET socket_
Definition udp_client-windows.h:31
~udp_client()
Definition udp_client-windows.h:95
static void throw_winsock_error_(const std::string &msg, int last_error)
Definition udp_client-windows.h:44
sockaddr_in addr_
Definition udp_client-windows.h:32
udp_client(const std::string &host, uint16_t port)
Definition udp_client-windows.h:64
void send(const char *data, size_t n_bytes)
Definition udp_client-windows.h:105
SOCKET fd() const
Definition udp_client-windows.h:100
#define INVALID_SOCKET
Definition Config.hpp:44
Definition async.h:26
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno)
Definition common-inl.h:75
#define NULL
Definition strtod.cpp:30
Definition format.h:1901
annotation details
Definition tag_strings.h:125