]> git.tdb.fi Git - libs/net.git/blobdiff - source/net/unix/socket.cpp
Add a dynamic receiver class for more flexible packet handling
[libs/net.git] / source / net / unix / socket.cpp
index 46c80b7a59000d4b36c788b7c566c5cddb570c15..2c1e2ad8d355ac9e0fe9a31b5292f0709535c2f8 100644 (file)
@@ -1,20 +1,23 @@
+#include "platform_api.h"
+#include "socket.h"
 #include <cerrno>
 #include <unistd.h>
 #include <fcntl.h>
-#include "platform_api.h"
 #include <msp/core/systemerror.h>
 #include <msp/io/handle_private.h>
 #include <msp/time/rawtime_private.h>
 #include "sockaddr_private.h"
-#include "socket.h"
 #include "socket_private.h"
 
+using namespace std;
+
 namespace Msp {
 namespace Net {
 
 void Socket::platform_init()
 {
        *priv->event = priv->handle;
+       set_inherit(false);
 }
 
 void Socket::platform_cleanup()
@@ -40,6 +43,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?0:O_CLOEXEC));
+}
+
 int Socket::Private::set_option(int level, int optname, const void *optval, socklen_t optlen)
 {
        return setsockopt(handle, level, optname, optval, optlen);
@@ -51,11 +60,11 @@ int Socket::Private::get_option(int level, int optname, void *optval, socklen_t
 }
 
 
-unsigned check_sys_error(int ret, const char *func)
+size_t check_sys_error(make_signed<size_t>::type ret, const char *func)
 {
        if(ret<0)
        {
-               if(errno==EAGAIN)
+               if(errno==EAGAIN || errno==EWOULDBLOCK)
                        return 0;
                else
                        throw system_error(func);