X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fsocket.cpp;h=7bccec6de1303997f07883898ea947b99fd12ba4;hb=bc7f7210f6963c7bd23cce9cea3d3e82a2968f98;hp=7ff77417c88672b3b703430d65b2035b2d047e91;hpb=65b029e8334d4dfc9a6161fc04740b03d56a8e2f;p=libs%2Fnet.git diff --git a/source/socket.cpp b/source/socket.cpp index 7ff7741..7bccec6 100644 --- a/source/socket.cpp +++ b/source/socket.cpp @@ -4,6 +4,7 @@ #include #endif #include +#include #include #include #include @@ -72,7 +73,16 @@ Socket::Socket(Family af, int type, int proto): Socket::~Socket() { - close(); + signal_flush_required.emit(); +#ifdef WIN32 + closesocket(handle); + CloseHandle(event); +#else + ::close(handle); +#endif + + delete local_addr; + delete peer_addr; } void Socket::set_block(bool b) @@ -105,36 +115,12 @@ 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(); } -void Socket::close() -{ - if(handle==MSP_NET_INVALID_SOCKET_HANDLE) - return; - - set_events(IO::P_NONE); - - signal_flush_required.emit(); -#ifdef WIN32 - closesocket(handle); - CloseHandle(event); -#else - ::close(handle); -#endif - handle = MSP_NET_INVALID_SOCKET_HANDLE; - connected = false; - signal_closed.emit(); - - delete local_addr; - local_addr = 0; - delete peer_addr; - peer_addr = 0; -} - void Socket::set_timeout(const Time::TimeDelta &timeout) { #ifndef WIN32 @@ -151,23 +137,21 @@ 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"); 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 +185,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 +204,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) {