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