]> git.tdb.fi Git - libs/net.git/blob - source/socket.cpp
Comment changes
[libs/net.git] / source / socket.cpp
1 #ifndef WIN32
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <sys/socket.h>
5 #endif
6 #include <iostream>
7 #include <msp/strings/formatter.h>
8 #include <msp/time/units.h>
9 #include "socket.h"
10
11 namespace {
12
13 #ifdef WIN32
14 class WinSockHelper
15 {
16 public:
17         WinSockHelper()
18         {
19                 WSADATA wsa_data;
20                 int err = WSAStartup(0x0002, &wsa_data);
21                 if(err)
22                         std::cerr<<"Failed to initialize WinSock: "<<err<<'\n';
23         }
24
25         ~WinSockHelper()
26         {
27                 WSACleanup();
28         }
29 };
30
31 WinSockHelper wsh;
32 #endif
33
34 }
35
36 namespace Msp {
37 namespace Net {
38
39 Socket::Socket(SocketHandle h, const SockAddr &paddr):
40         handle(h),
41         connected(true),
42         local_addr(0),
43         peer_addr(paddr.copy())
44 {
45         sockaddr_storage sa;
46         socklen_t size = sizeof(sockaddr_storage);
47         getsockname(handle, reinterpret_cast<sockaddr *>(&sa), &size);
48         local_addr = SockAddr::create(sa);
49
50 #ifdef WIN32
51         event = CreateEvent(0, false, false, 0);
52 #endif
53 }
54
55 Socket::Socket(Family af, int type, int proto):
56         connected(false),
57         local_addr(0),
58         peer_addr(0)
59 {
60         handle = socket(af, type, proto);
61
62 #ifdef WIN32
63         event = CreateEvent(0, false, false, 0);
64 #endif
65 }
66
67 Socket::~Socket()
68 {
69         close();
70 }
71
72 void Socket::set_block(bool b)
73 {
74         mode = (mode&~IO::M_NONBLOCK);
75         if(b)
76                 mode = (mode|IO::M_NONBLOCK);
77
78 #ifdef WIN32
79         u_long flag = !b;
80         ioctlsocket(handle, FIONBIO, &flag);
81 #else
82         int flags = fcntl(handle, F_GETFL);
83         fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
84 #endif
85 }
86
87 IO::Handle Socket::get_event_handle()
88 {
89 #ifdef WIN32
90         return event;
91 #else
92         return handle;
93 #endif
94 }
95
96
97 void Socket::bind(const SockAddr &addr)
98 {
99         check_state(false);
100
101         sockaddr_storage sa;
102         unsigned size = addr.fill_sockaddr(sa);
103
104         int err = ::bind(handle, reinterpret_cast<sockaddr *>(&sa), size);
105         if(err==-1)
106                 throw SystemError("Unable to bind", errno);
107
108         delete local_addr;
109         local_addr = addr.copy();
110 }
111
112 void Socket::close()
113 {
114         if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
115                 return;
116
117         set_events(IO::P_NONE);
118
119         signal_flush_required.emit();
120 #ifdef WIN32
121         closesocket(handle);
122         CloseHandle(event);
123 #else
124         ::close(handle);
125 #endif
126         handle = MSP_NET_INVALID_SOCKET_HANDLE;
127         connected = false;
128         signal_closed.emit();
129
130         delete local_addr;
131         local_addr = 0;
132         delete peer_addr;
133         peer_addr = 0;
134 }
135
136 void Socket::set_timeout(const Time::TimeDelta &timeout)
137 {
138 #ifndef WIN32
139         timeval tv;
140         timeout.fill_timeval(tv);
141         set_option(SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(timeval));
142         set_option(SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(timeval));
143 #else
144         DWORD msecs = static_cast<DWORD>(timeout/Time::msec);
145         set_option(SOL_SOCKET, SO_RCVTIMEO, &msecs, sizeof(DWORD));
146         set_option(SOL_SOCKET, SO_SNDTIMEO, &msecs, sizeof(DWORD));
147 #endif
148 }
149
150 const SockAddr &Socket::get_local_address() const
151 {
152         if(local_addr==0)
153                 throw InvalidState("Local address not set");
154         return *local_addr;
155 }
156
157 const SockAddr &Socket::get_peer_address() const
158 {
159         if(peer_addr==0)
160                 throw InvalidState("Peer address not set");
161         return *peer_addr;
162 }
163
164 void Socket::check_state(bool conn) const
165 {
166         if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
167                 throw Exception("Socket is closed");
168         if(conn && !connected)
169                 throw Exception("Socket is not connected");
170 }
171
172 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen)
173 {
174 #ifdef WIN32
175         return setsockopt(handle, level, optname, reinterpret_cast<const char *>(optval), optlen);
176 #else
177         return setsockopt(handle, level, optname, optval, optlen);
178 #endif
179 }
180
181 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) const
182 {
183 #ifdef WIN32
184         return getsockopt(handle, level, optname, reinterpret_cast<char *>(optval), optlen);
185 #else
186         return getsockopt(handle, level, optname, optval, optlen);
187 #endif
188 }
189
190 unsigned Socket::do_write(const char *buf, unsigned size)
191 {
192         check_state(true);
193
194         if(size==0)
195                 return 0;
196
197         int ret = ::send(handle, buf, size, 0);
198         if(ret<0)
199         {
200                 if(errno==EAGAIN)
201                         return 0;
202                 else
203                         throw SystemError("Writing to socket failed", errno);
204         }
205
206         return ret;
207 }
208
209 unsigned Socket::do_read(char *buf, unsigned size)
210 {
211         check_state(true);
212
213         if(size==0)
214                 return 0;
215
216         int ret = ::recv(handle, buf, size, 0);
217         if(ret<0)
218         {
219                 if(errno==EAGAIN)
220                         return 0;
221                 else
222                         throw SystemError("Reading from socket failed", errno);
223         }
224         else if(ret==0 && !eof_flag)
225         {
226                 eof_flag = true;
227                 signal_end_of_file.emit();
228                 set_events(IO::P_NONE);
229         }
230
231         return ret;
232 }
233
234 } // namespace Net
235 } // namespace Msp