Discussion:
[asio-users] Proper close of ASIO TLS connection and reuse afterwards
Sandra Schreiner
2016-02-03 13:22:27 UTC
Permalink
Hello,

I really would like to close the connection between client and server and reopen it afterwards. After I tried out various approaches, it seems i can't get a proper solution without any error message to work.

Beside different other solutions I tried checked out this post http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error and tested the following variations of the accepted answer:
A) PartyA initiates shutdown() and waits for PartyB to respond with a shutdown()
B) PartyA initiates shutdown() but does not wait for PartyB to respond

In case (A), if I use shutdown on client and session side, I get the mentioned EoF error. But without any possibility to fetch the error, because the only available socket.shutdown() does have void as return value and does not take any parameters. Im using asio standalone version 1.10.6.

In case (B) the callback for the write operation is never called. Therefore the program just hangs.

Server accept start
Server accept success
Session start
Server accept start
Client connect success
Session handshake success
Client handshake success
Client initiateDisconnect enter
Session Read failed
Session shutdown

The code is the following (in first run I would like to cancel the connection by client):

Client (io_service is used with asio::work)
---------------------------------------------
void close(const asio::error_code &err){
std::cout << "Client close enter" << "\n";
}
void initiateDisconnect(){
std::cout << "Client initiateDisconnect enter" << "\n";
socket_.async_shutdown(std::bind(&client::close, this, std::placeholders::_1));
std::string dummy = "s";
asio::async_write(socket_,
asio::buffer(dummy, dummy.size()),
std::bind(&client::handle_write, this,
std::placeholders::_1,
std::placeholders::_2));
}
void handle_write(const asio::error_code& error, size_t bytes_transferred){
std::cout << "Client handle_write";
//... check error
}

Session (waits in handle_read)
---------------------

void handleShutdown(){
if(!isDown){
std::cout << " Session shutdown "<< "\n";
socket_.shutdown();
isDown = true;
delete this;
}
}
void handle_read(const asio::error_code& error,
size_t bytes_transferred) {
if (!error) {
std::cout << " Session Read success "<< "\n";
asio::async_write(socket_,
asio::buffer(data_, bytes_transferred),
std::bind(&session::handle_write, this,
std::placeholders::_1));
}
else {
std::cout << " Session Read failed "<< "\n";
handleShutdown();
}
}

How is a proper connection close done?
Many thanks for any help in advance,
Best regards
Sandra
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Sandra Schreiner
2016-02-03 17:05:38 UTC
Permalink
Hello,

I managed to get this working for a one-directional close. The client can close the connection if it calls shutdown, catches the exception and the lowest_layer of the socket is closed in the catch block.

Unfortunately I don't know how I can initiate a shutdown from server side. The client is only reading on the socket if data was send from it in advance. As long as no data is send by the client 'asio::work' ensures that the io_service will not finish. But this also means that the shutdown of the session is pending and not retrieved on client side promptly.

But it is also not possible to start a read operation per default (which should wait for the session shutdown) because the client might also want to send data. Because everything is currently in one thread, the read would block the write. I thought about using two threads - one for reading and one for writing - on the clientside, but I'm not sure if this is the 'best-practice' solution.

Any ideas on that matter?

Many thanks for any hints in advance.
Best regards
Sandra

________________________________________
Von: Sandra Schreiner [***@stud.hs-kl.de]
Gesendet: Mittwoch, 3. Februar 2016 14:22
An: asio-***@lists.sourceforge.net
Betreff: [asio-users] Proper close of ASIO TLS connection and reuse afterwards

Hello,

I really would like to close the connection between client and server and reopen it afterwards. After I tried out various approaches, it seems i can't get a proper solution without any error message to work.

Beside different other solutions I tried checked out this post http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error and tested the following variations of the accepted answer:
A) PartyA initiates shutdown() and waits for PartyB to respond with a shutdown()
B) PartyA initiates shutdown() but does not wait for PartyB to respond

In case (A), if I use shutdown on client and session side, I get the mentioned EoF error. But without any possibility to fetch the error, because the only available socket.shutdown() does have void as return value and does not take any parameters. Im using asio standalone version 1.10.6.

In case (B) the callback for the write operation is never called. Therefore the program just hangs.

Server accept start
Server accept success
Session start
Server accept start
Client connect success
Session handshake success
Client handshake success
Client initiateDisconnect enter
Session Read failed
Session shutdown

The code is the following (in first run I would like to cancel the connection by client):

Client (io_service is used with asio::work)
---------------------------------------------
void close(const asio::error_code &err){
std::cout << "Client close enter" << "\n";
}
void initiateDisconnect(){
std::cout << "Client initiateDisconnect enter" << "\n";
socket_.async_shutdown(std::bind(&client::close, this, std::placeholders::_1));
std::string dummy = "s";
asio::async_write(socket_,
asio::buffer(dummy, dummy.size()),
std::bind(&client::handle_write, this,
std::placeholders::_1,
std::placeholders::_2));
}
void handle_write(const asio::error_code& error, size_t bytes_transferred){
std::cout << "Client handle_write";
//... check error
}

Session (waits in handle_read)
---------------------

void handleShutdown(){
if(!isDown){
std::cout << " Session shutdown "<< "\n";
socket_.shutdown();
isDown = true;
delete this;
}
}
void handle_read(const asio::error_code& error,
size_t bytes_transferred) {
if (!error) {
std::cout << " Session Read success "<< "\n";
asio::async_write(socket_,
asio::buffer(data_, bytes_transferred),
std::bind(&session::handle_write, this,
std::placeholders::_1));
}
else {
std::cout << " Session Read failed "<< "\n";
handleShutdown();
}
}

How is a proper connection close done?
Many thanks for any help in advance,
Best regards
Sandra
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Roger Austin (Australia)
2016-02-03 21:14:04 UTC
Permalink
Hi Sandra,

You can initiate an async_read on the client without blocking the client from initiating subsequent writes. However, if your client is single-threaded and blocked in io_service::Run (due to asio::work) it is hard to see how it could initiate anything.

Cheers,
Roger Austin

-----Original Message-----
From: Sandra Schreiner [mailto:***@stud.hs-kl.de]
Sent: Thursday, 4 February 2016 4:06 AM
To: asio-***@lists.sourceforge.net
Subject: Re: [asio-users] Proper close of ASIO TLS connection and reuse afterwards

Hello,

I managed to get this working for a one-directional close. The client can close the connection if it calls shutdown, catches the exception and the lowest_layer of the socket is closed in the catch block.

Unfortunately I don't know how I can initiate a shutdown from server side. The client is only reading on the socket if data was send from it in advance. As long as no data is send by the client 'asio::work' ensures that the io_service will not finish. But this also means that the shutdown of the session is pending and not retrieved on client side promptly.

But it is also not possible to start a read operation per default (which should wait for the session shutdown) because the client might also want to send data. Because everything is currently in one thread, the read would block the write. I thought about using two threads - one for reading and one for writing - on the clientside, but I'm not sure if this is the 'best-practice' solution.

Any ideas on that matter?

Many thanks for any hints in advance.
Best regards
Sandra

________________________________________
Von: Sandra Schreiner [***@stud.hs-kl.de]
Gesendet: Mittwoch, 3. Februar 2016 14:22
An: asio-***@lists.sourceforge.net
Betreff: [asio-users] Proper close of ASIO TLS connection and reuse afterwards

Hello,

I really would like to close the connection between client and server and reopen it afterwards. After I tried out various approaches, it seems i can't get a proper solution without any error message to work.

Beside different other solutions I tried checked out this post http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error and tested the following variations of the accepted answer:
A) PartyA initiates shutdown() and waits for PartyB to respond with a shutdown()
B) PartyA initiates shutdown() but does not wait for PartyB to respond

In case (A), if I use shutdown on client and session side, I get the mentioned EoF error. But without any possibility to fetch the error, because the only available socket.shutdown() does have void as return value and does not take any parameters. Im using asio standalone version 1.10.6.

In case (B) the callback for the write operation is never called. Therefore the program just hangs.

Server accept start
Server accept success
Session start
Server accept start
Client connect success
Session handshake success
Client handshake success
Client initiateDisconnect enter
Session Read failed
Session shutdown

The code is the following (in first run I would like to cancel the connection by client):

Client (io_service is used with asio::work)
---------------------------------------------
void close(const asio::error_code &err){
std::cout << "Client close enter" << "\n"; } void initiateDisconnect(){
std::cout << "Client initiateDisconnect enter" << "\n";
socket_.async_shutdown(std::bind(&client::close, this, std::placeholders::_1));
std::string dummy = "s";
asio::async_write(socket_,
asio::buffer(dummy, dummy.size()),
std::bind(&client::handle_write, this,
std::placeholders::_1,
std::placeholders::_2)); } void handle_write(const asio::error_code& error, size_t bytes_transferred){
std::cout << "Client handle_write";
//... check error
}

Session (waits in handle_read)
---------------------

void handleShutdown(){
if(!isDown){
std::cout << " Session shutdown "<< "\n";
socket_.shutdown();
isDown = true;
delete this;
}
}
void handle_read(const asio::error_code& error,
size_t bytes_transferred) {
if (!error) {
std::cout << " Session Read success "<< "\n";
asio::async_write(socket_,
asio::buffer(data_, bytes_transferred),
std::bind(&session::handle_write, this,
std::placeholders::_1));
}
else {
std::cout << " Session Read failed "<< "\n";
handleShutdown();
}
}

How is a proper connection close done?
Many thanks for any help in advance,
Best regards
Sandra
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month Monitor end-to-end web transactions and take corrective actions now Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month Monitor end-to-end web transactions and take corrective actions now Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio

Loading...