Skip to content
This repository was archived by the owner on Jun 12, 2018. It is now read-only.

Commit e287353

Browse files
committed
Now keeps the connection alive till send_from_queue is finished (in case of disconnect)
1 parent 3f4c9f1 commit e287353

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

server_ws.hpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ namespace SimpleWeb {
5454

5555
class SendData {
5656
public:
57-
SendData(std::shared_ptr<SendStream> header_stream, std::shared_ptr<SendStream> message_stream,
57+
SendData(std::shared_ptr<Connection> connection, std::shared_ptr<SendStream> header_stream, std::shared_ptr<SendStream> message_stream,
5858
const std::function<void(const boost::system::error_code)> &callback) :
59-
header_stream(header_stream), message_stream(message_stream), callback(callback) {}
59+
connection(connection), header_stream(header_stream), message_stream(message_stream), callback(callback) {}
60+
std::shared_ptr<Connection> connection; //Added to keep the connection object alive
6061
std::shared_ptr<SendStream> header_stream;
6162
std::shared_ptr<SendStream> message_stream;
6263
std::function<void(const boost::system::error_code)> callback;
@@ -71,10 +72,6 @@ namespace SimpleWeb {
7172

7273
void send_from_queue() {
7374
strand.post([this]() {
74-
if(!socket) {
75-
return;
76-
}
77-
7875
boost::asio::async_write(*socket, send_queue.begin()->header_stream->streambuf,
7976
strand.wrap([this](const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
8077
if(!ec) {
@@ -258,7 +255,7 @@ namespace SimpleWeb {
258255
header_stream->put(static_cast<unsigned char>(length));
259256

260257
connection->strand.post([this, connection, header_stream, message_stream, callback]() {
261-
connection->send_queue.emplace_back(header_stream, message_stream, callback);
258+
connection->send_queue.emplace_back(connection, header_stream, message_stream, callback);
262259
if(connection->send_queue.size()==1)
263260
connection->send_from_queue();
264261
});

0 commit comments

Comments
 (0)