TCP Server

Overview

The TCP Server is one of the three real-time communication servers provided by Diarkis, allowing the implementation of custom commands for applications.

A command is a formatted packet that is sent from the client and processed by the server. This is how the Diarkis server cluster interacts with the client.

Setting Up the TCP Server

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

  • You can expose built-in commands for each module by using diarkisexec.SetupDiarkis(). Customize the settings by specifying the ConfigPath for each module.

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

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

⚠️ Note that these functions should be executed before calling diarkisexec.StartDiarkis().

For more details, please refer to the API Reference for diarkisexec.

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 Configuration

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

TCP Server Configuration

The configuration is described in JSON format.

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

connectionTTL

10

The TTL for the connection. Clients will be disconnected from the server after this time is exceeded.

address

"127.0.0.1"

The address for the UDP server to bind to.

nic

"eth0"

The interface name for obtaining the address. Used when the address is unspecified.

port

"7100"

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

maxRcvSize

8000

The maximum TCP packet size (bytes) for each request packet.

noDelay

false

Set to true to disable the Nagle algorithm (no buffering before writing).

enableEncryption

true

Set to false to disable packet encryption and decryption. HTTP server should also have similar settings.

⚠️ In a cloud environment, the address should be a private IP address, and you should use the DIARKIS_CLOUD_ENV environment variable.

For more details, please refer to the API Reference for the server.

Last updated