Native SDK · C ABI first

Learn CoAkka Runtime with C/C++

C is the public ABI of CoAkka Runtime Core. C++ may wrap that ABI with RAII, but it does not create a second runtime. Start with identity, lifecycle and effective facts before opening host lanes, routes or transport.

Beginner · 01 / 03

Install Core SDK from the official channel

SDK 3.3 carries public headers, its CMake config and exactly five native images. Publisher signing is absent, so the checksum is a required gate before extraction.

Terminal · toolchain
cmake --version
cc --version
c++ --version
Terminal · download and verify
curl -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.gz

Version law: Core uses MAJOR.MINOR. This is Core 3.3, ABI 1, build 16bdd692faf918d1a959c073ad20318392659f4b.

Beginner · 02 / 03

Run lifecycle in C

Create a project next to the SDK and copy these files. Core copies creation strings; the app still stops and destroys its owner exactly once on every exit path.

CMakeLists.txt
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()
first_run.c
#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;
}
Terminal
cmake -S . -B build \
  -DCMAKE_PREFIX_PATH=../coakka-runtime-native-v2-3.3
cmake --build build --config Release
./build/coakka_runtime_c

Expected result

CoAkka Runtime 3.3 ABI=1 state=1 routes=0
strict_no_drop
Strict admission intent; Core still owns the policy and effective result.
queue_capacity
A requested bound. Read Core facts instead of reconstructing a default.
struct_size
The byte span the caller permits Core to write in the versioned C ABI.

Beginner · 03 / 03

Wrap the same C ABI with C++ RAII

The C++ sample only adds an exception-safe owner. Routing, transport, queueing and lifecycle state remain Core-owned.

first_run.cpp
#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;
  }
}
Terminal
./build/coakka_runtime_cpp

The destructor is a safety net; the success path still calls stop explicitly so shutdown failure is not hidden.

Advanced · 04

Open the right boundary for real work

The public C ABI is the connector foundation. A native app-host owns its poll loop and closes each host handle exactly once. Core owns routing, terminal matching, transport and bounds. Do not duplicate those engines or infer defaults in C++.

Use the host-language connector for application business handlers when one exists. Open raw host lanes only when the application truly owns a native adapter and its lifecycle.