1 #include "platform_api.h"
2 #include <msp/core/systemerror.h>
3 #include <msp/io/handle_private.h>
4 #include "sockaddr_private.h"
6 #include "socket_private.h"
13 Socket::Socket(const Private &p):
14 priv(make_unique<Private>())
18 priv->handle = p.handle;
21 getsockname(priv->handle, reinterpret_cast<sockaddr *>(&sa.addr), &sa.size);
22 local_addr.reset(SockAddr::new_from_sys(sa));
27 Socket::Socket(Family af, int type, int proto):
28 priv(make_unique<Private>())
32 // TODO use SOCK_CLOEXEC on Linux
33 priv->handle = socket(family_to_sys(af), type, proto);
43 void Socket::set_block(bool b)
45 IO::adjust_mode(mode, IO::M_NONBLOCK, !b);
49 void Socket::set_inherit(bool i)
51 IO::adjust_mode(mode, IO::M_INHERIT, i);
55 const IO::Handle &Socket::get_handle(IO::Mode)
57 // TODO could this be implemented somehow?
58 throw unsupported("Socket::get_handle");
61 const IO::Handle &Socket::get_event_handle()
66 void Socket::bind(const SockAddr &addr)
68 SockAddr::SysAddr sa = addr.to_sys();
70 int err = ::bind(priv->handle, reinterpret_cast<sockaddr *>(&sa.addr), sa.size);
72 throw system_error("bind");
74 local_addr.reset(addr.copy());
77 const SockAddr &Socket::get_local_address() const
80 throw bad_socket_state("not bound");
84 void Socket::set_socket_events(unsigned e)
86 IO::PollEvent pe = static_cast<IO::PollEvent>(e&4095);
92 set_platform_events(e);