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

Commit

Permalink
Now keeps the connection alive till send_from_queue is finished (in c…
Browse files Browse the repository at this point in the history
…ase of disconnect)
  • Loading branch information
eidheim committed Dec 15, 2015
1 parent 3f4c9f1 commit e287353
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions server_ws.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ namespace SimpleWeb {

class SendData {
public:
SendData(std::shared_ptr<SendStream> header_stream, std::shared_ptr<SendStream> message_stream,
SendData(std::shared_ptr<Connection> connection, std::shared_ptr<SendStream> header_stream, std::shared_ptr<SendStream> message_stream,
const std::function<void(const boost::system::error_code)> &callback) :
header_stream(header_stream), message_stream(message_stream), callback(callback) {}
connection(connection), header_stream(header_stream), message_stream(message_stream), callback(callback) {}
std::shared_ptr<Connection> connection; //Added to keep the connection object alive
std::shared_ptr<SendStream> header_stream;
std::shared_ptr<SendStream> message_stream;
std::function<void(const boost::system::error_code)> callback;
Expand All @@ -71,10 +72,6 @@ namespace SimpleWeb {

void send_from_queue() {
strand.post([this]() {
if(!socket) {
return;
}

boost::asio::async_write(*socket, send_queue.begin()->header_stream->streambuf,
strand.wrap([this](const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
if(!ec) {
Expand Down Expand Up @@ -258,7 +255,7 @@ namespace SimpleWeb {
header_stream->put(static_cast<unsigned char>(length));

connection->strand.post([this, connection, header_stream, message_stream, callback]() {
connection->send_queue.emplace_back(header_stream, message_stream, callback);
connection->send_queue.emplace_back(connection, header_stream, message_stream, callback);
if(connection->send_queue.size()==1)
connection->send_from_queue();
});
Expand Down

0 comments on commit e287353

Please sign in to comment.