nbd-proxy closing fixes

This commit fixes:
  - handling of virtual media unmount method
  - cancels unix socket async accept upon early websocket closing
    (reproduction with rapid start/stop button pressing or closing
     websocket just after negotation msg from NBD server)

Tested:
  - unmount method via WebUI
  - unix socket accept cancellation - modified NBD server to close
    websocket after sending negotation message & rapid start/stop button
    pressing

Signed-off-by: Iwona Winiarska <iwona.winiarska@intel.com>
Signed-off-by: Jan Sowinski <jan.sowinski@intel.com>
Change-Id: Ibcbb87a7e35cfbee8c8b4686f64c9090c66f0c17
diff --git a/include/nbd_proxy.hpp b/include/nbd_proxy.hpp
index 64578f2..6922ae2 100644
--- a/include/nbd_proxy.hpp
+++ b/include/nbd_proxy.hpp
@@ -52,16 +52,6 @@
     {
         BMCWEB_LOG_DEBUG << "NbdProxyServer destructor";
         close();
-        connection.close();
-
-        if (peerSocket)
-        {
-            BMCWEB_LOG_DEBUG << "peerSocket->close()";
-            peerSocket->close();
-            peerSocket.reset();
-            BMCWEB_LOG_DEBUG << "std::remove(" << socketId << ")";
-            std::remove(socketId.c_str());
-        }
     }
 
     std::string getEndpointId() const
@@ -76,7 +66,8 @@
                                              stream_protocol::socket socket) {
                 if (ec)
                 {
-                    BMCWEB_LOG_ERROR << "Cannot accept new connection: " << ec;
+                    BMCWEB_LOG_ERROR << "UNIX socket: async_accept error = "
+                                     << ec.message();
                     return;
                 }
                 if (peerSocket)
@@ -101,8 +92,8 @@
                                const bool status) {
             if (ec)
             {
-                BMCWEB_LOG_ERROR << "DBus error: " << ec
-                                 << ", cannot call mount method";
+                BMCWEB_LOG_ERROR << "DBus error: cannot call mount method = "
+                                 << ec.message();
                 return;
             }
         };
@@ -122,6 +113,15 @@
 
     void close()
     {
+        acceptor.close();
+        if (peerSocket)
+        {
+            BMCWEB_LOG_DEBUG << "peerSocket->close()";
+            peerSocket->close();
+            peerSocket.reset();
+            BMCWEB_LOG_DEBUG << "std::remove(" << socketId << ")";
+            std::remove(socketId.c_str());
+        }
         // The reference to session should exists until unmount is
         // called
         auto unmountHandler = [](const boost::system::error_code ec) {
@@ -156,7 +156,7 @@
                 if (ec)
                 {
                     BMCWEB_LOG_ERROR << "UNIX socket: async_read_some error = "
-                                     << ec;
+                                     << ec.message();
                     // UNIX socket has been closed by peer, best we can do is to
                     // break all connections
                     close();
@@ -208,11 +208,15 @@
                 uxWriteInProgress = false;
                 if (ec)
                 {
-                    BMCWEB_LOG_ERROR << "UNIX: async_write error = " << ec;
+                    BMCWEB_LOG_ERROR << "UNIX: async_write error = "
+                                     << ec.message();
                     return;
                 }
                 // Retrigger doWrite if there is something in buffer
-                doWrite();
+                if (ws2uxBuf.size() > 0)
+                {
+                    doWrite();
+                }
             });
     }
 
@@ -269,7 +273,7 @@
 
                 if (ec)
                 {
-                    BMCWEB_LOG_ERROR << "DBus error: " << ec;
+                    BMCWEB_LOG_ERROR << "DBus error: " << ec.message();
                     return;
                 }
 
@@ -356,8 +360,8 @@
                     BMCWEB_LOG_DEBUG << "No session to close";
                     return;
                 }
-                // Remove reference to session in global map
                 session->second->close();
+                // Remove reference to session in global map
                 sessions.erase(session);
             })
         .onmessage([](crow::websocket::Connection& conn,