#include "socket.h"
#include "socket_private.h"
+using namespace std;
+
namespace Msp {
namespace Net {
{
mode = IO::M_RDWR;
+ // TODO use SOCK_CLOEXEC on Linux
priv->handle = socket(family_to_sys(af), type, proto);
platform_init();
void Socket::set_block(bool b)
{
- mode = (mode&~IO::M_NONBLOCK);
- if(b)
- mode = (mode|IO::M_NONBLOCK);
-
+ IO::adjust_mode(mode, IO::M_NONBLOCK, !b);
priv->set_block(b);
}
+void Socket::set_inherit(bool i)
+{
+ IO::adjust_mode(mode, IO::M_INHERIT, i);
+ priv->set_inherit(i);
+}
+
+const IO::Handle &Socket::get_handle(IO::Mode)
+{
+ // TODO could this be implemented somehow?
+ throw logic_error("Socket::get_handle");
+}
+
const IO::Handle &Socket::get_event_handle()
{
return priv->event;
~Socket();
virtual void set_block(bool);
+ virtual void set_inherit(bool);
+ const IO::Handle &get_handle(IO::Mode);
virtual const IO::Handle &get_event_handle();
/** Associates the socket with a local address. There must be no existing
IO::Handle event;
void set_block(bool);
+ void set_inherit(bool);
int set_option(int, int, const void *, socklen_t);
int get_option(int, int, void *, socklen_t *);
};
void Socket::platform_init()
{
*priv->event = priv->handle;
+ set_inherit(false);
}
void Socket::platform_cleanup()
fcntl(handle, F_SETFL, (flags&~O_NONBLOCK)|(b?0:O_NONBLOCK));
}
+void Socket::Private::set_inherit(bool i)
+{
+ int flags = fcntl(handle, F_GETFD);
+ fcntl(handle, F_SETFD, (flags&~O_CLOEXEC)|(i?O_CLOEXEC:0));
+}
+
int Socket::Private::set_option(int level, int optname, const void *optval, socklen_t optlen)
{
return setsockopt(handle, level, optname, optval, optlen);
ioctlsocket(handle, FIONBIO, &flag);
}
+void Socket::Private::set_inherit(bool i)
+{
+}
+
int Socket::Private::set_option(int level, int optname, const void *optval, socklen_t optlen)
{
return setsockopt(handle, level, optname, reinterpret_cast<const char *>(optval), optlen);