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 // registerClient and deregisterClient
struct LaminarClient { struct LaminarClient {
virtual void sendMessage(std::string payload) = 0; virtual void sendMessage(std::string payload) = 0;
virtual void close() = 0; virtual void close(bool now = true) = 0;
MonitorScope scope; MonitorScope scope;
}; };

View File

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