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 sent from the client and processed by the server. This is how Diarkis server clusters interact with clients.

Setting Up the TCP Server

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

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

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

  • You can set up a UDP server with diarkisexec.SetupDiarkisUDPServer(). The server settings can be customized with a JSON file as an argument.

⚠️ Note that 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},
		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()
}

Mesh Configuration

Specify the path to the JSON file with meshConfigPath. For details, refer to the Mesh Configuration.

TCP Server Configuration

The configuration is described in JSON.

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

connectionTTL

10

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

address

"127.0.0.1"

Address to bind the UDP server

nic

"eth0"

Interface name to obtain the address. Used if the address is not specified.

port

"7100"

Port for the UDP server to bind. The UDP server automatically searches for available ports starting from the specified port.

maxRcvSize

8000

Maximum TCP packet size (in bytes) for each request packet

noDelay

false

If set to true, the Nagle algorithm is disabled (no buffering before writing)

enableEncryption

true

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

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

For more details, refer to the server API Reference.

Last updated