# TCP サーバー

## 概要

TCP サーバーは、Diarkis の3つのリアルタイム通信サーバのうちの1つで、アプリケーションのためのカスタム・コマンドを実装することができます。

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

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

クライアントにビルトイン・コマンドを公開するには、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},
		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.SetupDiarkisTCPServer("/configs/tcp/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") を参照してください。

## TCP サーバの設定

設定は JSON で記述します。

```json
{
  "connectionTTL": 10,
  "address": "127.0.0.1",
  "nic": "eth0",
  "port": "7200",
  "maxRcvSize": 8000,
  "noDelay": false,
  "enableEncryption": true
}
```

<table><thead><tr><th width="230">キー</th><th width="119">デフォルト</th><th></th></tr></thead><tbody><tr><td>connectionTTL</td><td>10</td><td>接続の TTL。この時間を超えるとクライアントはサーバーから切断されます。</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>maxRcvSize</td><td>8000</td><td>各リクエスト・パケットの最大 TCP パケット・サイズ（バイト）</td></tr><tr><td>noDelay</td><td>false</td><td>true に設定すると、Nagle アルゴリズムを無効にします(書き込み前のバッファリングなし)</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/tcp.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.
