# Runtime Library

## 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](https://help.diarkis.io/en/diarkis-client/diarkis-module).\
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](https://help.diarkis.io/en/diarkis-client/diarkis-module/diarkis-nosureddo).

## 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.
