]> git.tdb.fi Git - libs/net.git/blobdiff - source/net/unix/socket.cpp
Move most platform-specific code into overlay directories
[libs/net.git] / source / net / unix / socket.cpp
diff --git a/source/net/unix/socket.cpp b/source/net/unix/socket.cpp
new file mode 100644 (file)
index 0000000..02ef7c3
--- /dev/null
@@ -0,0 +1,81 @@
+#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"
+
+namespace Msp {
+namespace Net {
+
+void Socket::platform_init()
+{
+       *priv->event = priv->handle;
+}
+
+void Socket::platform_cleanup()
+{
+       close(priv->handle);
+}
+
+void Socket::set_timeout(const Time::TimeDelta &timeout)
+{
+       timeval tv = Time::rawtime_to_timeval(timeout.raw());
+       priv->set_option(SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(timeval));
+       priv->set_option(SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(timeval));
+}
+
+void Socket::set_platform_events(unsigned)
+{
+}
+
+
+void Socket::Private::set_block(bool b)
+{
+       int flags = fcntl(handle, F_GETFL);
+       fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
+}
+
+int Socket::Private::set_option(int level, int optname, const void *optval, socklen_t optlen)
+{
+       return setsockopt(handle, level, optname, optval, optlen);
+}
+
+int Socket::Private::get_option(int level, int optname, void *optval, socklen_t *optlen)
+{
+       return getsockopt(handle, level, optname, optval, optlen);
+}
+
+
+unsigned check_sys_error(int ret, const char *func)
+{
+       if(ret<0)
+       {
+               if(errno==EAGAIN)
+                       return 0;
+               else
+                       throw system_error(func);
+       }
+
+       return ret;
+}
+
+bool check_sys_connect_error(int ret)
+{
+       if(ret<0)
+       {
+               if(errno==EINPROGRESS)
+                       return false;
+               else
+                       throw system_error("connect");
+       }
+
+       return true;
+}
+
+} // namespace Net
+} // namespace Msp