# UDP Server

## Overview

The UDP server is one of the three real-time communication servers of Diarkis.

It allows for the exposure of built-in commands from pre-prepared modules to clients, or for the implementation and exposure of custom commands.

Commands are formatted packets sent from the client and processed by the server. By using commands, the Diarkis server cluster interacts with clients.

## Setting up the UDP Server

To expose built-in commands to clients, you can set them up using the diarkisexec package.

* You can expose built-in commands by specifying modules with `diarkisexec.SetupDiarkis()`. By specifying the `ConfigPath` for each module, you can customize the settings.
* Custom commands can be exposed with `diarkisexec.SetServerCommandHandler()`.
* The setup of the UDP server can be done with `diarkisexec.SetupDiarkisUDPServer()`. The server settings can be customized using a JSON file as an argument.

> :warning: These functions need to be executed before calling `diarkisexec.StartDiarkis()`.

For more details, refer to the [diarkisexec API reference](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 Configuration

Specify the path to a JSON file for `meshConfigPath`. For more details, refer to [#mesh-she-ding](#mesh-she-ding "mention").

## UDP Server Configuration

Configure settings in JSON format.

```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">Key</th><th width="119">Default</th><th>Description</th></tr></thead><tbody><tr><td>enableP2P</td><td>true</td><td>When set to true, clients can obtain their public address for P2P use.</td></tr><tr><td>address</td><td>"127.0.0.1"</td><td>Address for the UDP server to bind to</td></tr><tr><td>nic</td><td>"eth0"</td><td>Name of the interface to obtain the address. Used when the address is not specified.</td></tr><tr><td>port</td><td>"7100"</td><td>Port for the UDP server to bind. The UDP server automatically finds an available port starting from the specified port.</td></tr><tr><td>connectionTTL</td><td>10</td><td>TTL for the connection. If this time is exceeded, the client is disconnected from the server.</td></tr><tr><td>sendUDPInterval</td><td>0</td><td>Interval for sending UDP packets (ms). If set to less than 10, sent packets are not buffered.</td></tr><tr><td>handleRUDPInterval</td><td>100<br>min: 10</td><td>Interval for sending and receiving RUDP packets (ms). The smaller the value, the more responsive (faster) the server will be, but at the cost of increased CPU load.</td></tr><tr><td>retryInterval</td><td>1000</td><td>Retry interval for RUDP packets (ms)</td></tr><tr><td>maxRetry</td><td>10</td><td>Maximum retry count for RUDP packets. If this value is exceeded, the RUDP connection is considered timed out and discarded.</td></tr><tr><td>rcvWorkers</td><td>Number of CPU cores</td><td>Number of goroutines for receiving UDP packets</td></tr><tr><td>enableEncryption</td><td>true</td><td>Setting to false disables packet encryption and decryption. The HTTP server must be configured similarly.</td></tr></tbody></table>

> :warning: In cloud environments, address should be set to a private IP address while using the environment variable `DIARKIS_CLOUD_ENV`.

For more details, refer to the [server API reference](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/en/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.
