]> git.tdb.fi Git - libs/net.git/commitdiff
Exception changes
authorMikko Rasa <tdb@tdb.fi>
Thu, 4 Aug 2011 22:57:25 +0000 (01:57 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 4 Aug 2011 22:57:25 +0000 (01:57 +0300)
12 files changed:
source/communicator.cpp
source/communicator.h
source/datagramsocket.cpp
source/inet6.cpp
source/protocol.cpp
source/protocol.h
source/resolve.cpp
source/sockaddr.cpp
source/socket.cpp
source/socket.h
source/streamlistensocket.cpp
source/streamsocket.cpp

index 1bb0fde49592f81d9f9c2fa91a22a52431f45e7a..0209f2d93e6783fd2a402dff1bfecad344da481b 100644 (file)
@@ -73,7 +73,7 @@ Communicator::~Communicator()
 void Communicator::initiate_handshake()
 {
        if(handshake_status!=0)
-               throw InvalidState("Handshaking is already underway or done");
+               throw sequence_error("handshaking already done");
 
        send_handshake();
        handshake_status = 1;
index 4bd5409c72587dfc9dbe9b98e43e04b0bbdf5d27..0aa152cd6c803e37410bc641abeb17f0d5748a3f 100644 (file)
@@ -7,6 +7,14 @@
 namespace Msp {
 namespace Net {
 
+class sequence_error: public std::logic_error
+{
+public:
+       sequence_error(const std::string &w): std::logic_error(w) { }
+       virtual ~sequence_error() throw() { }
+};
+
+
 class Communicator
 {
 public:
@@ -34,7 +42,7 @@ public:
        void send(const P &pkt)
        {
                if(handshake_status!=2)
-                       throw InvalidState("Handshaking is not done");
+                       throw sequence_error("handshaking not done");
                unsigned size = protocol.assemble(pkt, out_buf, buf_size);
                socket.write(out_buf, size);
        }
index aa097f13707a6efe71fc191f21fc17b9a7511145..e15a1d143c0281cc9686abd9b4e36a1da97a168c 100644 (file)
@@ -1,4 +1,5 @@
 #include <cerrno>
+#include <msp/core/systemerror.h>
 #include <msp/strings/format.h>
 #include "datagramsocket.h"
 
@@ -23,7 +24,13 @@ int DatagramSocket::connect(const SockAddr &addr)
 
        int err = ::connect(handle, reinterpret_cast<sockaddr *>(&sa), size);
        if(err==-1)
-               throw SystemError("Unable to connect", errno);
+       {
+#ifdef WIN32
+               throw system_error("connect", WSAGetLastError());
+#else
+               throw system_error("connect");
+#endif
+       }
 
        delete peer_addr;
        peer_addr = addr.copy();
@@ -54,7 +61,13 @@ unsigned DatagramSocket::sendto(const char *buf, unsigned size, const SockAddr &
                if(errno==EAGAIN)
                        return 0;
                else
-                       throw SystemError("Sendto failed", errno);
+               {
+#ifdef WIN32
+                       throw system_error("sendto", WSAGetLastError());
+#else
+                       throw system_error("sendto");
+#endif
+               }
        }
 
        return ret;
@@ -76,7 +89,13 @@ unsigned DatagramSocket::recvfrom(char *buf, unsigned size, SockAddr *&addr_)
                if(errno==EAGAIN)
                        return 0;
                else
-                       throw SystemError("Recvfrom failed", errno);
+               {
+#ifdef WIN32
+                       throw system_error("recvfrom", WSAGetLastError());
+#else
+                       throw system_error("recvfrom");
+#endif
+               }
        }
 
        addr_ = SockAddr::create(addr);
index 5073322f60df6cebf9f5d3a9197e4d38da84b5cc..6903a778d2998f56e03d851c7c6ba830346c7a96 100644 (file)
@@ -1,4 +1,3 @@
-#include <msp/core/except.h>
 #include <msp/strings/format.h>
 #include "inet6.h"
 
@@ -42,7 +41,7 @@ string Inet6Addr::str() const
 
 unsigned Inet6Addr::fill_sockaddr(sockaddr &) const
 {
-       throw Exception("Inet6Addr doesn't fit in struct sockaddr");
+       throw logic_error("sizeof(sockaddr_in6)>sizeof(sockaddr)");
 }
 
 unsigned Inet6Addr::fill_sockaddr(sockaddr_storage &sa) const
index 3e7d479b52407f4bca47f3057fdeef7ed507fd96..d56ac77a09f70eff7137da98beef2eb7112d8978 100644 (file)
@@ -8,6 +8,8 @@ using namespace std;
 
 namespace {
 
+using Msp::Net::buffer_error;
+
 template<typename T>
 class Assembler
 {
@@ -32,7 +34,7 @@ char *Assembler<T>::assemble(const T &v, char *data, char *end)
        for(unsigned i=0; i<sizeof(T); ++i)
        {
                if(data==end)
-                       throw Msp::Exception("Out of buffer space");
+                       throw buffer_error("overflow");
                *data++ = *--ptr;
        }
        return data;
@@ -43,7 +45,7 @@ char *Assembler<string>::assemble(const string &v, char *data, char *end)
 {
        data = Assembler<unsigned short>::assemble(v.size(), data, end);
        if(end-data<static_cast<int>(v.size()))
-               throw Msp::Exception("Out of buffer space");
+               throw buffer_error("overflow");
        memcpy(data, v.data(), v.size());
        return data+v.size();
 }
@@ -64,7 +66,7 @@ const char *Assembler<T>::disassemble(T &v, const char *data, const char *end)
        for(unsigned i=0; i<sizeof(T); ++i)
        {
                if(data==end)
-                       throw Msp::Exception("Premature end of data");
+                       throw buffer_error("underflow");
                *--ptr = *data++;
        }
        return data;
@@ -76,7 +78,7 @@ const char *Assembler<string>::disassemble(string &v, const char *data, const ch
        unsigned short size;
        data = Assembler<unsigned short>::disassemble(size, data, end);
        if(end-data<size)
-               throw Msp::Exception("Premature end of data");
+               throw buffer_error("underflow");
        v.assign(data, data+size);
        return data+size;
 }
@@ -138,7 +140,7 @@ unsigned Protocol::disassemble(ReceiverBase &rcv, const char *data, unsigned siz
        unsigned id = (udata[0]<<8)+udata[1];
        unsigned psz = (udata[2]<<8)+udata[3];
        if(psz>size)
-               throw InvalidParameterValue("Not enough data for packet");
+               throw bad_packet("truncated");
        const PacketDefBase &pdef = get_packet_by_id(id);
        const char *ptr = pdef.disassemble(rcv, data+4, data+psz);
        return ptr-data;
index 4bde291ea5c263ce519ea4cac690a6120758fc92..75b3514bb43b2729c915902f7a33374e8b1b608c 100644 (file)
@@ -2,13 +2,29 @@
 #define MSP_NET_PROTOCOL_H_
 
 #include <map>
+#include <stdexcept>
 #include <vector>
-#include <msp/core/except.h>
 #include "receiver.h"
 
 namespace Msp {
 namespace Net {
 
+class bad_packet: public std::runtime_error
+{
+public:
+       bad_packet(const std::string &w): std::runtime_error(w) { }
+       virtual ~bad_packet() throw() { }
+};
+
+
+class buffer_error: public std::runtime_error
+{
+public:
+       buffer_error(const std::string &w): std::runtime_error(w) { }
+       virtual ~buffer_error() throw() { }
+};
+
+
 class Protocol
 {
 private:
@@ -88,7 +104,7 @@ protected:
                {
                        PacketReceiver<P> *prcv = dynamic_cast<PacketReceiver<P> *>(&r);
                        if(!prcv)
-                               throw Exception("Packet type not supported by receiver");
+                               throw bad_packet("unsupported");
                        P pkt;
                        for(typename std::vector<FieldBase<P> *>::const_iterator i=fields.begin(); i!=fields.end(); ++i)
                                d = (*i)->disassemble(pkt, d, e);
index 6575dd2502dea51d07098a35af7e7ffdf93b7347..31b15fb9dd40ece09c09d2e25515bce25c2e00fd 100644 (file)
@@ -4,6 +4,7 @@
 #else
 #include <netdb.h>
 #endif
+#include <msp/core/systemerror.h>
 #include <msp/strings/format.h>
 #include "socket.h"
 #include "resolve.h"
@@ -49,9 +50,9 @@ SockAddr *resolve(const string &s, Family family)
        }
        else
 #ifdef WIN32
-               throw Exception(format("Can't resolve '%s': %d", host, err));
+               throw system_error("getaddrinfo", WSAGetLastError());
 #else
-               throw Exception(format("Can't resolve '%s': %s", host, gai_strerror(err)));
+               throw system_error("getaddrinfo", gai_strerror(err));
 #endif
 }
 
index 08babb12c5f881c84a59471c06b178e4e2c173a1..f6123ca38c685e9b3631593bdd9e89b4de248587 100644 (file)
@@ -1,7 +1,9 @@
-#include <msp/core/except.h>
+#include <stdexcept>
 #include "inet.h"
 #include "inet6.h"
 
+using namespace std;
+
 namespace Msp {
 namespace Net {
 
@@ -14,7 +16,7 @@ SockAddr *SockAddr::create(const sockaddr_storage &sa)
        case AF_INET6:
                return new Inet6Addr(reinterpret_cast<const sockaddr_in6 &>(sa));
        default:
-               throw InvalidParameterValue("Unknown address family");
+               throw invalid_argument("SockAddr::create");
        }
 }
 
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)
        {
index 5536146544c3de098993d1af06295caeecd075be..c2c17f07dc2d2f42611bbb818bbf45c920c6c04f 100644 (file)
 namespace Msp {
 namespace Net {
 
+class bad_socket_state: public std::logic_error
+{
+public:
+       bad_socket_state(const std::string &w): std::logic_error(w) { }
+       virtual ~bad_socket_state() throw() { }
+};
+
+
 class Socket: public IO::EventObject
 {
 protected:
index ccb683a1a31937b18b46db991aad021ea6b8fec8..693f3040dc85c0a81dcc1bd6b8271dea8a91f88a 100644 (file)
@@ -1,9 +1,12 @@
 #include <cerrno>
 #include <msp/core/refptr.h>
+#include <msp/core/systemerror.h>
 #include <msp/strings/format.h>
 #include "streamlistensocket.h"
 #include "streamsocket.h"
 
+using namespace std;
+
 namespace Msp {
 namespace Net {
 
@@ -14,7 +17,8 @@ StreamListenSocket::StreamListenSocket(Family af, int proto):
 
 int StreamListenSocket::connect(const SockAddr &)
 {
-       throw Exception("Can't connect a listen socket");
+       // XXX This function needs to go away
+       throw logic_error("Can't connect a listen socket");
 }
 
 void StreamListenSocket::listen(const SockAddr &addr, unsigned backlog)
@@ -23,7 +27,7 @@ void StreamListenSocket::listen(const SockAddr &addr, unsigned backlog)
 
        int err = ::listen(handle, backlog);
        if(err==-1)
-               throw SystemError("Unable to listen", errno);
+               throw system_error("listen");
 
 #ifdef WIN32
        WSAEventSelect(handle, event, FD_ACCEPT);
@@ -36,7 +40,7 @@ void StreamListenSocket::listen(const SockAddr &addr, unsigned backlog)
 StreamSocket *StreamListenSocket::accept()
 {
        if(!listening)
-               throw InvalidState("Socket is not listening");
+               throw bad_socket_state("not listening");
 
        sockaddr_storage sa;
        socklen_t size = sizeof(sockaddr_storage);
index 2576009452bddcb9df8ee22898c42ce612fcc171..cd810d5253e14e2990d204c4af12096f5be7278b 100644 (file)
@@ -2,6 +2,7 @@
 #include <sys/socket.h>
 #endif
 #include <cerrno>
+#include <msp/core/systemerror.h>
 #include <msp/io/poll.h>
 #include <msp/strings/format.h>
 #include "streamsocket.h"
@@ -28,14 +29,14 @@ 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,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
                }
 
@@ -73,7 +74,7 @@ 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);
@@ -90,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);
@@ -102,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