1
0
mirror of https://github.com/ohwgiles/laminar.git synced 2024-10-27 20:34:20 +00:00

better socket closing

This commit is contained in:
Oliver Giles 2015-09-23 08:15:08 +02:00
parent f923762c7e
commit 3ee5b4f8f6
2 changed files with 9 additions and 6 deletions

View File

@ -69,7 +69,7 @@ struct MonitorScope {
// registerClient and deregisterClient
struct LaminarClient {
virtual void sendMessage(std::string payload) = 0;
virtual void close() = 0;
virtual void close(bool now = true) = 0;
MonitorScope scope;
};

View File

@ -189,7 +189,7 @@ public:
// 404
c->set_status(websocketpp::http::status_code::not_found);
}
c->lc->close();
c->lc->close(false);
});
// Handle new websocket connection. Parse the URL to determine
@ -303,10 +303,11 @@ struct Server::WebsocketConnection : public LaminarClient, public std::streambuf
payload.swap(outputBuffer);
writePaf = kj::newPromiseAndFulfiller<void>();
if(payload.empty()) {
stream->shutdownWrite();
return kj::Promise<void>(kj::READY_NOW);
} else {
return stream->write(payload.data(), payload.size()).then([this](){
return closeOnComplete ? kj::Promise<void>(kj::READY_NOW) : writeTask();
return closeOnComplete ? stream->shutdownWrite(), kj::Promise<void>(kj::READY_NOW) : writeTask();
}).attach(kj::mv(payload));
}
});
@ -316,10 +317,12 @@ struct Server::WebsocketConnection : public LaminarClient, public std::streambuf
cn->send(payload, websocketpp::frame::opcode::text);
}
void close() override {
void close(bool now) override {
closeOnComplete = true;
outputBuffer.clear();
writePaf.fulfiller->fulfill();
if(now) {
outputBuffer.clear();
writePaf.fulfiller->fulfill();
}
}
std::streamsize xsputn(const char* s, std::streamsize sz) override {