]> git.tdb.fi Git - libs/net.git/commitdiff
Remove the close method from Socket, making it more RAII
authorMikko Rasa <tdb@tdb.fi>
Fri, 5 Aug 2011 21:52:45 +0000 (00:52 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 5 Aug 2011 21:52:45 +0000 (00:52 +0300)
source/socket.cpp
source/socket.h

index c845471a58ed13d20608b4768e13879389370016..7bccec6de1303997f07883898ea947b99fd12ba4 100644 (file)
@@ -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");
 }
index c2c17f07dc2d2f42611bbb818bbf45c920c6c04f..dbe31512a6f677eeed424d3a12748a50d8fc87eb 100644 (file)
@@ -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;