13 #ifndef __STOUT_OS_WINDOWS_SOCKET_HPP__ 14 #define __STOUT_OS_WINDOWS_SOCKET_HPP__ 16 #include <glog/logging.h> 31 WORD requestedVersion = MAKEWORD(2, 2);
34 const int result = ::WSAStartup(requestedVersion, &data);
36 const int error = ::WSAGetLastError();
37 LOG(ERROR) <<
"Could not initialize WinSock, error code : " <<
error;
43 if (LOBYTE(data.wVersion) < 2 ||
44 (LOBYTE(data.wVersion) == 2 && HIBYTE(data.wVersion) != 2)) {
45 LOG(ERROR) <<
"Incorrect WinSock version found : " << LOBYTE(data.wVersion)
46 <<
"." << HIBYTE(data.wVersion);
50 if (::WSACleanup() != 0) {
51 const int error = ::WSAGetLastError();
52 LOG(ERROR) <<
"Could not cleanup WinSock, error code : " <<
error;
68 while (retriesLeft > 0) {
69 const int result = ::WSACleanup();
71 const int error = ::WSAGetLastError();
73 if (error == WSANOTINITIALISED) {
78 if (error == WSAEINPROGRESS) {
79 LOG(ERROR) <<
"Waiting for outstanding WinSock calls to complete.";
83 LOG(ERROR) <<
"Could not cleanup WinSock, error code : " <<
error;
89 if (retriesLeft == 0) {
119 DWORD
flags = WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT)
121 SOCKET s = ::WSASocketW(family, type, protocol,
nullptr, 0,
flags);
122 if (s == INVALID_SOCKET) {
133 const int_fd& fd, sockaddr* addr, socklen_t* addrlen)
135 return int_fd(::
accept(fd, addr, reinterpret_cast<int*>(addrlen)));
144 const int_fd& fd,
const sockaddr* addr, socklen_t addrlen)
146 CHECK_LE(addrlen, INT32_MAX);
152 const int_fd& fd,
const sockaddr*
address, socklen_t addrlen)
154 CHECK_LE(addrlen, INT32_MAX);
160 const int_fd& fd,
const void* buf,
size_t len,
int flags)
162 CHECK_LE(len, INT32_MAX);
164 fd, static_cast<const char*>(buf), static_cast<int>(len), flags);
170 CHECK_LE(len, INT32_MAX);
171 return ::recv(fd, static_cast<char*>(buf), static_cast<int>(len), flags);
176 #endif // __STOUT_OS_WINDOWS_SOCKET_HPP__ SSIZE_T ssize_t
Definition: windows.hpp:186
Try< int_fd > socket(int family, int type, int protocol)
Definition: socket.hpp:43
Try< Address > address(int_fd s)
Returns the Address with the assigned ip and assigned port.
Definition: network.hpp:79
Definition: error.hpp:123
bool is_inprogress_error(int error)
Definition: socket.hpp:80
int bind(const int_fd &fd, const sockaddr *addr, socklen_t addrlen)
Definition: socket.hpp:143
int_fd accept(const int_fd &fd, sockaddr *addr, socklen_t *addrlen)
Definition: socket.hpp:132
bool is_restartable_error(int error)
Definition: socket.hpp:65
ssize_t send(const int_fd &fd, const void *buf, size_t len, int flags)
Definition: socket.hpp:159
bool wsa_initialize()
Definition: socket.hpp:28
ssize_t recv(const int_fd &fd, void *buf, size_t len, int flags)
Definition: socket.hpp:168
std::string error(const std::string &msg, uint32_t code)
bool wsa_cleanup()
Definition: socket.hpp:62
Try< uint32_t > type(const std::string &path)
int connect(const int_fd &fd, const sockaddr *address, socklen_t addrlen)
Definition: socket.hpp:151
int int_fd
Definition: int_fd.hpp:35
bool is_retryable_error(int error)
Definition: socket.hpp:74