From 4696a776e2875697ea0d6a733609dd9c37918971 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 5 Aug 2011 01:57:25 +0300 Subject: [PATCH] Exception changes --- source/communicator.cpp | 2 +- source/communicator.h | 10 +++++++++- source/datagramsocket.cpp | 25 ++++++++++++++++++++++--- source/inet6.cpp | 3 +-- source/protocol.cpp | 12 +++++++----- source/protocol.h | 20 ++++++++++++++++++-- source/resolve.cpp | 5 +++-- source/sockaddr.cpp | 6 ++++-- source/socket.cpp | 15 ++++++++------- source/socket.h | 8 ++++++++ source/streamlistensocket.cpp | 10 +++++++--- source/streamsocket.cpp | 17 +++++++++-------- 12 files changed, 97 insertions(+), 36 deletions(-) diff --git a/source/communicator.cpp b/source/communicator.cpp index 1bb0fde..0209f2d 100644 --- a/source/communicator.cpp +++ b/source/communicator.cpp @@ -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; diff --git a/source/communicator.h b/source/communicator.h index 4bd5409..0aa152c 100644 --- a/source/communicator.h +++ b/source/communicator.h @@ -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); } diff --git a/source/datagramsocket.cpp b/source/datagramsocket.cpp index aa097f1..e15a1d1 100644 --- a/source/datagramsocket.cpp +++ b/source/datagramsocket.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "datagramsocket.h" @@ -23,7 +24,13 @@ int DatagramSocket::connect(const SockAddr &addr) int err = ::connect(handle, reinterpret_cast(&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); diff --git a/source/inet6.cpp b/source/inet6.cpp index 5073322..6903a77 100644 --- a/source/inet6.cpp +++ b/source/inet6.cpp @@ -1,4 +1,3 @@ -#include #include #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 diff --git a/source/protocol.cpp b/source/protocol.cpp index 3e7d479..d56ac77 100644 --- a/source/protocol.cpp +++ b/source/protocol.cpp @@ -8,6 +8,8 @@ using namespace std; namespace { +using Msp::Net::buffer_error; + template class Assembler { @@ -32,7 +34,7 @@ char *Assembler::assemble(const T &v, char *data, char *end) for(unsigned i=0; i::assemble(const string &v, char *data, char *end) { data = Assembler::assemble(v.size(), data, end); if(end-data(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::disassemble(T &v, const char *data, const char *end) for(unsigned i=0; i::disassemble(string &v, const char *data, const ch unsigned short size; data = Assembler::disassemble(size, data, end); if(end-datasize) - 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; diff --git a/source/protocol.h b/source/protocol.h index 4bde291..75b3514 100644 --- a/source/protocol.h +++ b/source/protocol.h @@ -2,13 +2,29 @@ #define MSP_NET_PROTOCOL_H_ #include +#include #include -#include #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

*prcv = dynamic_cast *>(&r); if(!prcv) - throw Exception("Packet type not supported by receiver"); + throw bad_packet("unsupported"); P pkt; for(typename std::vector *>::const_iterator i=fields.begin(); i!=fields.end(); ++i) d = (*i)->disassemble(pkt, d, e); diff --git a/source/resolve.cpp b/source/resolve.cpp index 6575dd2..31b15fb 100644 --- a/source/resolve.cpp +++ b/source/resolve.cpp @@ -4,6 +4,7 @@ #else #include #endif +#include #include #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 } diff --git a/source/sockaddr.cpp b/source/sockaddr.cpp index 08babb1..f6123ca 100644 --- a/source/sockaddr.cpp +++ b/source/sockaddr.cpp @@ -1,7 +1,9 @@ -#include +#include #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(sa)); default: - throw InvalidParameterValue("Unknown address family"); + throw invalid_argument("SockAddr::create"); } } diff --git a/source/socket.cpp b/source/socket.cpp index 7ff7741..c845471 100644 --- a/source/socket.cpp +++ b/source/socket.cpp @@ -4,6 +4,7 @@ #include #endif #include +#include #include #include #include @@ -105,7 +106,7 @@ void Socket::bind(const SockAddr &addr) int err = ::bind(handle, reinterpret_cast(&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) { diff --git a/source/socket.h b/source/socket.h index 5536146..c2c17f0 100644 --- a/source/socket.h +++ b/source/socket.h @@ -10,6 +10,14 @@ 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: diff --git a/source/streamlistensocket.cpp b/source/streamlistensocket.cpp index ccb683a..693f304 100644 --- a/source/streamlistensocket.cpp +++ b/source/streamlistensocket.cpp @@ -1,9 +1,12 @@ #include #include +#include #include #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); diff --git a/source/streamsocket.cpp b/source/streamsocket.cpp index 2576009..cd810d5 100644 --- a/source/streamsocket.cpp +++ b/source/streamsocket.cpp @@ -2,6 +2,7 @@ #include #endif #include +#include #include #include #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(&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 -- 2.45.2