]> git.tdb.fi Git - libs/net.git/blob - source/net/socket.cpp
Add shutdown support for client sockets
[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         mode = IO::M_RDWR;
50
51         priv->handle = p.handle;
52
53         SockAddr::SysAddr sa;
54         getsockname(priv->handle, reinterpret_cast<sockaddr *>(&sa.addr), &sa.size);
55         local_addr = SockAddr::new_from_sys(sa);
56
57 #ifdef WIN32
58         *priv->event = CreateEvent(0, false, false, 0);
59 #else
60         *priv->event = priv->handle;
61 #endif
62 }
63
64 Socket::Socket(Family af, int type, int proto):
65         priv(new Private),
66         local_addr(0)
67 {
68         mode = IO::M_RDWR;
69
70         priv->handle = socket(family_to_sys(af), type, proto);
71
72 #ifdef WIN32
73         *priv->event = CreateEvent(0, false, false, 0);
74 #else
75         *priv->event = priv->handle;
76 #endif
77 }
78
79 Socket::~Socket()
80 {
81 #ifdef WIN32
82         closesocket(priv->handle);
83         CloseHandle(*priv->event);
84 #else
85         ::close(priv->handle);
86 #endif
87
88         delete local_addr;
89         delete priv;
90 }
91
92 void Socket::set_block(bool b)
93 {
94         mode = (mode&~IO::M_NONBLOCK);
95         if(b)
96                 mode = (mode|IO::M_NONBLOCK);
97
98 #ifdef WIN32
99         u_long flag = !b;
100         ioctlsocket(priv->handle, FIONBIO, &flag);
101 #else
102         int flags = fcntl(priv->handle, F_GETFL);
103         fcntl(priv->handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
104 #endif
105 }
106
107 const IO::Handle &Socket::get_event_handle()
108 {
109         return priv->event;
110 }
111
112
113 void Socket::bind(const SockAddr &addr)
114 {
115         SockAddr::SysAddr sa = addr.to_sys();
116
117         int err = ::bind(priv->handle, reinterpret_cast<sockaddr *>(&sa.addr), sa.size);
118         if(err==-1)
119                 throw system_error("bind");
120
121         delete local_addr;
122         local_addr = addr.copy();
123 }
124
125 const SockAddr &Socket::get_local_address() const
126 {
127         if(local_addr==0)
128                 throw bad_socket_state("not bound");
129         return *local_addr;
130 }
131
132 void Socket::set_timeout(const Time::TimeDelta &timeout)
133 {
134 #ifndef WIN32
135         timeval tv = Time::rawtime_to_timeval(timeout.raw());
136         priv->set_option(SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(timeval));
137         priv->set_option(SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(timeval));
138 #else
139         DWORD msecs = static_cast<DWORD>(timeout/Time::msec);
140         priv->set_option(SOL_SOCKET, SO_RCVTIMEO, &msecs, sizeof(DWORD));
141         priv->set_option(SOL_SOCKET, SO_SNDTIMEO, &msecs, sizeof(DWORD));
142 #endif
143 }
144
145
146 int Socket::Private::set_option(int level, int optname, const void *optval, socklen_t optlen)
147 {
148 #ifdef WIN32
149         return setsockopt(handle, level, optname, reinterpret_cast<const char *>(optval), optlen);
150 #else
151         return setsockopt(handle, level, optname, optval, optlen);
152 #endif
153 }
154
155 int Socket::Private::get_option(int level, int optname, void *optval, socklen_t *optlen)
156 {
157 #ifdef WIN32
158         return getsockopt(handle, level, optname, reinterpret_cast<char *>(optval), optlen);
159 #else
160         return getsockopt(handle, level, optname, optval, optlen);
161 #endif
162 }
163
164 } // namespace Net
165 } // namespace Msp