]> git.tdb.fi Git - libs/net.git/blob - source/net/windows/socket.cpp
Move most platform-specific code into overlay directories
[libs/net.git] / source / net / windows / socket.cpp
1 #include <iostream>
2 #include "platform_api.h"
3 #include <msp/core/systemerror.h>
4 #include <msp/io/handle_private.h>
5 #include "sockaddr_private.h"
6 #include "socket.h"
7 #include "socket_private.h"
8
9 namespace {
10
11 class WinSockHelper
12 {
13 public:
14         WinSockHelper()
15         {
16                 WSADATA wsa_data;
17                 int err = WSAStartup(0x0002, &wsa_data);
18                 if(err)
19                         std::cerr<<"Failed to initialize WinSock: "<<err<<'\n';
20         }
21
22         ~WinSockHelper()
23         {
24                 WSACleanup();
25         }
26 };
27
28 WinSockHelper wsh;
29
30 }
31
32
33 namespace Msp {
34 namespace Net {
35
36 void Socket::platform_init()
37 {
38         *priv->event = CreateEvent(0, false, false, 0);
39 }
40
41 void Socket::platform_cleanup()
42 {
43         closesocket(priv->handle);
44         CloseHandle(*priv->event);
45 }
46
47 void Socket::set_timeout(const Time::TimeDelta &timeout)
48 {
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));
52 }
53
54 void Socket::set_platform_events(unsigned e)
55 {
56         long ws_events = 0;
57         if(e&S_INPUT)
58                 ws_events |= FD_READ|FD_CLOSE;
59         if(e&S_CONNECT)
60                 ws_events |= FD_CONNECT;
61         if(e&S_ACCEPT)
62                 ws_events |= FD_ACCEPT;
63         WSAEventSelect(priv->handle, *priv->event, ws_events);
64 }
65
66
67 void Socket::Private::set_block(bool b)
68 {
69         u_long flag = !b;
70         ioctlsocket(handle, FIONBIO, &flag);
71 }
72
73 int Socket::Private::set_option(int level, int optname, const void *optval, socklen_t optlen)
74 {
75         return setsockopt(handle, level, optname, reinterpret_cast<const char *>(optval), optlen);
76 }
77
78 int Socket::Private::get_option(int level, int optname, void *optval, socklen_t *optlen)
79 {
80         return getsockopt(handle, level, optname, reinterpret_cast<char *>(optval), optlen);
81 }
82
83
84 unsigned check_sys_error(int ret, const char *func)
85 {
86         if(ret<0)
87         {
88                 int err_code = WSAGetLastError();
89                 if(err_code==WSAEWOULDBLOCK)
90                         return 0;
91                 else
92                         throw system_error(func, err_code);
93         }
94
95         return ret;
96 }
97
98 bool check_sys_connect_error(int ret)
99 {
100         if(ret==SOCKET_ERROR)
101         {
102                 int err_code = WSAGetLastError();
103                 if(err_code==WSAEWOULDBLOCK)
104                         return false;
105                 else
106                         throw system_error("connect", err_code);
107         }
108
109         return true;
110 }
111
112 } // namespace Net
113 } // namespace Msp