Skip to content

Commit

Permalink
simple: add [[nodiscard]]
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Nov 29, 2024
1 parent 4ee584f commit e8f1e4a
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/simple.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ struct was_simple {
* @return true on success, false on error (end-of-socket or
* I/O error, including EAGAIN/EWOULDBLOCK)
*/
[[nodiscard]]
bool Fill(bool dontwait);

/**
Expand All @@ -128,6 +129,7 @@ struct was_simple {
* which will be discarded, e.g. if Read() returns nullptr
* with errno=E2BIG.
*/
[[nodiscard]]
enum was_command PeekCommand() const noexcept {
return (enum was_command)input_buffer.header.command;
}
Expand All @@ -136,13 +138,15 @@ struct was_simple {
* @return the current WAS control packet or nullptr if no
* complete packet (header + payload) has been received yet
*/
[[nodiscard]]
const struct was_control_packet *Get();

/**
* @return the next WAS control packet (discarding the current
* control packet, if one was fully received) or nullptr if
* another complete packet has not been received yet
*/
[[nodiscard]]
const struct was_control_packet *Next();

/**
Expand All @@ -153,13 +157,15 @@ struct was_simple {
* error (end-of-socket or I/O error) with errno set; E2BIG
* means the packet payload is too large for the #input_buffer
*/
[[nodiscard]]
const struct was_control_packet *Read(bool dontwait);

/**
* Send the #output_buffer to the control socket.
*
* @return true on success, false on I/O error
*/
[[nodiscard]]
bool Flush();

/**
Expand All @@ -176,20 +182,23 @@ struct was_simple {
*
* @return true on success, false on I/O error
*/
[[nodiscard]]
bool Send(const void *data, size_t length);

/**
* Assemble a WAS control header and send it.
*
* @return true on success, false on I/O error
*/
[[nodiscard]]
bool SendHeader(enum was_command command, size_t length);

/**
* Send a WAS control packet without a payload.
*
* @return true on success, false on I/O error
*/
[[nodiscard]]
bool SendEmpty(enum was_command command) {
return SendHeader(command, 0);
}
Expand All @@ -199,6 +208,7 @@ struct was_simple {
*
* @return true on success, false on I/O error
*/
[[nodiscard]]
bool SendPacket(enum was_command command,
const void *payload, size_t length) {
return SendHeader(command, length) && Send(payload, length);
Expand All @@ -209,6 +219,7 @@ struct was_simple {
*
* @return true on success, false on I/O error
*/
[[nodiscard]]
bool SendUint64(enum was_command command, uint64_t payload) {
return SendPacket(command, &payload, sizeof(payload));
}
Expand Down Expand Up @@ -289,6 +300,7 @@ struct was_simple {
* Clamp the given size to the number of remaining bytes, if
* that is known.
*/
[[nodiscard]]
size_t ClampRemaining(size_t size) const {
if (known_length) {
uint64_t remaining = announced - received;
Expand Down Expand Up @@ -350,6 +362,7 @@ struct was_simple {
/**
* Can this much data be sent?
*/
[[nodiscard]]
bool CanSend(size_t nbytes) const {
return !no_body && (!known_length || sent + nbytes <= announced);
}
Expand Down Expand Up @@ -486,6 +499,7 @@ struct was_simple {
*/
int dev_null = -2;

[[nodiscard]]
was_simple(int control_fd, int input_fd, int output_fd) noexcept
:control(control_fd), input(input_fd), output(output_fd)
{
Expand All @@ -499,6 +513,7 @@ struct was_simple {
request.Deinit();
}

[[nodiscard]]
bool HasRequestBody() const {
assert(response.state != Response::State::NONE);

Expand All @@ -522,65 +537,101 @@ struct was_simple {
/**
* @return true if the connection can be reused
*/
[[nodiscard]]
bool FinishRequest();

/**
* @return true if no more control packets can be sent for the
* current request
*/
[[nodiscard]]
bool IsControlFinished() const {
return response.state == Response::State::END ||
response.state == Response::State::STOP;
}

[[nodiscard]]
bool ApplyRequestPacket(const struct was_control_packet &packet);

[[nodiscard]]
bool ApplyPendingControl();

[[nodiscard]]
bool ReadAndApplyControl();

[[nodiscard]]
const char *Accept(const char *would_block=nullptr);

[[nodiscard]]
enum was_simple_poll_result PollInput(int timeout_ms);

[[nodiscard]]
bool Received(size_t nbytes);

[[nodiscard]]
ssize_t Read(void *buffer, size_t length);

[[nodiscard]]
int64_t GetInputRemaining() const {
if (input.premature)
return -1;

return input.GetRemaining();
}

[[nodiscard]]
bool CloseInput();

[[nodiscard]]
bool SetStatus(http_status_t status);

[[nodiscard]]
bool SetHeader(std::string_view name, std::string_view value) noexcept;

[[nodiscard]]
bool SetLength(uint64_t length);

[[nodiscard]]
enum was_simple_poll_result PollOutput(int timeout_ms);

[[nodiscard]]
bool Write(const void *data, size_t length);

[[nodiscard]]
ssize_t Splice(size_t max_length) noexcept;

[[nodiscard]]
bool SpliceAll(bool end) noexcept;

[[nodiscard]]
bool SpliceTo(int out_fd) noexcept;

[[nodiscard]]
bool SetResponseStateBody();

[[nodiscard]]
bool DiscardAllInput();

[[nodiscard]]
bool CloseDiscardInput() noexcept {
return (input.stopped || (CloseInput() && control.Flush())) &&
DiscardAllInput();
}

[[nodiscard]]
bool WantMetrics() const noexcept {
assert(response.state != Response::State::NONE);

return request.want_metrics;
}

[[nodiscard]]
bool SendMetric(std::string_view name, float value) noexcept;

[[nodiscard]]
bool End();

[[nodiscard]]
bool Abort();
};

Expand Down

0 comments on commit e8f1e4a

Please sign in to comment.