From: Mikko Rasa Date: Fri, 5 Aug 2011 21:52:45 +0000 (+0300) Subject: Remove the close method from Socket, making it more RAII X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=bc7f7210f6963c7bd23cce9cea3d3e82a2968f98;p=libs%2Fnet.git Remove the close method from Socket, making it more RAII --- diff --git a/source/socket.cpp b/source/socket.cpp index c845471..7bccec6 100644 --- a/source/socket.cpp +++ b/source/socket.cpp @@ -73,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) @@ -112,30 +121,6 @@ void Socket::bind(const SockAddr &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 @@ -165,8 +150,6 @@ const SockAddr &Socket::get_peer_address() const void Socket::check_state(bool conn) const { - if(handle==MSP_NET_INVALID_SOCKET_HANDLE) - throw bad_socket_state("socket is closed"); if(conn && !connected) throw bad_socket_state("not connected"); } diff --git a/source/socket.h b/source/socket.h index c2c17f0..dbe3151 100644 --- a/source/socket.h +++ b/source/socket.h @@ -45,9 +45,6 @@ public: type. */ virtual int connect(const SockAddr &) = 0; - /// Closes the socket. Most operations will throw an exception after this. - void close(); - void set_timeout(const Time::TimeDelta &); const SockAddr &get_local_address() const; const SockAddr &get_peer_address() const;