X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fstreamsocket.cpp;h=3fa6b9a13daafda29f3cd47f8ee1ef7d01cc3953;hb=e6f09239fae7ff675e1068365a06803dcc8f8d1f;hp=e5f904ffdcbf4bd26851148b16790ad12a2ab374;hpb=4feef9d7a2d96668660686d3812973e6f788359a;p=libs%2Fnet.git diff --git a/source/streamsocket.cpp b/source/streamsocket.cpp index e5f904f..3fa6b9a 100644 --- a/source/streamsocket.cpp +++ b/source/streamsocket.cpp @@ -2,40 +2,41 @@ #include #endif #include +#include +#include #include -#include +#include +#include "sockaddr_private.h" +#include "socket_private.h" #include "streamsocket.h" namespace Msp { namespace Net { -StreamSocket::StreamSocket(SocketHandle h, const SockAddr &paddr): - Socket(h, paddr), - connecting(false) +StreamSocket::StreamSocket(const Private &p, const SockAddr &paddr): + ClientSocket(p, paddr) { #ifdef WIN32 - WSAEventSelect(handle, event, FD_READ|FD_CLOSE); + WSAEventSelect(priv->handle, *priv->event, FD_READ|FD_CLOSE); #endif set_events(IO::P_INPUT); } StreamSocket::StreamSocket(Family af, int proto): - Socket(af, SOCK_STREAM, proto), - connecting(false) + ClientSocket(af, SOCK_STREAM, proto) { } -int StreamSocket::poll_connect(const Time::TimeDelta &timeout) +bool 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,51 +50,46 @@ 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); connected = true; - - return 0; } - return 1; + return connected; } -int StreamSocket::connect(const SockAddr &addr) +bool 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); + SockAddr::SysAddr sa = addr.to_sys(); #ifdef WIN32 - int err = WSAConnect(handle, reinterpret_cast(&sa), size, 0, 0, 0, 0); + int err = WSAConnect(priv->handle, reinterpret_cast(&sa.addr), 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.addr), sa.size); if(err==-1) { if(errno==EINPROGRESS) @@ -102,7 +98,7 @@ int StreamSocket::connect(const SockAddr &addr) set_events(IO::P_OUTPUT); } else - throw SystemError("Unable to connect", errno); + throw system_error("connect"); } #endif @@ -110,9 +106,9 @@ int StreamSocket::connect(const SockAddr &addr) peer_addr = addr.copy(); delete local_addr; - size = sizeof(sockaddr_storage); - getsockname(handle, reinterpret_cast(&sa), &size); - local_addr = SockAddr::create(sa); + SockAddr::SysAddr lsa; + getsockname(priv->handle, reinterpret_cast(&lsa.addr), &lsa.size); + local_addr = SockAddr::from_sys(lsa); if(err==0) { @@ -121,7 +117,7 @@ int StreamSocket::connect(const SockAddr &addr) signal_connect_finished.emit(0); } - return (err==0)?0:1; + return connected; } void StreamSocket::on_event(IO::PollEvent ev) @@ -143,7 +139,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); }