Commit 4b86d247 authored by octal's avatar octal

Response::send(): added an overload for char arrays to avoid string creation and memory allocation

parent 68083860
......@@ -420,7 +420,7 @@ ResponseStream::ends() {
}
Async::Promise<ssize_t>
Response::putOnWire(const std::string& body)
Response::putOnWire(const char* data, size_t len)
{
try {
std::ostream os(&buf_);
......@@ -445,10 +445,13 @@ Response::putOnWire(const std::string& body)
OUT(os << crlf);
}
OUT(writeHeader<Header::ContentLength>(os, body.size()));
OUT(writeHeader<Header::ContentLength>(os, len));
OUT(os << crlf);
os << body;
if (len > 0) {
OUT(os.write(data, len));
}
auto buffer = buf_.buffer();
......
......@@ -287,7 +287,7 @@ public:
Async::Promise<ssize_t> send(Code code) {
code_ = code;
return putOnWire("");
return putOnWire(nullptr, 0);
}
Async::Promise<ssize_t> send(
Code code,
......@@ -304,7 +304,27 @@ public:
headers_.add(std::make_shared<Header::ContentType>(mime));
}
return putOnWire(body);
return putOnWire(body.c_str(), body.size());
}
template<size_t N>
Async::Promise<ssize_t> send(
Code code,
const char (&arr)[N],
const Mime::MediaType& mime = Mime::MediaType())
{
/* @Refactor: code duplication */
code_ = code;
if (mime.isValid()) {
auto contentType = headers_.tryGet<Header::ContentType>();
if (contentType)
contentType->setMime(mime);
else
headers_.add(std::make_shared<Header::ContentType>(mime));
}
return putOnWire(arr, N - 1);
}
ResponseStream stream(Code code, size_t streamSize = DefaultStreamSize) {
......@@ -336,7 +356,7 @@ private:
peer_ = peer;
}
Async::Promise<ssize_t> putOnWire(const std::string& body);
Async::Promise<ssize_t> putOnWire(const char* data, size_t len);
std::weak_ptr<Tcp::Peer> peer_;
DynamicStreamBuf buf_;
......
......@@ -76,7 +76,7 @@ IoWorker::pin(const CpuSet& set) {
}
void
IoWorker::armTimer(
IoWorker::armTimerMs(
std::chrono::milliseconds value,
Async::Resolver resolve, Async::Rejection reject)
{
......
......@@ -39,10 +39,10 @@ public:
void pin(const CpuSet& set);
void shutdown();
template<typename Duration>
template<typename Duration>
void armTimer(Duration timeout, Async::Resolver resolve, Async::Rejection reject) {
armTimer(std::chrono::duration_cast<std::chrono::milliseconds>(timeout),
armTimerMs(std::chrono::duration_cast<std::chrono::milliseconds>(timeout),
std::move(resolve),
std::move(reject));
}
......@@ -67,7 +67,7 @@ private:
};
void
armTimer(std::chrono::milliseconds value, Async::Resolver, Async::Rejection reject);
armTimerMs(std::chrono::milliseconds value, Async::Resolver, Async::Rejection reject);
struct Timer {
Timer(std::chrono::milliseconds value,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment