X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fstreamsocket.cpp;h=c6cd214e18dc08dc1620cd190f27287dd8c6ce11;hb=4b408e693bcb004ccaa6958610e946cfbeff9465;hp=2576009452bddcb9df8ee22898c42ce612fcc171;hpb=729a80158708fd499d942663eb5115d93d247f46;p=libs%2Fnet.git diff --git a/source/streamsocket.cpp b/source/streamsocket.cpp index 2576009..c6cd214 100644 --- a/source/streamsocket.cpp +++ b/source/streamsocket.cpp @@ -2,19 +2,22 @@ #include #endif #include +#include +#include #include #include +#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(&sa), size, 0, 0, 0, 0); + int err = WSAConnect(priv->handle, reinterpret_cast(&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(&sa), size); + int err = ::connect(priv->handle, reinterpret_cast(&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(&sa), &size); + getsockname(priv->handle, reinterpret_cast(&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); }