> For the complete documentation index, see [llms.txt](https://help.diarkis.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.diarkis.io/en/diarkis-client/runtime-library.md).

# Runtime Library

Diarkis Client Runtime Library provides the core runtime functionality for real-time communication, event handling, and data synchronization between client applications and the Diarkis server cluster.

## Overview

The **Diarkis Runtime Library** includes core low-level features fundamental to the runtime.

## Main Features

* Basic Functions
  * Diarkis TCP/UDP/RUDP communication
  * Thread management
  * Memory management and custom allocator
  * NAT type determination
* Functions of each module

### Diarkis TCP/UDP/RUDP Communication

The **Diarkis Runtime Library** supports communication using TCP/UDP/RUDP.

### Thread Management

The **Diarkis Runtime Library** provides `Diarkis::DiarkisThread` to execute thread operations cross-platform.\
By using `Diarkis::DiarkisThread`, you can manipulate threads with the same interface across all supported platforms.

### Memory Management and Custom Allocator

In the **Diarkis Runtime Library**, you can replace the internal memory allocation/deallocation process by setting a custom allocator.\
Implement your custom allocator by inheriting `Diarkis::ICustomAllocator`, and set it in the runtime using `Diarkis::SetCustomAllocator`.\
An implementation sample code can be found in `samples/room_broadcast/main.cpp`.

### NAT Type Determination

The **Diarkis Runtime Library** provides functionality for NAT type determination in collaboration with the Diarkis server.

### Functions of Each Module

For the functionalities of each module, it is recommended to use the [Diarkis Module](/en/diarkis-client/diarkis-module.md).\
If you wish to use the modules' functions directly from the runtime library, please refer to the [C++ API documentation](https://docs.diarkis.io/docs/cpp/current/annotated.html).

## Resources Used by the Diarkis Runtime Library

The Diarkis Runtime Library allocates and uses the following resources internally:

* Sockets
  * A socket is created for each UDP server connection/TCP server connection.
  * UDP
    * A socket is created within the runtime library when you call DiarkisUdp::Connect(Async) / DiarkisUdp::ConnectDualMode(Async).
    * P2P uses the socket created with UDP.
  * TCP
    * A socket is created within the runtime library when you call DiarkisTcp::Connect / DiarkisTcp::ConnectDualMode.
  * If there are multiple Diarkis servers to connect to, you need to create multiple instances of DiarkisInterfaceBase, which increases the number of sockets accordingly.
* Threads
  * One thread is created for TCP connections, and two threads for UDP connections.
  * When using the Diarkis client, threads are also created on the Diarkis Module side. For details, please refer to [Diarkis Threads](/en/diarkis-client/diarkis-module/diarkis-nosureddo.md).

## Caution

* The APIs of the **Diarkis Module** are **not** thread-safe. If using the **Diarkis Module** in multiple threads, implement exclusive control on the application side.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.diarkis.io/en/diarkis-client/runtime-library.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
