From bc7f7210f6963c7bd23cce9cea3d3e82a2968f98 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 6 Aug 2011 00:52:45 +0300 Subject: [PATCH] Remove the close method from Socket, making it more RAII --- source/socket.cpp | 37 ++++++++++--------------------------- source/socket.h | 3 --- 2 files changed, 10 insertions(+), 30 deletions(-) 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; -- 2.43.0