]> git.tdb.fi Git - libs/net.git/blobdiff - source/streamsocket.cpp
Don't close socket on Communicator error
[libs/net.git] / source / streamsocket.cpp
index c5e19685f6f2cd615f6c34aa8532b045f7c691b2..cd810d5253e14e2990d204c4af12096f5be7278b 100644 (file)
@@ -2,16 +2,14 @@
 #include <sys/socket.h>
 #endif
 #include <cerrno>
+#include <msp/core/systemerror.h>
 #include <msp/io/poll.h>
-#include <msp/strings/formatter.h>
+#include <msp/strings/format.h>
 #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,23 @@ 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 +50,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 +69,12 @@ 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 +91,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<sockaddr *>(&sa), size);
@@ -122,7 +103,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 +127,6 @@ int StreamSocket::connect(const SockAddr &addr)
 
 void StreamSocket::on_event(IO::PollEvent ev)
 {
-       //cout<<"StreamSocket::on_event "<<ev<<'\n';
        if((ev&(IO::P_OUTPUT|IO::P_ERROR)) && connecting)
        {
                int err;