]> git.tdb.fi Git - libs/net.git/blobdiff - source/streamsocket.cpp
Hide platform details of Socket behind pimpl
[libs/net.git] / source / streamsocket.cpp
index e5f904ffdcbf4bd26851148b16790ad12a2ab374..c6cd214e18dc08dc1620cd190f27287dd8c6ce11 100644 (file)
@@ -2,19 +2,22 @@
 #include <sys/socket.h>
 #endif
 #include <cerrno>
+#include <msp/core/systemerror.h>
+#include <msp/io/handle_private.h>
 #include <msp/io/poll.h>
-#include <msp/strings/formatter.h>
+#include <msp/strings/format.h>
+#include "socket_private.h"
 #include "streamsocket.h"
 
 namespace Msp {
 namespace Net {
 
-StreamSocket::StreamSocket(SocketHandle h, const SockAddr &paddr):
-       Socket(h, paddr),
+StreamSocket::StreamSocket(const Private &p, const SockAddr &paddr):
+       Socket(p, paddr),
        connecting(false)
 {
 #ifdef WIN32
-       WSAEventSelect(handle, event, FD_READ|FD_CLOSE);
+       WSAEventSelect(priv->handle, *priv->event, FD_READ|FD_CLOSE);
 #endif
        set_events(IO::P_INPUT);
 }
@@ -26,16 +29,15 @@ StreamSocket::StreamSocket(Family af, int proto):
 
 int StreamSocket::poll_connect(const Time::TimeDelta &timeout)
 {
-       check_state(false);
        if(!connecting)
-               throw InvalidState("No connection attempt going on");
+               throw bad_socket_state("not connecting");
 
        int res = poll(*this, IO::P_OUTPUT, timeout);
        if(res==-1)
 #ifdef WIN32
-               throw Exception(format("Connection polling failed: %d", WSAGetLastError()));
+               throw system_error("poll", WSAGetLastError());
 #else
-               throw SystemError("Connection polling failed", errno);
+               throw system_error("poll");
 #endif
        else if(res>0)
        {
@@ -49,14 +51,14 @@ int StreamSocket::poll_connect(const Time::TimeDelta &timeout)
                {
                        set_events(IO::P_NONE);
 #ifdef WIN32
-                       throw Exception(format("Connection failed: %d", err));
+                       throw system_error("connect", WSAGetLastError());
 #else
-                       throw SystemError("Connection failed", err);
+                       throw system_error("connect");
 #endif
                }
 
 #ifdef WIN32
-               WSAEventSelect(handle, event, FD_READ|FD_CLOSE);
+               WSAEventSelect(priv->handle, *priv->event, FD_READ|FD_CLOSE);
 #endif
                set_events(IO::P_INPUT);
 
@@ -70,30 +72,28 @@ int StreamSocket::poll_connect(const Time::TimeDelta &timeout)
 
 int StreamSocket::connect(const SockAddr &addr)
 {
-       check_state(false);
-
        if(connected)
-               throw InvalidState("Socket is already connected");
+               throw bad_socket_state("already connected");
 
        sockaddr_storage sa;
        socklen_t size = addr.fill_sockaddr(sa);
 
 #ifdef WIN32
-       int err = WSAConnect(handle, reinterpret_cast<sockaddr *>(&sa), size, 0, 0, 0, 0);
+       int err = WSAConnect(priv->handle, reinterpret_cast<sockaddr *>(&sa), size, 0, 0, 0, 0);
        if(err==SOCKET_ERROR)
        {
                int err_code = WSAGetLastError();
                if(err_code==WSAEWOULDBLOCK)
                {
                        connecting = true;
-                       WSAEventSelect(handle, event, FD_CONNECT);
+                       WSAEventSelect(priv->handle, *priv->event, FD_CONNECT);
                        set_events(IO::P_OUTPUT);
                }
                else
-                       throw Exception(format("Unable to connect: %d", err_code));
+                       throw system_error("connect", err_code);
        }
 #else
-       int err = ::connect(handle, reinterpret_cast<sockaddr *>(&sa), size);
+       int err = ::connect(priv->handle, reinterpret_cast<sockaddr *>(&sa), size);
        if(err==-1)
        {
                if(errno==EINPROGRESS)
@@ -102,7 +102,7 @@ int StreamSocket::connect(const SockAddr &addr)
                        set_events(IO::P_OUTPUT);
                }
                else
-                       throw SystemError("Unable to connect", errno);
+                       throw system_error("connect");
        }
 #endif
 
@@ -111,7 +111,7 @@ int StreamSocket::connect(const SockAddr &addr)
 
        delete local_addr;
        size = sizeof(sockaddr_storage);
-       getsockname(handle, reinterpret_cast<sockaddr *>(&sa), &size);
+       getsockname(priv->handle, reinterpret_cast<sockaddr *>(&sa), &size);
        local_addr = SockAddr::create(sa);
 
        if(err==0)
@@ -143,7 +143,7 @@ void StreamSocket::on_event(IO::PollEvent ev)
                }
 
 #ifdef WIN32
-               WSAEventSelect(handle, event, FD_READ|FD_CLOSE);
+               WSAEventSelect(priv->handle, *priv->event, FD_READ|FD_CLOSE);
 #endif
                set_events((err==0) ? IO::P_INPUT : IO::P_NONE);
        }