]> git.tdb.fi Git - libs/net.git/blobdiff - source/socket.cpp
Exception changes
[libs/net.git] / source / socket.cpp
index 7ff77417c88672b3b703430d65b2035b2d047e91..c845471a58ed13d20608b4768e13879389370016 100644 (file)
@@ -4,6 +4,7 @@
 #include <sys/socket.h>
 #endif
 #include <iostream>
+#include <msp/core/systemerror.h>
 #include <msp/io/handle_private.h>
 #include <msp/strings/format.h>
 #include <msp/time/rawtime_private.h>
@@ -105,7 +106,7 @@ void Socket::bind(const SockAddr &addr)
 
        int err = ::bind(handle, reinterpret_cast<sockaddr *>(&sa), size);
        if(err==-1)
-               throw SystemError("Unable to bind", errno);
+               throw system_error("bind");
 
        delete local_addr;
        local_addr = addr.copy();
@@ -151,23 +152,23 @@ void Socket::set_timeout(const Time::TimeDelta &timeout)
 const SockAddr &Socket::get_local_address() const
 {
        if(local_addr==0)
-               throw InvalidState("Local address not set");
+               throw bad_socket_state("not bound");
        return *local_addr;
 }
 
 const SockAddr &Socket::get_peer_address() const
 {
        if(peer_addr==0)
-               throw InvalidState("Peer address not set");
+               throw bad_socket_state("not connected");
        return *peer_addr;
 }
 
 void Socket::check_state(bool conn) const
 {
        if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
-               throw Exception("Socket is closed");
+               throw bad_socket_state("socket is closed");
        if(conn && !connected)
-               throw Exception("Socket is not connected");
+               throw bad_socket_state("not connected");
 }
 
 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen)
@@ -201,7 +202,7 @@ unsigned Socket::do_write(const char *buf, unsigned size)
                if(errno==EAGAIN)
                        return 0;
                else
-                       throw SystemError("Writing to socket failed", errno);
+                       throw system_error("send");
        }
 
        return ret;
@@ -220,7 +221,7 @@ unsigned Socket::do_read(char *buf, unsigned size)
                if(errno==EAGAIN)
                        return 0;
                else
-                       throw SystemError("Reading from socket failed", errno);
+                       throw system_error("recv");
        }
        else if(ret==0 && !eof_flag)
        {