]> git.tdb.fi Git - libs/net.git/blob - source/net/socket.cpp
Hide the set_option and get_option functions in socket_private.h
[libs/net.git] / source / net / socket.cpp
1 #ifndef WIN32
2 #include <cerrno>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <sys/socket.h>
6 #endif
7 #include <iostream>
8 #include <msp/core/systemerror.h>
9 #include <msp/io/handle_private.h>
10 #include <msp/strings/format.h>
11 #include <msp/time/rawtime_private.h>
12 #include <msp/time/units.h>
13 #include "sockaddr_private.h"
14 #include "socket.h"
15 #include "socket_private.h"
16
17 namespace {
18
19 #ifdef WIN32
20 class WinSockHelper
21 {
22 public:
23         WinSockHelper()
24         {
25                 WSADATA wsa_data;
26                 int err = WSAStartup(0x0002, &wsa_data);
27                 if(err)
28                         std::cerr<<"Failed to initialize WinSock: "<<err<<'\n';
29         }
30
31         ~WinSockHelper()
32         {
33                 WSACleanup();
34         }
35 };
36
37 WinSockHelper wsh;
38 #endif
39
40 }
41
42 namespace Msp {
43 namespace Net {
44
45 Socket::Socket(const Private &p):
46         priv(new Private),
47         local_addr(0)
48 {
49         priv->handle = p.handle;
50
51         SockAddr::SysAddr sa;
52         getsockname(priv->handle, reinterpret_cast<sockaddr *>(&sa.addr), &sa.size);
53         local_addr = SockAddr::new_from_sys(sa);
54
55 #ifdef WIN32
56         *priv->event = CreateEvent(0, false, false, 0);
57 #else
58         *priv->event = priv->handle;
59 #endif
60 }
61
62 Socket::Socket(Family af, int type, int proto):
63         priv(new Private),
64         local_addr(0)
65 {
66         priv->handle = socket(family_to_sys(af), type, proto);
67
68 #ifdef WIN32
69         *priv->event = CreateEvent(0, false, false, 0);
70 #else
71         *priv->event = priv->handle;
72 #endif
73 }
74
75 Socket::~Socket()
76 {
77 #ifdef WIN32
78         closesocket(priv->handle);
79         CloseHandle(*priv->event);
80 #else
81         ::close(priv->handle);
82 #endif
83
84         delete local_addr;
85         delete priv;
86 }
87
88 void Socket::set_block(bool b)
89 {
90         mode = (mode&~IO::M_NONBLOCK);
91         if(b)
92                 mode = (mode|IO::M_NONBLOCK);
93
94 #ifdef WIN32
95         u_long flag = !b;
96         ioctlsocket(priv->handle, FIONBIO, &flag);
97 #else
98         int flags = fcntl(priv->handle, F_GETFL);
99         fcntl(priv->handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
100 #endif
101 }
102
103 const IO::Handle &Socket::get_event_handle()
104 {
105         return priv->event;
106 }
107
108
109 void Socket::bind(const SockAddr &addr)
110 {
111         SockAddr::SysAddr sa = addr.to_sys();
112
113         int err = ::bind(priv->handle, reinterpret_cast<sockaddr *>(&sa.addr), sa.size);
114         if(err==-1)
115                 throw system_error("bind");
116
117         delete local_addr;
118         local_addr = addr.copy();
119 }
120
121 const SockAddr &Socket::get_local_address() const
122 {
123         if(local_addr==0)
124                 throw bad_socket_state("not bound");
125         return *local_addr;
126 }
127
128 void Socket::set_timeout(const Time::TimeDelta &timeout)
129 {
130 #ifndef WIN32
131         timeval tv = Time::rawtime_to_timeval(timeout.raw());
132         priv->set_option(SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(timeval));
133         priv->set_option(SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(timeval));
134 #else
135         DWORD msecs = static_cast<DWORD>(timeout/Time::msec);
136         priv->set_option(SOL_SOCKET, SO_RCVTIMEO, &msecs, sizeof(DWORD));
137         priv->set_option(SOL_SOCKET, SO_SNDTIMEO, &msecs, sizeof(DWORD));
138 #endif
139 }
140
141
142 int Socket::Private::set_option(int level, int optname, const void *optval, socklen_t optlen)
143 {
144 #ifdef WIN32
145         return setsockopt(handle, level, optname, reinterpret_cast<const char *>(optval), optlen);
146 #else
147         return setsockopt(handle, level, optname, optval, optlen);
148 #endif
149 }
150
151 int Socket::Private::get_option(int level, int optname, void *optval, socklen_t *optlen)
152 {
153 #ifdef WIN32
154         return getsockopt(handle, level, optname, reinterpret_cast<char *>(optval), optlen);
155 #else
156         return getsockopt(handle, level, optname, optval, optlen);
157 #endif
158 }
159
160 } // namespace Net
161 } // namespace Msp