# ランタイム・ライブラリ

## 概要

**Diarkis ランタイム・ライブラリ** はランタイムのコアとなる低レベルな機能が含まれています。

## 主な機能

* 基盤機能
  * Diarkis TCP/UDP/RUDP 通信
  * スレッド管理
  * メモリ管理とカスタム ・アロケーター
  * NAT タイプ判定
  * [Packet Manipulator](/diarkis-client/runtime-library/packet-manipulator.md)
* 各モジュールの機能

### Diarkis TCP/UDP/RUDP 通信

**Diarkis ランタイム・ライブラリ** では TCP/UDP/RUDP による通信をサポートしています。

### スレッド管理

**Diarkis ランタイム・ライブラリ** ではスレッドの操作をクロス・プラットフォームで実行する `Diarkis::DiarkisThread` を提供しています。\
`Diarkis::DiarkisThread` を使用することにより対応プラットフォームすべてで同じインターフェイスを使用してスレッドを操作することが可能となります。

### メモリ管理とカスタム・アロケーター

**Diarkis ランタイム・ライブラリ**ではカスタム・アロケーターを設定することにより、**Diarkis ランタイム・ライブラリ**内部のメモリの確保/開放処理をユーザーが置き換えることができます。\
`Diarkis::ICustomAllocator` を継承してユーザー独自のアロケーターを実装し、`Diarkis::SetCustomAllocator` を使用してランタイムに設定してください。\
`samples/room_broadcast/main.cpp` に実装サンプル・コードがあります。

### NAT タイプ判定

**Diarkis ランタイム・ライブラリ** では Diarkis サーバーと連携した NAT タイプ判定の機能を提供しています。

### 各モジュールの機能

各モジュールの機能については [Diarkis Module](/diarkis-client/diarkis-module.md) を使用することをおすすめします。\
ランタイム・ライブラリから直接各モジュールの機能を使用する場合は [C++ API ドキュメント](https://docs.diarkis.io/docs/cpp/current/annotated.html) を参照してください。

## Diarkis ランタイム・ライブラリが使用するリソース

Diarkis ランタイム・ライブラリでは以下のリソースを内部で確保して使用します。

* ソケット
  * ソケットは、UDP サーバ接続時 / TCP サーバ接続時に１つずつ作成されます。
  * UDP
    * DiarkisUdp::Connect(Async) / DiarkisUdp::ConnectDualMode(Async) を呼び出した際にランタイム・ライブラリで作成されます。
    * P2P は、UDP で作成した ソケット を使用されます。
  * TCP
    * DiarkisTcp::Connect / DiarkisTcp::ConnectDualMode を呼び出した際にランタイム・ライブラリで作成されます。
  * 接続する Diarkis サーバが複数ある場合は、DiarkisInterfaceBase のインスタンスを複数作成する必要がありますので、インスタンス分だけ Socket 数が増えます。
* スレッド
  * TCP 接続時は 1 スレッド、UDP 接続時は 2 スレッド作成します。
  * Diarkis クライアントを使用する際には、Diarkis Module 側でもスレッドを作成しています。詳しくは [Diarkis のスレッド](/diarkis-client/diarkis-module/diarkis-nosureddo.md) をご確認ください。

## 注意点

* **Diarkis Module** の API はスレッド・セーフでは**ありません**。複数スレッドで **Diarkis Module** を利用する場合はアプリケーション側で排他制御を行ってください。


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

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

The question should be specific, self-contained, and written in natural language.
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.
