From: Mikko Rasa Date: Tue, 5 Dec 2017 06:40:46 +0000 (+0200) Subject: Implement the required virtual functions in Socket X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=647e0e3df1ddb4208492a8c336bf12741f5146c0;p=libs%2Fnet.git Implement the required virtual functions in Socket --- diff --git a/source/net/socket.cpp b/source/net/socket.cpp index e8e5eaf..85cc987 100644 --- a/source/net/socket.cpp +++ b/source/net/socket.cpp @@ -5,6 +5,8 @@ #include "socket.h" #include "socket_private.h" +using namespace std; + namespace Msp { namespace Net { @@ -29,6 +31,7 @@ Socket::Socket(Family af, int type, int proto): { mode = IO::M_RDWR; + // TODO use SOCK_CLOEXEC on Linux priv->handle = socket(family_to_sys(af), type, proto); platform_init(); @@ -44,13 +47,22 @@ Socket::~Socket() 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; diff --git a/source/net/socket.h b/source/net/socket.h index 1f60f9b..fe5c605 100644 --- a/source/net/socket.h +++ b/source/net/socket.h @@ -42,6 +42,8 @@ public: ~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 diff --git a/source/net/socket_private.h b/source/net/socket_private.h index 742877b..86ce587 100644 --- a/source/net/socket_private.h +++ b/source/net/socket_private.h @@ -22,6 +22,7 @@ struct Socket::Private 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 *); }; diff --git a/source/net/unix/socket.cpp b/source/net/unix/socket.cpp index 46c80b7..a9e9c58 100644 --- a/source/net/unix/socket.cpp +++ b/source/net/unix/socket.cpp @@ -15,6 +15,7 @@ namespace Net { void Socket::platform_init() { *priv->event = priv->handle; + set_inherit(false); } void Socket::platform_cleanup() @@ -40,6 +41,12 @@ void Socket::Private::set_block(bool b) 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); diff --git a/source/net/windows/socket.cpp b/source/net/windows/socket.cpp index f4fab44..a7268e8 100644 --- a/source/net/windows/socket.cpp +++ b/source/net/windows/socket.cpp @@ -70,6 +70,10 @@ void Socket::Private::set_block(bool b) 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(optval), optlen);