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 a 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 clients, you can set up using the diarkisexec package.

  • With diarkisexec.SetupDiarkis(), you can specify modules to expose built-in commands. You can customize the configuration by specifying the ConfigPath for each module.

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

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

⚠️ The functions above must 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 in meshConfigPath. For more details, refer to #mesh-she-ding.

UDP Server Configuration

Configurations are described 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

By setting to true, clients can obtain their public address for P2P.

address

"127.0.0.1"

Address to bind the UDP server

nic

"eth0"

Name of the interface to obtain the address. Used when the address is unspecified.

port

"7100"

Port to which the UDP server binds. It will automatically find available ports starting from the specified one.

connectionTTL

10

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

sendUDPInterval

0

Interval for sending UDP packets (milliseconds). If set to less than 10, send packets will not be buffered.

handleRUDPInterval

100 min: 10

Interval for RUDP packet transmission (milliseconds). A smaller value means the server will respond more sensitively (faster), but at the cost of increased CPU load.

retryInterval

1000

Retrial interval for RUDP packets (milliseconds)

maxRetry

10

Maximum retrials 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 to receive UDP packets

enableEncryption

true

If set to false, packet encryption and decryption are disabled. The HTTP server must also be configured similarly.

⚠️ In cloud environments, use a private IP address for address, and utilize the environment variable DIARKIS_CLOUD_ENV.

For more details, refer to the server API reference.

Last updated