6#define WIN32_LEAN_AND_MEAN
18#pragma comment(lib, "Ws2_32.lib")
19#pragma comment(lib, "Mswsock.lib")
20#pragma comment(lib, "AdvApi32.lib")
33 auto rv = WSAStartup(MAKEWORD(2, 2), &wsaData);
43 ::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, last_error,
44 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, (
sizeof(buf) /
sizeof(
char)),
NULL);
78 void connect(
const std::string& host,
int port)
87 ZeroMemory(&hints,
sizeof(hints));
89 hints.ai_family = AF_INET;
90 hints.ai_socktype = SOCK_STREAM;
91 hints.ai_flags = AI_NUMERICSERV;
92 hints.ai_protocol = 0;
94 auto port_str = std::to_string(port);
95 struct addrinfo* addrinfo_result;
96 auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result);
107 for (
auto*
rp = addrinfo_result;
rp !=
nullptr;
rp =
rp->ai_next)
109 socket_ = socket(
rp->ai_family,
rp->ai_socktype,
rp->ai_protocol);
126 ::freeaddrinfo(addrinfo_result);
135 ::setsockopt(
socket_, IPPROTO_TCP, TCP_NODELAY,
reinterpret_cast<char*
>(&enable_flag),
sizeof(enable_flag));
142 size_t bytes_sent = 0;
143 while (bytes_sent < n_bytes)
145 const int send_flags = 0;
146 auto write_result =
::send(
socket_,
data + bytes_sent, (
int)(n_bytes - bytes_sent), send_flags);
154 if (write_result == 0)
158 bytes_sent +=
static_cast<size_t>(write_result);
#define PHOTON_CLOSE
Definition Socket.hpp:38
#define SOCKET_ERROR
Definition Socket.hpp:27
#define WSAGetLastError()
Definition Socket.hpp:30
Definition tcp_client-windows.h:27
static void init_winsock_()
Definition tcp_client-windows.h:30
bool is_connected() const
Definition tcp_client-windows.h:61
void connect(const std::string &host, int port)
Definition tcp_client-windows.h:78
~tcp_client()
Definition tcp_client-windows.h:55
void send(const char *data, size_t n_bytes)
Definition tcp_client-windows.h:140
tcp_client()
Definition tcp_client-windows.h:50
SOCKET fd() const
Definition tcp_client-windows.h:72
static void throw_winsock_error_(const std::string &msg, int last_error)
Definition tcp_client-windows.h:40
void close()
Definition tcp_client-windows.h:66
SOCKET socket_
Definition tcp_client-windows.h:28
#define INVALID_SOCKET
Definition Config.hpp:44
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno)
Definition common-inl.h:75
#define NULL
Definition strtod.cpp:30
rp
Definition tag_strings.h:66
annotation details
Definition tag_strings.h:125