]> git.tdb.fi Git - libs/net.git/blob - source/net/unix/socket.cpp
Move most platform-specific code into overlay directories
[libs/net.git] / source / net / unix / socket.cpp
1 #include <cerrno>
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include "platform_api.h"
5 #include <msp/core/systemerror.h>
6 #include <msp/io/handle_private.h>
7 #include <msp/time/rawtime_private.h>
8 #include "sockaddr_private.h"
9 #include "socket.h"
10 #include "socket_private.h"
11
12 namespace Msp {
13 namespace Net {
14
15 void Socket::platform_init()
16 {
17         *priv->event = priv->handle;
18 }
19
20 void Socket::platform_cleanup()
21 {
22         close(priv->handle);
23 }
24
25 void Socket::set_timeout(const Time::TimeDelta &timeout)
26 {
27         timeval tv = Time::rawtime_to_timeval(timeout.raw());
28         priv->set_option(SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(timeval));
29         priv->set_option(SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(timeval));
30 }
31
32 void Socket::set_platform_events(unsigned)
33 {
34 }
35
36
37 void Socket::Private::set_block(bool b)
38 {
39         int flags = fcntl(handle, F_GETFL);
40         fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
41 }
42
43 int Socket::Private::set_option(int level, int optname, const void *optval, socklen_t optlen)
44 {
45         return setsockopt(handle, level, optname, optval, optlen);
46 }
47
48 int Socket::Private::get_option(int level, int optname, void *optval, socklen_t *optlen)
49 {
50         return getsockopt(handle, level, optname, optval, optlen);
51 }
52
53
54 unsigned check_sys_error(int ret, const char *func)
55 {
56         if(ret<0)
57         {
58                 if(errno==EAGAIN)
59                         return 0;
60                 else
61                         throw system_error(func);
62         }
63
64         return ret;
65 }
66
67 bool check_sys_connect_error(int ret)
68 {
69         if(ret<0)
70         {
71                 if(errno==EINPROGRESS)
72                         return false;
73                 else
74                         throw system_error("connect");
75         }
76
77         return true;
78 }
79
80 } // namespace Net
81 } // namespace Msp