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

Function runtime_worker_handle_application

src/daemon/runtime.c:1331–1383  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1329}
1330
1331static bool runtime_worker_handle_application(cbm_daemon_runtime_worker_t *worker,
1332 const uint8_t *payload, uint32_t length) {
1333 if (!payload || length < APPLICATION_REQUEST_PREFIX_SIZE) {
1334 return false;
1335 }
1336 cbm_daemon_runtime_application_token_t request_token = runtime_get_u64(payload);
1337 uint32_t request_length = runtime_get_u32(payload + 8);
1338 if (request_length > CBM_DAEMON_RUNTIME_APPLICATION_PAYLOAD_MAX ||
1339 length != APPLICATION_REQUEST_PREFIX_SIZE + request_length ||
1340 request_token == CBM_DAEMON_RUNTIME_APPLICATION_TOKEN_INVALID ||
1341 request_token <= worker->last_application_request_token) {
1342 return false;
1343 }
1344 /* A structurally valid token is consumed at admission, including BUSY,
1345 * UNAVAILABLE, and local allocation/thread failures. Reusing a token after
1346 * any terminal response would make late cancellation controls ambiguous. */
1347 worker->last_application_request_token = request_token;
1348 cbm_daemon_runtime_service_t *service = worker->service;
1349 if (!service->application.request) {
1350 return runtime_worker_send_application_response(
1351 worker, request_token, CBM_DAEMON_RUNTIME_APPLICATION_UNAVAILABLE, NULL, 0, false);
1352 }
1353 (void)runtime_worker_reap_application(worker, false);
1354 if (worker->application_thread_started) {
1355 return runtime_worker_send_application_response(
1356 worker, request_token, CBM_DAEMON_RUNTIME_APPLICATION_BUSY, NULL, 0, false);
1357 }
1358
1359 uint8_t *request_copy = NULL;
1360 if (request_length > 0) {
1361 request_copy = malloc(request_length);
1362 if (!request_copy) {
1363 return runtime_worker_send_application_response(
1364 worker, request_token, CBM_DAEMON_RUNTIME_APPLICATION_HANDLER_ERROR, NULL, 0,
1365 false);
1366 }
1367 memcpy(request_copy, payload + APPLICATION_REQUEST_PREFIX_SIZE, request_length);
1368 }
1369 worker->application_request_token = request_token;
1370 worker->application_request = request_copy;
1371 worker->application_request_length = request_length;
1372 atomic_store_explicit(&worker->application_thread_done, false, memory_order_release);
1373 if (cbm_thread_create(&worker->application_thread, RUNTIME_WORKER_STACK_SIZE,
1374 runtime_application_worker, worker) != 0) {
1375 free(worker->application_request);
1376 worker->application_request = NULL;
1377 worker->application_request_length = 0;
1378 return runtime_worker_send_application_response(
1379 worker, request_token, CBM_DAEMON_RUNTIME_APPLICATION_HANDLER_ERROR, NULL, 0, false);
1380 }
1381 worker->application_thread_started = true;
1382 return true;
1383}
1384
1385static bool runtime_worker_handle_application_cancel(cbm_daemon_runtime_worker_t *worker,
1386 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