Native SDK · C ABI trước
Học CoAkka Runtime qua C/C++
C là public ABI của CoAkka Runtime Core; C++ có thể bọc ABI đó bằng RAII nhưng không tạo một runtime thứ hai. Bài đầu tập trung vào identity, lifecycle và effective facts trước khi mở host lanes, route hay transport.
Beginner · 01 / 03
Cài Core SDK từ kênh chính thức
SDK 3.3 chứa header public, CMake config và đúng năm
native image. Archive chưa có publisher signing, vì vậy checksum
là gate bắt buộc trước khi giải nén.
cmake --version
cc --version
c++ --versioncurl -fL -o coakka-runtime-native-v2-3.3.tar.gz \
'https://raw.githubusercontent.com/phuong-tran/coakka-publish/ccc44e1126408497989a151ba7dbe198c8399c49/coakka-runtime/native/releases/3.3%2B16bdd692faf918d1a959c073ad20318392659f4b/coakka-runtime-native-v2-3.3.tar.gz'
printf '%s %s\n' \
'5d0f27c564ab92f09ef2c3ffb551fb4295f4ed74771fec1cced29bf5e524f504' \
'coakka-runtime-native-v2-3.3.tar.gz' | shasum -a 256 -c -
tar -xzf coakka-runtime-native-v2-3.3.tar.gzVersion law: Core dùng MAJOR.MINOR;
SDK này là Core 3.3, ABI 1, build
16bdd692faf918d1a959c073ad20318392659f4b.
Beginner · 02 / 03
Chạy lifecycle bằng C
Tạo project cạnh thư mục SDK, rồi copy hai file dưới. Cấu hình
được copy khi create; app vẫn phải stop và destroy
đúng một lần trên mọi đường thoát.
cmake_minimum_required(VERSION 3.20)
project(CoAkkaPortalNativeExamples LANGUAGES C CXX)
find_package(CoAkkaRuntimeNativeV2 CONFIG REQUIRED)
add_executable(coakka_runtime_c first_run.c)
target_link_libraries(coakka_runtime_c PRIVATE CoAkkaRuntimeNativeV2::runtime_v2)
target_compile_features(coakka_runtime_c PRIVATE c_std_11)
add_executable(coakka_runtime_cpp first_run.cpp)
target_link_libraries(coakka_runtime_cpp PRIVATE CoAkkaRuntimeNativeV2::runtime_v2)
target_compile_features(coakka_runtime_cpp PRIVATE cxx_std_20)
if(MSVC)
target_compile_options(coakka_runtime_c PRIVATE /W4 /WX)
target_compile_options(coakka_runtime_cpp PRIVATE /W4 /WX)
else()
target_compile_options(coakka_runtime_c PRIVATE -Wall -Wextra -Wpedantic -Werror)
target_compile_options(coakka_runtime_cpp PRIVATE -Wall -Wextra -Wpedantic -Werror)
endif()
#include "coakka/v2/control.h"
#include "coakka/v2/runtime.h"
#include "coakka/v2/transport.h"
#include "coakka/v2/utils.h"
#include <stdio.h>
#include <string.h>
static int require_ok(coakka_v2_status_t status, const char *operation) {
if (status == COAKKA_V2_OK) {
return 0;
}
fprintf(stderr, "%s failed: %s\n", operation, coakka_v2_status_name(status));
return 1;
}
int main(void) {
coakka_v2_runtime_info_t info;
memset(&info, 0, sizeof(info));
info.struct_size = sizeof(info);
if (require_ok(coakka_v2_runtime_get_info(&info), "runtime_get_info")) {
return 1;
}
const coakka_v2_runtime_config_t config = {
.system_name = "portal-native-c",
.node_id = "portal-native-c-1",
.strict_no_drop = 1,
.queue_capacity = 64,
};
coakka_v2_runtime_t *runtime = coakka_v2_runtime_create(&config);
if (runtime == NULL) {
fputs("runtime_create failed\n", stderr);
return 1;
}
coakka_v2_host_handles_t handles;
memset(&handles, 0, sizeof(handles));
handles.struct_size = sizeof(handles);
handles.request_write_fd = -1;
handles.response_read_fd = -1;
handles.deadletter_read_fd = -1;
handles.control_write_fd = -1;
handles.monitor_read_fd = -1;
handles.delivered_request_read_fd = -1;
if (require_ok(coakka_v2_runtime_get_host_handles(runtime, &handles),
"get_host_handles")) {
coakka_v2_runtime_destroy(runtime);
return 1;
}
const coakka_v2_control_snapshot_t snapshot = {
.generation = 1,
.routes = NULL,
.route_count = 0,
};
if (require_ok(coakka_v2_runtime_apply_control_snapshot(runtime, &snapshot),
"apply_snapshot") ||
require_ok(coakka_v2_runtime_start(runtime), "runtime_start")) {
coakka_v2_runtime_destroy(runtime);
coakka_v2_host_handles_close(&handles);
return 1;
}
coakka_v2_runtime_stats_t stats;
memset(&stats, 0, sizeof(stats));
stats.struct_size = sizeof(stats);
const int stats_failed = require_ok(
coakka_v2_runtime_get_stats(runtime, &stats), "runtime_get_stats");
printf("CoAkka Runtime %s ABI=%u state=%d routes=%zu\n", info.runtime_version,
info.abi_version, (int)stats.runtime_state, stats.route_count);
const int stop_failed =
require_ok(coakka_v2_runtime_stop(runtime), "runtime_stop");
coakka_v2_runtime_destroy(runtime);
coakka_v2_host_handles_close(&handles);
return stats_failed || stop_failed;
}
cmake -S . -B build \
-DCMAKE_PREFIX_PATH=../coakka-runtime-native-v2-3.3
cmake --build build --config Release
./build/coakka_runtime_cKết quả mong đợi
CoAkka Runtime 3.3 ABI=1 state=1 routes=0
strict_no_drop- Intent admission nghiêm; Core vẫn là nơi sở hữu policy và kết quả hiệu lực.
queue_capacity- Bound được yêu cầu. Core có thể chuẩn hóa theo safety cap; đọc stats thay vì đoán.
struct_size- Giới hạn byte span mà caller cho phép Core ghi, bảo vệ contract versioned C ABI.
Beginner · 03 / 03
Bọc cùng C ABI bằng C++ RAII
C++ sample chỉ thêm owner tự dọn trên exception. Routing, transport, queue và lifecycle state vẫn do cùng native Core sở hữu.
#include "coakka/v2/control.h"
#include "coakka/v2/runtime.h"
#include "coakka/v2/transport.h"
#include "coakka/v2/utils.h"
#include <iostream>
#include <stdexcept>
#include <string>
namespace {
void require_ok(coakka_v2_status_t status, const char *operation) {
if (status != COAKKA_V2_OK) {
throw std::runtime_error(std::string(operation) +
" failed: " + coakka_v2_status_name(status));
}
}
class Runtime final {
public:
explicit Runtime(const coakka_v2_runtime_config_t &config)
: runtime_(coakka_v2_runtime_create(&config)) {
if (runtime_ == nullptr) {
throw std::runtime_error("runtime_create failed");
}
handles_.struct_size = sizeof(handles_);
handles_.request_write_fd = -1;
handles_.response_read_fd = -1;
handles_.deadletter_read_fd = -1;
handles_.control_write_fd = -1;
handles_.monitor_read_fd = -1;
handles_.delivered_request_read_fd = -1;
const auto status = coakka_v2_runtime_get_host_handles(runtime_, &handles_);
if (status != COAKKA_V2_OK) {
coakka_v2_runtime_destroy(runtime_);
runtime_ = nullptr;
coakka_v2_host_handles_close(&handles_);
require_ok(status, "get_host_handles");
}
}
~Runtime() {
if (runtime_ != nullptr) {
coakka_v2_runtime_destroy(runtime_);
coakka_v2_host_handles_close(&handles_);
}
}
Runtime(const Runtime &) = delete;
Runtime &operator=(const Runtime &) = delete;
coakka_v2_runtime_t *get() const noexcept { return runtime_; }
private:
coakka_v2_runtime_t *runtime_;
coakka_v2_host_handles_t handles_{};
};
} // namespace
int main() {
try {
coakka_v2_runtime_info_t info{};
info.struct_size = sizeof(info);
require_ok(coakka_v2_runtime_get_info(&info), "runtime_get_info");
const coakka_v2_runtime_config_t config = {
.system_name = "portal-native-cpp",
.node_id = "portal-native-cpp-1",
.strict_no_drop = 1,
.queue_capacity = 64,
};
Runtime runtime(config);
const coakka_v2_control_snapshot_t snapshot = {
.generation = 1,
.routes = nullptr,
.route_count = 0,
};
require_ok(
coakka_v2_runtime_apply_control_snapshot(runtime.get(), &snapshot),
"apply_snapshot");
require_ok(coakka_v2_runtime_start(runtime.get()), "runtime_start");
coakka_v2_runtime_stats_t stats{};
stats.struct_size = sizeof(stats);
require_ok(coakka_v2_runtime_get_stats(runtime.get(), &stats),
"runtime_get_stats");
std::cout << "CoAkka Runtime " << info.runtime_version
<< " ABI=" << info.abi_version
<< " state=" << static_cast<int>(stats.runtime_state)
<< " routes=" << stats.route_count << '\n';
require_ok(coakka_v2_runtime_stop(runtime.get()), "runtime_stop");
return 0;
} catch (const std::exception &error) {
std::cerr << error.what() << '\n';
return 1;
}
}
./build/coakka_runtime_cppDestructor là safety net; đường thành công vẫn gọi
stop tường minh để lỗi shutdown không bị nuốt.
Advanced · 04
Mở đúng boundary khi app cần làm việc thật
Public C ABI là foundation của connector. Khi tự viết native app-host, app sở hữu poll loop và đóng từng host handle đúng một lần; Core sở hữu route, terminal matching, transport và bounds. Không copy lại engine hoặc suy đoán effective defaults ở C++.
Nếu mục tiêu là app business handler, hãy dùng connector của host language khi có. Chỉ mở raw host lanes khi app thực sự sở hữu adapter native và lifecycle của nó.