MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / runtime_worker_handle_application

Function runtime_worker_handle_application

src/daemon/runtime.c:1292–1344  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1290}
1291
1292static bool runtime_worker_handle_application(cbm_daemon_runtime_worker_t *worker,
1293 const uint8_t *payload, uint32_t length) {
1294 if (!payload || length < APPLICATION_REQUEST_PREFIX_SIZE) {
1295 return false;
1296 }
1297 cbm_daemon_runtime_application_token_t request_token = runtime_get_u64(payload);
1298 uint32_t request_length = runtime_get_u32(payload + 8);
1299 if (request_length > CBM_DAEMON_RUNTIME_APPLICATION_PAYLOAD_MAX ||
1300 length != APPLICATION_REQUEST_PREFIX_SIZE + request_length ||
1301 request_token == CBM_DAEMON_RUNTIME_APPLICATION_TOKEN_INVALID ||
1302 request_token <= worker->last_application_request_token) {
1303 return false;
1304 }
1305 /* A structurally valid token is consumed at admission, including BUSY,
1306 * UNAVAILABLE, and local allocation/thread failures. Reusing a token after
1307 * any terminal response would make late cancellation controls ambiguous. */
1308 worker->last_application_request_token = request_token;
1309 cbm_daemon_runtime_service_t *service = worker->service;
1310 if (!service->application.request) {
1311 return runtime_worker_send_application_response(
1312 worker, request_token, CBM_DAEMON_RUNTIME_APPLICATION_UNAVAILABLE, NULL, 0, false);
1313 }
1314 (void)runtime_worker_reap_application(worker, false);
1315 if (worker->application_thread_started) {
1316 return runtime_worker_send_application_response(
1317 worker, request_token, CBM_DAEMON_RUNTIME_APPLICATION_BUSY, NULL, 0, false);
1318 }
1319
1320 uint8_t *request_copy = NULL;
1321 if (request_length > 0) {
1322 request_copy = malloc(request_length);
1323 if (!request_copy) {
1324 return runtime_worker_send_application_response(
1325 worker, request_token, CBM_DAEMON_RUNTIME_APPLICATION_HANDLER_ERROR, NULL, 0,
1326 false);
1327 }
1328 memcpy(request_copy, payload + APPLICATION_REQUEST_PREFIX_SIZE, request_length);
1329 }
1330 worker->application_request_token = request_token;
1331 worker->application_request = request_copy;
1332 worker->application_request_length = request_length;
1333 atomic_store_explicit(&worker->application_thread_done, false, memory_order_release);
1334 if (cbm_thread_create(&worker->application_thread, RUNTIME_WORKER_STACK_SIZE,
1335 runtime_application_worker, worker) != 0) {
1336 free(worker->application_request);
1337 worker->application_request = NULL;
1338 worker->application_request_length = 0;
1339 return runtime_worker_send_application_response(
1340 worker, request_token, CBM_DAEMON_RUNTIME_APPLICATION_HANDLER_ERROR, NULL, 0, false);
1341 }
1342 worker->application_thread_started = true;
1343 return true;
1344}
1345
1346static bool runtime_worker_handle_application_cancel(cbm_daemon_runtime_worker_t *worker,
1347 const uint8_t *payload, uint32_t length) {

Callers 1

Calls 5

runtime_get_u64Function · 0.85
runtime_get_u32Function · 0.85
cbm_thread_createFunction · 0.85

Tested by

no test coverage detected