Skip to content

Commit

Permalink
was/async/Control: pass std::span to Send()
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Nov 6, 2023
1 parent b6648c1 commit a03a6a9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
11 changes: 6 additions & 5 deletions src/was/async/Control.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Control.hxx"
#include "Error.hxx"
#include "system/Error.hxx"
#include "util/SpanCast.hxx"

#include <was/protocol.h>

Expand Down Expand Up @@ -183,23 +184,23 @@ Control::Finish(size_t payload_length) noexcept

bool
Control::Send(enum was_command cmd,
const void *payload, size_t payload_length) noexcept
std::span<const std::byte> payload) noexcept
{
assert(!done);

void *dest = Start(cmd, payload_length);
void *dest = Start(cmd, payload.size());
if (dest == nullptr)
return false;

memcpy(dest, payload, payload_length);
Finish(payload_length);
memcpy(dest, payload.data(), payload.size());
Finish(payload.size());
return true;
}

bool
Control::SendString(enum was_command cmd, std::string_view payload) noexcept
{
return Send(cmd, payload.data(), payload.size());
return Send(cmd, AsBytes(payload));
}

bool
Expand Down
12 changes: 6 additions & 6 deletions src/was/async/Control.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ public:
bool FlushOutput() noexcept;

bool Send(enum was_command cmd,
const void *payload, size_t payload_length) noexcept;

bool SendEmpty(enum was_command cmd) noexcept {
return Send(cmd, nullptr, 0);
}
std::span<const std::byte> payload={}) noexcept;

bool SendString(enum was_command cmd,
std::string_view payload) noexcept;
Expand All @@ -98,8 +94,12 @@ public:
bool SendPair(enum was_command cmd, std::string_view name,
std::string_view value) noexcept;

bool SendT(enum was_command cmd, const auto &payload) noexcept {
return Send(cmd, std::as_bytes(std::span{&payload, 1}));
}

bool SendUint64(enum was_command cmd, uint64_t payload) noexcept {
return Send(cmd, &payload, sizeof(payload));
return SendT(cmd, payload);
}

bool SendArray(enum was_command cmd,
Expand Down
7 changes: 3 additions & 4 deletions src/was/async/SimpleServer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,7 @@ SimpleServer::SendResponse(SimpleResponse &&response) noexcept

request.cancel_ptr = nullptr;

if (!control.Send(WAS_COMMAND_STATUS, &response.status,
sizeof(response.status)))
if (!control.SendT(WAS_COMMAND_STATUS, response.status))
return false;

if (response.body && http_method_is_empty(request.method)) {
Expand All @@ -369,13 +368,13 @@ SimpleServer::SendResponse(SimpleResponse &&response) noexcept
return false;

if (response.body) {
if (!control.SendEmpty(WAS_COMMAND_DATA) ||
if (!control.Send(WAS_COMMAND_DATA) ||
!control.SendUint64(WAS_COMMAND_LENGTH, response.body.size()))
return false;

output.Activate(std::move(response.body));
} else {
if (!control.SendEmpty(WAS_COMMAND_NO_DATA))
if (!control.Send(WAS_COMMAND_NO_DATA))
return false;
}

Expand Down

0 comments on commit a03a6a9

Please sign in to comment.