Files
firesse/stream.cc

51 lines
901 B
C++
Raw Normal View History

2019-05-05 00:11:57 -07:00
#include "stream.h"
#include "index.h"
2019-05-05 00:11:57 -07:00
namespace firesse {
Stream::Stream(firecgi::Request* request, Index* index)
: request_(request),
index_(index) {
index_->Add(this);
}
Stream::~Stream() {
std::lock_guard l(mu_);
index_->Remove(this);
}
void Stream::OnClose(const std::function<void()>& callback) {
on_close_ = callback;
}
2019-05-05 00:11:57 -07:00
bool Stream::WriteEvent(const std::string& data, uint64_t id, const std::string& type) {
2019-05-11 22:27:33 -07:00
{
std::lock_guard l(mu_);
2019-05-12 08:56:57 -07:00
index_->Freshen(this);
2019-05-11 22:27:33 -07:00
}
2019-05-09 23:37:00 -07:00
return request_->InTransaction<bool>([=]() {
if (id) {
request_->WriteBody("id: ", std::to_string(id), "\n");
}
if (!type.empty()) {
request_->WriteBody("event: ", type, "\n");
}
request_->WriteBody("data: ", data, "\n\n");
return request_->Flush();
});
2019-05-05 00:11:57 -07:00
}
bool Stream::End() {
return request_->End();
}
2019-05-11 22:27:33 -07:00
void Stream::Close() {
if (on_close_) {
on_close_();
}
}
2019-05-05 00:11:57 -07:00
} // namespace firesse