Branch data Line data Source code
1 : : /*
2 : : * Copyright 2025 Jinwoo Sung
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at
7 : : *
8 : : * http://www.apache.org/licenses/LICENSE-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : #pragma once
18 : : #include <cstddef>
19 : : #include <optional>
20 : :
21 : : #include "wirestead/base/constants.hpp"
22 : : #include "wirestead/wrapper/send_result.hpp"
23 : : namespace wirestead::wrapper::detail {
24 : : // Stage-1 size validation only: success means the payload passed this check,
25 : : // not that a transport accepted it. Line callers include their delimiter.
26 : 544942 : constexpr SendResult validate_payload_size(std::size_t size,
27 : : std::optional<std::size_t> queue_limit = std::nullopt) noexcept {
28 [ + + ]: 544942 : if (size == 0) return SendResult::reject(SendRejection::InvalidArgument);
29 [ + + + + : 544639 : if (size > base::constants::MAX_BUFFER_SIZE || (queue_limit && size > *queue_limit)) {
+ + + + ]
30 : 491 : return SendResult::reject(SendRejection::TooLarge);
31 : : }
32 : 544148 : return SendResult::accept();
33 : : }
34 : :
35 : : // Invalid sizes reach the transport without a capacity wait. It still owns
36 : : // final rejection, callbacks and accounting until public send APIs migrate.
37 : 608 : constexpr bool payload_needs_capacity(std::size_t size,
38 : : std::optional<std::size_t> queue_limit = std::nullopt) noexcept {
39 : 608 : return validate_payload_size(size, queue_limit).accepted();
40 : : }
41 : : } // namespace wirestead::wrapper::detail
|