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.

⚠️ These functions need to be executed before calling diarkisexec.StartDiarkis().

For more details, refer to the diarkisexec API reference.

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 UDP Server.

UDP Server Configuration

Configure settings in JSON format.

{
  "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
}
Key
Default
Description

enableP2P

true

When set to true, clients can obtain their public address for P2P use.

address

"127.0.0.1"

Address for the UDP server to bind to

nic

"eth0"

Name of the interface to obtain the address. Used when the address is not specified.

port

"7100"

Port for the UDP server to bind. The UDP server automatically finds an available port starting from the specified port.

connectionTTL

10

TTL for the connection. If this time is exceeded, the client is disconnected from the server.

sendUDPInterval

0

Interval for sending UDP packets (ms). If set to less than 10, sent packets are not buffered.

handleRUDPInterval

100 min: 10

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.

retryInterval

1000

Retry interval for RUDP packets (ms)

maxRetry

10

Maximum retry count for RUDP packets. If this value is exceeded, the RUDP connection is considered timed out and discarded.

rcvWorkers

Number of CPU cores

Number of goroutines for receiving UDP packets

enableEncryption

true

Setting to false disables packet encryption and decryption. The HTTP server must be configured similarly.

⚠️ 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.

Last updated

Was this helpful?