How to close the connection properly?

1,487 views
Skip to first unread message

rudresh pandey

unread,
Nov 13, 2013, 6:12:21 AM11/13/13
to webso...@googlegroups.com
Hi,

I am new to websocket++. I am using version  /0.3.0-alpha4  in a GUI application in debug mode.
So far, I am able to connect with uri ( ws://localhost:30000). 
On closing the application, I am closing the connection like..
     
       m_client.stop();
while (!this->m_client.stopped()) 
{
::Sleep(50);
}

if(m_hdl._empty() == false)
{
m_client.close(m_hdl, websocketpp::close::status::normal, "Success");
}

      m_hdl.reset();

Application is closing but there is lots of memory leak being detected. If I am not calling connect method which is as follows, then no memory leaks!  Since no connection made. Nothing to free. So some where prb is in closing the connection. Please Help.

bool WebsocketClient::Connect( std::string &uri)
{
  websocketpp::lib::error_code ec;
 client::connection_ptr con = m_client.get_connection(uri, ec);
if (ec) 
{
return false;
}

// Grab a handle for this connection so we can talk to it in a thread
// safe manor after the event loop starts.
m_hdl = con->get_handle();

// Queue the connection. No DNS queries or network connections will be
// made until the io_service event loop is run.
m_client.connect(con);

// Create a thread to run the ASIO io_service event loop
websocketpp::lib::thread asio_thread(&client::run, &m_client);
}

Peter Thorson

unread,
Nov 13, 2013, 8:22:46 AM11/13/13
to rudresh pandey, webso...@googlegroups.com
Hi Rudresh,

m_client.stop() will forcibly stop the endpoint itself, all outstanding network operations will be cancelled. After calling this, you will not be able to do any clean closes.

This line:
m_client.close(m_hdl, websocketpp::close::status::normal, "Success");

will initiate a clean close for the connection referred to by m_hdl.

By default, a client endpoint will stop automatically when all of its connections have been cleanly closed, there is no need to explicitly call endpoint::stop. If you have enabled perpetual mode using endpoint::start_perpetual(), your client endpoint will stay open indefinitely even after all connections are done (this is used if you intend to make new connections later and don't want to reset/restart the endpoint). If you are using this feature, you will need to call endpoint::stop_perpetual() to put the endpoint back into a mode where it cleanly exits when it is done.

In short: asking all outstanding connections to close cleanly using endpoint::close(hdl,...) should result in the endpoint run method exiting normally once everything is cleaned up.

---------

One other note in regards to your WebsocketClient::Connect method. You are creating a thread on the stack in this method and then immediately leaving the function. This may result in some strange behavior. Either the thread's destructor will attempt to join the thread (in which case Connect will block until all connections are done) or the thread will get destroyed and no additional network communication will happen.

If you intend WebsocketClient::Connect to launch a thread and then return you will need to store the thread somewhere that isn't local to the Connect method. If you intend Connect to block until network operations are done you don't need a thread at all and can simply call m_client.run();

--
You received this message because you are subscribed to the Google Groups "WebSocket++" group.
To unsubscribe from this group and stop receiving emails from it, send an email to websocketpp...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

rudresh pandey

unread,
Nov 13, 2013, 9:35:30 AM11/13/13
to webso...@googlegroups.com, rudresh pandey


Thanks Peter for the response.
My requirement is such that on init of my GUI application I have to create connection and It should keep alive until application get closed. i.e while running the application I will receive messages from ws server after uncertain time period. And I need both ws and wss.
Rightnow, I am working with ws.

If I am calling simply m_client.run(). It block my application but I am receiving data time to time. It stuck where it is after calling the connect method. So I need that handler (on_message etc.) should run in background while one can access the application. While exploring the telemetry_client example, I did so (created thread). In this case, I can access my application while receiving data in background. But there is strange behavior as you mentioned but on closing. I see that I am not getting call in on_close handler. 

Keeping my requirement in mind, Can you suggest how to do so? I want to achieve following in short 

=>Receive data/message in on_message handler in background
=>Can be closed if required.

Please Help!

Thank you
Reply all
Reply to author
Forward
0 new messages