-
Notifications
You must be signed in to change notification settings - Fork 13
Add an outbound binary send primitive to all transports #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2d30ccf
Add an outbound binary send primitive to all transports
chrisuthe d1140fe
Merge branch 'rev-fix/01' into rev-fix/02-binary-send
chrisuthe 97ee82a
Merge branch 'source-role/01-protocol' into source-role/02-binary-send
chrisuthe beb673a
Merge branch 'source-role/01-protocol' into source-role/02-binary-send
chrisuthe 914e365
Harden the ESP binary send slot and the exactly-once wire tests
chrisuthe ad70ef0
Scope binary-send work reclamation to its owning server
chrisuthe e44822a
Guard binary-send reclamation on a clean httpd_stop
chrisuthe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // Copyright 2026 Sendspin Contributors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| /// @file binary_send_registry.h | ||
| /// @brief Owner-scoped keep-alive registry for work queued into a queue with no cancellation | ||
| /// hook (ESP httpd_queue_work); platform-free so its transitions are unit-tested on host | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <algorithm> | ||
| #include <memory> | ||
| #include <mutex> | ||
| #include <vector> | ||
|
|
||
| namespace sendspin { | ||
|
|
||
| /// @brief Keeps queued work blocks alive until their worker claims them or their owner's queue | ||
| /// is reclaimed | ||
| /// | ||
| /// A Block must carry a `std::shared_ptr<Block> self` member. engage() parks the block's | ||
| /// self-reference (keeping it alive independently of everything else) and records it under an | ||
| /// opaque owner; claim() atomically takes the self-reference back for the worker that is about | ||
| /// to run; reclaim() breaks the self-references of ONE owner's still-engaged blocks. Scoping | ||
| /// discipline is the safety contract: reclaim an owner only once its queue can no longer run | ||
| /// workers (after httpd_stop for that handle), so a claim() against a reclaimed block never | ||
| /// happens with a dangling pointer, and other owners' queued work is never touched. | ||
| template <typename Block> | ||
| class BinarySendRegistry { | ||
| public: | ||
| /// @brief Parks the block's self-reference and records it under the owner | ||
| void engage(const std::shared_ptr<Block>& block, const void* owner) { | ||
| std::lock_guard<std::mutex> lock(this->mutex_); | ||
| block->self = block; | ||
| this->entries_.push_back(Entry{owner, block}); | ||
| } | ||
|
|
||
| /// @brief Takes the self-reference back for the worker about to run | ||
| /// @return The keep-alive reference, or empty if the block is no longer engaged (already | ||
| /// claimed, or its owner was reclaimed). | ||
| std::shared_ptr<Block> claim(Block* raw) { | ||
| std::lock_guard<std::mutex> lock(this->mutex_); | ||
| auto it = std::find_if(this->entries_.begin(), this->entries_.end(), | ||
| [&](const Entry& e) { return e.block.get() == raw; }); | ||
| if (it == this->entries_.end()) { | ||
| return {}; | ||
| } | ||
| std::shared_ptr<Block> keep = std::move(it->block->self); | ||
| this->entries_.erase(it); | ||
| return keep; | ||
| } | ||
|
|
||
| /// @brief Breaks the self-references of every block engaged under the owner | ||
| /// @return Number of blocks reclaimed. | ||
| size_t reclaim(const void* owner) { | ||
| std::lock_guard<std::mutex> lock(this->mutex_); | ||
| size_t count = 0; | ||
| for (auto it = this->entries_.begin(); it != this->entries_.end();) { | ||
| if (it->owner == owner) { | ||
| it->block->self.reset(); | ||
| it = this->entries_.erase(it); | ||
| ++count; | ||
| } else { | ||
| ++it; | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| private: | ||
| struct Entry { | ||
| const void* owner; | ||
| std::shared_ptr<Block> block; | ||
| }; | ||
|
|
||
| std::mutex mutex_; | ||
| std::vector<Entry> entries_; | ||
| }; | ||
|
|
||
| } // namespace sendspin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.