2 #include "platform_api.h"
3 #include <msp/core/systemerror.h>
4 #include <msp/io/handle_private.h>
5 #include "sockaddr_private.h"
7 #include "socket_private.h"
17 int err = WSAStartup(0x0002, &wsa_data);
19 std::cerr<<"Failed to initialize WinSock: "<<err<<'\n';
36 void Socket::platform_init()
38 *priv->event = CreateEvent(0, false, false, 0);
41 void Socket::platform_cleanup()
43 closesocket(priv->handle);
44 CloseHandle(*priv->event);
47 void Socket::set_timeout(const Time::TimeDelta &timeout)
49 DWORD msecs = static_cast<DWORD>(timeout/Time::msec);
50 priv->set_option(SOL_SOCKET, SO_RCVTIMEO, &msecs, sizeof(DWORD));
51 priv->set_option(SOL_SOCKET, SO_SNDTIMEO, &msecs, sizeof(DWORD));
54 void Socket::set_platform_events(unsigned e)
58 ws_events |= FD_READ|FD_CLOSE;
60 ws_events |= FD_CONNECT;
62 ws_events |= FD_ACCEPT;
63 WSAEventSelect(priv->handle, *priv->event, ws_events);
67 void Socket::Private::set_block(bool b)
70 ioctlsocket(handle, FIONBIO, &flag);
73 void Socket::Private::set_inherit(bool)
77 int Socket::Private::set_option(int level, int optname, const void *optval, socklen_t optlen)
79 return setsockopt(handle, level, optname, reinterpret_cast<const char *>(optval), optlen);
82 int Socket::Private::get_option(int level, int optname, void *optval, socklen_t *optlen)
84 return getsockopt(handle, level, optname, reinterpret_cast<char *>(optval), optlen);
88 unsigned check_sys_error(int ret, const char *func)
92 int err_code = WSAGetLastError();
93 if(err_code==WSAEWOULDBLOCK)
96 throw system_error(func, err_code);
102 bool check_sys_connect_error(int ret)
104 if(ret==SOCKET_ERROR)
106 int err_code = WSAGetLastError();
107 if(err_code==WSAEWOULDBLOCK)
110 throw system_error("connect", err_code);