Files
core/sdk/c/dever_ffi.c
T
2026-11-27 06:03:56 +00:00

120 lines
6.3 KiB
C

/* 同步 C ABI;取消由 Dever 进程监管设定宽限,SDK 不新增线程调度。 */
#include "wire.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int text(yyjson_val *value, const char *expected) {
return yyjson_is_str(value) && yyjson_get_len(value) == strlen(expected) && memcmp(yyjson_get_str(value), expected, strlen(expected)) == 0;
}
static int identifier(yyjson_val *message, uint64_t *id) {
yyjson_val *value = yyjson_obj_get(message, "id");
if (!yyjson_is_uint(value) || yyjson_get_uint(value) > INT64_MAX) return 0;
*id = yyjson_get_uint(value);
return 1;
}
static int declared(yyjson_val *operations, yyjson_val *operation) {
size_t i, length; yyjson_val *item;
yyjson_arr_foreach(operations, i, length, item) if (yyjson_equals(item, operation)) return 1;
return 0;
}
static int handshake(const dever_ffi_handler *handler, yyjson_val *contract, int *opened) {
yyjson_doc *doc = dever_receive();
if (doc == NULL) return 0;
yyjson_val *hello = yyjson_doc_get_root(doc);
int okay = yyjson_obj_size(hello) == 8 && text(yyjson_obj_get(hello, "kind"), "hello") && text(yyjson_obj_get(hello, "version"), "dever-component-1");
const char *identity[] = {"port", "schema", "adapter", "operations", "capabilities"};
for (size_t i = 0; okay && i < 5; ++i) okay = yyjson_equals(yyjson_obj_get(hello, identity[i]), yyjson_obj_get(contract, identity[i]));
yyjson_val *setting = yyjson_obj_get(hello, "setting");
if (okay) okay = setting != NULL;
if (okay && handler->open != NULL) {
size_t size; char *data = yyjson_val_write(setting, 0, &size);
okay = data != NULL && handler->open(handler->context, (dever_ffi_slice){data, size}) == DEVER_FFI_OK;
free(data);
}
if (okay) {
*opened = 1;
yyjson_mut_doc *ready = yyjson_doc_mut_copy(doc, NULL);
if (ready == NULL) okay = 0;
else {
yyjson_mut_val *root = yyjson_mut_doc_get_root(ready);
yyjson_mut_obj_remove_key(root, "setting");
yyjson_mut_obj_remove_key(root, "kind");
okay = yyjson_mut_obj_add_str(ready, root, "kind", "ready") && dever_send(ready);
yyjson_mut_doc_free(ready);
}
}
yyjson_doc_free(doc);
return okay;
}
static int invoke(const dever_ffi_handler *handler, yyjson_val *contract, yyjson_val *message, uint64_t id, char *buffer) {
yyjson_val *operation = yyjson_obj_get(message, "operation");
yyjson_val *payload = yyjson_obj_get(message, "payload");
if (payload == NULL || !yyjson_is_str(operation) || !declared(yyjson_obj_get(contract, "operations"), operation)) return 0;
size_t size; char *data = yyjson_val_write(payload, 0, &size);
if (data == NULL) return 0;
dever_ffi_output returned = {buffer, DEVER_MAX_BYTES - 256, 0};
int status = handler->invoke(handler->context, (dever_ffi_slice){yyjson_get_str(operation), yyjson_get_len(operation)}, (dever_ffi_slice){data, size}, &returned);
free(data);
if (returned.data != buffer || returned.capacity != DEVER_MAX_BYTES - 256 || returned.size > returned.capacity || (status != DEVER_FFI_OK && status != DEVER_FFI_ERROR)) return 0;
yyjson_doc *result = dever_parse(buffer, (size_t)returned.size, status == DEVER_FFI_OK ? 3 : 2, status == DEVER_FFI_OK ? 1 : 0);
if (result == NULL) return 0;
yyjson_val *value = yyjson_doc_get_root(result);
yyjson_val *error = NULL;
int okay = 1;
if (status == DEVER_FFI_ERROR) {
error = yyjson_obj_get(value, "error");
payload = yyjson_obj_get(value, "payload");
okay = yyjson_obj_size(value) == 2 && yyjson_is_str(error) && payload != NULL && yyjson_obj_getn(yyjson_obj_get(contract, "errors"), yyjson_get_str(error), yyjson_get_len(error)) != NULL;
} else payload = value;
yyjson_mut_doc *reply = yyjson_mut_doc_new(NULL);
if (reply == NULL) okay = 0;
if (okay) {
yyjson_mut_val *root = yyjson_mut_obj(reply);
yyjson_mut_doc_set_root(reply, root);
okay = root != NULL && yyjson_mut_obj_add_str(reply,root,"kind",error ? "error" : "result") && yyjson_mut_obj_add_uint(reply,root,"id",id) && yyjson_mut_obj_add_val(reply,root,"payload",yyjson_val_mut_copy(reply,payload));
if (okay && error) okay = yyjson_mut_obj_add_val(reply,root,"error",yyjson_val_mut_copy(reply,error));
// callback 解析已经包含外层帧头预算,此处直接编码,不重扫载荷。
if (okay) okay = dever_send(reply);
}
yyjson_mut_doc_free(reply); yyjson_doc_free(result);
return okay;
}
int dever_ffi_serve(const dever_ffi_handler *handler, dever_ffi_slice manifest) {
if (handler == NULL || handler->abi != DEVER_FFI_ABI_V1 || handler->invoke == NULL) return 1;
yyjson_doc *contract = dever_parse(manifest.data, (size_t)manifest.size, 0, 0);
if (contract == NULL) return 1;
int opened = 0;
int okay = handshake(handler, yyjson_doc_get_root(contract), &opened);
char *buffer = okay ? malloc(DEVER_MAX_BYTES - 256) : NULL;
okay = okay && buffer != NULL;
uint64_t next = 1;
int cancelled = 0, stopped = 0;
while (okay && !stopped) {
yyjson_doc *doc = dever_receive();
if (doc == NULL) { okay = 0; break; }
yyjson_val *message = yyjson_doc_get_root(doc), *kind = yyjson_obj_get(message,"kind");
uint64_t id;
if (text(kind,"shutdown")) {
okay = yyjson_obj_size(message) == 1 && dever_send_value(message);
stopped = 1;
} else if (text(kind,"health")) {
okay = yyjson_obj_size(message) == 2 && identifier(message,&id) && id == 0 && dever_send_value(message);
} else if (text(kind,"cancel")) {
okay = yyjson_obj_size(message) == 2 && identifier(message,&id) && id > 0 && id == next-1 && !cancelled;
cancelled = 1;
} else if (text(kind,"call")) {
okay = yyjson_obj_size(message) == 4 && identifier(message,&id) && id == next && invoke(handler,yyjson_doc_get_root(contract),message,id,buffer);
++next; cancelled = 0;
} else okay = 0;
yyjson_doc_free(doc);
}
if (opened && handler->close != NULL) handler->close(handler->context);
free(buffer); yyjson_doc_free(contract);
if (!okay) fputs("native FFI Worker protocol or callback failed\n",stderr);
return okay && stopped ? 0 : 1;
}