# UDP サーバー

## 概要

UDP サーバーは、Diarkis の3つのリアルタイム・コミュニケーション・サーバーのうちの1つです。

クライアントにあらかじめ用意されたモジュールのビルトイン・コマンドの公開や、カスタム・コマンドを実装して公開することができます。

コマンドとは、クライアントから送信され、サーバーで処理されるフォーマットされたパケットのことです。コマンドを利用し、Diarkis のサーバー・クラスターがクライアントと対話します。

## UDP サーバーのセットアップ

クライアントにビルトイン・コマンドを公開するには、diarkisexec パッケージを利用してセットアップできます。

* `diarkisexec.SetupDiarkis()` でモジュールを指定してビルトイン・コマンドを公開できます。各モジュールの `ConfigPath` を指定することで、設定をカスタマイズできます。
* `diarkisexec.SetServerCommandHandler()` でカスタム・コマンドを公開できます。
* `diarkisexec.SetupDiarkisUDPServer()` で UDP サーバーのセットアップができます。引数の JSON ファイルでサーバーの設定をカスタマイズできます。

> :warning: 上記の関数は、 `diarkisexec.StartDiarkis()` を呼ぶ前に実行する必要があります。

詳細は [diarkisexec の API リファレンス](https://docs.diarkis.io/docs/server/current/diarkis/diarkisexec/index.html)を参照して下さい。

```go
package main

import (
	"github.com/Diarkis/diarkis/diarkisexec"
	"github.com/Diarkis/diarkis/server"
	"github.com/Diarkis/diarkis/user"
)

var ver uint8 = 10
var cmd uint16 = 100

func main() {
	logConfigPath := "/configs/shared/log.json"
	meshConfigPath := "/configs/shared/mesh.json"

	diarkisexec.SetupDiarkis(logConfigPath, meshConfigPath, &diarkisexec.Modules{
		Room:       &diarkisexec.Options{ExposeCommands: true},
		P2P:        &diarkisexec.Options{ExposeCommands: true},
		Group:      &diarkisexec.Options{ConfigPath: "/configs/shared/group.json", ExposeCommands: true},
		Dive:       &diarkisexec.Options{ConfigPath: "/configs/shared/dive.json", ExposeCommands: true},
		Field:      &diarkisexec.Options{ConfigPath: "/configs/shared/field.json", ExposeCommands: true},
		DM:         &diarkisexec.Options{ConfigPath: "/configs/shared/dm.json", ExposeCommands: true},
		MatchMaker: &diarkisexec.Options{ConfigPath: "/configs/shared/matching.json", ExposeCommands: true},
		Session:    &diarkisexec.Options{ConfigPath: "/configs/shared/session.json", ExposeCommands: true},
	})

	diarkisexec.SetupDiarkisUDPServer("/configs/udp/main.json")

	diarkisexec.SetServerCommandHandler(ver, cmd, helloWorld)

	diarkisexec.StartDiarkis()
}

func helloWorld(ver uint8, cmd uint16, payload []byte, userData *user.User, next func(error)) {
	userData.ServerRespond([]byte("Hello World"), ver, cmd, server.Ok, true)
	next(nil)
}
```

## Mesh 設定

meshConfigPath には JSON ファイルのパスを指定します。詳細は [#mesh-she-ding](#mesh-she-ding "mention") を参照してください。

## UDP サーバーの設定

設定は JSON で記述します。

```json
{
  "enableP2P": true,
  "address": "127.0.0.1",
  "nic": "eth0",
  "port": "7000",
  "connectionTTL": 10,
  "sendUDPInterval": 0,
  "handleRUDPInterval": 100,
  "rcvWorkers": 1,
  "retryInterval": 1000,
  "maxRetry": 10,
  "enableEncryption": true
}
```

<table><thead><tr><th width="201">キー</th><th width="119">デフォルト</th><th></th></tr></thead><tbody><tr><td>enableP2P</td><td>true</td><td>true に設定すると、クライアントはP2P用に自分のパブリック・アドレスを取得することができます。</td></tr><tr><td>address</td><td>"127.0.0.1"</td><td>バインドする UDP サーバーのアドレス</td></tr><tr><td>nic</td><td>"eth0"</td><td>アドレスを取得するインターフェース名。アドレスが未指定の場合に利用します。</td></tr><tr><td>port</td><td>"7100"</td><td>UDP サーバーがバインドするためのポート。UDP サーバーは、指定されたポートから始まる利用可能なポートを自動的に探します。</td></tr><tr><td>connectionTTL</td><td>10</td><td>接続の TTL。この時間を超えるとクライアントはサーバーから切断されます。</td></tr><tr><td>sendUDPInterval</td><td>0</td><td>UDP パケットの送信間隔（ミリ秒）。10より小さく設定すると、送信パケットはバッファリングされません。</td></tr><tr><td>handleRUDPInterval</td><td>100<br>min: 10</td><td>RUDP パケットの送受信間隔（ミリ秒）。 値が小さいほど、サーバーはより敏感に（高速に）応答するようになりますが、その代償として CPU 負荷が増加します。</td></tr><tr><td>retryInterval</td><td>1000</td><td>RUDP パケットの再試行間隔（ミリ秒）</td></tr><tr><td>maxRetry</td><td>10</td><td>RUDP パケットの最大再試行回数。この値を超えた場合、RUDP 接続はタイムアウトとみなされ破棄されます。</td></tr><tr><td>rcvWorkers</td><td>CPUコア数</td><td>UDP パケットを受信する goroutine の数</td></tr><tr><td>enableEncryption</td><td>true</td><td>falseに設定すると、パケットの暗号化と復号化が無効になります。HTTP サーバーも同様の設定をする必要があります。</td></tr></tbody></table>

> :warning: クラウド環境では、address はプライベート IP アドレスとし、環境変数 `DIARKIS_CLOUD_ENV` を併用してください。

詳細は [server の API リファレンス](https://docs.diarkis.io/docs/server/current/diarkis/server/index.html)を参照して下さい。


---

# 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-server/udp.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.
