X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fstreamsocket.cpp;h=59590846f16d0661917b4134b4b6aaa9ef919abb;hb=e2f4514a4530e0b5c4eb270bab970ab9391b5085;hp=c5e19685f6f2cd615f6c34aa8532b045f7c691b2;hpb=a81c41acd873cda7f40bca634782230d9e57dc4f;p=libs%2Fnet.git diff --git a/source/streamsocket.cpp b/source/streamsocket.cpp index c5e1968..5959084 100644 --- a/source/streamsocket.cpp +++ b/source/streamsocket.cpp @@ -2,16 +2,14 @@ #include #endif #include +#include #include -#include +#include #include "streamsocket.h" namespace Msp { namespace Net { -/** -Used by StreamListenSocket to construct a new socket from accept. -*/ StreamSocket::StreamSocket(SocketHandle h, const SockAddr &paddr): Socket(h, paddr), connecting(false) @@ -22,32 +20,22 @@ StreamSocket::StreamSocket(SocketHandle h, const SockAddr &paddr): set_events(IO::P_INPUT); } -/** -Constructs a new StreamSocket. -*/ StreamSocket::StreamSocket(Family af, int proto): Socket(af, SOCK_STREAM, proto), connecting(false) { } -/** -Checks the status of an ongoing connection attempt. If the connection fails -with an error, an exception is thrown. - -@return 0 if the connection finished, 1 if not -*/ 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) { @@ -61,9 +49,9 @@ 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 } @@ -80,20 +68,10 @@ int StreamSocket::poll_connect(const Time::TimeDelta &timeout) return 1; } -/** -Connects the socket to a remote address. In non-blocking mode, this function -may return before the connection is finished. The caller must then use either -the poll_connect function or an EventDispatcher to determine when the -connection is finished. - -@return 0 if the connection finished, 1 if it is in progress -*/ 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); @@ -110,7 +88,7 @@ int StreamSocket::connect(const SockAddr &addr) 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); @@ -122,7 +100,7 @@ int StreamSocket::connect(const SockAddr &addr) set_events(IO::P_OUTPUT); } else - throw SystemError("Unable to connect", errno); + throw system_error("connect"); } #endif @@ -146,7 +124,6 @@ int StreamSocket::connect(const SockAddr &addr) void StreamSocket::on_event(IO::PollEvent ev) { - //cout<<"StreamSocket::on_event "<