title: Howto reconnect to a server which does not answer to close()
body: <p>I am using the <code>C++ REST SDK</code> ("Casablanca") to receive feed from websocket servers. </p>
  
  <p>Sometimes, I need to close the connection and reconnect. The library does not have a <code>reconnect</code> function, so I close the old connection and simply open a new one. The problem is that the server on the other side does, as it seems, not answer to my close message.</p>
  
  <p>This is the code:</p>
  
  <pre><code>web::websockets::client::websocket_callback_client*  m_clClient;
  
  ///...
  
  void Connection::reconnect()
  {
      std::cout &lt;&lt; "Debug 1" &lt;&lt; std::endl;
      this-&gt;m_clClient.close().get();
      delete this-&gt;m_clClient;
      this-&gt;m_clClient = new web::websockets::client::websocket_callback_client();
      std::cout &lt;&lt; "Debug2" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>As I said, it seems like the server does not answer to <code>close()</code>, so the program gets stuck at <code>this-&gt;m_clClient.close().get();</code> forever.</p>
  
  <p>I also tried deleting the session without sending a <code>close()</code>, like so:</p>
  
  <pre><code>web::websockets::client::websocket_callback_client*  m_clClient;
  
  ///...
  
  void Connection::reconnect()
  {
      std::cout &lt;&lt; "Debug 1" &lt;&lt; std::endl;
      delete this-&gt;m_clClient;
      this-&gt;m_clClient = new web::websockets::client::websocket_callback_client();
      std::cout &lt;&lt; "Debug2" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>But the program still gets stuck after "Debug 1". I think the <code>close()</code> is called in the destructor of <code>websocket_callback_client</code> if it hasn't been done before. </p>
  
  <p>I have searched the source code of the library and found the destructor of <code>websocket_callback_client</code> here: <a href="https://github.com/Microsoft/cpprestsdk/blob/master/Release/src/websockets/client/ws_client_wspp.cpp" rel="nofollow">link</a> :</p>
  
  <pre><code>    ~wspp_callback_client()
      {
          _ASSERTE(m_state &lt; DESTROYED);
          std::unique_lock&lt;std::mutex&gt; lock(m_wspp_client_lock);
  
          // Now, what states could we be in?
          switch (m_state) {
          case DESTROYED:
              // This should be impossible
              std::abort();
          case CREATED:
              lock.unlock();
              break;
          case CLOSED:
          case CONNECTING:
          case CONNECTED:
          case CLOSING:
              // Unlock the mutex so connect/close can use it.
              lock.unlock();
              try
              {
                  // This will do nothing in the already-connected case
                  pplx::task&lt;void&gt;(m_connect_tce).get();
              }
              catch (...) {}
              try
              {
                  // This will do nothing in the already-closing case
                  close().wait();
              }
              catch (...) {}
              break;
          }
  
          // At this point, there should be no more references to me.
          m_state = DESTROYED;
  }
  </code></pre>
  
  <p>As you can see under <code>case CLOSING:</code> it waits for the connection to close.</p>
  
  <p>The only possibility I can see at this point is to call <code>close()</code> without waiting for the answer and then overwriting the pointer with a new client, leaving the old one undeleted. This would be a very unclean solution.</p>
  
  <p>What can I do to close the session and open a new one?</p>