Dispatch returns a result envelope

dpm_execute fills a dpm_result on every call: status and error_code
from the module's return value, and a payload the module hands back
through dpm_set_result. The payload's layout belongs to the module and
is documented per command; its release function is carried in the
envelope and invoked by dpm_result_release. The context keeps a stack
of the envelopes in progress, so a module calling a peer receives the
peer's payload in its own envelope and its caller sees only what the
module sets itself.

The good fixture returns a payload and counts its releases, and the
modules test covers the envelope fields, the payload round trip, single
release, discard on a NULL envelope, and a payload set outside any
dispatch. The prose documents describe the envelope and the payload
header a module ships.
This commit is contained in:
2026-09-07 14:53:24 -04:00
parent 0291e61fd8
commit 55c852586b
13 changed files with 404 additions and 39 deletions

View File

@@ -24,6 +24,23 @@
*/
#include <cstring>
/* The one library call a module makes to return data, declared here
the way the rest of the contract is. */
extern "C" void dpm_set_result(void* ctx, void* data, void (*release)(void*));
namespace {
/* Counts how many times the payload below has been released, so a
test can observe the release path. */
int releases = 0;
void release_payload(void* data) {
(void)data;
releases++;
}
const char* payload = "pong";
} // namespace
extern "C" const char* dpm_module_version(void) {
return "1.2.3";
}
@@ -38,16 +55,23 @@ extern "C" const char* dpm_module_aliases(void) {
return "goodie, gd";
}
/* Answers "ping" with 42 so a dispatch round trip is observable, and
0 for anything else. */
/* Answers "ping" with 42 so a dispatch round trip is observable,
"payload" with the static payload above, "releases" with the number
of times that payload has been released, and 0 for anything else. */
extern "C" int dpm_module_execute(void* ctx, const char* command,
int argc, char** argv) {
(void)ctx;
(void)argc;
(void)argv;
if (command && std::strcmp(command, "ping") == 0) {
return 42;
}
if (command && std::strcmp(command, "payload") == 0) {
dpm_set_result(ctx, const_cast<char*>(payload), release_payload);
return 0;
}
if (command && std::strcmp(command, "releases") == 0) {
return releases;
}
return 0;
}