UDP Server

Overview

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

It can expose built-in commands of pre-prepared modules to clients, as well as implement and expose custom commands.

A command is a formatted packet sent from the client and processed by the server. Using commands, the Diarkis server cluster interacts with the client.

Setting Up the UDP Server

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

  • You can expose built-in commands by specifying the module with diarkisexec.SetupDiarkis(). Customize the configuration by specifying the ConfigPath of each module.

  • You can expose custom commands with diarkisexec.SetServerCommandHandler().

  • You can set up the UDP server with diarkisexec.SetupDiarkisUDPServer(). Customize the server settings with a JSON file passed as an argument.

⚠️ The above functions must be executed before calling diarkisexec.StartDiarkis().

For more details, refer to the diarkisexec API reference.

package main

var ver uint8  = 10
var cmd uint16 = 100

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

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()
}

Mesh Configuration

Specify the path to the JSON file in meshConfigPath. For more details, refer to #mesh-she-ding.

UDP Server Configuration

The configuration is written in 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
}
KeyDefaultDescription

enableP2P

true

When set to true, the client can obtain its public address for P2P.

address

"127.0.0.1"

The address to bind the UDP server to.

nic

"eth0"

The interface name for obtaining the address. Used when the address is not specified.

port

"7100"

The port for the UDP server to bind to. The UDP server will automatically find available ports starting from the specified port.

connectionTTL

10

The TTL of the connection. If this time is exceeded, the client will be disconnected from the server.

sendUDPInterval

0

The interval (in milliseconds) for sending UDP packets. If set below 10, sent packets will not be buffered.

handleRUDPInterval

100 min: 10

The interval (in milliseconds) for sending and receiving RUDP packets. The smaller the value, the more responsive (faster) the server will be, but at the cost of increased CPU load.

retryInterval

1000

The retry interval (in milliseconds) for RUDP packets.

maxRetry

10

The maximum number of retries for RUDP packets. If this value is exceeded, the RUDP connection is considered timed out and discarded.

rcvWorkers

Number of CPU cores

The number of goroutines for receiving UDP packets.

enableEncryption

true

If set to false, packet encryption and decryption will be disabled. A similar configuration is required for the HTTP server.

⚠️ In cloud environments, set address to the private IP address and use the environment variable DIARKIS_CLOUD_ENV.

For more details, refer to the server API reference.

Last updated